webgl_instancing_dynamic.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - dynamic</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. import { GUI } from './jsm/libs/dat.gui.module.js';
  14. var camera, scene, renderer, stats;
  15. var mesh;
  16. var amount = parseInt( window.location.search.substr( 1 ) ) || 10;
  17. var count = Math.pow( amount, 3 );
  18. var dummy = new THREE.Object3D();
  19. init();
  20. animate();
  21. function init() {
  22. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  23. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  24. camera.lookAt( 0, 0, 0 );
  25. scene = new THREE.Scene();
  26. var loader = new THREE.BufferGeometryLoader();
  27. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  28. geometry.computeVertexNormals();
  29. geometry.scale( 0.5, 0.5, 0.5 );
  30. var material = new THREE.MeshNormalMaterial();
  31. // check overdraw
  32. // var material = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.1, transparent: true } );
  33. mesh = new THREE.InstancedMesh( geometry, material, count );
  34. scene.add( mesh );
  35. //
  36. var gui = new GUI();
  37. gui.add( mesh, 'count', 0, count );
  38. } );
  39. //
  40. renderer = new THREE.WebGLRenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. document.body.appendChild( renderer.domElement );
  44. //
  45. stats = new Stats();
  46. document.body.appendChild( stats.dom );
  47. //
  48. window.addEventListener( 'resize', onWindowResize, false );
  49. }
  50. function onWindowResize() {
  51. camera.aspect = window.innerWidth / window.innerHeight;
  52. camera.updateProjectionMatrix();
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. }
  55. //
  56. function animate() {
  57. requestAnimationFrame( animate );
  58. render();
  59. stats.update();
  60. }
  61. function render() {
  62. if ( mesh ) {
  63. var time = Date.now() * 0.001;
  64. mesh.rotation.x = Math.sin( time / 4 );
  65. mesh.rotation.y = Math.sin( time / 2 );
  66. var i = 0;
  67. var offset = ( amount - 1 ) / 2;
  68. for ( var x = 0; x < amount; x ++ ) {
  69. for ( var y = 0; y < amount; y ++ ) {
  70. for ( var z = 0; z < amount; z ++ ) {
  71. dummy.position.set( offset - x, offset - y, offset - z );
  72. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  73. dummy.rotation.z = dummy.rotation.y * 2;
  74. dummy.updateMatrix();
  75. mesh.setMatrixAt( i ++, dummy.matrix );
  76. }
  77. }
  78. }
  79. mesh.instanceMatrix.needsUpdate = true;
  80. }
  81. renderer.render( scene, camera );
  82. }
  83. </script>
  84. </body>
  85. </html>