webgl_mesh_batch.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - mesh - batch</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. <style>
  9. #info {
  10. background-color: rgba(0,0,0,0.75);
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - mesh - batch
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { BatchedMesh } from 'three/addons/objects/BatchedMesh.js';
  32. let stats, gui, guiStatsEl;
  33. let camera, controls, scene, renderer;
  34. let geometries, mesh;
  35. const ids = [];
  36. const matrix = new THREE.Matrix4();
  37. //
  38. const position = new THREE.Vector3();
  39. const rotation = new THREE.Euler();
  40. const quaternion = new THREE.Quaternion();
  41. const scale = new THREE.Vector3();
  42. //
  43. const MAX_GEOMETRY_COUNT = 8192;
  44. const Method = {
  45. BATCHED: 'BATCHED',
  46. NAIVE: 'NAIVE'
  47. };
  48. const api = {
  49. method: Method.BATCHED,
  50. count: 256,
  51. dynamic: 16
  52. };
  53. init();
  54. initGeometries();
  55. initMesh();
  56. animate();
  57. //
  58. function randomizeMatrix( matrix ) {
  59. position.x = Math.random() * 40 - 20;
  60. position.y = Math.random() * 40 - 20;
  61. position.z = Math.random() * 40 - 20;
  62. rotation.x = Math.random() * 2 * Math.PI;
  63. rotation.y = Math.random() * 2 * Math.PI;
  64. rotation.z = Math.random() * 2 * Math.PI;
  65. quaternion.setFromEuler( rotation );
  66. scale.x = scale.y = scale.z = 0.5 + ( Math.random() * 0.5 );
  67. return matrix.compose( position, quaternion, scale );
  68. }
  69. function randomizeRotationSpeed( rotation ) {
  70. rotation.x = Math.random() * 0.01;
  71. rotation.y = Math.random() * 0.01;
  72. rotation.z = Math.random() * 0.01;
  73. return rotation;
  74. }
  75. function initGeometries() {
  76. geometries = [
  77. new THREE.ConeGeometry( 1.0, 2.0 ),
  78. new THREE.BoxGeometry( 2.0, 2.0, 2.0 ),
  79. new THREE.SphereGeometry( 1.0, 16, 8 ),
  80. ];
  81. }
  82. function createMaterial() {
  83. return new THREE.MeshNormalMaterial();
  84. }
  85. function cleanup() {
  86. if ( mesh ) {
  87. mesh.parent.remove( mesh );
  88. if ( mesh.dispose ) {
  89. mesh.dispose();
  90. }
  91. }
  92. }
  93. function initMesh() {
  94. cleanup();
  95. if ( api.method === Method.BATCHED ) {
  96. initBatchedMesh();
  97. } else {
  98. initRegularMesh();
  99. }
  100. }
  101. function initRegularMesh() {
  102. mesh = new THREE.Group();
  103. const material = createMaterial();
  104. for ( let i = 0; i < api.count; i ++ ) {
  105. const child = new THREE.Mesh( geometries[ i % geometries.length ], material );
  106. randomizeMatrix( child.matrix );
  107. child.matrix.decompose( child.position, child.quaternion, child.scale );
  108. child.userData.rotationSpeed = randomizeRotationSpeed( new THREE.Euler() );
  109. mesh.add( child );
  110. }
  111. scene.add( mesh );
  112. }
  113. function initBatchedMesh() {
  114. const geometryCount = api.count;
  115. const vertexCount = api.count * 512;
  116. const indexCount = api.count * 1024;
  117. const euler = new THREE.Euler();
  118. const matrix = new THREE.Matrix4();
  119. mesh = new BatchedMesh( geometryCount, vertexCount, indexCount, createMaterial() );
  120. mesh.userData.rotationSpeeds = [];
  121. // disable full-object frustum culling since all of the objects can be dynamic.
  122. mesh.frustumCulled = false;
  123. ids.length = 0;
  124. for ( let i = 0; i < api.count; i ++ ) {
  125. const id = mesh.addGeometry( geometries[ i % geometries.length ] );
  126. mesh.setMatrixAt( id, randomizeMatrix( matrix ) );
  127. const rotationMatrix = new THREE.Matrix4();
  128. rotationMatrix.makeRotationFromEuler( randomizeRotationSpeed( euler ) );
  129. mesh.userData.rotationSpeeds.push( rotationMatrix );
  130. ids.push( id );
  131. }
  132. scene.add( mesh );
  133. }
  134. function init() {
  135. const width = window.innerWidth;
  136. const height = window.innerHeight;
  137. // camera
  138. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 100 );
  139. camera.position.z = 30;
  140. // renderer
  141. renderer = new THREE.WebGLRenderer( { antialias: true } );
  142. renderer.setPixelRatio( window.devicePixelRatio );
  143. renderer.setSize( width, height );
  144. document.body.appendChild( renderer.domElement );
  145. // scene
  146. scene = new THREE.Scene();
  147. scene.background = new THREE.Color( 0xffffff );
  148. // controls
  149. controls = new OrbitControls( camera, renderer.domElement );
  150. controls.autoRotate = true;
  151. controls.autoRotateSpeed = 1.0;
  152. // stats
  153. stats = new Stats();
  154. document.body.appendChild( stats.dom );
  155. // gui
  156. gui = new GUI();
  157. gui.add( api, 'count', 1, MAX_GEOMETRY_COUNT ).step( 1 ).onChange( initMesh );
  158. gui.add( api, 'dynamic', 0, MAX_GEOMETRY_COUNT ).step( 1 );
  159. gui.add( api, 'method', Method ).onChange( initMesh );
  160. guiStatsEl = document.createElement( 'li' );
  161. guiStatsEl.classList.add( 'gui-stats' );
  162. // listeners
  163. window.addEventListener( 'resize', onWindowResize );
  164. }
  165. //
  166. function onWindowResize() {
  167. const width = window.innerWidth;
  168. const height = window.innerHeight;
  169. camera.aspect = width / height;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( width, height );
  172. }
  173. function animate() {
  174. requestAnimationFrame( animate );
  175. animateMeshes();
  176. controls.update();
  177. stats.update();
  178. render();
  179. }
  180. function animateMeshes() {
  181. const loopNum = Math.min( api.count, api.dynamic );
  182. if ( api.method === Method.BATCHED ) {
  183. for ( let i = 0; i < loopNum; i ++ ) {
  184. const rotationMatrix = mesh.userData.rotationSpeeds[ i ];
  185. const id = ids[ i ];
  186. mesh.getMatrixAt( id, matrix );
  187. matrix.multiply( rotationMatrix );
  188. mesh.setMatrixAt( id, matrix );
  189. }
  190. } else {
  191. for ( let i = 0; i < loopNum; i ++ ) {
  192. const child = mesh.children[ i ];
  193. const rotationSpeed = child.userData.rotationSpeed;
  194. child.rotation.set(
  195. child.rotation.x + rotationSpeed.x,
  196. child.rotation.y + rotationSpeed.y,
  197. child.rotation.z + rotationSpeed.z
  198. );
  199. }
  200. }
  201. }
  202. function render() {
  203. renderer.render( scene, camera );
  204. }
  205. </script>
  206. </body>
  207. </html>