babylonjs.serializers.module.d.ts 24 KB

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