webgl_mesh_batch.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. ids.length = 0;
  122. for ( let i = 0; i < api.count; i ++ ) {
  123. const id = mesh.applyGeometry( geometries[ i % geometries.length ] );
  124. mesh.setMatrixAt( id, randomizeMatrix( matrix ) );
  125. const rotationMatrix = new THREE.Matrix4();
  126. rotationMatrix.makeRotationFromEuler( randomizeRotationSpeed( euler ) );
  127. mesh.userData.rotationSpeeds.push( rotationMatrix );
  128. ids.push( id );
  129. }
  130. scene.add( mesh );
  131. }
  132. function init() {
  133. const width = window.innerWidth;
  134. const height = window.innerHeight;
  135. // camera
  136. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 100 );
  137. camera.position.z = 30;
  138. // renderer
  139. renderer = new THREE.WebGLRenderer( { antialias: true } );
  140. renderer.setPixelRatio( window.devicePixelRatio );
  141. renderer.setSize( width, height );
  142. document.body.appendChild( renderer.domElement );
  143. // scene
  144. scene = new THREE.Scene();
  145. scene.background = new THREE.Color( 0xffffff );
  146. // controls
  147. controls = new OrbitControls( camera, renderer.domElement );
  148. controls.autoRotate = true;
  149. controls.autoRotateSpeed = 1.0;
  150. // stats
  151. stats = new Stats();
  152. document.body.appendChild( stats.dom );
  153. // gui
  154. gui = new GUI();
  155. gui.add( api, 'count', 1, MAX_GEOMETRY_COUNT ).step( 1 ).onChange( initMesh );
  156. gui.add( api, 'dynamic', 0, MAX_GEOMETRY_COUNT ).step( 1 );
  157. gui.add( api, 'method', Method ).onChange( initMesh );
  158. guiStatsEl = document.createElement( 'li' );
  159. guiStatsEl.classList.add( 'gui-stats' );
  160. // listeners
  161. window.addEventListener( 'resize', onWindowResize );
  162. }
  163. //
  164. function onWindowResize() {
  165. const width = window.innerWidth;
  166. const height = window.innerHeight;
  167. camera.aspect = width / height;
  168. camera.updateProjectionMatrix();
  169. renderer.setSize( width, height );
  170. }
  171. function animate() {
  172. requestAnimationFrame( animate );
  173. animateMeshes();
  174. controls.update();
  175. stats.update();
  176. render();
  177. }
  178. function animateMeshes() {
  179. const loopNum = Math.min( api.count, api.dynamic );
  180. if ( api.method === Method.BATCHED ) {
  181. for ( let i = 0; i < loopNum; i ++ ) {
  182. const rotationMatrix = mesh.userData.rotationSpeeds[ i ];
  183. const id = ids[ i ];
  184. mesh.getMatrixAt( id, matrix );
  185. matrix.multiply( rotationMatrix );
  186. mesh.setMatrixAt( id, matrix );
  187. }
  188. } else {
  189. for ( let i = 0; i < loopNum; i ++ ) {
  190. const child = mesh.children[ i ];
  191. const rotationSpeed = child.userData.rotationSpeed;
  192. child.rotation.set(
  193. child.rotation.x + rotationSpeed.x,
  194. child.rotation.y + rotationSpeed.y,
  195. child.rotation.z + rotationSpeed.z
  196. );
  197. }
  198. }
  199. }
  200. function render() {
  201. renderer.render( scene, camera );
  202. }
  203. </script>
  204. </body>
  205. </html>