geometry.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import type { Nullable, FloatArray, DataArray, IndicesArray } from "../types";
  2. import type { Scene } from "../scene";
  3. import type { Vector2 } from "../Maths/math.vector";
  4. import { Vector3 } from "../Maths/math.vector";
  5. import type { IGetSetVerticesData } from "../Meshes/mesh.vertexData";
  6. import { VertexData } from "../Meshes/mesh.vertexData";
  7. import { VertexBuffer } from "../Buffers/buffer";
  8. import type { Effect } from "../Materials/effect";
  9. import { BoundingInfo } from "../Culling/boundingInfo";
  10. import type { DataBuffer } from "../Buffers/dataBuffer";
  11. import type { AbstractScene } from "../abstractScene";
  12. import type { Mesh } from "../Meshes/mesh";
  13. import type { AbstractEngine } from "../Engines/abstractEngine";
  14. /**
  15. * Class used to store geometry data (vertex buffers + index buffer)
  16. */
  17. export declare class Geometry implements IGetSetVerticesData {
  18. /**
  19. * Gets or sets the ID of the geometry
  20. */
  21. id: string;
  22. /**
  23. * Gets or sets the unique ID of the geometry
  24. */
  25. uniqueId: number;
  26. /**
  27. * Gets the delay loading state of the geometry (none by default which means not delayed)
  28. */
  29. delayLoadState: number;
  30. /**
  31. * Gets the file containing the data to load when running in delay load state
  32. */
  33. delayLoadingFile: Nullable<string>;
  34. /**
  35. * Callback called when the geometry is updated
  36. */
  37. onGeometryUpdated: (geometry: Geometry, kind?: string) => void;
  38. private _scene;
  39. private _engine;
  40. private _meshes;
  41. private _totalVertices;
  42. private _totalIndices?;
  43. /** @internal */
  44. _loadedUniqueId: string;
  45. /** @internal */
  46. _indices: IndicesArray;
  47. /** @internal */
  48. _vertexBuffers: {
  49. [key: string]: VertexBuffer;
  50. };
  51. private _isDisposed;
  52. private _extend;
  53. private _boundingBias;
  54. /** @internal */
  55. _delayInfo: Array<string>;
  56. private _indexBuffer;
  57. private _indexBufferIsUpdatable;
  58. /** @internal */
  59. _boundingInfo: Nullable<BoundingInfo>;
  60. /** @internal */
  61. _delayLoadingFunction: Nullable<(any: any, geometry: Geometry) => void>;
  62. /** @internal */
  63. _softwareSkinningFrameId: number;
  64. private _vertexArrayObjects;
  65. private _updatable;
  66. /** @internal */
  67. _positions: Nullable<Vector3[]>;
  68. private _positionsCache;
  69. /** @internal */
  70. _parentContainer: Nullable<AbstractScene>;
  71. /**
  72. * Gets or sets the Bias Vector to apply on the bounding elements (box/sphere), the max extend is computed as v += v * bias.x + bias.y, the min is computed as v -= v * bias.x + bias.y
  73. */
  74. get boundingBias(): Vector2;
  75. /**
  76. * Gets or sets the Bias Vector to apply on the bounding elements (box/sphere), the max extend is computed as v += v * bias.x + bias.y, the min is computed as v -= v * bias.x + bias.y
  77. */
  78. set boundingBias(value: Vector2);
  79. /**
  80. * Static function used to attach a new empty geometry to a mesh
  81. * @param mesh defines the mesh to attach the geometry to
  82. * @returns the new Geometry
  83. */
  84. static CreateGeometryForMesh(mesh: Mesh): Geometry;
  85. /** Get the list of meshes using this geometry */
  86. get meshes(): Mesh[];
  87. /**
  88. * If set to true (false by default), the bounding info applied to the meshes sharing this geometry will be the bounding info defined at the class level
  89. * and won't be computed based on the vertex positions (which is what we get when useBoundingInfoFromGeometry = false)
  90. */
  91. useBoundingInfoFromGeometry: boolean;
  92. /**
  93. * Creates a new geometry
  94. * @param id defines the unique ID
  95. * @param scene defines the hosting scene
  96. * @param vertexData defines the VertexData used to get geometry data
  97. * @param updatable defines if geometry must be updatable (false by default)
  98. * @param mesh defines the mesh that will be associated with the geometry
  99. */
  100. constructor(id: string, scene?: Scene, vertexData?: VertexData, updatable?: boolean, mesh?: Nullable<Mesh>);
  101. /**
  102. * Gets the current extend of the geometry
  103. */
  104. get extend(): {
  105. minimum: Vector3;
  106. maximum: Vector3;
  107. };
  108. /**
  109. * Gets the hosting scene
  110. * @returns the hosting Scene
  111. */
  112. getScene(): Scene;
  113. /**
  114. * Gets the hosting engine
  115. * @returns the hosting Engine
  116. */
  117. getEngine(): AbstractEngine;
  118. /**
  119. * Defines if the geometry is ready to use
  120. * @returns true if the geometry is ready to be used
  121. */
  122. isReady(): boolean;
  123. /**
  124. * Gets a value indicating that the geometry should not be serialized
  125. */
  126. get doNotSerialize(): boolean;
  127. /** @internal */
  128. _rebuild(): void;
  129. /**
  130. * Affects all geometry data in one call
  131. * @param vertexData defines the geometry data
  132. * @param updatable defines if the geometry must be flagged as updatable (false as default)
  133. */
  134. setAllVerticesData(vertexData: VertexData, updatable?: boolean): void;
  135. /**
  136. * Set specific vertex data
  137. * @param kind defines the data kind (Position, normal, etc...)
  138. * @param data defines the vertex data to use
  139. * @param updatable defines if the vertex must be flagged as updatable (false as default)
  140. * @param stride defines the stride to use (0 by default). This value is deduced from the kind value if not specified
  141. */
  142. setVerticesData(kind: string, data: FloatArray, updatable?: boolean, stride?: number): void;
  143. /**
  144. * Removes a specific vertex data
  145. * @param kind defines the data kind (Position, normal, etc...)
  146. */
  147. removeVerticesData(kind: string): void;
  148. /**
  149. * Affect a vertex buffer to the geometry. the vertexBuffer.getKind() function is used to determine where to store the data
  150. * @param buffer defines the vertex buffer to use
  151. * @param totalVertices defines the total number of vertices for position kind (could be null)
  152. * @param disposeExistingBuffer disposes the existing buffer, if any (default: true)
  153. */
  154. setVerticesBuffer(buffer: VertexBuffer, totalVertices?: Nullable<number>, disposeExistingBuffer?: boolean): void;
  155. /**
  156. * Update a specific vertex buffer
  157. * This function will directly update the underlying DataBuffer according to the passed numeric array or Float32Array
  158. * It will do nothing if the buffer is not updatable
  159. * @param kind defines the data kind (Position, normal, etc...)
  160. * @param data defines the data to use
  161. * @param offset defines the offset in the target buffer where to store the data
  162. * @param useBytes set to true if the offset is in bytes
  163. */
  164. updateVerticesDataDirectly(kind: string, data: DataArray, offset: number, useBytes?: boolean): void;
  165. /**
  166. * Update a specific vertex buffer
  167. * This function will create a new buffer if the current one is not updatable
  168. * @param kind defines the data kind (Position, normal, etc...)
  169. * @param data defines the data to use
  170. * @param updateExtends defines if the geometry extends must be recomputed (false by default)
  171. */
  172. updateVerticesData(kind: string, data: FloatArray, updateExtends?: boolean): void;
  173. private _updateBoundingInfo;
  174. /**
  175. * @internal
  176. */
  177. _bind(effect: Nullable<Effect>, indexToBind?: Nullable<DataBuffer>, overrideVertexBuffers?: {
  178. [kind: string]: Nullable<VertexBuffer>;
  179. }, overrideVertexArrayObjects?: {
  180. [key: string]: WebGLVertexArrayObject;
  181. }): void;
  182. /**
  183. * Gets total number of vertices
  184. * @returns the total number of vertices
  185. */
  186. getTotalVertices(): number;
  187. /**
  188. * Gets a specific vertex data attached to this geometry. Float data is constructed if the vertex buffer data cannot be returned directly.
  189. * @param kind defines the data kind (Position, normal, etc...)
  190. * @param copyWhenShared defines if the returned array must be cloned upon returning it if the current geometry is shared between multiple meshes
  191. * @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
  192. * @returns a float array containing vertex data
  193. */
  194. getVerticesData(kind: string, copyWhenShared?: boolean, forceCopy?: boolean): Nullable<FloatArray>;
  195. /**
  196. * Returns a boolean defining if the vertex data for the requested `kind` is updatable
  197. * @param kind defines the data kind (Position, normal, etc...)
  198. * @returns true if the vertex buffer with the specified kind is updatable
  199. */
  200. isVertexBufferUpdatable(kind: string): boolean;
  201. /**
  202. * Gets a specific vertex buffer
  203. * @param kind defines the data kind (Position, normal, etc...)
  204. * @returns a VertexBuffer
  205. */
  206. getVertexBuffer(kind: string): Nullable<VertexBuffer>;
  207. /**
  208. * Returns all vertex buffers
  209. * @returns an object holding all vertex buffers indexed by kind
  210. */
  211. getVertexBuffers(): Nullable<{
  212. [key: string]: VertexBuffer;
  213. }>;
  214. /**
  215. * Gets a boolean indicating if specific vertex buffer is present
  216. * @param kind defines the data kind (Position, normal, etc...)
  217. * @returns true if data is present
  218. */
  219. isVerticesDataPresent(kind: string): boolean;
  220. /**
  221. * Gets a list of all attached data kinds (Position, normal, etc...)
  222. * @returns a list of string containing all kinds
  223. */
  224. getVerticesDataKinds(): string[];
  225. /**
  226. * Update index buffer
  227. * @param indices defines the indices to store in the index buffer
  228. * @param offset defines the offset in the target buffer where to store the data
  229. * @param gpuMemoryOnly defines a boolean indicating that only the GPU memory must be updated leaving the CPU version of the indices unchanged (false by default)
  230. */
  231. updateIndices(indices: IndicesArray, offset?: number, gpuMemoryOnly?: boolean): void;
  232. /**
  233. * Sets the index buffer for this geometry.
  234. * @param indexBuffer Defines the index buffer to use for this geometry
  235. * @param totalVertices Defines the total number of vertices used by the buffer
  236. * @param totalIndices Defines the total number of indices in the index buffer
  237. */
  238. setIndexBuffer(indexBuffer: DataBuffer, totalVertices: number, totalIndices: number): void;
  239. /**
  240. * Creates a new index buffer
  241. * @param indices defines the indices to store in the index buffer
  242. * @param totalVertices defines the total number of vertices (could be null)
  243. * @param updatable defines if the index buffer must be flagged as updatable (false by default)
  244. */
  245. setIndices(indices: IndicesArray, totalVertices?: Nullable<number>, updatable?: boolean): void;
  246. /**
  247. * Return the total number of indices
  248. * @returns the total number of indices
  249. */
  250. getTotalIndices(): number;
  251. /**
  252. * Gets the index buffer array
  253. * @param copyWhenShared defines if the returned array must be cloned upon returning it if the current geometry is shared between multiple meshes
  254. * @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
  255. * @returns the index buffer array
  256. */
  257. getIndices(copyWhenShared?: boolean, forceCopy?: boolean): Nullable<IndicesArray>;
  258. /**
  259. * Gets the index buffer
  260. * @returns the index buffer
  261. */
  262. getIndexBuffer(): Nullable<DataBuffer>;
  263. /**
  264. * @internal
  265. */
  266. _releaseVertexArrayObject(effect?: Nullable<Effect>): void;
  267. /**
  268. * Release the associated resources for a specific mesh
  269. * @param mesh defines the source mesh
  270. * @param shouldDispose defines if the geometry must be disposed if there is no more mesh pointing to it
  271. */
  272. releaseForMesh(mesh: Mesh, shouldDispose?: boolean): void;
  273. /**
  274. * Apply current geometry to a given mesh
  275. * @param mesh defines the mesh to apply geometry to
  276. */
  277. applyToMesh(mesh: Mesh): void;
  278. private _updateExtend;
  279. private _applyToMesh;
  280. private _notifyUpdate;
  281. /**
  282. * Load the geometry if it was flagged as delay loaded
  283. * @param scene defines the hosting scene
  284. * @param onLoaded defines a callback called when the geometry is loaded
  285. */
  286. load(scene: Scene, onLoaded?: () => void): void;
  287. private _queueLoad;
  288. /**
  289. * Invert the geometry to move from a right handed system to a left handed one.
  290. */
  291. toLeftHanded(): void;
  292. /** @internal */
  293. _resetPointsArrayCache(): void;
  294. /** @internal */
  295. _generatePointsArray(): boolean;
  296. /**
  297. * Gets a value indicating if the geometry is disposed
  298. * @returns true if the geometry was disposed
  299. */
  300. isDisposed(): boolean;
  301. private _disposeVertexArrayObjects;
  302. /**
  303. * Free all associated resources
  304. */
  305. dispose(): void;
  306. /**
  307. * Clone the current geometry into a new geometry
  308. * @param id defines the unique ID of the new geometry
  309. * @returns a new geometry object
  310. */
  311. copy(id: string): Geometry;
  312. /**
  313. * Serialize the current geometry info (and not the vertices data) into a JSON object
  314. * @returns a JSON representation of the current geometry data (without the vertices data)
  315. */
  316. serialize(): any;
  317. private _toNumberArray;
  318. /**
  319. * Release any memory retained by the cached data on the Geometry.
  320. *
  321. * Call this function to reduce memory footprint of the mesh.
  322. * Vertex buffers will not store CPU data anymore (this will prevent picking, collisions or physics to work correctly)
  323. */
  324. clearCachedData(): void;
  325. /**
  326. * Serialize all vertices data into a JSON object
  327. * @returns a JSON representation of the current geometry data
  328. */
  329. serializeVerticeData(): any;
  330. /**
  331. * Extracts a clone of a mesh geometry
  332. * @param mesh defines the source mesh
  333. * @param id defines the unique ID of the new geometry object
  334. * @returns the new geometry object
  335. */
  336. static ExtractFromMesh(mesh: Mesh, id: string): Nullable<Geometry>;
  337. /**
  338. * You should now use Tools.RandomId(), this method is still here for legacy reasons.
  339. * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  340. * Be aware Math.random() could cause collisions, but:
  341. * "All but 6 of the 128 bits of the ID are randomly generated, which means that for any two ids, there's a 1 in 2^^122 (or 5.3x10^^36) chance they'll collide"
  342. * @returns a string containing a new GUID
  343. */
  344. static RandomId(): string;
  345. private static _GetGeometryByLoadedUniqueId;
  346. /**
  347. * @internal
  348. */
  349. static _ImportGeometry(parsedGeometry: any, mesh: Mesh): void;
  350. private static _CleanMatricesWeights;
  351. /**
  352. * Create a new geometry from persisted data (Using .babylon file format)
  353. * @param parsedVertexData defines the persisted data
  354. * @param scene defines the hosting scene
  355. * @param rootUrl defines the root url to use to load assets (like delayed data)
  356. * @returns the new geometry object
  357. */
  358. static Parse(parsedVertexData: any, scene: Scene, rootUrl: string): Nullable<Geometry>;
  359. }