webgl_geometry_hierarchy2.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import Stats from './jsm/libs/stats.module.js';
  13. var camera, scene, renderer, stats, root;
  14. var mouseX = 0, mouseY = 0;
  15. var windowHalfX = window.innerWidth / 2;
  16. var windowHalfY = window.innerHeight / 2;
  17. init();
  18. animate();
  19. function init() {
  20. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 15000 );
  21. camera.position.z = 500;
  22. scene = new THREE.Scene();
  23. scene.background = new THREE.Color( 0xffffff );
  24. var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
  25. var material = new THREE.MeshNormalMaterial();
  26. root = new THREE.Mesh( geometry, material );
  27. root.position.x = 1000;
  28. scene.add( root );
  29. var amount = 200, object, parent = root;
  30. for ( var i = 0; i < amount; i ++ ) {
  31. object = new THREE.Mesh( geometry, material );
  32. object.position.x = 100;
  33. parent.add( object );
  34. parent = object;
  35. }
  36. parent = root;
  37. for ( var i = 0; i < amount; i ++ ) {
  38. object = new THREE.Mesh( geometry, material );
  39. object.position.x = - 100;
  40. parent.add( object );
  41. parent = object;
  42. }
  43. parent = root;
  44. for ( var i = 0; i < amount; i ++ ) {
  45. object = new THREE.Mesh( geometry, material );
  46. object.position.y = - 100;
  47. parent.add( object );
  48. parent = object;
  49. }
  50. parent = root;
  51. for ( var i = 0; i < amount; i ++ ) {
  52. object = new THREE.Mesh( geometry, material );
  53. object.position.y = 100;
  54. parent.add( object );
  55. parent = object;
  56. }
  57. parent = root;
  58. for ( var i = 0; i < amount; i ++ ) {
  59. object = new THREE.Mesh( geometry, material );
  60. object.position.z = - 100;
  61. parent.add( object );
  62. parent = object;
  63. }
  64. parent = root;
  65. for ( var i = 0; i < amount; i ++ ) {
  66. object = new THREE.Mesh( geometry, material );
  67. object.position.z = 100;
  68. parent.add( object );
  69. parent = object;
  70. }
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. document.body.appendChild( renderer.domElement );
  76. //
  77. stats = new Stats();
  78. document.body.appendChild( stats.dom );
  79. //
  80. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  81. //
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. }
  84. function onWindowResize() {
  85. windowHalfX = window.innerWidth / 2;
  86. windowHalfY = window.innerHeight / 2;
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function onDocumentMouseMove( event ) {
  92. mouseX = ( event.clientX - windowHalfX ) * 10;
  93. mouseY = ( event.clientY - windowHalfY ) * 10;
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. stats.update();
  100. }
  101. function render() {
  102. var time = Date.now() * 0.001;
  103. var rx = Math.sin( time * 0.7 ) * 0.2;
  104. var ry = Math.sin( time * 0.3 ) * 0.1;
  105. var rz = Math.sin( time * 0.2 ) * 0.1;
  106. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  107. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  108. camera.lookAt( scene.position );
  109. root.traverse( function ( object ) {
  110. object.rotation.x = rx;
  111. object.rotation.y = ry;
  112. object.rotation.z = rz;
  113. } );
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>