BatchedMesh.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. [page:Mesh] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. A special version of [page:Mesh] with multi draw batch rendering support. Use
  14. [name] if you have to render a large number of objects with the same
  15. material but with different world transformations. The usage of [name] will
  16. help you to reduce the number of draw calls and thus improve the overall
  17. rendering performance in your application.
  18. <br/>
  19. <br/>
  20. If the [link:https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_multi_draw WEBGL_multi_draw extension] is
  21. not supported then a less performant fallback is used.
  22. </p>
  23. <h2>Code Example</h2>
  24. <code>
  25. const box = new THREE.BoxGeometry( 1, 1, 1 );
  26. const sphere = new THREE.SphereGeometry( 1, 12, 12 );
  27. const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  28. // initialize and add geometries into the batched mesh
  29. const batchedMesh = new BatchedMesh( 10, 5000, 10000, material );
  30. const boxGeometryId = batchedMesh.addGeometry( box );
  31. const sphereGeometryId = batchedMesh.addGeometry( sphere );
  32. // create instances of those geometries
  33. const boxInstancedId1 = batchedMesh.addInstance( boxGeometryId );
  34. const boxInstancedId2 = batchedMesh.addInstance( boxGeometryId );
  35. const sphereInstancedId1 = batchedMesh.addInstance( sphereGeometryId );
  36. const sphereInstancedId2 = batchedMesh.addInstance( sphereGeometryId );
  37. // position the geometries
  38. batchedMesh.setMatrixAt( boxInstancedId1, boxMatrix1 );
  39. batchedMesh.setMatrixAt( boxInstancedId2, boxMatrix2 );
  40. batchedMesh.setMatrixAt( sphereInstancedId1, sphereMatrix1 );
  41. batchedMesh.setMatrixAt( sphereInstancedId2, sphereMatrix2 );
  42. scene.add( batchedMesh );
  43. </code>
  44. <h2>Examples</h2>
  45. <p>
  46. [example:webgl_mesh_batch WebGL / mesh / batch]<br />
  47. </p>
  48. <h2>Constructor</h2>
  49. <h3>
  50. [name](
  51. [param:Integer maxInstanceCount], [param:Integer maxVertexCount],
  52. [param:Integer maxIndexCount], [param:Material material],
  53. )
  54. </h3>
  55. <p>
  56. [page:Integer maxInstanceCount] - the max number of individual instances planned to be added and rendered.<br />
  57. [page:Integer maxVertexCount] - the max number of vertices to be used by all unique geometries.<br />
  58. [page:Integer maxIndexCount] - the max number of indices to be used by all unique geometries.<br />
  59. [page:Material material] - an instance of [page:Material]. Default is a new [page:MeshBasicMaterial].<br />
  60. </p>
  61. <h2>Properties</h2>
  62. <p>See the base [page:Mesh] class for common properties.</p>
  63. <h3>[property:Box3 boundingBox]</h3>
  64. <p>
  65. This bounding box encloses all instances of the [name]. Can be calculated
  66. with [page:.computeBoundingBox](). Default is `null`.
  67. </p>
  68. <h3>[property:Sphere boundingSphere]</h3>
  69. <p>
  70. This bounding sphere encloses all instances of the [name]. Can be
  71. calculated with [page:.computeBoundingSphere](). Default is `null`.
  72. </p>
  73. <h3>[property:Boolean perObjectFrustumCulled]</h3>
  74. <p>
  75. If true then the individual objects within the [name] are frustum culled. Default is `true`.
  76. </p>
  77. <h3>[property:Boolean sortObjects]</h3>
  78. <p>
  79. If true then the individual objects within the [name] are sorted to improve overdraw-related artifacts.
  80. If the material is marked as "transparent" objects are rendered back to front and if not then they are
  81. rendered front to back. Default is `true`.
  82. </p>
  83. <h3>[property:Integer maxInstanceCount]</h3>
  84. <p>
  85. The maximum number of individual instances that can be stored in the [name]. Read only.
  86. </p>
  87. <h3>[property:Boolean isBatchedMesh]</h3>
  88. <p>Read-only flag to check if a given object is of type [name].</p>
  89. <h2>Methods</h2>
  90. <p>See the base [page:Mesh] class for common methods.</p>
  91. <h3>[method:undefined computeBoundingBox]()</h3>
  92. <p>
  93. Computes the bounding box, updating [page:.boundingBox] attribute.<br />
  94. Bounding boxes aren't computed by default. They need to be explicitly
  95. computed, otherwise they are `null`.
  96. </p>
  97. <h3>[method:undefined computeBoundingSphere]()</h3>
  98. <p>
  99. Computes the bounding sphere, updating [page:.boundingSphere]
  100. attribute.<br />
  101. Bounding spheres aren't computed by default. They need to be explicitly
  102. computed, otherwise they are `null`.
  103. </p>
  104. <h3>[method:undefined dispose]()</h3>
  105. <p>
  106. Frees the GPU-related resources allocated by this instance. Call this
  107. method whenever this instance is no longer used in your app.
  108. </p>
  109. <h3>[method:this setCustomSort]( [param:Function sortFunction] )</h3>
  110. <p>
  111. Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera. The objects
  112. in the list include a "z" field to perform a depth-ordered sort with.
  113. </p>
  114. <h3>
  115. [method:undefined getColorAt]( [param:Integer instanceId], [param:Color target] )
  116. </h3>
  117. <p>
  118. [page:Integer instanceId]: The id of an instance to get the color of.
  119. </p>
  120. <p>
  121. [page:Color target]: The target object to copy the color in to.
  122. </p>
  123. <p>Get the color of the defined geometry.</p>
  124. <h3>
  125. [method:Matrix4 getMatrixAt]( [param:Integer instanceId], [param:Matrix4 target] )
  126. </h3>
  127. <p>
  128. [page:Integer instanceId]: The id of an instance to get the matrix of.
  129. </p>
  130. <p>
  131. [page:Matrix4 target]: This 4x4 matrix will be set to the local
  132. transformation matrix of the defined instance.
  133. </p>
  134. <p>Get the local transformation matrix of the defined instance.</p>
  135. <h3>
  136. [method:Boolean getVisibleAt]( [param:Integer instanceId] )
  137. </h3>
  138. <p>
  139. [page:Integer instanceId]: The id of an instance to get the visibility state of.
  140. </p>
  141. <p>Get whether the given instance is marked as "visible" or not.</p>
  142. <h3>
  143. [method:undefined setColorAt]( [param:Integer instanceId], [param:Color color] )
  144. </h3>
  145. <p>
  146. [page:Integer instanceId]: The id of the instance to set the color of.
  147. </p>
  148. <p>[page:Color color]: The color to set the instance to.</p>
  149. <p>
  150. Sets the given color to the defined geometry instance.
  151. </p>
  152. <h3>
  153. [method:this setMatrixAt]( [param:Integer instanceId], [param:Matrix4 matrix] )
  154. </h3>
  155. <p>
  156. [page:Integer instanceId]: The id of an instance to set the matrix of.
  157. </p>
  158. <p>
  159. [page:Matrix4 matrix]: A 4x4 matrix representing the local transformation
  160. of a single instance.
  161. </p>
  162. <p>
  163. Sets the given local transformation matrix to the defined instance.
  164. </p>
  165. <h3>
  166. [method:this setVisibleAt]( [param:Integer instanceId], [param:Boolean visible] )
  167. </h3>
  168. <p>
  169. [page:Integer instanceId]: The id of the instance to set the visibility of.
  170. </p>
  171. <p>
  172. [page:Boolean visible]: A boolean value indicating the visibility state.
  173. </p>
  174. <p>
  175. Sets the visibility of the instance at the given index.
  176. </p>
  177. <h3>
  178. [method:Integer addGeometry]( [param:BufferGeometry geometry], [param:Integer reservedVertexRange], [param:Integer reservedIndexRange] )
  179. </h3>
  180. <p>
  181. [page:BufferGeometry geometry]: The geometry to add into the [name].
  182. </p>
  183. <p>
  184. [page:Integer reservedVertexRange]: Optional parameter specifying the amount of vertex buffer space to reserve for the added geometry. This
  185. is necessary if it is planned to set a new geometry at this index at a later time that is larger than the original geometry. Defaults to
  186. the length of the given geometry vertex buffer.
  187. </p>
  188. <p>
  189. [page:Integer reservedIndexRange]: Optional parameter specifying the amount of index buffer space to reserve for the added geometry. This
  190. is necessary if it is planned to set a new geometry at this index at a later time that is larger than the original geometry. Defaults to
  191. the length of the given geometry index buffer.
  192. </p>
  193. <p>
  194. Adds the given geometry to the [name] and returns the associated geometry id referring to it to be used in other functions.
  195. </p>
  196. <h3>
  197. [method:Integer addInstance]( [param:Integer geometryId] )
  198. </h3>
  199. <p>
  200. [page:Integer geometryId]: The id of a previously added geometry via "addGeometry" to add into the [name] to render.
  201. </p>
  202. <p>
  203. Adds a new instance to the [name] using the geometry of the given geometryId and returns a new id referring to the new instance to be used
  204. by other functions.
  205. </p>
  206. <h3>
  207. [method:Integer setGeometryAt]( [param:Integer geometryId], [param:BufferGeometry geometry] )
  208. </h3>
  209. <p>
  210. [page:Integer geometryId]: Which geometry id to replace with this geometry.
  211. </p>
  212. <p>
  213. [page:BufferGeometry geometry]: The geometry to substitute at the given geometry id.
  214. </p>
  215. <p>
  216. Replaces the geometry at `geometryId` with the provided geometry. Throws an error if there is not enough space reserved for geometry.
  217. Calling this will change all instances that are rendering that geometry.
  218. </p>
  219. <!--
  220. <h3>
  221. [method:Integer getInstanceCountAt]( [param:Integer index] )
  222. </h3>
  223. <p>
  224. [page:Integer index]: The index of an instance. Values have to be in the
  225. range [0, count].
  226. </p>
  227. <p>
  228. Gets the instance count of the geometry at `index`. Returns `null` if instance counts are not configured.
  229. </p>
  230. <h3>
  231. [method:Integer setInstanceCountAt]( [param:Integer index], [param:Integer instanceCount ] )
  232. </h3>
  233. <p>
  234. [page:Integer index]: Which geometry index to configure an instance count for.
  235. </p>
  236. <p>
  237. [page:Integer instanceCount]: The number of instances to render of the given geometry index.
  238. </p>
  239. <p>
  240. Sets an instance count of the geometry at `index`.
  241. </p>
  242. -->
  243. <h2>Source</h2>
  244. <p>
  245. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  246. </p>
  247. </body>
  248. </html>