webgl_mesh_batch.html 6.8 KB

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