dracoCompression.d.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { AutoReleaseWorkerPool } from "../../Misc/workerPool";
  2. import type { IDisposable, Scene } from "../../scene";
  3. import { Geometry } from "../geometry";
  4. import { VertexData } from "../mesh.vertexData";
  5. import { type AttributeData } from "./dracoCompressionWorker";
  6. interface MeshData {
  7. indices?: Uint16Array | Uint32Array;
  8. attributes: Array<AttributeData>;
  9. totalVertices: number;
  10. }
  11. /**
  12. * Configuration for Draco compression
  13. */
  14. export interface IDracoCompressionConfiguration {
  15. /**
  16. * Configuration for the decoder.
  17. */
  18. decoder: {
  19. /**
  20. * The url to the WebAssembly module.
  21. */
  22. wasmUrl?: string;
  23. /**
  24. * The url to the WebAssembly binary.
  25. */
  26. wasmBinaryUrl?: string;
  27. /**
  28. * The url to the fallback JavaScript module.
  29. */
  30. fallbackUrl?: string;
  31. /**
  32. * Optional worker pool to use for async decoding instead of creating a new worker pool.
  33. */
  34. workerPool?: AutoReleaseWorkerPool;
  35. /**
  36. * Optional ArrayBuffer of the WebAssembly binary
  37. */
  38. wasmBinary?: ArrayBuffer;
  39. /**
  40. * The decoder module if already available.
  41. */
  42. jsModule?: any;
  43. };
  44. }
  45. /**
  46. * Options for Draco compression
  47. */
  48. export interface IDracoCompressionOptions {
  49. /**
  50. * The number of workers for async operations. Specify `0` to disable web workers and run synchronously in the current context.
  51. */
  52. numWorkers?: number;
  53. /**
  54. * Optional ArrayBuffer of the WebAssembly binary.
  55. * If provided it will be used instead of loading the binary from wasmBinaryUrl.
  56. */
  57. wasmBinary?: ArrayBuffer;
  58. /**
  59. * Optional worker pool to use for async decoding.
  60. * If provided, numWorkers will be ignored and the worker pool will be used instead.
  61. * If provided the draco script will not be loaded from the DracoConfiguration.
  62. */
  63. workerPool?: AutoReleaseWorkerPool;
  64. }
  65. /**
  66. * Draco compression (https://google.github.io/draco/)
  67. *
  68. * This class wraps the Draco module.
  69. *
  70. * **Encoder**
  71. *
  72. * The encoder is not currently implemented.
  73. *
  74. * **Decoder**
  75. *
  76. * By default, the configuration points to a copy of the Draco decoder files for glTF from the babylon.js preview cdn https://preview.babylonjs.com/draco_wasm_wrapper_gltf.js.
  77. *
  78. * To update the configuration, use the following code:
  79. * ```javascript
  80. * DracoCompression.Configuration = {
  81. * decoder: {
  82. * wasmUrl: "<url to the WebAssembly library>",
  83. * wasmBinaryUrl: "<url to the WebAssembly binary>",
  84. * fallbackUrl: "<url to the fallback JavaScript library>",
  85. * }
  86. * };
  87. * ```
  88. *
  89. * Draco has two versions, one for WebAssembly and one for JavaScript. The decoder configuration can be set to only support WebAssembly or only support the JavaScript version.
  90. * Decoding will automatically fallback to the JavaScript version if WebAssembly version is not configured or if WebAssembly is not supported by the browser.
  91. * Use `DracoCompression.DecoderAvailable` to determine if the decoder configuration is available for the current context.
  92. *
  93. * To decode Draco compressed data, get the default DracoCompression object and call decodeMeshToGeometryAsync:
  94. * ```javascript
  95. * var geometry = await DracoCompression.Default.decodeMeshToGeometryAsync(data);
  96. * ```
  97. *
  98. * @see https://playground.babylonjs.com/#DMZIBD#0
  99. */
  100. export declare class DracoCompression implements IDisposable {
  101. private _workerPoolPromise?;
  102. private _decoderModulePromise?;
  103. /**
  104. * The configuration. Defaults to the following urls:
  105. * - wasmUrl: "https://cdn.babylonjs.com/draco_wasm_wrapper_gltf.js"
  106. * - wasmBinaryUrl: "https://cdn.babylonjs.com/draco_decoder_gltf.wasm"
  107. * - fallbackUrl: "https://cdn.babylonjs.com/draco_decoder_gltf.js"
  108. */
  109. static Configuration: IDracoCompressionConfiguration;
  110. /**
  111. * Returns true if the decoder configuration is available.
  112. */
  113. static get DecoderAvailable(): boolean;
  114. /**
  115. * Default number of workers to create when creating the draco compression object.
  116. */
  117. static DefaultNumWorkers: number;
  118. private static GetDefaultNumWorkers;
  119. private static _Default;
  120. /**
  121. * Default instance for the draco compression object.
  122. */
  123. static get Default(): DracoCompression;
  124. /**
  125. * Constructor
  126. * @param numWorkers The number of workers for async operations Or an options object. Specify `0` to disable web workers and run synchronously in the current context.
  127. */
  128. constructor(numWorkers?: number | IDracoCompressionOptions);
  129. /**
  130. * Stop all async operations and release resources.
  131. */
  132. dispose(): void;
  133. /**
  134. * Returns a promise that resolves when ready. Call this manually to ensure draco compression is ready before use.
  135. * @returns a promise that resolves when ready
  136. */
  137. whenReadyAsync(): Promise<void>;
  138. /**
  139. * Decode Draco compressed mesh data to mesh data.
  140. * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
  141. * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
  142. * @param gltfNormalizedOverride A map of attributes from vertex buffer kinds to normalized flags to override the Draco normalization
  143. * @returns A promise that resolves with the decoded mesh data
  144. */
  145. decodeMeshToMeshDataAsync(data: ArrayBuffer | ArrayBufferView, attributes?: {
  146. [kind: string]: number;
  147. }, gltfNormalizedOverride?: {
  148. [kind: string]: boolean;
  149. }): Promise<MeshData>;
  150. /**
  151. * Decode Draco compressed mesh data to Babylon geometry.
  152. * @param name The name to use when creating the geometry
  153. * @param scene The scene to use when creating the geometry
  154. * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
  155. * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
  156. * @returns A promise that resolves with the decoded geometry
  157. */
  158. decodeMeshToGeometryAsync(name: string, scene: Scene, data: ArrayBuffer | ArrayBufferView, attributes?: {
  159. [kind: string]: number;
  160. }): Promise<Geometry>;
  161. /** @internal */
  162. _decodeMeshToGeometryForGltfAsync(name: string, scene: Scene, data: ArrayBuffer | ArrayBufferView, attributes: {
  163. [kind: string]: number;
  164. }, gltfNormalizedOverride: {
  165. [kind: string]: boolean;
  166. }): Promise<Geometry>;
  167. /**
  168. * Decode Draco compressed mesh data to Babylon vertex data.
  169. * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
  170. * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
  171. * @returns A promise that resolves with the decoded vertex data
  172. * @deprecated Use {@link decodeMeshToGeometryAsync} for better performance in some cases
  173. */
  174. decodeMeshAsync(data: ArrayBuffer | ArrayBufferView, attributes?: {
  175. [kind: string]: number;
  176. }): Promise<VertexData>;
  177. }
  178. export {};