babylonjs.serializers.module.d.ts 24 KB

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