babylonjs.serializers.module.d.ts 24 KB

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