babylonjs.serializers.d.ts 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. declare module BABYLON {
  2. /**
  3. * Class for generating OBJ data from a Babylon scene.
  4. */
  5. export class OBJExport {
  6. /**
  7. * Exports the geometry of a Mesh array in .OBJ file format (text)
  8. * @param mesh defines the list of meshes to serialize
  9. * @param materials defines if materials should be exported
  10. * @param matlibname defines the name of the associated mtl file
  11. * @param globalposition defines if the exported positions are globals or local to the exported mesh
  12. * @returns the OBJ content
  13. */
  14. static OBJ(mesh: Mesh[], materials?: boolean, matlibname?: string, globalposition?: boolean): string;
  15. /**
  16. * Exports the material(s) of a mesh in .MTL file format (text)
  17. * @param mesh defines the mesh to extract the material from
  18. * @returns the mtl content
  19. */
  20. static MTL(mesh: Mesh): string;
  21. }
  22. }
  23. declare module BABYLON {
  24. /** @hidden */
  25. export var __IGLTFExporterExtension: number;
  26. /**
  27. * Interface for extending the exporter
  28. * @hidden
  29. */
  30. export interface IGLTFExporterExtension {
  31. /**
  32. * The name of this extension
  33. */
  34. readonly name: string;
  35. /**
  36. * Defines whether this extension is enabled
  37. */
  38. enabled: boolean;
  39. /**
  40. * Defines whether this extension is required
  41. */
  42. required: boolean;
  43. }
  44. }
  45. declare module BABYLON.GLTF2.Exporter {
  46. /** @hidden */
  47. export var __IGLTFExporterExtensionV2: number;
  48. /**
  49. * Interface for a glTF exporter extension
  50. * @hidden
  51. */
  52. export interface IGLTFExporterExtensionV2 extends IGLTFExporterExtension, IDisposable {
  53. /**
  54. * Define this method to modify the default behavior before exporting a texture
  55. * @param context The context when loading the asset
  56. * @param babylonTexture The glTF texture info property
  57. * @param mimeType The mime-type of the generated image
  58. * @returns A promise that resolves with the exported glTF texture info when the export is complete, or null if not handled
  59. */
  60. preExportTextureAsync?(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Nullable<Promise<Texture>>;
  61. /**
  62. * Define this method to modify the default behavior when exporting texture info
  63. * @param context The context when loading the asset
  64. * @param meshPrimitive glTF mesh primitive
  65. * @param babylonSubMesh Babylon submesh
  66. * @param binaryWriter glTF serializer binary writer instance
  67. * @returns nullable IMeshPrimitive promise
  68. */
  69. postExportMeshPrimitiveAsync?(context: string, meshPrimitive: IMeshPrimitive, babylonSubMesh: SubMesh, binaryWriter: _BinaryWriter): Nullable<Promise<IMeshPrimitive>>;
  70. /**
  71. * Define this method to modify the default behavior when exporting a node
  72. * @param context The context when exporting the node
  73. * @param node glTF node
  74. * @param babylonNode BabylonJS node
  75. * @returns nullable INode promise
  76. */
  77. postExportNodeAsync?(context: string, node: INode, babylonNode: Node): Nullable<Promise<INode>>;
  78. /**
  79. * Called after the exporter state changes to EXPORTING
  80. */
  81. onExporting?(): void;
  82. }
  83. }
  84. declare module BABYLON.GLTF2.Exporter {
  85. /**
  86. * Utility methods for working with glTF material conversion properties. This class should only be used internally
  87. * @hidden
  88. */
  89. export class _GLTFMaterialExporter {
  90. /**
  91. * Represents the dielectric specular values for R, G and B
  92. */
  93. private static readonly _DielectricSpecular;
  94. /**
  95. * Allows the maximum specular power to be defined for material calculations
  96. */
  97. private static readonly _MaxSpecularPower;
  98. /**
  99. * Mapping to store textures
  100. */
  101. private _textureMap;
  102. /**
  103. * Numeric tolerance value
  104. */
  105. private static readonly _Epsilon;
  106. /**
  107. * Reference to the glTF Exporter
  108. */
  109. private _exporter;
  110. constructor(exporter: _Exporter);
  111. /**
  112. * Specifies if two colors are approximately equal in value
  113. * @param color1 first color to compare to
  114. * @param color2 second color to compare to
  115. * @param epsilon threshold value
  116. */
  117. private static FuzzyEquals;
  118. /**
  119. * Gets the materials from a Babylon scene and converts them to glTF materials
  120. * @param scene babylonjs scene
  121. * @param mimeType texture mime type
  122. * @param images array of images
  123. * @param textures array of textures
  124. * @param materials array of materials
  125. * @param imageData mapping of texture names to base64 textures
  126. * @param hasTextureCoords specifies if texture coordinates are present on the material
  127. */
  128. _convertMaterialsToGLTFAsync(babylonMaterials: Material[], mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  129. /**
  130. * Makes a copy of the glTF material without the texture parameters
  131. * @param originalMaterial original glTF material
  132. * @returns glTF material without texture parameters
  133. */
  134. _stripTexturesFromMaterial(originalMaterial: IMaterial): IMaterial;
  135. /**
  136. * Specifies if the material has any texture parameters present
  137. * @param material glTF Material
  138. * @returns boolean specifying if texture parameters are present
  139. */
  140. _hasTexturesPresent(material: IMaterial): boolean;
  141. /**
  142. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
  143. * @param babylonStandardMaterial
  144. * @returns glTF Metallic Roughness Material representation
  145. */
  146. _convertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  147. /**
  148. * Computes the metallic factor
  149. * @param diffuse diffused value
  150. * @param specular specular value
  151. * @param oneMinusSpecularStrength one minus the specular strength
  152. * @returns metallic value
  153. */
  154. static _SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  155. /**
  156. * Sets the glTF alpha mode to a glTF material from the Babylon Material
  157. * @param glTFMaterial glTF material
  158. * @param babylonMaterial Babylon material
  159. */
  160. private static _SetAlphaMode;
  161. /**
  162. * Converts a Babylon Standard Material to a glTF Material
  163. * @param babylonStandardMaterial BJS Standard Material
  164. * @param mimeType mime type to use for the textures
  165. * @param images array of glTF image interfaces
  166. * @param textures array of glTF texture interfaces
  167. * @param materials array of glTF material interfaces
  168. * @param imageData map of image file name to data
  169. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  170. */
  171. _convertStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  172. /**
  173. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  174. * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
  175. * @param mimeType mime type to use for the textures
  176. * @param images array of glTF image interfaces
  177. * @param textures array of glTF texture interfaces
  178. * @param materials array of glTF material interfaces
  179. * @param imageData map of image file name to data
  180. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  181. */
  182. _convertPBRMetallicRoughnessMaterialAsync(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  183. /**
  184. * Converts an image typed array buffer to a base64 image
  185. * @param buffer typed array buffer
  186. * @param width width of the image
  187. * @param height height of the image
  188. * @param mimeType mimetype of the image
  189. * @returns base64 image string
  190. */
  191. private _createBase64FromCanvasAsync;
  192. /**
  193. * Generates a white texture based on the specified width and height
  194. * @param width width of the texture in pixels
  195. * @param height height of the texture in pixels
  196. * @param scene babylonjs scene
  197. * @returns white texture
  198. */
  199. private _createWhiteTexture;
  200. /**
  201. * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null
  202. * @param texture1 first texture to resize
  203. * @param texture2 second texture to resize
  204. * @param scene babylonjs scene
  205. * @returns resized textures or null
  206. */
  207. private _resizeTexturesToSameDimensions;
  208. /**
  209. * Converts an array of pixels to a Float32Array
  210. * Throws an error if the pixel format is not supported
  211. * @param pixels - array buffer containing pixel values
  212. * @returns Float32 of pixels
  213. */
  214. private _convertPixelArrayToFloat32;
  215. /**
  216. * Convert Specular Glossiness Textures to Metallic Roughness
  217. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  218. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  219. * @param diffuseTexture texture used to store diffuse information
  220. * @param specularGlossinessTexture texture used to store specular and glossiness information
  221. * @param factors specular glossiness material factors
  222. * @param mimeType the mime type to use for the texture
  223. * @returns pbr metallic roughness interface or null
  224. */
  225. private _convertSpecularGlossinessTexturesToMetallicRoughnessAsync;
  226. /**
  227. * Converts specular glossiness material properties to metallic roughness
  228. * @param specularGlossiness interface with specular glossiness material properties
  229. * @returns interface with metallic roughness material properties
  230. */
  231. private _convertSpecularGlossinessToMetallicRoughness;
  232. /**
  233. * Calculates the surface reflectance, independent of lighting conditions
  234. * @param color Color source to calculate brightness from
  235. * @returns number representing the perceived brightness, or zero if color is undefined
  236. */
  237. private _getPerceivedBrightness;
  238. /**
  239. * Returns the maximum color component value
  240. * @param color
  241. * @returns maximum color component value, or zero if color is null or undefined
  242. */
  243. private _getMaxComponent;
  244. /**
  245. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
  246. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  247. * @param mimeType mime type to use for the textures
  248. * @param images array of glTF image interfaces
  249. * @param textures array of glTF texture interfaces
  250. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  251. * @param imageData map of image file name to data
  252. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  253. * @returns glTF PBR Metallic Roughness factors
  254. */
  255. private _convertMetalRoughFactorsToMetallicRoughnessAsync;
  256. private _getGLTFTextureSampler;
  257. private _getGLTFTextureWrapMode;
  258. private _getGLTFTextureWrapModesSampler;
  259. /**
  260. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
  261. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  262. * @param mimeType mime type to use for the textures
  263. * @param images array of glTF image interfaces
  264. * @param textures array of glTF texture interfaces
  265. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  266. * @param imageData map of image file name to data
  267. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  268. * @returns glTF PBR Metallic Roughness factors
  269. */
  270. private _convertSpecGlossFactorsToMetallicRoughnessAsync;
  271. /**
  272. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  273. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  274. * @param mimeType mime type to use for the textures
  275. * @param images array of glTF image interfaces
  276. * @param textures array of glTF texture interfaces
  277. * @param materials array of glTF material interfaces
  278. * @param imageData map of image file name to data
  279. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  280. */
  281. _convertPBRMaterialAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  282. private setMetallicRoughnessPbrMaterial;
  283. private getPixelsFromTexture;
  284. /**
  285. * Extracts a texture from a Babylon texture into file data and glTF data
  286. * @param babylonTexture Babylon texture to extract
  287. * @param mimeType Mime Type of the babylonTexture
  288. * @return glTF texture info, or null if the texture format is not supported
  289. */
  290. _exportTextureAsync(babylonTexture: BaseTexture, mimeType: ImageMimeType): Promise<Nullable<ITextureInfo>>;
  291. _exportTextureInfoAsync(babylonTexture: BaseTexture, mimeType: ImageMimeType): Promise<Nullable<ITextureInfo>>;
  292. /**
  293. * Builds a texture from base64 string
  294. * @param base64Texture base64 texture string
  295. * @param baseTextureName Name to use for the texture
  296. * @param mimeType image mime type for the texture
  297. * @param images array of images
  298. * @param textures array of textures
  299. * @param imageData map of image data
  300. * @returns glTF texture info, or null if the texture format is not supported
  301. */
  302. private _getTextureInfoFromBase64;
  303. }
  304. }
  305. declare module BABYLON {
  306. /**
  307. * Class for holding and downloading glTF file data
  308. */
  309. export class GLTFData {
  310. /**
  311. * Object which contains the file name as the key and its data as the value
  312. */
  313. glTFFiles: {
  314. [fileName: string]: string | Blob;
  315. };
  316. /**
  317. * Initializes the glTF file object
  318. */
  319. constructor();
  320. /**
  321. * Downloads the glTF data as files based on their names and data
  322. */
  323. downloadFiles(): void;
  324. }
  325. }
  326. declare module BABYLON {
  327. /**
  328. * Holds a collection of exporter options and parameters
  329. */
  330. export interface IExportOptions {
  331. /**
  332. * Function which indicates whether a babylon node should be exported or not
  333. * @param node source Babylon node. It is used to check whether it should be exported to glTF or not
  334. * @returns boolean, which indicates whether the node should be exported (true) or not (false)
  335. */
  336. shouldExportNode?(node: Node): boolean;
  337. /**
  338. * Function used to extract the part of node's metadata that will be exported into glTF node extras
  339. * @param metadata source metadata to read from
  340. * @returns the data to store to glTF node extras
  341. */
  342. metadataSelector?(metadata: any): any;
  343. /**
  344. * The sample rate to bake animation curves
  345. */
  346. animationSampleRate?: number;
  347. /**
  348. * Begin serialization without waiting for the scene to be ready
  349. */
  350. exportWithoutWaitingForScene?: boolean;
  351. }
  352. /**
  353. * Class for generating glTF data from a Babylon scene.
  354. */
  355. export class GLTF2Export {
  356. /**
  357. * Exports the geometry of the scene to .gltf file format asynchronously
  358. * @param scene Babylon scene with scene hierarchy information
  359. * @param filePrefix File prefix to use when generating the glTF file
  360. * @param options Exporter options
  361. * @returns Returns an object with a .gltf file and associates texture names
  362. * as keys and their data and paths as values
  363. */
  364. static GLTFAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData>;
  365. private static _PreExportAsync;
  366. private static _PostExportAsync;
  367. /**
  368. * Exports the geometry of the scene to .glb file format asychronously
  369. * @param scene Babylon scene with scene hierarchy information
  370. * @param filePrefix File prefix to use when generating glb file
  371. * @param options Exporter options
  372. * @returns Returns an object with a .glb filename as key and data as value
  373. */
  374. static GLBAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData>;
  375. }
  376. }
  377. declare module BABYLON.GLTF2.Exporter {
  378. /**
  379. * @hidden
  380. */
  381. export class _GLTFUtilities {
  382. /**
  383. * Creates a buffer view based on the supplied arguments
  384. * @param bufferIndex index value of the specified buffer
  385. * @param byteOffset byte offset value
  386. * @param byteLength byte length of the bufferView
  387. * @param byteStride byte distance between conequential elements
  388. * @param name name of the buffer view
  389. * @returns bufferView for glTF
  390. */
  391. static _CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView;
  392. /**
  393. * Creates an accessor based on the supplied arguments
  394. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  395. * @param name The name of the accessor
  396. * @param type The type of the accessor
  397. * @param componentType The datatype of components in the attribute
  398. * @param count The number of attributes referenced by this accessor
  399. * @param byteOffset The offset relative to the start of the bufferView in bytes
  400. * @param min Minimum value of each component in this attribute
  401. * @param max Maximum value of each component in this attribute
  402. * @returns accessor for glTF
  403. */
  404. static _CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor;
  405. /**
  406. * Calculates the minimum and maximum values of an array of position floats
  407. * @param positions Positions array of a mesh
  408. * @param vertexStart Starting vertex offset to calculate min and max values
  409. * @param vertexCount Number of vertices to check for min and max values
  410. * @returns min number array and max number array
  411. */
  412. static _CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): {
  413. min: number[];
  414. max: number[];
  415. };
  416. /**
  417. * Converts a new right-handed Vector3
  418. * @param vector vector3 array
  419. * @returns right-handed Vector3
  420. */
  421. static _GetRightHandedPositionVector3(vector: Vector3): Vector3;
  422. /**
  423. * Converts a Vector3 to right-handed
  424. * @param vector Vector3 to convert to right-handed
  425. */
  426. static _GetRightHandedPositionVector3FromRef(vector: Vector3): void;
  427. /**
  428. * Converts a three element number array to right-handed
  429. * @param vector number array to convert to right-handed
  430. */
  431. static _GetRightHandedPositionArray3FromRef(vector: number[]): void;
  432. /**
  433. * Converts a new right-handed Vector3
  434. * @param vector vector3 array
  435. * @returns right-handed Vector3
  436. */
  437. static _GetRightHandedNormalVector3(vector: Vector3): Vector3;
  438. /**
  439. * Converts a Vector3 to right-handed
  440. * @param vector Vector3 to convert to right-handed
  441. */
  442. static _GetRightHandedNormalVector3FromRef(vector: Vector3): void;
  443. /**
  444. * Converts a three element number array to right-handed
  445. * @param vector number array to convert to right-handed
  446. */
  447. static _GetRightHandedNormalArray3FromRef(vector: number[]): void;
  448. /**
  449. * Converts a Vector4 to right-handed
  450. * @param vector Vector4 to convert to right-handed
  451. */
  452. static _GetRightHandedVector4FromRef(vector: Vector4): void;
  453. /**
  454. * Converts a Vector4 to right-handed
  455. * @param vector Vector4 to convert to right-handed
  456. */
  457. static _GetRightHandedArray4FromRef(vector: number[]): void;
  458. /**
  459. * Converts a Quaternion to right-handed
  460. * @param quaternion Source quaternion to convert to right-handed
  461. */
  462. static _GetRightHandedQuaternionFromRef(quaternion: Quaternion): void;
  463. /**
  464. * Converts a Quaternion to right-handed
  465. * @param quaternion Source quaternion to convert to right-handed
  466. */
  467. static _GetRightHandedQuaternionArrayFromRef(quaternion: number[]): void;
  468. static _NormalizeTangentFromRef(tangent: Vector4): void;
  469. }
  470. }
  471. declare module BABYLON.GLTF2.Exporter {
  472. /**
  473. * Converts Babylon Scene into glTF 2.0.
  474. * @hidden
  475. */
  476. export class _Exporter {
  477. /**
  478. * Stores the glTF to export
  479. */
  480. _glTF: IGLTF;
  481. /**
  482. * Stores all generated buffer views, which represents views into the main glTF buffer data
  483. */
  484. _bufferViews: IBufferView[];
  485. /**
  486. * Stores all the generated accessors, which is used for accessing the data within the buffer views in glTF
  487. */
  488. _accessors: IAccessor[];
  489. /**
  490. * Stores all the generated nodes, which contains transform and/or mesh information per node
  491. */
  492. private _nodes;
  493. /**
  494. * Stores all the generated glTF scenes, which stores multiple node hierarchies
  495. */
  496. private _scenes;
  497. /**
  498. * Stores all the generated mesh information, each containing a set of primitives to render in glTF
  499. */
  500. private _meshes;
  501. /**
  502. * Stores all the generated material information, which represents the appearance of each primitive
  503. */
  504. _materials: IMaterial[];
  505. _materialMap: {
  506. [materialID: number]: number;
  507. };
  508. /**
  509. * Stores all the generated texture information, which is referenced by glTF materials
  510. */
  511. _textures: ITexture[];
  512. /**
  513. * Stores all the generated image information, which is referenced by glTF textures
  514. */
  515. _images: IImage[];
  516. /**
  517. * Stores all the texture samplers
  518. */
  519. _samplers: ISampler[];
  520. /**
  521. * Stores all the generated animation samplers, which is referenced by glTF animations
  522. */
  523. /**
  524. * Stores the animations for glTF models
  525. */
  526. private _animations;
  527. /**
  528. * Stores the total amount of bytes stored in the glTF buffer
  529. */
  530. private _totalByteLength;
  531. /**
  532. * Stores a reference to the Babylon scene containing the source geometry and material information
  533. */
  534. _babylonScene: Scene;
  535. /**
  536. * Stores a map of the image data, where the key is the file name and the value
  537. * is the image data
  538. */
  539. _imageData: {
  540. [fileName: string]: {
  541. data: Uint8Array;
  542. mimeType: ImageMimeType;
  543. };
  544. };
  545. /**
  546. * Stores a map of the unique id of a node to its index in the node array
  547. */
  548. private _nodeMap;
  549. /**
  550. * Specifies if the Babylon scene should be converted to right-handed on export
  551. */
  552. _convertToRightHandedSystem: boolean;
  553. /**
  554. * Baked animation sample rate
  555. */
  556. private _animationSampleRate;
  557. private _options;
  558. private _localEngine;
  559. _glTFMaterialExporter: _GLTFMaterialExporter;
  560. private _extensions;
  561. private static _ExtensionNames;
  562. private static _ExtensionFactories;
  563. private _applyExtensions;
  564. _extensionsPreExportTextureAsync(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Nullable<Promise<BaseTexture>>;
  565. _extensionsPostExportMeshPrimitiveAsync(context: string, meshPrimitive: IMeshPrimitive, babylonSubMesh: SubMesh, binaryWriter: _BinaryWriter): Nullable<Promise<IMeshPrimitive>>;
  566. _extensionsPostExportNodeAsync(context: string, node: INode, babylonNode: Node): Nullable<Promise<INode>>;
  567. private _forEachExtensions;
  568. private _extensionsOnExporting;
  569. /**
  570. * Load glTF serializer extensions
  571. */
  572. private _loadExtensions;
  573. /**
  574. * Creates a glTF Exporter instance, which can accept optional exporter options
  575. * @param babylonScene Babylon scene object
  576. * @param options Options to modify the behavior of the exporter
  577. */
  578. constructor(babylonScene: Scene, options?: IExportOptions);
  579. /**
  580. * Registers a glTF exporter extension
  581. * @param name Name of the extension to export
  582. * @param factory The factory function that creates the exporter extension
  583. */
  584. static RegisterExtension(name: string, factory: (exporter: _Exporter) => IGLTFExporterExtensionV2): void;
  585. /**
  586. * Un-registers an exporter extension
  587. * @param name The name fo the exporter extension
  588. * @returns A boolean indicating whether the extension has been un-registered
  589. */
  590. static UnregisterExtension(name: string): boolean;
  591. /**
  592. * Lazy load a local engine with premultiplied alpha set to false
  593. */
  594. _getLocalEngine(): Engine;
  595. private reorderIndicesBasedOnPrimitiveMode;
  596. /**
  597. * Reorders the vertex attribute data based on the primitive mode. This is necessary when indices are not available and the winding order is
  598. * clock-wise during export to glTF
  599. * @param submesh BabylonJS submesh
  600. * @param primitiveMode Primitive mode of the mesh
  601. * @param sideOrientation the winding order of the submesh
  602. * @param vertexBufferKind The type of vertex attribute
  603. * @param meshAttributeArray The vertex attribute data
  604. * @param byteOffset The offset to the binary data
  605. * @param binaryWriter The binary data for the glTF file
  606. */
  607. private reorderVertexAttributeDataBasedOnPrimitiveMode;
  608. /**
  609. * Reorders the vertex attributes in the correct triangle mode order . This is necessary when indices are not available and the winding order is
  610. * clock-wise during export to glTF
  611. * @param submesh BabylonJS submesh
  612. * @param primitiveMode Primitive mode of the mesh
  613. * @param sideOrientation the winding order of the submesh
  614. * @param vertexBufferKind The type of vertex attribute
  615. * @param meshAttributeArray The vertex attribute data
  616. * @param byteOffset The offset to the binary data
  617. * @param binaryWriter The binary data for the glTF file
  618. */
  619. private reorderTriangleFillMode;
  620. /**
  621. * Reorders the vertex attributes in the correct triangle strip order. This is necessary when indices are not available and the winding order is
  622. * clock-wise during export to glTF
  623. * @param submesh BabylonJS submesh
  624. * @param primitiveMode Primitive mode of the mesh
  625. * @param sideOrientation the winding order of the submesh
  626. * @param vertexBufferKind The type of vertex attribute
  627. * @param meshAttributeArray The vertex attribute data
  628. * @param byteOffset The offset to the binary data
  629. * @param binaryWriter The binary data for the glTF file
  630. */
  631. private reorderTriangleStripDrawMode;
  632. /**
  633. * Reorders the vertex attributes in the correct triangle fan order. This is necessary when indices are not available and the winding order is
  634. * clock-wise during export to glTF
  635. * @param submesh BabylonJS submesh
  636. * @param primitiveMode Primitive mode of the mesh
  637. * @param sideOrientation the winding order of the submesh
  638. * @param vertexBufferKind The type of vertex attribute
  639. * @param meshAttributeArray The vertex attribute data
  640. * @param byteOffset The offset to the binary data
  641. * @param binaryWriter The binary data for the glTF file
  642. */
  643. private reorderTriangleFanMode;
  644. /**
  645. * Writes the vertex attribute data to binary
  646. * @param vertices The vertices to write to the binary writer
  647. * @param byteOffset The offset into the binary writer to overwrite binary data
  648. * @param vertexAttributeKind The vertex attribute type
  649. * @param meshAttributeArray The vertex attribute data
  650. * @param binaryWriter The writer containing the binary data
  651. */
  652. private writeVertexAttributeData;
  653. /**
  654. * Writes mesh attribute data to a data buffer
  655. * Returns the bytelength of the data
  656. * @param vertexBufferKind Indicates what kind of vertex data is being passed in
  657. * @param meshAttributeArray Array containing the attribute data
  658. * @param binaryWriter The buffer to write the binary data to
  659. * @param indices Used to specify the order of the vertex data
  660. */
  661. writeAttributeData(vertexBufferKind: string, meshAttributeArray: FloatArray, byteStride: number, binaryWriter: _BinaryWriter): void;
  662. /**
  663. * Generates glTF json data
  664. * @param shouldUseGlb Indicates whether the json should be written for a glb file
  665. * @param glTFPrefix Text to use when prefixing a glTF file
  666. * @param prettyPrint Indicates whether the json file should be pretty printed (true) or not (false)
  667. * @returns json data as string
  668. */
  669. private generateJSON;
  670. /**
  671. * Generates data for .gltf and .bin files based on the glTF prefix string
  672. * @param glTFPrefix Text to use when prefixing a glTF file
  673. * @returns GLTFData with glTF file data
  674. */
  675. _generateGLTFAsync(glTFPrefix: string): Promise<GLTFData>;
  676. /**
  677. * Creates a binary buffer for glTF
  678. * @returns array buffer for binary data
  679. */
  680. private _generateBinaryAsync;
  681. /**
  682. * Pads the number to a multiple of 4
  683. * @param num number to pad
  684. * @returns padded number
  685. */
  686. private _getPadding;
  687. /**
  688. * Generates a glb file from the json and binary data
  689. * Returns an object with the glb file name as the key and data as the value
  690. * @param glTFPrefix
  691. * @returns object with glb filename as key and data as value
  692. */
  693. _generateGLBAsync(glTFPrefix: string): Promise<GLTFData>;
  694. /**
  695. * Sets the TRS for each node
  696. * @param node glTF Node for storing the transformation data
  697. * @param babylonTransformNode Babylon mesh used as the source for the transformation data
  698. */
  699. private setNodeTransformation;
  700. private getVertexBufferFromMesh;
  701. /**
  702. * Creates a bufferview based on the vertices type for the Babylon mesh
  703. * @param kind Indicates the type of vertices data
  704. * @param babylonTransformNode The Babylon mesh to get the vertices data from
  705. * @param binaryWriter The buffer to write the bufferview data to
  706. */
  707. private createBufferViewKind;
  708. /**
  709. * The primitive mode of the Babylon mesh
  710. * @param babylonMesh The BabylonJS mesh
  711. */
  712. private getMeshPrimitiveMode;
  713. /**
  714. * Sets the primitive mode of the glTF mesh primitive
  715. * @param meshPrimitive glTF mesh primitive
  716. * @param primitiveMode The primitive mode
  717. */
  718. private setPrimitiveMode;
  719. /**
  720. * Sets the vertex attribute accessor based of the glTF mesh primitive
  721. * @param meshPrimitive glTF mesh primitive
  722. * @param attributeKind vertex attribute
  723. * @returns boolean specifying if uv coordinates are present
  724. */
  725. private setAttributeKind;
  726. /**
  727. * Sets data for the primitive attributes of each submesh
  728. * @param mesh glTF Mesh object to store the primitive attribute information
  729. * @param babylonTransformNode Babylon mesh to get the primitive attribute data from
  730. * @param binaryWriter Buffer to write the attribute data to
  731. */
  732. private setPrimitiveAttributesAsync;
  733. /**
  734. * Creates a glTF scene based on the array of meshes
  735. * Returns the the total byte offset
  736. * @param babylonScene Babylon scene to get the mesh data from
  737. * @param binaryWriter Buffer to write binary data to
  738. */
  739. private createSceneAsync;
  740. /**
  741. * Creates a mapping of Node unique id to node index and handles animations
  742. * @param babylonScene Babylon Scene
  743. * @param nodes Babylon transform nodes
  744. * @param binaryWriter Buffer to write binary data to
  745. * @returns Node mapping of unique id to index
  746. */
  747. private createNodeMapAndAnimationsAsync;
  748. /**
  749. * Creates a glTF node from a Babylon mesh
  750. * @param babylonMesh Source Babylon mesh
  751. * @param binaryWriter Buffer for storing geometry data
  752. * @returns glTF node
  753. */
  754. private createNodeAsync;
  755. }
  756. /**
  757. * @hidden
  758. *
  759. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  760. */
  761. export class _BinaryWriter {
  762. /**
  763. * Array buffer which stores all binary data
  764. */
  765. private _arrayBuffer;
  766. /**
  767. * View of the array buffer
  768. */
  769. private _dataView;
  770. /**
  771. * byte offset of data in array buffer
  772. */
  773. private _byteOffset;
  774. /**
  775. * Initialize binary writer with an initial byte length
  776. * @param byteLength Initial byte length of the array buffer
  777. */
  778. constructor(byteLength: number);
  779. /**
  780. * Resize the array buffer to the specified byte length
  781. * @param byteLength
  782. */
  783. private resizeBuffer;
  784. /**
  785. * Get an array buffer with the length of the byte offset
  786. * @returns ArrayBuffer resized to the byte offset
  787. */
  788. getArrayBuffer(): ArrayBuffer;
  789. /**
  790. * Get the byte offset of the array buffer
  791. * @returns byte offset
  792. */
  793. getByteOffset(): number;
  794. /**
  795. * Stores an UInt8 in the array buffer
  796. * @param entry
  797. * @param byteOffset If defined, specifies where to set the value as an offset.
  798. */
  799. setUInt8(entry: number, byteOffset?: number): void;
  800. /**
  801. * Gets an UInt32 in the array buffer
  802. * @param entry
  803. * @param byteOffset If defined, specifies where to set the value as an offset.
  804. */
  805. getUInt32(byteOffset: number): number;
  806. getVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  807. setVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  808. getVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  809. setVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  810. /**
  811. * Stores a Float32 in the array buffer
  812. * @param entry
  813. */
  814. setFloat32(entry: number, byteOffset?: number): void;
  815. /**
  816. * Stores an UInt32 in the array buffer
  817. * @param entry
  818. * @param byteOffset If defined, specifies where to set the value as an offset.
  819. */
  820. setUInt32(entry: number, byteOffset?: number): void;
  821. }
  822. }
  823. declare module BABYLON.GLTF2.Exporter {
  824. /**
  825. * @hidden
  826. * Interface to store animation data.
  827. */
  828. export interface _IAnimationData {
  829. /**
  830. * Keyframe data.
  831. */
  832. inputs: number[];
  833. /**
  834. * Value data.
  835. */
  836. outputs: number[][];
  837. /**
  838. * Animation interpolation data.
  839. */
  840. samplerInterpolation: AnimationSamplerInterpolation;
  841. /**
  842. * Minimum keyframe value.
  843. */
  844. inputsMin: number;
  845. /**
  846. * Maximum keyframe value.
  847. */
  848. inputsMax: number;
  849. }
  850. /**
  851. * @hidden
  852. */
  853. export interface _IAnimationInfo {
  854. /**
  855. * The target channel for the animation
  856. */
  857. animationChannelTargetPath: AnimationChannelTargetPath;
  858. /**
  859. * The glTF accessor type for the data.
  860. */
  861. dataAccessorType: AccessorType.VEC3 | AccessorType.VEC4;
  862. /**
  863. * Specifies if quaternions should be used.
  864. */
  865. useQuaternion: boolean;
  866. }
  867. /**
  868. * @hidden
  869. * Utility class for generating glTF animation data from BabylonJS.
  870. */
  871. export class _GLTFAnimation {
  872. /**
  873. * @ignore
  874. *
  875. * Creates glTF channel animation from BabylonJS animation.
  876. * @param babylonTransformNode - BabylonJS mesh.
  877. * @param animation - animation.
  878. * @param animationChannelTargetPath - The target animation channel.
  879. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  880. * @param useQuaternion - Specifies if quaternions are used.
  881. * @returns nullable IAnimationData
  882. */
  883. static _CreateNodeAnimation(babylonTransformNode: TransformNode, animation: Animation, animationChannelTargetPath: AnimationChannelTargetPath, convertToRightHandedSystem: boolean, useQuaternion: boolean, animationSampleRate: number): Nullable<_IAnimationData>;
  884. private static _DeduceAnimationInfo;
  885. /**
  886. * @ignore
  887. * Create node animations from the transform node animations
  888. * @param babylonNode
  889. * @param runtimeGLTFAnimation
  890. * @param idleGLTFAnimations
  891. * @param nodeMap
  892. * @param nodes
  893. * @param binaryWriter
  894. * @param bufferViews
  895. * @param accessors
  896. * @param convertToRightHandedSystem
  897. */
  898. static _CreateNodeAnimationFromNodeAnimations(babylonNode: Node, runtimeGLTFAnimation: IAnimation, idleGLTFAnimations: IAnimation[], nodeMap: {
  899. [key: number]: number;
  900. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  901. /**
  902. * @ignore
  903. * Create node animations from the animation groups
  904. * @param babylonScene
  905. * @param glTFAnimations
  906. * @param nodeMap
  907. * @param nodes
  908. * @param binaryWriter
  909. * @param bufferViews
  910. * @param accessors
  911. * @param convertToRightHandedSystem
  912. */
  913. static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: {
  914. [key: number]: number;
  915. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  916. private static AddAnimation;
  917. /**
  918. * Create a baked animation
  919. * @param babylonTransformNode BabylonJS mesh
  920. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  921. * @param animationChannelTargetPath animation target channel
  922. * @param minFrame minimum animation frame
  923. * @param maxFrame maximum animation frame
  924. * @param fps frames per second of the animation
  925. * @param inputs input key frames of the animation
  926. * @param outputs output key frame data of the animation
  927. * @param convertToRightHandedSystem converts the values to right-handed
  928. * @param useQuaternion specifies if quaternions should be used
  929. */
  930. private static _CreateBakedAnimation;
  931. private static _ConvertFactorToVector3OrQuaternion;
  932. private static _SetInterpolatedValue;
  933. /**
  934. * Creates linear animation from the animation key frames
  935. * @param babylonTransformNode BabylonJS mesh
  936. * @param animation BabylonJS animation
  937. * @param animationChannelTargetPath The target animation channel
  938. * @param frameDelta The difference between the last and first frame of the animation
  939. * @param inputs Array to store the key frame times
  940. * @param outputs Array to store the key frame data
  941. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  942. * @param useQuaternion Specifies if quaternions are used in the animation
  943. */
  944. private static _CreateLinearOrStepAnimation;
  945. /**
  946. * Creates cubic spline animation from the animation key frames
  947. * @param babylonTransformNode BabylonJS mesh
  948. * @param animation BabylonJS animation
  949. * @param animationChannelTargetPath The target animation channel
  950. * @param frameDelta The difference between the last and first frame of the animation
  951. * @param inputs Array to store the key frame times
  952. * @param outputs Array to store the key frame data
  953. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  954. * @param useQuaternion Specifies if quaternions are used in the animation
  955. */
  956. private static _CreateCubicSplineAnimation;
  957. private static _GetBasePositionRotationOrScale;
  958. /**
  959. * Adds a key frame value
  960. * @param keyFrame
  961. * @param animation
  962. * @param outputs
  963. * @param animationChannelTargetPath
  964. * @param basePositionRotationOrScale
  965. * @param convertToRightHandedSystem
  966. * @param useQuaternion
  967. */
  968. private static _AddKeyframeValue;
  969. /**
  970. * Determine the interpolation based on the key frames
  971. * @param keyFrames
  972. * @param animationChannelTargetPath
  973. * @param useQuaternion
  974. */
  975. private static _DeduceInterpolation;
  976. /**
  977. * Adds an input tangent or output tangent to the output data
  978. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  979. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  980. * @param outputs The animation data by keyframe
  981. * @param animationChannelTargetPath The target animation channel
  982. * @param interpolation The interpolation type
  983. * @param keyFrame The key frame with the animation data
  984. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  985. * @param useQuaternion Specifies if quaternions are used
  986. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  987. */
  988. private static AddSplineTangent;
  989. /**
  990. * Get the minimum and maximum key frames' frame values
  991. * @param keyFrames animation key frames
  992. * @returns the minimum and maximum key frame value
  993. */
  994. private static calculateMinMaxKeyFrames;
  995. }
  996. }
  997. declare module BABYLON.GLTF2.Exporter {
  998. /** @hidden */
  999. export var textureTransformPixelShader: {
  1000. name: string;
  1001. shader: string;
  1002. };
  1003. }
  1004. declare module BABYLON.GLTF2.Exporter.Extensions {
  1005. /**
  1006. * @hidden
  1007. */
  1008. export class KHR_texture_transform implements IGLTFExporterExtensionV2 {
  1009. /** Name of this extension */
  1010. readonly name: string;
  1011. /** Defines whether this extension is enabled */
  1012. enabled: boolean;
  1013. /** Defines whether this extension is required */
  1014. required: boolean;
  1015. /** Reference to the glTF exporter */
  1016. private _exporter;
  1017. constructor(exporter: _Exporter);
  1018. dispose(): void;
  1019. preExportTextureAsync(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Nullable<Promise<Texture>>;
  1020. /**
  1021. * Transform the babylon texture by the offset, rotation and scale parameters using a procedural texture
  1022. * @param babylonTexture
  1023. * @param offset
  1024. * @param rotation
  1025. * @param scale
  1026. * @param scene
  1027. */
  1028. private _textureTransformTextureAsync;
  1029. }
  1030. }
  1031. declare module BABYLON.GLTF2.Exporter.Extensions {
  1032. /**
  1033. * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_lights_punctual/README.md)
  1034. */
  1035. export class KHR_lights_punctual implements IGLTFExporterExtensionV2 {
  1036. /** The name of this extension. */
  1037. readonly name: string;
  1038. /** Defines whether this extension is enabled. */
  1039. enabled: boolean;
  1040. /** Defines whether this extension is required */
  1041. required: boolean;
  1042. /** Reference to the glTF exporter */
  1043. private _exporter;
  1044. private _lights;
  1045. /** @hidden */
  1046. constructor(exporter: _Exporter);
  1047. /** @hidden */
  1048. dispose(): void;
  1049. /** @hidden */
  1050. onExporting(): void;
  1051. /**
  1052. * Define this method to modify the default behavior when exporting a node
  1053. * @param context The context when exporting the node
  1054. * @param node glTF node
  1055. * @param babylonNode BabylonJS node
  1056. * @returns nullable INode promise
  1057. */
  1058. postExportNodeAsync(context: string, node: INode, babylonNode: Node): Nullable<Promise<INode>>;
  1059. }
  1060. }
  1061. declare module BABYLON {
  1062. /**
  1063. * Class for generating STL data from a Babylon scene.
  1064. */
  1065. export class STLExport {
  1066. /**
  1067. * Exports the geometry of a Mesh array in .STL file format (ASCII)
  1068. * @param meshes list defines the mesh to serialize
  1069. * @param download triggers the automatic download of the file.
  1070. * @param fileName changes the downloads fileName.
  1071. * @param binary changes the STL to a binary type.
  1072. * @param isLittleEndian toggle for binary type exporter.
  1073. * @returns the STL as UTF8 string
  1074. */
  1075. static CreateSTL(meshes: Mesh[], download?: boolean, fileName?: string, binary?: boolean, isLittleEndian?: boolean): any;
  1076. }
  1077. }