babylonjs.serializers.d.ts 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. * Gets the glTF alpha mode from the Babylon Material
  157. * @param babylonMaterial Babylon Material
  158. * @returns The Babylon alpha mode value
  159. */
  160. _getAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  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. * The sample rate to bake animation curves
  339. */
  340. animationSampleRate?: number;
  341. /**
  342. * Begin serialization without waiting for the scene to be ready
  343. */
  344. exportWithoutWaitingForScene?: boolean;
  345. }
  346. /**
  347. * Class for generating glTF data from a Babylon scene.
  348. */
  349. export class GLTF2Export {
  350. /**
  351. * Exports the geometry of the scene to .gltf file format asynchronously
  352. * @param scene Babylon scene with scene hierarchy information
  353. * @param filePrefix File prefix to use when generating the glTF file
  354. * @param options Exporter options
  355. * @returns Returns an object with a .gltf file and associates texture names
  356. * as keys and their data and paths as values
  357. */
  358. static GLTFAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData>;
  359. private static _PreExportAsync;
  360. private static _PostExportAsync;
  361. /**
  362. * Exports the geometry of the scene to .glb file format asychronously
  363. * @param scene Babylon scene with scene hierarchy information
  364. * @param filePrefix File prefix to use when generating glb file
  365. * @param options Exporter options
  366. * @returns Returns an object with a .glb filename as key and data as value
  367. */
  368. static GLBAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData>;
  369. }
  370. }
  371. declare module BABYLON.GLTF2.Exporter {
  372. /**
  373. * @hidden
  374. */
  375. export class _GLTFUtilities {
  376. /**
  377. * Creates a buffer view based on the supplied arguments
  378. * @param bufferIndex index value of the specified buffer
  379. * @param byteOffset byte offset value
  380. * @param byteLength byte length of the bufferView
  381. * @param byteStride byte distance between conequential elements
  382. * @param name name of the buffer view
  383. * @returns bufferView for glTF
  384. */
  385. static _CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView;
  386. /**
  387. * Creates an accessor based on the supplied arguments
  388. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  389. * @param name The name of the accessor
  390. * @param type The type of the accessor
  391. * @param componentType The datatype of components in the attribute
  392. * @param count The number of attributes referenced by this accessor
  393. * @param byteOffset The offset relative to the start of the bufferView in bytes
  394. * @param min Minimum value of each component in this attribute
  395. * @param max Maximum value of each component in this attribute
  396. * @returns accessor for glTF
  397. */
  398. static _CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor;
  399. /**
  400. * Calculates the minimum and maximum values of an array of position floats
  401. * @param positions Positions array of a mesh
  402. * @param vertexStart Starting vertex offset to calculate min and max values
  403. * @param vertexCount Number of vertices to check for min and max values
  404. * @returns min number array and max number array
  405. */
  406. static _CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): {
  407. min: number[];
  408. max: number[];
  409. };
  410. /**
  411. * Converts a new right-handed Vector3
  412. * @param vector vector3 array
  413. * @returns right-handed Vector3
  414. */
  415. static _GetRightHandedPositionVector3(vector: Vector3): Vector3;
  416. /**
  417. * Converts a Vector3 to right-handed
  418. * @param vector Vector3 to convert to right-handed
  419. */
  420. static _GetRightHandedPositionVector3FromRef(vector: Vector3): void;
  421. /**
  422. * Converts a three element number array to right-handed
  423. * @param vector number array to convert to right-handed
  424. */
  425. static _GetRightHandedPositionArray3FromRef(vector: number[]): void;
  426. /**
  427. * Converts a new right-handed Vector3
  428. * @param vector vector3 array
  429. * @returns right-handed Vector3
  430. */
  431. static _GetRightHandedNormalVector3(vector: Vector3): Vector3;
  432. /**
  433. * Converts a Vector3 to right-handed
  434. * @param vector Vector3 to convert to right-handed
  435. */
  436. static _GetRightHandedNormalVector3FromRef(vector: Vector3): void;
  437. /**
  438. * Converts a three element number array to right-handed
  439. * @param vector number array to convert to right-handed
  440. */
  441. static _GetRightHandedNormalArray3FromRef(vector: number[]): void;
  442. /**
  443. * Converts a Vector4 to right-handed
  444. * @param vector Vector4 to convert to right-handed
  445. */
  446. static _GetRightHandedVector4FromRef(vector: Vector4): void;
  447. /**
  448. * Converts a Vector4 to right-handed
  449. * @param vector Vector4 to convert to right-handed
  450. */
  451. static _GetRightHandedArray4FromRef(vector: number[]): void;
  452. /**
  453. * Converts a Quaternion to right-handed
  454. * @param quaternion Source quaternion to convert to right-handed
  455. */
  456. static _GetRightHandedQuaternionFromRef(quaternion: Quaternion): void;
  457. /**
  458. * Converts a Quaternion to right-handed
  459. * @param quaternion Source quaternion to convert to right-handed
  460. */
  461. static _GetRightHandedQuaternionArrayFromRef(quaternion: number[]): void;
  462. static _NormalizeTangentFromRef(tangent: Vector4): void;
  463. }
  464. }
  465. declare module BABYLON.GLTF2.Exporter {
  466. /**
  467. * Converts Babylon Scene into glTF 2.0.
  468. * @hidden
  469. */
  470. export class _Exporter {
  471. /**
  472. * Stores the glTF to export
  473. */
  474. _glTF: IGLTF;
  475. /**
  476. * Stores all generated buffer views, which represents views into the main glTF buffer data
  477. */
  478. _bufferViews: IBufferView[];
  479. /**
  480. * Stores all the generated accessors, which is used for accessing the data within the buffer views in glTF
  481. */
  482. _accessors: IAccessor[];
  483. /**
  484. * Stores all the generated nodes, which contains transform and/or mesh information per node
  485. */
  486. private _nodes;
  487. /**
  488. * Stores all the generated glTF scenes, which stores multiple node hierarchies
  489. */
  490. private _scenes;
  491. /**
  492. * Stores all the generated mesh information, each containing a set of primitives to render in glTF
  493. */
  494. private _meshes;
  495. /**
  496. * Stores all the generated material information, which represents the appearance of each primitive
  497. */
  498. _materials: IMaterial[];
  499. _materialMap: {
  500. [materialID: number]: number;
  501. };
  502. /**
  503. * Stores all the generated texture information, which is referenced by glTF materials
  504. */
  505. _textures: ITexture[];
  506. /**
  507. * Stores all the generated image information, which is referenced by glTF textures
  508. */
  509. _images: IImage[];
  510. /**
  511. * Stores all the texture samplers
  512. */
  513. _samplers: ISampler[];
  514. /**
  515. * Stores all the generated animation samplers, which is referenced by glTF animations
  516. */
  517. /**
  518. * Stores the animations for glTF models
  519. */
  520. private _animations;
  521. /**
  522. * Stores the total amount of bytes stored in the glTF buffer
  523. */
  524. private _totalByteLength;
  525. /**
  526. * Stores a reference to the Babylon scene containing the source geometry and material information
  527. */
  528. _babylonScene: Scene;
  529. /**
  530. * Stores a map of the image data, where the key is the file name and the value
  531. * is the image data
  532. */
  533. _imageData: {
  534. [fileName: string]: {
  535. data: Uint8Array;
  536. mimeType: ImageMimeType;
  537. };
  538. };
  539. /**
  540. * Stores a map of the unique id of a node to its index in the node array
  541. */
  542. private _nodeMap;
  543. /**
  544. * Specifies if the Babylon scene should be converted to right-handed on export
  545. */
  546. _convertToRightHandedSystem: boolean;
  547. /**
  548. * Baked animation sample rate
  549. */
  550. private _animationSampleRate;
  551. /**
  552. * Callback which specifies if a node should be exported or not
  553. */
  554. private _shouldExportNode;
  555. private _localEngine;
  556. _glTFMaterialExporter: _GLTFMaterialExporter;
  557. private _extensions;
  558. private static _ExtensionNames;
  559. private static _ExtensionFactories;
  560. private _applyExtensions;
  561. _extensionsPreExportTextureAsync(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Nullable<Promise<BaseTexture>>;
  562. _extensionsPostExportMeshPrimitiveAsync(context: string, meshPrimitive: IMeshPrimitive, babylonSubMesh: SubMesh, binaryWriter: _BinaryWriter): Nullable<Promise<IMeshPrimitive>>;
  563. _extensionsPostExportNodeAsync(context: string, node: INode, babylonNode: Node): Nullable<Promise<INode>>;
  564. private _forEachExtensions;
  565. private _extensionsOnExporting;
  566. /**
  567. * Load glTF serializer extensions
  568. */
  569. private _loadExtensions;
  570. /**
  571. * Creates a glTF Exporter instance, which can accept optional exporter options
  572. * @param babylonScene Babylon scene object
  573. * @param options Options to modify the behavior of the exporter
  574. */
  575. constructor(babylonScene: Scene, options?: IExportOptions);
  576. /**
  577. * Registers a glTF exporter extension
  578. * @param name Name of the extension to export
  579. * @param factory The factory function that creates the exporter extension
  580. */
  581. static RegisterExtension(name: string, factory: (exporter: _Exporter) => IGLTFExporterExtensionV2): void;
  582. /**
  583. * Un-registers an exporter extension
  584. * @param name The name fo the exporter extension
  585. * @returns A boolean indicating whether the extension has been un-registered
  586. */
  587. static UnregisterExtension(name: string): boolean;
  588. /**
  589. * Lazy load a local engine with premultiplied alpha set to false
  590. */
  591. _getLocalEngine(): Engine;
  592. private reorderIndicesBasedOnPrimitiveMode;
  593. /**
  594. * Reorders the vertex attribute data based on the primitive mode. This is necessary when indices are not available and the winding order is
  595. * clock-wise during export to glTF
  596. * @param submesh BabylonJS submesh
  597. * @param primitiveMode Primitive mode of the mesh
  598. * @param sideOrientation the winding order of the submesh
  599. * @param vertexBufferKind The type of vertex attribute
  600. * @param meshAttributeArray The vertex attribute data
  601. * @param byteOffset The offset to the binary data
  602. * @param binaryWriter The binary data for the glTF file
  603. */
  604. private reorderVertexAttributeDataBasedOnPrimitiveMode;
  605. /**
  606. * Reorders the vertex attributes in the correct triangle mode order . This is necessary when indices are not available and the winding order is
  607. * clock-wise during export to glTF
  608. * @param submesh BabylonJS submesh
  609. * @param primitiveMode Primitive mode of the mesh
  610. * @param sideOrientation the winding order of the submesh
  611. * @param vertexBufferKind The type of vertex attribute
  612. * @param meshAttributeArray The vertex attribute data
  613. * @param byteOffset The offset to the binary data
  614. * @param binaryWriter The binary data for the glTF file
  615. */
  616. private reorderTriangleFillMode;
  617. /**
  618. * Reorders the vertex attributes in the correct triangle strip order. This is necessary when indices are not available and the winding order is
  619. * clock-wise during export to glTF
  620. * @param submesh BabylonJS submesh
  621. * @param primitiveMode Primitive mode of the mesh
  622. * @param sideOrientation the winding order of the submesh
  623. * @param vertexBufferKind The type of vertex attribute
  624. * @param meshAttributeArray The vertex attribute data
  625. * @param byteOffset The offset to the binary data
  626. * @param binaryWriter The binary data for the glTF file
  627. */
  628. private reorderTriangleStripDrawMode;
  629. /**
  630. * Reorders the vertex attributes in the correct triangle fan order. This is necessary when indices are not available and the winding order is
  631. * clock-wise during export to glTF
  632. * @param submesh BabylonJS submesh
  633. * @param primitiveMode Primitive mode of the mesh
  634. * @param sideOrientation the winding order of the submesh
  635. * @param vertexBufferKind The type of vertex attribute
  636. * @param meshAttributeArray The vertex attribute data
  637. * @param byteOffset The offset to the binary data
  638. * @param binaryWriter The binary data for the glTF file
  639. */
  640. private reorderTriangleFanMode;
  641. /**
  642. * Writes the vertex attribute data to binary
  643. * @param vertices The vertices to write to the binary writer
  644. * @param byteOffset The offset into the binary writer to overwrite binary data
  645. * @param vertexAttributeKind The vertex attribute type
  646. * @param meshAttributeArray The vertex attribute data
  647. * @param binaryWriter The writer containing the binary data
  648. */
  649. private writeVertexAttributeData;
  650. /**
  651. * Writes mesh attribute data to a data buffer
  652. * Returns the bytelength of the data
  653. * @param vertexBufferKind Indicates what kind of vertex data is being passed in
  654. * @param meshAttributeArray Array containing the attribute data
  655. * @param binaryWriter The buffer to write the binary data to
  656. * @param indices Used to specify the order of the vertex data
  657. */
  658. writeAttributeData(vertexBufferKind: string, meshAttributeArray: FloatArray, byteStride: number, binaryWriter: _BinaryWriter): void;
  659. /**
  660. * Generates glTF json data
  661. * @param shouldUseGlb Indicates whether the json should be written for a glb file
  662. * @param glTFPrefix Text to use when prefixing a glTF file
  663. * @param prettyPrint Indicates whether the json file should be pretty printed (true) or not (false)
  664. * @returns json data as string
  665. */
  666. private generateJSON;
  667. /**
  668. * Generates data for .gltf and .bin files based on the glTF prefix string
  669. * @param glTFPrefix Text to use when prefixing a glTF file
  670. * @returns GLTFData with glTF file data
  671. */
  672. _generateGLTFAsync(glTFPrefix: string): Promise<GLTFData>;
  673. /**
  674. * Creates a binary buffer for glTF
  675. * @returns array buffer for binary data
  676. */
  677. private _generateBinaryAsync;
  678. /**
  679. * Pads the number to a multiple of 4
  680. * @param num number to pad
  681. * @returns padded number
  682. */
  683. private _getPadding;
  684. /**
  685. * Generates a glb file from the json and binary data
  686. * Returns an object with the glb file name as the key and data as the value
  687. * @param glTFPrefix
  688. * @returns object with glb filename as key and data as value
  689. */
  690. _generateGLBAsync(glTFPrefix: string): Promise<GLTFData>;
  691. /**
  692. * Sets the TRS for each node
  693. * @param node glTF Node for storing the transformation data
  694. * @param babylonTransformNode Babylon mesh used as the source for the transformation data
  695. */
  696. private setNodeTransformation;
  697. private getVertexBufferFromMesh;
  698. /**
  699. * Creates a bufferview based on the vertices type for the Babylon mesh
  700. * @param kind Indicates the type of vertices data
  701. * @param babylonTransformNode The Babylon mesh to get the vertices data from
  702. * @param binaryWriter The buffer to write the bufferview data to
  703. */
  704. private createBufferViewKind;
  705. /**
  706. * The primitive mode of the Babylon mesh
  707. * @param babylonMesh The BabylonJS mesh
  708. */
  709. private getMeshPrimitiveMode;
  710. /**
  711. * Sets the primitive mode of the glTF mesh primitive
  712. * @param meshPrimitive glTF mesh primitive
  713. * @param primitiveMode The primitive mode
  714. */
  715. private setPrimitiveMode;
  716. /**
  717. * Sets the vertex attribute accessor based of the glTF mesh primitive
  718. * @param meshPrimitive glTF mesh primitive
  719. * @param attributeKind vertex attribute
  720. * @returns boolean specifying if uv coordinates are present
  721. */
  722. private setAttributeKind;
  723. /**
  724. * Sets data for the primitive attributes of each submesh
  725. * @param mesh glTF Mesh object to store the primitive attribute information
  726. * @param babylonTransformNode Babylon mesh to get the primitive attribute data from
  727. * @param binaryWriter Buffer to write the attribute data to
  728. */
  729. private setPrimitiveAttributesAsync;
  730. /**
  731. * Creates a glTF scene based on the array of meshes
  732. * Returns the the total byte offset
  733. * @param babylonScene Babylon scene to get the mesh data from
  734. * @param binaryWriter Buffer to write binary data to
  735. */
  736. private createSceneAsync;
  737. /**
  738. * Creates a mapping of Node unique id to node index and handles animations
  739. * @param babylonScene Babylon Scene
  740. * @param nodes Babylon transform nodes
  741. * @param shouldExportNode Callback specifying if a transform node should be exported
  742. * @param binaryWriter Buffer to write binary data to
  743. * @returns Node mapping of unique id to index
  744. */
  745. private createNodeMapAndAnimationsAsync;
  746. /**
  747. * Creates a glTF node from a Babylon mesh
  748. * @param babylonMesh Source Babylon mesh
  749. * @param binaryWriter Buffer for storing geometry data
  750. * @returns glTF node
  751. */
  752. private createNodeAsync;
  753. }
  754. /**
  755. * @hidden
  756. *
  757. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  758. */
  759. export class _BinaryWriter {
  760. /**
  761. * Array buffer which stores all binary data
  762. */
  763. private _arrayBuffer;
  764. /**
  765. * View of the array buffer
  766. */
  767. private _dataView;
  768. /**
  769. * byte offset of data in array buffer
  770. */
  771. private _byteOffset;
  772. /**
  773. * Initialize binary writer with an initial byte length
  774. * @param byteLength Initial byte length of the array buffer
  775. */
  776. constructor(byteLength: number);
  777. /**
  778. * Resize the array buffer to the specified byte length
  779. * @param byteLength
  780. */
  781. private resizeBuffer;
  782. /**
  783. * Get an array buffer with the length of the byte offset
  784. * @returns ArrayBuffer resized to the byte offset
  785. */
  786. getArrayBuffer(): ArrayBuffer;
  787. /**
  788. * Get the byte offset of the array buffer
  789. * @returns byte offset
  790. */
  791. getByteOffset(): number;
  792. /**
  793. * Stores an UInt8 in the array buffer
  794. * @param entry
  795. * @param byteOffset If defined, specifies where to set the value as an offset.
  796. */
  797. setUInt8(entry: number, byteOffset?: number): void;
  798. /**
  799. * Gets an UInt32 in the array buffer
  800. * @param entry
  801. * @param byteOffset If defined, specifies where to set the value as an offset.
  802. */
  803. getUInt32(byteOffset: number): number;
  804. getVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  805. setVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  806. getVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  807. setVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  808. /**
  809. * Stores a Float32 in the array buffer
  810. * @param entry
  811. */
  812. setFloat32(entry: number, byteOffset?: number): void;
  813. /**
  814. * Stores an UInt32 in the array buffer
  815. * @param entry
  816. * @param byteOffset If defined, specifies where to set the value as an offset.
  817. */
  818. setUInt32(entry: number, byteOffset?: number): void;
  819. }
  820. }
  821. declare module BABYLON.GLTF2.Exporter {
  822. /**
  823. * @hidden
  824. * Interface to store animation data.
  825. */
  826. export interface _IAnimationData {
  827. /**
  828. * Keyframe data.
  829. */
  830. inputs: number[];
  831. /**
  832. * Value data.
  833. */
  834. outputs: number[][];
  835. /**
  836. * Animation interpolation data.
  837. */
  838. samplerInterpolation: AnimationSamplerInterpolation;
  839. /**
  840. * Minimum keyframe value.
  841. */
  842. inputsMin: number;
  843. /**
  844. * Maximum keyframe value.
  845. */
  846. inputsMax: number;
  847. }
  848. /**
  849. * @hidden
  850. */
  851. export interface _IAnimationInfo {
  852. /**
  853. * The target channel for the animation
  854. */
  855. animationChannelTargetPath: AnimationChannelTargetPath;
  856. /**
  857. * The glTF accessor type for the data.
  858. */
  859. dataAccessorType: AccessorType.VEC3 | AccessorType.VEC4;
  860. /**
  861. * Specifies if quaternions should be used.
  862. */
  863. useQuaternion: boolean;
  864. }
  865. /**
  866. * @hidden
  867. * Utility class for generating glTF animation data from BabylonJS.
  868. */
  869. export class _GLTFAnimation {
  870. /**
  871. * @ignore
  872. *
  873. * Creates glTF channel animation from BabylonJS animation.
  874. * @param babylonTransformNode - BabylonJS mesh.
  875. * @param animation - animation.
  876. * @param animationChannelTargetPath - The target animation channel.
  877. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  878. * @param useQuaternion - Specifies if quaternions are used.
  879. * @returns nullable IAnimationData
  880. */
  881. static _CreateNodeAnimation(babylonTransformNode: TransformNode, animation: Animation, animationChannelTargetPath: AnimationChannelTargetPath, convertToRightHandedSystem: boolean, useQuaternion: boolean, animationSampleRate: number): Nullable<_IAnimationData>;
  882. private static _DeduceAnimationInfo;
  883. /**
  884. * @ignore
  885. * Create node animations from the transform node animations
  886. * @param babylonNode
  887. * @param runtimeGLTFAnimation
  888. * @param idleGLTFAnimations
  889. * @param nodeMap
  890. * @param nodes
  891. * @param binaryWriter
  892. * @param bufferViews
  893. * @param accessors
  894. * @param convertToRightHandedSystem
  895. */
  896. static _CreateNodeAnimationFromNodeAnimations(babylonNode: Node, runtimeGLTFAnimation: IAnimation, idleGLTFAnimations: IAnimation[], nodeMap: {
  897. [key: number]: number;
  898. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  899. /**
  900. * @ignore
  901. * Create node animations from the animation groups
  902. * @param babylonScene
  903. * @param glTFAnimations
  904. * @param nodeMap
  905. * @param nodes
  906. * @param binaryWriter
  907. * @param bufferViews
  908. * @param accessors
  909. * @param convertToRightHandedSystem
  910. */
  911. static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: {
  912. [key: number]: number;
  913. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  914. private static AddAnimation;
  915. /**
  916. * Create a baked animation
  917. * @param babylonTransformNode BabylonJS mesh
  918. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  919. * @param animationChannelTargetPath animation target channel
  920. * @param minFrame minimum animation frame
  921. * @param maxFrame maximum animation frame
  922. * @param fps frames per second of the animation
  923. * @param inputs input key frames of the animation
  924. * @param outputs output key frame data of the animation
  925. * @param convertToRightHandedSystem converts the values to right-handed
  926. * @param useQuaternion specifies if quaternions should be used
  927. */
  928. private static _CreateBakedAnimation;
  929. private static _ConvertFactorToVector3OrQuaternion;
  930. private static _SetInterpolatedValue;
  931. /**
  932. * Creates linear animation from the animation key frames
  933. * @param babylonTransformNode BabylonJS mesh
  934. * @param animation BabylonJS animation
  935. * @param animationChannelTargetPath The target animation channel
  936. * @param frameDelta The difference between the last and first frame of the animation
  937. * @param inputs Array to store the key frame times
  938. * @param outputs Array to store the key frame data
  939. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  940. * @param useQuaternion Specifies if quaternions are used in the animation
  941. */
  942. private static _CreateLinearOrStepAnimation;
  943. /**
  944. * Creates cubic spline animation from the animation key frames
  945. * @param babylonTransformNode BabylonJS mesh
  946. * @param animation BabylonJS animation
  947. * @param animationChannelTargetPath The target animation channel
  948. * @param frameDelta The difference between the last and first frame of the animation
  949. * @param inputs Array to store the key frame times
  950. * @param outputs Array to store the key frame data
  951. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  952. * @param useQuaternion Specifies if quaternions are used in the animation
  953. */
  954. private static _CreateCubicSplineAnimation;
  955. private static _GetBasePositionRotationOrScale;
  956. /**
  957. * Adds a key frame value
  958. * @param keyFrame
  959. * @param animation
  960. * @param outputs
  961. * @param animationChannelTargetPath
  962. * @param basePositionRotationOrScale
  963. * @param convertToRightHandedSystem
  964. * @param useQuaternion
  965. */
  966. private static _AddKeyframeValue;
  967. /**
  968. * Determine the interpolation based on the key frames
  969. * @param keyFrames
  970. * @param animationChannelTargetPath
  971. * @param useQuaternion
  972. */
  973. private static _DeduceInterpolation;
  974. /**
  975. * Adds an input tangent or output tangent to the output data
  976. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  977. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  978. * @param outputs The animation data by keyframe
  979. * @param animationChannelTargetPath The target animation channel
  980. * @param interpolation The interpolation type
  981. * @param keyFrame The key frame with the animation data
  982. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  983. * @param useQuaternion Specifies if quaternions are used
  984. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  985. */
  986. private static AddSplineTangent;
  987. /**
  988. * Get the minimum and maximum key frames' frame values
  989. * @param keyFrames animation key frames
  990. * @returns the minimum and maximum key frame value
  991. */
  992. private static calculateMinMaxKeyFrames;
  993. }
  994. }
  995. declare module BABYLON.GLTF2.Exporter {
  996. /** @hidden */
  997. export var textureTransformPixelShader: {
  998. name: string;
  999. shader: string;
  1000. };
  1001. }
  1002. declare module BABYLON.GLTF2.Exporter.Extensions {
  1003. /**
  1004. * @hidden
  1005. */
  1006. export class KHR_texture_transform implements IGLTFExporterExtensionV2 {
  1007. /** Name of this extension */
  1008. readonly name: string;
  1009. /** Defines whether this extension is enabled */
  1010. enabled: boolean;
  1011. /** Defines whether this extension is required */
  1012. required: boolean;
  1013. /** Reference to the glTF exporter */
  1014. private _exporter;
  1015. constructor(exporter: _Exporter);
  1016. dispose(): void;
  1017. preExportTextureAsync(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Nullable<Promise<Texture>>;
  1018. /**
  1019. * Transform the babylon texture by the offset, rotation and scale parameters using a procedural texture
  1020. * @param babylonTexture
  1021. * @param offset
  1022. * @param rotation
  1023. * @param scale
  1024. * @param scene
  1025. */
  1026. textureTransformTextureAsync(babylonTexture: Texture, offset: Vector2, rotation: number, scale: Vector2, scene: Scene): Promise<BaseTexture>;
  1027. }
  1028. }
  1029. declare module BABYLON.GLTF2.Exporter.Extensions {
  1030. /**
  1031. * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_lights_punctual/README.md)
  1032. */
  1033. export class KHR_lights_punctual implements IGLTFExporterExtensionV2 {
  1034. /** The name of this extension. */
  1035. readonly name: string;
  1036. /** Defines whether this extension is enabled. */
  1037. enabled: boolean;
  1038. /** Defines whether this extension is required */
  1039. required: boolean;
  1040. /** Reference to the glTF exporter */
  1041. private _exporter;
  1042. private _lights;
  1043. /** @hidden */
  1044. constructor(exporter: _Exporter);
  1045. /** @hidden */
  1046. dispose(): void;
  1047. /** @hidden */
  1048. onExporting(): void;
  1049. /**
  1050. * Define this method to modify the default behavior when exporting a node
  1051. * @param context The context when exporting the node
  1052. * @param node glTF node
  1053. * @param babylonNode BabylonJS node
  1054. * @returns nullable INode promise
  1055. */
  1056. postExportNodeAsync(context: string, node: INode, babylonNode: Node): Nullable<Promise<INode>>;
  1057. }
  1058. }
  1059. declare module BABYLON {
  1060. /**
  1061. * Class for generating STL data from a Babylon scene.
  1062. */
  1063. export class STLExport {
  1064. /**
  1065. * Exports the geometry of a Mesh array in .STL file format (ASCII)
  1066. * @param meshes list defines the mesh to serialize
  1067. * @param download triggers the automatic download of the file.
  1068. * @param fileName changes the downloads fileName.
  1069. * @param binary changes the STL to a binary type.
  1070. * @param isLittleEndian toggle for binary type exporter.
  1071. * @returns the STL as UTF8 string
  1072. */
  1073. static CreateSTL(meshes: Mesh[], download?: boolean, fileName?: string, binary?: boolean, isLittleEndian?: boolean): any;
  1074. }
  1075. }