webgl_geometry_hierarchy2.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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/RequestAnimationFrame.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var geometry, objects;
  25. var mouseX = 0, mouseY = 0;
  26. var windowHalfX = window.innerWidth / 2;
  27. var windowHalfY = window.innerHeight / 2;
  28. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  35. camera.position.z = 500;
  36. camera.useTarget = true;
  37. scene = new THREE.Scene();
  38. scene.fog = new THREE.Fog( 0xffffff, 1, 10000 );
  39. var geometry = new THREE.CubeGeometry( 100, 100, 100 );
  40. var material = new THREE.MeshNormalMaterial();
  41. var mesh = new THREE.Mesh( geometry, material );
  42. mesh.position.x = 1000;
  43. objects = [];
  44. objects.push( mesh );
  45. var amount = 200, child, object = mesh;
  46. for ( var i = 0; i < amount; i ++ ) {
  47. child = new THREE.Mesh( geometry, material );
  48. child.position.x = 100;
  49. object.add( child );
  50. object = child;
  51. objects.push( child );
  52. }
  53. object = mesh;
  54. for ( var i = 0; i < amount; i ++ ) {
  55. child = new THREE.Mesh( geometry, material );
  56. child.position.x = - 100;
  57. object.add( child );
  58. object = child;
  59. objects.push( child );
  60. }
  61. object = mesh;
  62. for ( var i = 0; i < amount; i ++ ) {
  63. child = new THREE.Mesh( geometry, material );
  64. child.position.y = - 100;
  65. object.add( child );
  66. object = child;
  67. objects.push( child );
  68. }
  69. object = mesh;
  70. for ( var i = 0; i < amount; i ++ ) {
  71. child = new THREE.Mesh( geometry, material );
  72. child.position.y = 100;
  73. object.add( child );
  74. object = child;
  75. objects.push( child );
  76. }
  77. object = mesh;
  78. for ( var i = 0; i < amount; i ++ ) {
  79. child = new THREE.Mesh( geometry, material );
  80. child.position.z = - 100;
  81. object.add( child );
  82. object = child;
  83. objects.push( child );
  84. }
  85. object = mesh;
  86. for ( var i = 0; i < amount; i ++ ) {
  87. child = new THREE.Mesh( geometry, material );
  88. child.position.z = 100;
  89. object.add( child );
  90. object = child;
  91. objects.push( child );
  92. }
  93. scene.add( mesh );
  94. renderer = new THREE.WebGLRenderer();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.sortObjects = false;
  97. container.appendChild( renderer.domElement );
  98. stats = new Stats();
  99. stats.domElement.style.position = 'absolute';
  100. stats.domElement.style.top = '0px';
  101. stats.domElement.style.zIndex = 100;
  102. container.appendChild( stats.domElement );
  103. }
  104. function onDocumentMouseMove(event) {
  105. mouseX = ( event.clientX - windowHalfX ) * 10;
  106. mouseY = ( event.clientY - windowHalfY ) * 10;
  107. }
  108. //
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. render();
  112. stats.update();
  113. }
  114. function render() {
  115. var rx = Math.sin( new Date().getTime() * 0.0007 ) * 0.2,
  116. ry = Math.sin( new Date().getTime() * 0.0003 ) * 0.1,
  117. rz = Math.sin( new Date().getTime() * 0.0002 ) * 0.1;
  118. camera.position.x += ( mouseX - camera.position.x ) * .05;
  119. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  120. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  121. objects[ i ].rotation.x = rx;
  122. objects[ i ].rotation.y = ry;
  123. objects[ i ].rotation.z = rz;
  124. }
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>