canvas_geometry_hierarchy.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry hierarchy 2</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background:#fff;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script src="../build/Three.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, renderer;
  23. var geometry, group;
  24. var mouseX = 0, mouseY = 0;
  25. var windowHalfX = window.innerWidth / 2;
  26. var windowHalfY = window.innerHeight / 2;
  27. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  28. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. scene = new THREE.Scene();
  34. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  35. camera.position.z = 500;
  36. camera.target = new THREE.Vector3();
  37. scene.add( camera );
  38. var geometry = new THREE.CubeGeometry( 100, 100, 100 );
  39. var material = new THREE.MeshNormalMaterial();
  40. group = new THREE.Object3D();
  41. for ( var i = 0; i < 200; i ++ ) {
  42. var mesh = new THREE.Mesh( geometry, material );
  43. mesh.overdraw = true;
  44. mesh.position.x = Math.random() * 2000 - 1000;
  45. mesh.position.y = Math.random() * 2000 - 1000;
  46. mesh.position.z = Math.random() * 2000 - 1000;
  47. mesh.rotation.x = Math.random() * 360 * ( Math.PI / 180 );
  48. mesh.rotation.y = Math.random() * 360 * ( Math.PI / 180 );
  49. mesh.matrixAutoUpdate = false;
  50. mesh.updateMatrix();
  51. group.add( mesh );
  52. }
  53. scene.add( group );
  54. renderer = new THREE.CanvasRenderer();
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.sortObjects = false;
  57. container.appendChild( renderer.domElement );
  58. stats = new Stats();
  59. stats.domElement.style.position = 'absolute';
  60. stats.domElement.style.top = '0px';
  61. stats.domElement.style.zIndex = 100;
  62. container.appendChild( stats.domElement );
  63. }
  64. function onDocumentMouseMove(event) {
  65. mouseX = ( event.clientX - windowHalfX ) * 10;
  66. mouseY = ( event.clientY - windowHalfY ) * 10;
  67. }
  68. //
  69. function animate() {
  70. requestAnimationFrame( animate );
  71. render();
  72. stats.update();
  73. }
  74. function render() {
  75. camera.position.x += ( mouseX - camera.position.x ) * .05;
  76. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  77. camera.lookAt( scene.position );
  78. group.rotation.x = Math.sin( new Date().getTime() * 0.0007 ) * 0.5;
  79. group.rotation.y = Math.sin( new Date().getTime() * 0.0003 ) * 0.5;
  80. group.rotation.z = Math.sin( new Date().getTime() * 0.0002 ) * 0.5;
  81. renderer.render( scene, camera );
  82. }
  83. </script>
  84. </body>
  85. </html>