BatchedMesh.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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:Object getGeometryRangeAt]( [param:Integer geometryId], [param:Object target] )
  144. </h3>
  145. <p>
  146. [page:Integer geometryId]: The id of the geometry to get the range of.
  147. </p>
  148. <p>
  149. [page:Object target]: Optional target object to copy the range in to.
  150. </p>
  151. <p>Get the range representing the subset of triangles related to the attached geometry, indicating the starting offset and count, or `null` if invalid.</p>
  152. <p>Return an object of the form:</p>
  153. <code>{ start: Integer, count: Integer }</code>
  154. <h3>
  155. [method:Integer getGeometryIdAt]( [param:Integer instanceId] )
  156. </h3>
  157. <p>
  158. [page:Integer instanceId]: The id of an instance to get the geometryIndex of.
  159. </p>
  160. <p>Get the geometryIndex of the defined instance.</p>
  161. <h3>
  162. [method:undefined setColorAt]( [param:Integer instanceId], [param:Color color] )
  163. </h3>
  164. <p>
  165. [page:Integer instanceId]: The id of the instance to set the color of.
  166. </p>
  167. <p>[page:Color color]: The color to set the instance to.</p>
  168. <p>
  169. Sets the given color to the defined geometry instance.
  170. </p>
  171. <h3>
  172. [method:this setMatrixAt]( [param:Integer instanceId], [param:Matrix4 matrix] )
  173. </h3>
  174. <p>
  175. [page:Integer instanceId]: The id of an instance to set the matrix of.
  176. </p>
  177. <p>
  178. [page:Matrix4 matrix]: A 4x4 matrix representing the local transformation
  179. of a single instance.
  180. </p>
  181. <p>
  182. Sets the given local transformation matrix to the defined instance.
  183. </p>
  184. <h3>
  185. [method:this setVisibleAt]( [param:Integer instanceId], [param:Boolean visible] )
  186. </h3>
  187. <p>
  188. [page:Integer instanceId]: The id of the instance to set the visibility of.
  189. </p>
  190. <p>
  191. [page:Boolean visible]: A boolean value indicating the visibility state.
  192. </p>
  193. <p>
  194. Sets the visibility of the instance at the given index.
  195. </p>
  196. <h3>
  197. [method:this setGeometryIdAt]( [param:Integer instanceId], [param:Integer geometryId] )
  198. </h3>
  199. <p>
  200. [page:Integer instanceId]: The id of the instance to set the geometryIndex of.
  201. </p>
  202. <p>
  203. [page:Integer geometryId]: The geometryIndex to be use by the instance.
  204. </p>
  205. <p>
  206. Sets the geometryIndex of the instance at the given index.
  207. </p>
  208. <h3>
  209. [method:Integer addGeometry]( [param:BufferGeometry geometry], [param:Integer reservedVertexRange], [param:Integer reservedIndexRange] )
  210. </h3>
  211. <p>
  212. [page:BufferGeometry geometry]: The geometry to add into the [name].
  213. </p>
  214. <p>
  215. [page:Integer reservedVertexRange]: Optional parameter specifying the amount of vertex buffer space to reserve for the added geometry. This
  216. 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
  217. the length of the given geometry vertex buffer.
  218. </p>
  219. <p>
  220. [page:Integer reservedIndexRange]: Optional parameter specifying the amount of index buffer space to reserve for the added geometry. This
  221. 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
  222. the length of the given geometry index buffer.
  223. </p>
  224. <p>
  225. Adds the given geometry to the [name] and returns the associated geometry id referring to it to be used in other functions.
  226. </p>
  227. <h3>
  228. [method:Integer addInstance]( [param:Integer geometryId] )
  229. </h3>
  230. <p>
  231. [page:Integer geometryId]: The id of a previously added geometry via "addGeometry" to add into the [name] to render.
  232. </p>
  233. <p>
  234. 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
  235. by other functions.
  236. </p>
  237. <h3>
  238. [method:Integer deleteInstance]( [param:Integer instanceId] )
  239. </h3>
  240. <p>
  241. [page:Integer instanceId]: The id of an instance to remove from the [name] that was previously added via "addInstance".
  242. </p>
  243. <p>
  244. Removes an existing instance from the [name] using the given instanceId.
  245. </p>
  246. <h3>
  247. [method:Integer setGeometryAt]( [param:Integer geometryId], [param:BufferGeometry geometry] )
  248. </h3>
  249. <p>
  250. [page:Integer geometryId]: Which geometry id to replace with this geometry.
  251. </p>
  252. <p>
  253. [page:BufferGeometry geometry]: The geometry to substitute at the given geometry id.
  254. </p>
  255. <p>
  256. Replaces the geometry at `geometryId` with the provided geometry. Throws an error if there is not enough space reserved for geometry.
  257. Calling this will change all instances that are rendering that geometry.
  258. </p>
  259. <!--
  260. <h3>
  261. [method:Integer getInstanceCountAt]( [param:Integer index] )
  262. </h3>
  263. <p>
  264. [page:Integer index]: The index of an instance. Values have to be in the
  265. range [0, count].
  266. </p>
  267. <p>
  268. Gets the instance count of the geometry at `index`. Returns `null` if instance counts are not configured.
  269. </p>
  270. <h3>
  271. [method:Integer setInstanceCountAt]( [param:Integer index], [param:Integer instanceCount ] )
  272. </h3>
  273. <p>
  274. [page:Integer index]: Which geometry index to configure an instance count for.
  275. </p>
  276. <p>
  277. [page:Integer instanceCount]: The number of instances to render of the given geometry index.
  278. </p>
  279. <p>
  280. Sets an instance count of the geometry at `index`.
  281. </p>
  282. -->
  283. <h2>Source</h2>
  284. <p>
  285. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  286. </p>
  287. </body>
  288. </html>