threex
Version:
Game Extensions for three.js http://www.threejsgames.com/extensions/
164 lines (147 loc) • 6.08 kB
HTML
<!DOCTYPE html>
<script src='../../../vendor/three.js/build/three.js'></script>
<script src='../threex.claraiomaterials.js'></script>
<div style='position:absolute; top: 10px; left: 10px'>
<label>Fabric:</label>
<button onclick='setMaterial(this.innerText);updateMesh();'>Leather</button>
<button onclick='setMaterial(this.innerText);updateMesh();'>Jean</button>
<button onclick='setMaterial(this.innerText);updateMesh();'>BlackLeather</button>
<hr>
<label>Architectural :</label>
<button onclick='setMaterial(this.innerText);updateMesh();'>Stone</button>
<button onclick='setMaterial(this.innerText);updateMesh();'>Cobblestone</button>
<hr>
<label>Environmental :</label>
<button onclick='setMaterial(this.innerText);updateMesh();'>Lava</button>
<button onclick='setMaterial(this.innerText);updateMesh();'>Grass</button>
<button onclick='setMaterial(this.innerText);updateMesh();'>Sand</button>
<hr>
<label>Metals :</label>
<button onclick='setMaterial(this.innerText);updateMesh();'>Metal</button>
<button onclick='setMaterial(this.innerText);updateMesh();'>BrushedMetal</button>
<hr>
<label>Woods :</label>
<button onclick='setMaterial(this.innerText);updateMesh();'>Wood</button>
<hr>
<label>Shape:</label>
<button onclick='setGeometry(this.innerText);updateMesh();'>Cube</button>
<button onclick='setGeometry(this.innerText);updateMesh();'>Sphere</button>
<button onclick='setGeometry(this.innerText);updateMesh();'>Torus</button>
<button onclick='setGeometry(this.innerText);updateMesh();'>TorusKnot</button>
</div>
<body style='margin: 0px; background-color: #bbbbbb; overflow: hidden;'><script>
var renderer = new THREE.WebGLRenderer({
antialias : true
});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var updateFcts = [];
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 100 );
camera.position.z = 2;
//////////////////////////////////////////////////////////////////////////////////
// add Lights //
//////////////////////////////////////////////////////////////////////////////////
;(function(){
// add a ambient light
var light = new THREE.AmbientLight( 0x202020 )
scene.add( light )
// add a light in front
var light = new THREE.DirectionalLight('white', 1)
light.position.set(0.5, 0.5, 2)
scene.add( light )
// add a light behind
var light = new THREE.DirectionalLight('white', 0.75)
light.position.set(-0.5, -0.5, -2)
scene.add( light )
})()
//////////////////////////////////////////////////////////////////////////////////
// add an object and make it move //
//////////////////////////////////////////////////////////////////////////////////
var mesh = null
var material = null
var geometry = null
setMaterial('Lava')
setGeometry('Cube')
updateMesh()
updateFcts.push(function(delta, now){
mesh.rotation.x += 0.2 * delta;
mesh.rotation.y += 0.3 * delta;
})
function setMaterial(type){
if( type === 'Sand' ){
material = THREEx.ClaraioMaterials.createSand()
}else if( type === 'Leather' ){
material = THREEx.ClaraioMaterials.createLeather()
}else if( type === 'Jean' ){
material = THREEx.ClaraioMaterials.createJean()
}else if( type === 'BlackLeather' ){
material = THREEx.ClaraioMaterials.createBlackLeather()
}else if( type === 'Lava' ){
material = THREEx.ClaraioMaterials.createLava()
}else if( type === 'Metal' ){
material = THREEx.ClaraioMaterials.createMetal()
}else if( type === 'BrushedMetal' ){
material = THREEx.ClaraioMaterials.createBrushedMetal()
}else if( type === 'Stone' ){
material = THREEx.ClaraioMaterials.createStone()
}else if( type === 'Cobblestone' ){
material = THREEx.ClaraioMaterials.createCobblestone()
}else if( type === 'Grass' ){
material = THREEx.ClaraioMaterials.createGrass()
}else if( type === 'Wood' ){
material = THREEx.ClaraioMaterials.createWood()
}else console.assert(false)
}
function setGeometry(type){
if( type === 'Sphere' ){
geometry = new THREE.SphereGeometry( 1, 32, 16);
}else if( type === 'Cube' ){
geometry = new THREE.CubeGeometry( 1, 1, 1);
}else if( type === 'Torus' ){
geometry = new THREE.TorusGeometry( 1-0.25, 0.25, 32, 32);
}else if( type === 'TorusKnot' ){
geometry = new THREE.TorusKnotGeometry( 1-0.25, 0.25, 128, 16);
}else console.assert(false)
}
function updateMesh(){
if( mesh ) scene.remove(mesh)
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh)
}
//////////////////////////////////////////////////////////////////////////////////
// Camera Controls //
//////////////////////////////////////////////////////////////////////////////////
var mouse = {x : 0, y : 0}
document.addEventListener('mousemove', function(event){
mouse.x = (event.clientX / window.innerWidth ) - 0.5
mouse.y = (event.clientY / window.innerHeight) - 0.5
}, false)
updateFcts.push(function(delta, now){
camera.position.x += (mouse.x*5 - camera.position.x) * (delta*3)
camera.position.y += (mouse.y*5 - camera.position.y) * (delta*3)
camera.lookAt( scene.position )
})
//////////////////////////////////////////////////////////////////////////////////
// render the scene //
//////////////////////////////////////////////////////////////////////////////////
updateFcts.push(function(){
renderer.render( scene, camera );
})
//////////////////////////////////////////////////////////////////////////////////
// loop runner //
//////////////////////////////////////////////////////////////////////////////////
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
updateFcts.forEach(function(updateFn){
updateFn(deltaMsec/1000, nowMsec/1000)
})
})
</script></body>