webgl_instancing_dynamic.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  35. scene.add( mesh );
  36. //
  37. var gui = new GUI();
  38. gui.add( mesh, 'count', 0, count );
  39. } );
  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. window.addEventListener( 'resize', onWindowResize, false );
  50. }
  51. function onWindowResize() {
  52. camera.aspect = window.innerWidth / window.innerHeight;
  53. camera.updateProjectionMatrix();
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. }
  56. //
  57. function animate() {
  58. requestAnimationFrame( animate );
  59. render();
  60. stats.update();
  61. }
  62. function render() {
  63. if ( mesh ) {
  64. var time = Date.now() * 0.001;
  65. mesh.rotation.x = Math.sin( time / 4 );
  66. mesh.rotation.y = Math.sin( time / 2 );
  67. var i = 0;
  68. var offset = ( amount - 1 ) / 2;
  69. for ( var x = 0; x < amount; x ++ ) {
  70. for ( var y = 0; y < amount; y ++ ) {
  71. for ( var z = 0; z < amount; z ++ ) {
  72. dummy.position.set( offset - x, offset - y, offset - z );
  73. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  74. dummy.rotation.z = dummy.rotation.y * 2;
  75. dummy.updateMatrix();
  76. mesh.setMatrixAt( i ++, dummy.matrix );
  77. }
  78. }
  79. }
  80. mesh.instanceMatrix.needsUpdate = true;
  81. }
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>