setup.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <% if( withRequirejs ){ %>(function(){
  2. <% } %><% if( withDetectorjs ){ %> // detect WebGL
  3. if( !Detector.webgl ){
  4. Detector.addGetWebGLMessage();
  5. throw 'WebGL Not Available'
  6. }
  7. <% } %> // setup webgl renderer full page
  8. var renderer = new THREE.WebGLRenderer();
  9. renderer.setSize( window.innerWidth, window.innerHeight );
  10. document.body.appendChild( renderer.domElement );
  11. // setup a scene and camera
  12. var scene = new THREE.Scene();
  13. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000);
  14. camera.position.z = 3;
  15. // declare the rendering loop
  16. var onRenderFcts= [];
  17. <% if( withDetectorjs ){ %> // handle window resize events
  18. var winResize = new THREEx.WindowResize(renderer, camera)
  19. <% } %> //////////////////////////////////////////////////////////////////////////////////
  20. // default 3 points lightning //
  21. //////////////////////////////////////////////////////////////////////////////////
  22. // var ambientLight= new THREE.AmbientLight( 0x020202 )
  23. // scene.add( ambientLight)
  24. // var frontLight = new THREE.DirectionalLight('white', 1)
  25. // frontLight.position.set(0.5, 0.5, 2)
  26. // scene.add( frontLight )
  27. // var backLight = new THREE.DirectionalLight('white', 0.75)
  28. // backLight.position.set(-0.5, -0.5, -2)
  29. // scene.add( backLight )
  30. //////////////////////////////////////////////////////////////////////////////////
  31. // add an object and make it move //
  32. //////////////////////////////////////////////////////////////////////////////////
  33. // var geometry = new THREE.CubeGeometry( 1, 1, 1);
  34. // var material = new THREE.MeshPhongMaterial();
  35. // var mesh = new THREE.Mesh( geometry, material );
  36. // scene.add( mesh );
  37. // onRenderFcts.push(function(delta, now){
  38. // mesh.rotateX(0.5 * delta);
  39. // mesh.rotateY(2.0 * delta);
  40. // })
  41. //////////////////////////////////////////////////////////////////////////////////
  42. // Camera Controls //
  43. //////////////////////////////////////////////////////////////////////////////////
  44. var mouse = {x : 0, y : 0}
  45. document.addEventListener('mousemove', function(event){
  46. mouse.x = (event.clientX / window.innerWidth ) - 0.5
  47. mouse.y = (event.clientY / window.innerHeight) - 0.5
  48. }, false)
  49. // onRenderFcts.push(function(delta, now){
  50. // camera.position.x += (mouse.x*5 - camera.position.x) * (delta*3)
  51. // camera.position.y += (mouse.y*5 - camera.position.y) * (delta*3)
  52. // camera.lookAt( scene.position )
  53. // })
  54. //////////////////////////////////////////////////////////////////////////////////
  55. // render the scene //
  56. //////////////////////////////////////////////////////////////////////////////////
  57. // onRenderFcts.push(function(){
  58. // renderer.render( scene, camera );
  59. // })
  60. //////////////////////////////////////////////////////////////////////////////////
  61. // Rendering Loop runner //
  62. //////////////////////////////////////////////////////////////////////////////////
  63. var lastTimeMsec= null
  64. requestAnimationFrame(function animate(nowMsec){
  65. // keep looping
  66. requestAnimationFrame( animate );
  67. // measure time
  68. lastTimeMsec = lastTimeMsec || nowMsec-1000/60
  69. var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
  70. lastTimeMsec = nowMsec
  71. // call each update function
  72. onRenderFcts.forEach(function(onRenderFct){
  73. onRenderFct(deltaMsec/1000, nowMsec/1000);
  74. });
  75. });
  76. <% if( withRequirejs ){ %>
  77. module.exports = {
  78. Scene : scene,
  79. Renderer : renderer,
  80. onRender : onRenderFcts,
  81. Camera : camera,
  82. Mouse : mouse
  83. };
  84. })();
  85. <% } %>