webgl_mesh_batch.html 6.3 KB

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