babylonjs.serializers.d.ts 45 KB

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