instancedMesh.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import type { Nullable, FloatArray, IndicesArray } from "../types";
  2. import type { Vector3 } from "../Maths/math.vector";
  3. import { Matrix } from "../Maths/math.vector";
  4. import type { Camera } from "../Cameras/camera";
  5. import type { Node } from "../node";
  6. import { AbstractMesh } from "../Meshes/abstractMesh";
  7. import { Mesh } from "../Meshes/mesh";
  8. import type { Material } from "../Materials/material";
  9. import type { Skeleton } from "../Bones/skeleton";
  10. import { TransformNode } from "./transformNode";
  11. import type { Light } from "../Lights/light";
  12. import { VertexBuffer } from "../Buffers/buffer";
  13. /**
  14. * Creates an instance based on a source mesh.
  15. */
  16. export declare class InstancedMesh extends AbstractMesh {
  17. private _sourceMesh;
  18. private _currentLOD;
  19. private _billboardWorldMatrix;
  20. /** @internal */
  21. _indexInSourceMeshInstanceArray: number;
  22. /** @internal */
  23. _distanceToCamera: number;
  24. /** @internal */
  25. _previousWorldMatrix: Nullable<Matrix>;
  26. /**
  27. * Creates a new InstancedMesh object from the mesh source.
  28. * @param name defines the name of the instance
  29. * @param source the mesh to create the instance from
  30. */
  31. constructor(name: string, source: Mesh);
  32. /**
  33. * @returns the string "InstancedMesh".
  34. */
  35. getClassName(): string;
  36. /** Gets the list of lights affecting that mesh */
  37. get lightSources(): Light[];
  38. _resyncLightSources(): void;
  39. _resyncLightSource(): void;
  40. _removeLightSource(): void;
  41. /**
  42. * If the source mesh receives shadows
  43. */
  44. get receiveShadows(): boolean;
  45. set receiveShadows(_value: boolean);
  46. /**
  47. * The material of the source mesh
  48. */
  49. get material(): Nullable<Material>;
  50. set material(_value: Nullable<Material>);
  51. /**
  52. * Visibility of the source mesh
  53. */
  54. get visibility(): number;
  55. set visibility(_value: number);
  56. /**
  57. * Skeleton of the source mesh
  58. */
  59. get skeleton(): Nullable<Skeleton>;
  60. set skeleton(_value: Nullable<Skeleton>);
  61. /**
  62. * Rendering ground id of the source mesh
  63. */
  64. get renderingGroupId(): number;
  65. set renderingGroupId(value: number);
  66. /**
  67. * @returns the total number of vertices (integer).
  68. */
  69. getTotalVertices(): number;
  70. /**
  71. * Returns a positive integer : the total number of indices in this mesh geometry.
  72. * @returns the number of indices or zero if the mesh has no geometry.
  73. */
  74. getTotalIndices(): number;
  75. /**
  76. * The source mesh of the instance
  77. */
  78. get sourceMesh(): Mesh;
  79. /**
  80. * Creates a new InstancedMesh object from the mesh model.
  81. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/copies/instances
  82. * @param name defines the name of the new instance
  83. * @returns a new InstancedMesh
  84. */
  85. createInstance(name: string): InstancedMesh;
  86. /**
  87. * Is this node ready to be used/rendered
  88. * @param completeCheck defines if a complete check (including materials and lights) has to be done (false by default)
  89. * @returns {boolean} is it ready
  90. */
  91. isReady(completeCheck?: boolean): boolean;
  92. /**
  93. * Returns an array of integers or a typed array (Int32Array, Uint32Array, Uint16Array) populated with the mesh indices.
  94. * @param kind kind of verticies to retrieve (eg. positions, normals, uvs, etc.)
  95. * @param copyWhenShared If true (default false) and and if the mesh geometry is shared among some other meshes, the returned array is a copy of the internal one.
  96. * @param forceCopy defines a boolean forcing the copy of the buffer no matter what the value of copyWhenShared is
  97. * @returns a float array or a Float32Array of the requested kind of data : positions, normals, uvs, etc.
  98. */
  99. getVerticesData(kind: string, copyWhenShared?: boolean, forceCopy?: boolean): Nullable<FloatArray>;
  100. /**
  101. * Sets the vertex data of the mesh geometry for the requested `kind`.
  102. * If the mesh has no geometry, a new Geometry object is set to the mesh and then passed this vertex data.
  103. * The `data` are either a numeric array either a Float32Array.
  104. * The parameter `updatable` is passed as is to the underlying Geometry object constructor (if initially none) or updater.
  105. * The parameter `stride` is an optional positive integer, it is usually automatically deducted from the `kind` (3 for positions or normals, 2 for UV, etc).
  106. * Note that a new underlying VertexBuffer object is created each call.
  107. * If the `kind` is the `PositionKind`, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.
  108. *
  109. * Possible `kind` values :
  110. * - VertexBuffer.PositionKind
  111. * - VertexBuffer.UVKind
  112. * - VertexBuffer.UV2Kind
  113. * - VertexBuffer.UV3Kind
  114. * - VertexBuffer.UV4Kind
  115. * - VertexBuffer.UV5Kind
  116. * - VertexBuffer.UV6Kind
  117. * - VertexBuffer.ColorKind
  118. * - VertexBuffer.MatricesIndicesKind
  119. * - VertexBuffer.MatricesIndicesExtraKind
  120. * - VertexBuffer.MatricesWeightsKind
  121. * - VertexBuffer.MatricesWeightsExtraKind
  122. *
  123. * Returns the Mesh.
  124. * @param kind defines vertex data kind
  125. * @param data defines the data source
  126. * @param updatable defines if the data must be flagged as updatable (false as default)
  127. * @param stride defines the vertex stride (optional)
  128. * @returns the current mesh
  129. */
  130. setVerticesData(kind: string, data: FloatArray, updatable?: boolean, stride?: number): AbstractMesh;
  131. /**
  132. * Updates the existing vertex data of the mesh geometry for the requested `kind`.
  133. * If the mesh has no geometry, it is simply returned as it is.
  134. * The `data` are either a numeric array either a Float32Array.
  135. * No new underlying VertexBuffer object is created.
  136. * If the `kind` is the `PositionKind` and if `updateExtends` is true, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.
  137. * If the parameter `makeItUnique` is true, a new global geometry is created from this positions and is set to the mesh.
  138. *
  139. * Possible `kind` values :
  140. * - VertexBuffer.PositionKind
  141. * - VertexBuffer.UVKind
  142. * - VertexBuffer.UV2Kind
  143. * - VertexBuffer.UV3Kind
  144. * - VertexBuffer.UV4Kind
  145. * - VertexBuffer.UV5Kind
  146. * - VertexBuffer.UV6Kind
  147. * - VertexBuffer.ColorKind
  148. * - VertexBuffer.MatricesIndicesKind
  149. * - VertexBuffer.MatricesIndicesExtraKind
  150. * - VertexBuffer.MatricesWeightsKind
  151. * - VertexBuffer.MatricesWeightsExtraKind
  152. *
  153. * Returns the Mesh.
  154. * @param kind defines vertex data kind
  155. * @param data defines the data source
  156. * @param updateExtends defines if extends info of the mesh must be updated (can be null). This is mostly useful for "position" kind
  157. * @param makeItUnique defines it the updated vertex buffer must be flagged as unique (false by default)
  158. * @returns the source mesh
  159. */
  160. updateVerticesData(kind: string, data: FloatArray, updateExtends?: boolean, makeItUnique?: boolean): Mesh;
  161. /**
  162. * Sets the mesh indices.
  163. * Expects an array populated with integers or a typed array (Int32Array, Uint32Array, Uint16Array).
  164. * If the mesh has no geometry, a new Geometry object is created and set to the mesh.
  165. * This method creates a new index buffer each call.
  166. * Returns the Mesh.
  167. * @param indices the source data
  168. * @param totalVertices defines the total number of vertices referenced by indices (could be null)
  169. * @returns source mesh
  170. */
  171. setIndices(indices: IndicesArray, totalVertices?: Nullable<number>): Mesh;
  172. /**
  173. * Boolean : True if the mesh owns the requested kind of data.
  174. * @param kind defines which buffer to check (positions, indices, normals, etc). Possible `kind` values :
  175. * - VertexBuffer.PositionKind
  176. * - VertexBuffer.UVKind
  177. * - VertexBuffer.UV2Kind
  178. * - VertexBuffer.UV3Kind
  179. * - VertexBuffer.UV4Kind
  180. * - VertexBuffer.UV5Kind
  181. * - VertexBuffer.UV6Kind
  182. * - VertexBuffer.ColorKind
  183. * - VertexBuffer.MatricesIndicesKind
  184. * - VertexBuffer.MatricesIndicesExtraKind
  185. * - VertexBuffer.MatricesWeightsKind
  186. * - VertexBuffer.MatricesWeightsExtraKind
  187. * @returns true if data kind is present
  188. */
  189. isVerticesDataPresent(kind: string): boolean;
  190. /**
  191. * @returns an array of indices (IndicesArray).
  192. */
  193. getIndices(): Nullable<IndicesArray>;
  194. get _positions(): Nullable<Vector3[]>;
  195. /**
  196. * This method recomputes and sets a new BoundingInfo to the mesh unless it is locked.
  197. * This means the mesh underlying bounding box and sphere are recomputed.
  198. * @param applySkeleton defines whether to apply the skeleton before computing the bounding info
  199. * @param applyMorph defines whether to apply the morph target before computing the bounding info
  200. * @returns the current mesh
  201. */
  202. refreshBoundingInfo(applySkeleton?: boolean, applyMorph?: boolean): InstancedMesh;
  203. /** @internal */
  204. _preActivate(): InstancedMesh;
  205. /**
  206. * @internal
  207. */
  208. _activate(renderId: number, intermediateRendering: boolean): boolean;
  209. /** @internal */
  210. _postActivate(): void;
  211. getWorldMatrix(): Matrix;
  212. get isAnInstance(): boolean;
  213. /**
  214. * Returns the current associated LOD AbstractMesh.
  215. * @param camera defines the camera to use to pick the LOD level
  216. * @returns a Mesh or `null` if no LOD is associated with the AbstractMesh
  217. */
  218. getLOD(camera: Camera): AbstractMesh;
  219. /**
  220. * @internal
  221. */
  222. _preActivateForIntermediateRendering(renderId: number): Mesh;
  223. /** @internal */
  224. _syncSubMeshes(): InstancedMesh;
  225. /** @internal */
  226. _generatePointsArray(): boolean;
  227. /** @internal */
  228. _updateBoundingInfo(): AbstractMesh;
  229. /**
  230. * Creates a new InstancedMesh from the current mesh.
  231. *
  232. * Returns the clone.
  233. * @param name the cloned mesh name
  234. * @param newParent the optional Node to parent the clone to.
  235. * @param doNotCloneChildren if `true` the model children aren't cloned.
  236. * @param newSourceMesh if set this mesh will be used as the source mesh instead of ths instance's one
  237. * @returns the clone
  238. */
  239. clone(name: string, newParent?: Nullable<Node>, doNotCloneChildren?: boolean, newSourceMesh?: Mesh): InstancedMesh;
  240. /**
  241. * Disposes the InstancedMesh.
  242. * Returns nothing.
  243. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  244. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  245. */
  246. dispose(doNotRecurse?: boolean, disposeMaterialAndTextures?: boolean): void;
  247. /**
  248. * @internal
  249. */
  250. _serializeAsParent(serializationObject: any): void;
  251. /**
  252. * Instantiate (when possible) or clone that node with its hierarchy
  253. * @param newParent defines the new parent to use for the instance (or clone)
  254. * @param options defines options to configure how copy is done
  255. * @param options.doNotInstantiate defines if the model must be instantiated or just cloned
  256. * @param options.newSourcedMesh newSourcedMesh the new source mesh for the instance (or clone)
  257. * @param onNewNodeCreated defines an option callback to call when a clone or an instance is created
  258. * @returns an instance (or a clone) of the current node with its hierarchy
  259. */
  260. instantiateHierarchy(newParent?: Nullable<TransformNode>, options?: {
  261. doNotInstantiate: boolean | ((node: TransformNode) => boolean);
  262. newSourcedMesh?: Mesh;
  263. }, onNewNodeCreated?: (source: TransformNode, clone: TransformNode) => void): Nullable<TransformNode>;
  264. }
  265. declare module "./mesh" {
  266. interface Mesh {
  267. /**
  268. * Register a custom buffer that will be instanced
  269. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/copies/instances#custom-buffers
  270. * @param kind defines the buffer kind
  271. * @param stride defines the stride in floats
  272. */
  273. registerInstancedBuffer(kind: string, stride: number): void;
  274. /**
  275. * Invalidate VertexArrayObjects belonging to the mesh (but not to the Geometry of the mesh).
  276. */
  277. _invalidateInstanceVertexArrayObject(): void;
  278. /**
  279. * true to use the edge renderer for all instances of this mesh
  280. */
  281. edgesShareWithInstances: boolean;
  282. /** @internal */
  283. _userInstancedBuffersStorage: {
  284. data: {
  285. [key: string]: Float32Array;
  286. };
  287. sizes: {
  288. [key: string]: number;
  289. };
  290. vertexBuffers: {
  291. [key: string]: Nullable<VertexBuffer>;
  292. };
  293. strides: {
  294. [key: string]: number;
  295. };
  296. vertexArrayObjects?: {
  297. [key: string]: WebGLVertexArrayObject;
  298. };
  299. };
  300. }
  301. }
  302. declare module "./abstractMesh" {
  303. interface AbstractMesh {
  304. /**
  305. * Object used to store instanced buffers defined by user
  306. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/copies/instances#custom-buffers
  307. */
  308. instancedBuffers: {
  309. [key: string]: any;
  310. };
  311. }
  312. }