babylon.glTF2Serializer.d.ts 24 KB

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