babylonjs.serializers.d.ts 24 KB

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