babylon.glTF2Serializer.d.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. declare module BABYLON {
  2. /**
  3. * Holds a collection of exporter options and parameters
  4. */
  5. interface IExporterOptions {
  6. /**
  7. * Function which indicates whether a babylon mesh should be exported or not.
  8. * @param mesh - source Babylon mesh. It is used to check whether it should be
  9. * exported to glTF or not.
  10. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  11. */
  12. shouldExportMesh?(mesh: AbstractMesh): boolean;
  13. }
  14. /**
  15. * Class for generating glTF data from a Babylon scene.
  16. */
  17. class GLTF2Export {
  18. /**
  19. * Exports the geometry of the scene to .gltf file format.
  20. * @param scene - Babylon scene with scene hierarchy information.
  21. * @param filePrefix - File prefix to use when generating the glTF file.
  22. * @param options - Exporter options.
  23. * @returns - Returns an object with a .gltf file and associates texture names
  24. * as keys and their data and paths as values.
  25. */
  26. static GLTF(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData;
  27. /**
  28. * Exports the geometry of the scene to .glb file format.
  29. * @param scene - Babylon scene with scene hierarchy information.
  30. * @param filePrefix - File prefix to use when generating glb file.
  31. * @param options - Exporter options.
  32. * @returns - Returns an object with a .glb filename as key and data as value
  33. */
  34. static GLB(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData;
  35. }
  36. }
  37. /**
  38. * Module for the Babylon glTF 2.0 exporter. Should ONLY be used internally.
  39. * @ignore - capitalization of GLTF2 module.
  40. */
  41. declare module BABYLON.GLTF2 {
  42. /**
  43. * Converts Babylon Scene into glTF 2.0.
  44. */
  45. class _Exporter {
  46. /**
  47. * Stores all generated buffer views, which represents views into the main glTF buffer data.
  48. */
  49. private bufferViews;
  50. /**
  51. * Stores all the generated accessors, which is used for accessing the data within the buffer views in glTF.
  52. */
  53. private accessors;
  54. /**
  55. * Stores all the generated nodes, which contains transform and/or mesh information per node.
  56. */
  57. private nodes;
  58. /**
  59. * Stores the glTF asset information, which represents the glTF version and this file generator.
  60. */
  61. private asset;
  62. /**
  63. * Stores all the generated glTF scenes, which stores multiple node hierarchies.
  64. */
  65. private scenes;
  66. /**
  67. * Stores all the generated mesh information, each containing a set of primitives to render in glTF.
  68. */
  69. private meshes;
  70. /**
  71. * Stores all the generated material information, which represents the appearance of each primitive.
  72. */
  73. private materials;
  74. /**
  75. * Stores all the generated texture information, which is referenced by glTF materials.
  76. */
  77. private textures;
  78. /**
  79. * Stores all the generated image information, which is referenced by glTF textures.
  80. */
  81. private images;
  82. /**
  83. * Stores the total amount of bytes stored in the glTF buffer.
  84. */
  85. private totalByteLength;
  86. /**
  87. * Stores a reference to the Babylon scene containing the source geometry and material information.
  88. */
  89. private babylonScene;
  90. /**
  91. * Stores the exporter options, which are optionally passed in from the glTF serializer.
  92. */
  93. private options?;
  94. /**
  95. * Stores a map of the image data, where the key is the file name and the value
  96. * is the image data.
  97. */
  98. private imageData;
  99. /**
  100. * Stores a map of the unique id of a node to its index in the node array.
  101. */
  102. private nodeMap;
  103. /**
  104. * Stores the binary buffer used to store geometry data.
  105. */
  106. private binaryBuffer;
  107. /**
  108. * Specifies if the Babylon scene should be converted to right-handed on export.
  109. */
  110. private convertToRightHandedSystem;
  111. /**
  112. * Creates a glTF Exporter instance, which can accept optional exporter options.
  113. * @param babylonScene - Babylon scene object
  114. * @param options - Options to modify the behavior of the exporter.
  115. */
  116. constructor(babylonScene: Scene, options?: IExporterOptions);
  117. /**
  118. * Creates a buffer view based on teh supplied arguments
  119. * @param bufferIndex - index value of the specified buffer
  120. * @param byteOffset - byte offset value
  121. * @param byteLength - byte length of the bufferView
  122. * @param byteStride - byte distance between conequential elements.
  123. * @param name - name of the buffer view
  124. * @returns - bufferView for glTF
  125. */
  126. private createBufferView(bufferIndex, byteOffset, byteLength, byteStride?, name?);
  127. /**
  128. * Creates an accessor based on the supplied arguments
  129. * @param bufferviewIndex - The index of the bufferview referenced by this accessor.
  130. * @param name - The name of the accessor.
  131. * @param type - The type of the accessor.
  132. * @param componentType - The datatype of components in the attribute.
  133. * @param count - The number of attributes referenced by this accessor.
  134. * @param byteOffset - The offset relative to the start of the bufferView in bytes.
  135. * @param min - Minimum value of each component in this attribute.
  136. * @param max - Maximum value of each component in this attribute.
  137. * @returns - accessor for glTF
  138. */
  139. private createAccessor(bufferviewIndex, name, type, componentType, count, byteOffset, min, max);
  140. /**
  141. * Calculates the minimum and maximum values of an array of position floats.
  142. * @param positions - Positions array of a mesh.
  143. * @param vertexStart - Starting vertex offset to calculate min and max values.
  144. * @param vertexCount - Number of vertices to check for min and max values.
  145. * @returns - min number array and max number array.
  146. */
  147. private calculateMinMaxPositions(positions, vertexStart, vertexCount);
  148. /**
  149. * Converts a vector3 array to right-handed.
  150. * @param vector - vector3 Array to convert to right-handed.
  151. * @returns - right-handed Vector3 array.
  152. */
  153. private static GetRightHandedVector3(vector);
  154. /**
  155. * Converts a vector4 array to right-handed.
  156. * @param vector - vector4 Array to convert to right-handed.
  157. * @returns - right-handed vector4 array.
  158. */
  159. private static GetRightHandedVector4(vector);
  160. /**
  161. * Converts a quaternion to right-handed.
  162. * @param quaternion - Source quaternion to convert to right-handed.
  163. */
  164. private static GetRightHandedQuaternion(quaternion);
  165. /**
  166. * Writes mesh attribute data to a data buffer.
  167. * Returns the bytelength of the data.
  168. * @param vertexBufferKind - Indicates what kind of vertex data is being passed in.
  169. * @param meshAttributeArray - Array containing the attribute data.
  170. * @param byteOffset - The offset to start counting bytes from.
  171. * @param dataBuffer - The buffer to write the binary data to.
  172. * @returns - Byte length of the attribute data.
  173. */
  174. private writeAttributeData(vertexBufferKind, meshAttributeArray, byteOffset, dataBuffer);
  175. /**
  176. * Generates glTF json data
  177. * @param shouldUseGlb - Indicates whether the json should be written for a glb file.
  178. * @param glTFPrefix - Text to use when prefixing a glTF file.
  179. * @param prettyPrint - Indicates whether the json file should be pretty printed (true) or not (false).
  180. * @returns - json data as string
  181. */
  182. private generateJSON(shouldUseGlb, glTFPrefix?, prettyPrint?);
  183. /**
  184. * Generates data for .gltf and .bin files based on the glTF prefix string
  185. * @param glTFPrefix - Text to use when prefixing a glTF file.
  186. * @returns - GLTFData with glTF file data.
  187. */
  188. _generateGLTF(glTFPrefix: string): _GLTFData;
  189. /**
  190. * Creates a binary buffer for glTF
  191. * @returns - array buffer for binary data
  192. */
  193. private generateBinary();
  194. /**
  195. * Pads the number to a multiple of 4
  196. * @param num - number to pad
  197. * @returns - padded number
  198. */
  199. private _getPadding(num);
  200. /**
  201. * Generates a glb file from the json and binary data.
  202. * Returns an object with the glb file name as the key and data as the value.
  203. * @param glTFPrefix
  204. * @returns - object with glb filename as key and data as value
  205. */
  206. _generateGLB(glTFPrefix: string): _GLTFData;
  207. /**
  208. * Sets the TRS for each node
  209. * @param node - glTF Node for storing the transformation data.
  210. * @param babylonMesh - Babylon mesh used as the source for the transformation data.
  211. */
  212. private setNodeTransformation(node, babylonMesh);
  213. /**
  214. * Creates a bufferview based on the vertices type for the Babylon mesh
  215. * @param kind - Indicates the type of vertices data.
  216. * @param babylonMesh - The Babylon mesh to get the vertices data from.
  217. * @param byteOffset - The offset from the buffer to start indexing from.
  218. * @param dataBuffer - The buffer to write the bufferview data to.
  219. * @returns bytelength of the bufferview data.
  220. */
  221. private createBufferViewKind(kind, babylonMesh, byteOffset, dataBuffer);
  222. /**
  223. * Sets data for the primitive attributes of each submesh
  224. * @param mesh - glTF Mesh object to store the primitive attribute information.
  225. * @param babylonMesh - Babylon mesh to get the primitive attribute data from.
  226. * @param byteOffset - The offset in bytes of the buffer data.
  227. * @param dataBuffer - Buffer to write the attribute data to.
  228. * @returns - bytelength of the primitive attributes plus the passed in byteOffset.
  229. */
  230. private setPrimitiveAttributes(mesh, babylonMesh, byteOffset, dataBuffer);
  231. /**
  232. * Creates a glTF scene based on the array of meshes.
  233. * Returns the the total byte offset.
  234. * @param babylonScene - Babylon scene to get the mesh data from.
  235. * @param byteOffset - Offset to start from in bytes.
  236. * @returns bytelength + byteoffset
  237. */
  238. private createScene(babylonScene, byteOffset);
  239. /**
  240. * Creates a mapping of Node unique id to node index
  241. * @param scene - Babylon Scene.
  242. * @param byteOffset - The initial byte offset.
  243. * @returns - Node mapping of unique id to index.
  244. */
  245. private createNodeMap(scene, byteOffset);
  246. /**
  247. * Creates a glTF node from a Babylon mesh.
  248. * @param babylonMesh - Source Babylon mesh.
  249. * @param byteOffset - The initial byte offset.
  250. * @param dataBuffer - Buffer for storing geometry data.
  251. * @returns - Object containing an INode and byteoffset.
  252. */
  253. private createNode(babylonMesh, byteOffset, dataBuffer);
  254. }
  255. }
  256. declare module BABYLON {
  257. /**
  258. * Class for holding and downloading glTF file data
  259. */
  260. class _GLTFData {
  261. /**
  262. * Object which contains the file name as the key and its data as the value.
  263. */
  264. glTFFiles: {
  265. [fileName: string]: string | Blob;
  266. };
  267. /**
  268. * Initializes the glTF file object.
  269. */
  270. constructor();
  271. /**
  272. * Downloads the glTF data as files based on their names and data.
  273. */
  274. downloadFiles(): void;
  275. }
  276. }
  277. declare module BABYLON.GLTF2 {
  278. /**
  279. * Utility methods for working with glTF material conversion properties. This class should only be used internally.
  280. */
  281. class _GLTFMaterial {
  282. /**
  283. * Represents the dielectric specular values for R, G and B.
  284. */
  285. private static readonly _dielectricSpecular;
  286. /**
  287. * Allows the maximum specular power to be defined for material calculations.
  288. */
  289. private static _maxSpecularPower;
  290. /**
  291. * Numeric tolerance value
  292. */
  293. private static _epsilon;
  294. /**
  295. * Specifies if two colors are approximately equal in value.
  296. * @param color1 - first color to compare to.
  297. * @param color2 - second color to compare to.
  298. * @param epsilon - threshold value
  299. */
  300. private static FuzzyEquals(color1, color2, epsilon);
  301. /**
  302. * Gets the materials from a Babylon scene and converts them to glTF materials.
  303. * @param scene - babylonjs scene.
  304. * @param mimeType - texture mime type.
  305. * @param images - array of images.
  306. * @param textures - array of textures.
  307. * @param materials - array of materials.
  308. * @param imageData - mapping of texture names to base64 textures
  309. * @param hasTextureCoords - specifies if texture coordinates are present on the material.
  310. */
  311. static _ConvertMaterialsToGLTF(babylonMaterials: Material[], mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  312. [fileName: string]: {
  313. data: Uint8Array;
  314. mimeType: ImageMimeType;
  315. };
  316. }, hasTextureCoords: boolean): void;
  317. /**
  318. * Makes a copy of the glTF material without the texture parameters.
  319. * @param originalMaterial - original glTF material.
  320. * @returns glTF material without texture parameters
  321. */
  322. static _StripTexturesFromMaterial(originalMaterial: IMaterial): IMaterial;
  323. /**
  324. * Specifies if the material has any texture parameters present.
  325. * @param material - glTF Material.
  326. * @returns boolean specifying if texture parameters are present
  327. */
  328. static _HasTexturesPresent(material: IMaterial): boolean;
  329. /**
  330. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material.
  331. * @param babylonStandardMaterial
  332. * @returns - glTF Metallic Roughness Material representation
  333. */
  334. static _ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  335. /**
  336. * Computes the metallic factor
  337. * @param diffuse - diffused value
  338. * @param specular - specular value
  339. * @param oneMinusSpecularStrength - one minus the specular strength
  340. * @returns - metallic value
  341. */
  342. static _SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  343. /**
  344. * Gets the glTF alpha mode from the Babylon Material
  345. * @param babylonMaterial - Babylon Material
  346. * @returns - The Babylon alpha mode value
  347. */
  348. static _GetAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  349. /**
  350. * Converts a Babylon Standard Material to a glTF Material.
  351. * @param babylonStandardMaterial - BJS Standard Material.
  352. * @param mimeType - mime type to use for the textures.
  353. * @param images - array of glTF image interfaces.
  354. * @param textures - array of glTF texture interfaces.
  355. * @param materials - array of glTF material interfaces.
  356. * @param imageData - map of image file name to data.
  357. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  358. */
  359. static _ConvertStandardMaterial(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  360. [fileName: string]: {
  361. data: Uint8Array;
  362. mimeType: ImageMimeType;
  363. };
  364. }, hasTextureCoords: boolean): void;
  365. /**
  366. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
  367. * @param babylonPBRMetalRoughMaterial - BJS PBR Metallic Roughness Material.
  368. * @param mimeType - mime type to use for the textures.
  369. * @param images - array of glTF image interfaces.
  370. * @param textures - array of glTF texture interfaces.
  371. * @param materials - array of glTF material interfaces.
  372. * @param imageData - map of image file name to data.
  373. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  374. */
  375. static _ConvertPBRMetallicRoughnessMaterial(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  376. [fileName: string]: {
  377. data: Uint8Array;
  378. mimeType: ImageMimeType;
  379. };
  380. }, hasTextureCoords: boolean): void;
  381. /**
  382. * Converts an image typed array buffer to a base64 image.
  383. * @param buffer - typed array buffer.
  384. * @param width - width of the image.
  385. * @param height - height of the image.
  386. * @param mimeType - mimetype of the image.
  387. * @returns - base64 image string.
  388. */
  389. private static _CreateBase64FromCanvas(buffer, width, height, mimeType);
  390. /**
  391. * Generates a white texture based on the specified width and height.
  392. * @param width - width of the texture in pixels.
  393. * @param height - height of the texture in pixels.
  394. * @param scene - babylonjs scene.
  395. * @returns - white texture.
  396. */
  397. private static _CreateWhiteTexture(width, height, scene);
  398. /**
  399. * 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.
  400. * @param texture1 - first texture to resize.
  401. * @param texture2 - second texture to resize.
  402. * @param scene - babylonjs scene.
  403. * @returns resized textures or null.
  404. */
  405. private static _ResizeTexturesToSameDimensions(texture1, texture2, scene);
  406. /**
  407. * Convert Specular Glossiness Textures to Metallic Roughness.
  408. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  409. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  410. * @param diffuseTexture - texture used to store diffuse information.
  411. * @param specularGlossinessTexture - texture used to store specular and glossiness information.
  412. * @param factors - specular glossiness material factors.
  413. * @param mimeType - the mime type to use for the texture.
  414. * @returns pbr metallic roughness interface or null.
  415. */
  416. private static _ConvertSpecularGlossinessTexturesToMetallicRoughness(diffuseTexture, specularGlossinessTexture, factors, mimeType);
  417. /**
  418. * Converts specular glossiness material properties to metallic roughness.
  419. * @param specularGlossiness - interface with specular glossiness material properties.
  420. * @returns - interface with metallic roughness material properties.
  421. */
  422. private static _ConvertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  423. /**
  424. * Calculates the surface reflectance, independent of lighting conditions.
  425. * @param color - Color source to calculate brightness from.
  426. * @returns number representing the perceived brightness, or zero if color is undefined.
  427. */
  428. private static _GetPerceivedBrightness(color);
  429. /**
  430. * Returns the maximum color component value.
  431. * @param color
  432. * @returns maximum color component value, or zero if color is null or undefined.
  433. */
  434. private static _GetMaxComponent(color);
  435. /**
  436. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors.
  437. * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
  438. * @param mimeType - mime type to use for the textures.
  439. * @param images - array of glTF image interfaces.
  440. * @param textures - array of glTF texture interfaces.
  441. * @param glTFPbrMetallicRoughness - glTF PBR Metallic Roughness interface.
  442. * @param imageData - map of image file name to data.
  443. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  444. * @returns - glTF PBR Metallic Roughness factors.
  445. */
  446. private static _ConvertMetalRoughFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
  447. /**
  448. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors.
  449. * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
  450. * @param mimeType - mime type to use for the textures.
  451. * @param images - array of glTF image interfaces.
  452. * @param textures - array of glTF texture interfaces.
  453. * @param glTFPbrMetallicRoughness - glTF PBR Metallic Roughness interface.
  454. * @param imageData - map of image file name to data.
  455. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  456. * @returns - glTF PBR Metallic Roughness factors.
  457. */
  458. private static _ConvertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
  459. /**
  460. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
  461. * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
  462. * @param mimeType - mime type to use for the textures.
  463. * @param images - array of glTF image interfaces.
  464. * @param textures - array of glTF texture interfaces.
  465. * @param materials - array of glTF material interfaces.
  466. * @param imageData - map of image file name to data.
  467. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  468. */
  469. static _ConvertPBRMaterial(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  470. [fileName: string]: {
  471. data: Uint8Array;
  472. mimeType: ImageMimeType;
  473. };
  474. }, hasTextureCoords: boolean): void;
  475. /**
  476. * Extracts a texture from a Babylon texture into file data and glTF data.
  477. * @param babylonTexture - Babylon texture to extract.
  478. * @param mimeType - Mime Type of the babylonTexture.
  479. * @param images - Array of glTF images.
  480. * @param textures - Array of glTF textures.
  481. * @param imageData - map of image file name and data.
  482. * @return - glTF texture info, or null if the texture format is not supported.
  483. */
  484. private static _ExportTexture(babylonTexture, mimeType, images, textures, imageData);
  485. /**
  486. * Builds a texture from base64 string.
  487. * @param base64Texture - base64 texture string.
  488. * @param textureName - Name to use for the texture.
  489. * @param mimeType - image mime type for the texture.
  490. * @param images - array of images.
  491. * @param textures - array of textures.
  492. * @param imageData - map of image data.
  493. * @returns - glTF texture info, or null if the texture format is not supported.
  494. */
  495. private static _GetTextureInfoFromBase64(base64Texture, textureName, mimeType, images, textures, imageData);
  496. }
  497. }