threex
Version:
Game Extensions for three.js http://www.threejsgames.com/extensions/
159 lines (140 loc) • 4.51 kB
HTML
<!DOCTYPE html>
<script src='vendor/three.js/three.js'></script>
<script src='../threex.romemodels.js'></script>
<div id='buttons' style='position: absolute'></div>
<body style='margin: 0px; overflow: hidden;'><script>
// setup webgl renderer full page
var renderer = new THREE.WebGLRenderer({
antialias : true
});
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xbbbbbb );
document.body.appendChild( renderer.domElement );
// setup a scene and camera
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.z = 3;
// declare the rendering loop
var onRenderFcts= [];
//////////////////////////////////////////////////////////////////////////////////
// default 3 points lightning //
//////////////////////////////////////////////////////////////////////////////////
var ambientLight= new THREE.AmbientLight( 0x606060 )
scene.add( ambientLight)
var frontLight = new THREE.DirectionalLight('white', 1)
frontLight.position.set(0.5, 0.5, 2)
scene.add( frontLight )
var backLight = new THREE.DirectionalLight('white', 0.75)
backLight.position.set(-0.5, -0.5, -2)
scene.add( backLight )
//////////////////////////////////////////////////////////////////////////////////
// List All Models //
//////////////////////////////////////////////////////////////////////////////////
var modelNames =
[ 'armHand',
'bearBlack',
'bearBrown',
'bison',
'black_widow',
'centipede',
'chowchow',
'cow',
'crab',
'deer',
'drownArm',
'eagle',
'elk',
'fish1',
'fish2',
'fish3',
'fish4',
'flamingo',
'fox',
'gator',
'goat',
'horse',
'hummingbird',
'moose',
'mountainlion',
'owl',
'panther',
'parrot',
'rabbit',
'raccoon',
'raven',
'retreiver',
'scorpion',
'sealRun',
'sealSwim',
'shadow2',
'stork',
'tarbuffaloA',
'tarbuffaloB',
'toad',
'treefrog',
'vulture',
'wolf']
var mesh;
onRenderFcts.push(function(delta, now){
if( mesh === undefined ) return
mesh.updateAnimation( delta*1000 );
mesh.rotation.y += delta * Math.PI*2 * -0.1
})
function switchValue(modelName){
var url = '../models/'+modelName+'.js'
THREEx.RomeModels.load(url, function(newMesh){
if( mesh && mesh.parent ) mesh.parent.remove(mesh)
mesh = newMesh
mesh.scale.multiplyScalar(1/200)
mesh.rotation.y = Math.PI/2
scene.add(mesh)
})
}
switchValue(modelNames[4])
//////////////////////////////////////////////////////////////////////////////////
// Build all buttons //
//////////////////////////////////////////////////////////////////////////////////
modelNames.forEach(function(modelName){
var element = document.createElement('button')
element.onclick = function(){
switchValue(this.innerHTML)
}
element.innerHTML = modelName
document.querySelector('#buttons').appendChild(element)
})
//////////////////////////////////////////////////////////////////////////////////
// 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)
// onRenderFcts.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 //
//////////////////////////////////////////////////////////////////////////////////
onRenderFcts.push(function(){
renderer.render( scene, camera );
})
//////////////////////////////////////////////////////////////////////////////////
// Rendering 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
onRenderFcts.forEach(function(onRenderFct){
onRenderFct(deltaMsec/1000, nowMsec/1000)
})
})
</script></body>