webgl_geometry_hierarchy.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry hierarchy</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, group;
  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, 10000 );
  21. camera.position.z = 500;
  22. scene = new THREE.Scene();
  23. scene.background = new THREE.Color( 0xffffff );
  24. scene.fog = new THREE.Fog( 0xffffff, 1, 10000 );
  25. var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
  26. var material = new THREE.MeshNormalMaterial();
  27. group = new THREE.Group();
  28. for ( var i = 0; i < 1000; i ++ ) {
  29. var mesh = new THREE.Mesh( geometry, material );
  30. mesh.position.x = Math.random() * 2000 - 1000;
  31. mesh.position.y = Math.random() * 2000 - 1000;
  32. mesh.position.z = Math.random() * 2000 - 1000;
  33. mesh.rotation.x = Math.random() * 2 * Math.PI;
  34. mesh.rotation.y = Math.random() * 2 * Math.PI;
  35. mesh.matrixAutoUpdate = false;
  36. mesh.updateMatrix();
  37. group.add( mesh );
  38. }
  39. scene.add( group );
  40. //
  41. renderer = new THREE.WebGLRenderer( { antialias: true } );
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. document.body.appendChild( renderer.domElement );
  45. //
  46. stats = new Stats();
  47. document.body.appendChild( stats.dom );
  48. //
  49. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  50. //
  51. window.addEventListener( 'resize', onWindowResize, false );
  52. }
  53. function onWindowResize() {
  54. windowHalfX = window.innerWidth / 2;
  55. windowHalfY = window.innerHeight / 2;
  56. camera.aspect = window.innerWidth / window.innerHeight;
  57. camera.updateProjectionMatrix();
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. }
  60. function onDocumentMouseMove( event ) {
  61. mouseX = ( event.clientX - windowHalfX ) * 10;
  62. mouseY = ( event.clientY - windowHalfY ) * 10;
  63. }
  64. //
  65. function animate() {
  66. requestAnimationFrame( animate );
  67. render();
  68. stats.update();
  69. }
  70. function render() {
  71. var time = Date.now() * 0.001;
  72. var rx = Math.sin( time * 0.7 ) * 0.5,
  73. ry = Math.sin( time * 0.3 ) * 0.5,
  74. rz = Math.sin( time * 0.2 ) * 0.5;
  75. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  76. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  77. camera.lookAt( scene.position );
  78. group.rotation.x = rx;
  79. group.rotation.y = ry;
  80. group.rotation.z = rz;
  81. renderer.render( scene, camera );
  82. }
  83. </script>
  84. </body>
  85. </html>