threex
Version:
Game Extensions for three.js http://www.threejsgames.com/extensions/
95 lines (80 loc) • 3.45 kB
HTML
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src='../../vendor/three-r67.js/build/three.min.js'></script>
<script src='../../vendor/three-r67.js/examples/js/controls/OrbitControls.js'></script>
<div style='position: absolute; top: 0px; width: 100%;font-family:arial; font-weight: bolder; padding-top: 5px;'>
Boilerplate for <a href="http://threejs.org" target="_blank">three.js</a>
-
works on desktop and mobile
</div><body style='margin: 0px; overflow: hidden; text-align:center;'><script>
//////////////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////////////
// init renderer
var renderer = new THREE.WebGLRenderer({
antialias : true,
});
renderer.setClearColor(new THREE.Color('lightgrey'), 1)
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// array of functions for the rendering loop
var onRenderFcts= [];
// init scene and camera
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.z = 2;
var controls = new THREE.OrbitControls(camera)
//////////////////////////////////////////////////////////////////////////////////
// Init light pool
//////////////////////////////////////////////////////////////////////////////////
// init the light pool BEFORE the first rendering
var lightPool = {
directional : new THREEx.LightPool(THREE.DirectionalLight, 3),
pointLight : new THREEx.LightPool(THREE.PointLight, 20),
}
//////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////
var light = lightPool.pointLight.pop()
light.color.set('white')
scene.add(light)
setTimeout(function(){
scene.remove(light)
lightPool.pointLight.push(light)
}, 1000)
//////////////////////////////////////////////////////////////////////////////////
// add an object in the scene
//////////////////////////////////////////////////////////////////////////////////
// add a torus
var geometry = new THREE.TorusKnotGeometry(0.5-0.12, 0.12);
var material = new THREE.MeshNormalMaterial();
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
//////////////////////////////////////////////////////////////////////////////////
// render the whole thing on the page
//////////////////////////////////////////////////////////////////////////////////
// handle window resize
window.addEventListener('resize', function(){
renderer.setSize( window.innerWidth, window.innerHeight )
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
}, false)
// render the scene
onRenderFcts.push(function(){
renderer.render( scene, camera );
})
// run the rendering loop
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>