babylonjs.serializers.module.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /// <reference types="babylonjs"/>
  2. declare module 'babylonjs-serializers' {
  3. export = BABYLON;
  4. }
  5. declare module BABYLON {
  6. class OBJExport {
  7. static OBJ(mesh: Mesh[], materials?: boolean, matlibname?: string, globalposition?: boolean): string;
  8. static MTL(mesh: Mesh): string;
  9. }
  10. }
  11. declare module BABYLON {
  12. /**
  13. * Holds a collection of exporter options and parameters
  14. */
  15. interface IExporterOptions {
  16. /**
  17. * Function which indicates whether a babylon mesh should be exported or not.
  18. * @param mesh - source Babylon mesh. It is used to check whether it should be
  19. * exported to glTF or not.
  20. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  21. */
  22. shouldExportMesh?(mesh: AbstractMesh): boolean;
  23. }
  24. /**
  25. * Class for generating glTF data from a Babylon scene.
  26. */
  27. class GLTF2Export {
  28. /**
  29. * Exports the geometry of the scene to .gltf file format.
  30. * @param scene - Babylon scene with scene hierarchy information.
  31. * @param filePrefix - File prefix to use when generating the glTF file.
  32. * @param options - Exporter options.
  33. * @returns - Returns an object with a .gltf file and associates texture names
  34. * as keys and their data and paths as values.
  35. */
  36. static GLTF(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData;
  37. /**
  38. * Exports the geometry of the scene to .glb file format.
  39. * @param scene - Babylon scene with scene hierarchy information.
  40. * @param filePrefix - File prefix to use when generating glb file.
  41. * @param options - Exporter options.
  42. * @returns - Returns an object with a .glb filename as key and data as value
  43. */
  44. static GLB(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData;
  45. }
  46. }
  47. /**
  48. * Module for the Babylon glTF 2.0 exporter. Should ONLY be used internally.
  49. * @ignore - capitalization of GLTF2 module.
  50. */
  51. declare module BABYLON.GLTF2 {
  52. /**
  53. * Converts Babylon Scene into glTF 2.0.
  54. */
  55. class _Exporter {
  56. /**
  57. * Stores all generated buffer views, which represents views into the main glTF buffer data.
  58. */
  59. private bufferViews;
  60. /**
  61. * Stores all the generated accessors, which is used for accessing the data within the buffer views in glTF.
  62. */
  63. private accessors;
  64. /**
  65. * Stores all the generated nodes, which contains transform and/or mesh information per node.
  66. */
  67. private nodes;
  68. /**
  69. * Stores the glTF asset information, which represents the glTF version and this file generator.
  70. */
  71. private asset;
  72. /**
  73. * Stores all the generated glTF scenes, which stores multiple node hierarchies.
  74. */
  75. private scenes;
  76. /**
  77. * Stores all the generated mesh information, each containing a set of primitives to render in glTF.
  78. */
  79. private meshes;
  80. /**
  81. * Stores all the generated material information, which represents the appearance of each primitive.
  82. */
  83. private materials;
  84. /**
  85. * Stores all the generated texture information, which is referenced by glTF materials.
  86. */
  87. private textures;
  88. /**
  89. * Stores all the generated image information, which is referenced by glTF textures.
  90. */
  91. private images;
  92. /**
  93. * Stores the total amount of bytes stored in the glTF buffer.
  94. */
  95. private totalByteLength;
  96. /**
  97. * Stores a reference to the Babylon scene containing the source geometry and material information.
  98. */
  99. private babylonScene;
  100. /**
  101. * Stores the exporter options, which are optionally passed in from the glTF serializer.
  102. */
  103. private options?;
  104. /**
  105. * Stores a map of the image data, where the key is the file name and the value
  106. * is the image data.
  107. */
  108. private imageData;
  109. /**
  110. * Creates a glTF Exporter instance, which can accept optional exporter options.
  111. * @param babylonScene - Babylon scene object
  112. * @param options - Options to modify the behavior of the exporter.
  113. */
  114. constructor(babylonScene: Scene, options?: IExporterOptions);
  115. /**
  116. * Creates a buffer view based on teh supplied arguments
  117. * @param bufferIndex - index value of the specified buffer
  118. * @param byteOffset - byte offset value
  119. * @param byteLength - byte length of the bufferView
  120. * @param byteStride - byte distance between conequential elements.
  121. * @param name - name of the buffer view
  122. * @returns - bufferView for glTF
  123. */
  124. private createBufferView(bufferIndex, byteOffset, byteLength, byteStride?, name?);
  125. /**
  126. * Creates an accessor based on the supplied arguments
  127. * @param bufferviewIndex
  128. * @param name
  129. * @param type
  130. * @param componentType
  131. * @param count
  132. * @param min
  133. * @param max
  134. * @returns - accessor for glTF
  135. */
  136. private createAccessor(bufferviewIndex, name, type, componentType, count, byteOffset?, min?, max?);
  137. /**
  138. * Calculates the minimum and maximum values of an array of floats, based on stride
  139. * @param buff - Data to check for min and max values.
  140. * @param vertexStart - Start offset to calculate min and max values.
  141. * @param vertexCount - Number of vertices to check for min and max values.
  142. * @param stride - Offset between consecutive attributes.
  143. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  144. * @returns - min number array and max number array.
  145. */
  146. private calculateMinMax(buff, vertexStart, vertexCount, stride, useRightHandedSystem);
  147. /**
  148. * Writes mesh attribute data to a data buffer.
  149. * Returns the bytelength of the data.
  150. * @param vertexBufferKind - Indicates what kind of vertex data is being passed in.
  151. * @param meshAttributeArray - Array containing the attribute data.
  152. * @param strideSize - Represents the offset between consecutive attributes
  153. * @param byteOffset - The offset to start counting bytes from.
  154. * @param dataBuffer - The buffer to write the binary data to.
  155. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  156. * @returns - Byte length of the attribute data.
  157. */
  158. private writeAttributeData(vertexBufferKind, meshAttributeArray, strideSize, vertexBufferOffset, byteOffset, dataBuffer, useRightHandedSystem);
  159. /**
  160. * Generates glTF json data
  161. * @param shouldUseGlb - Indicates whether the json should be written for a glb file.
  162. * @param glTFPrefix - Text to use when prefixing a glTF file.
  163. * @param prettyPrint - Indicates whether the json file should be pretty printed (true) or not (false).
  164. * @returns - json data as string
  165. */
  166. private generateJSON(shouldUseGlb, glTFPrefix?, prettyPrint?);
  167. /**
  168. * Generates data for .gltf and .bin files based on the glTF prefix string
  169. * @param glTFPrefix - Text to use when prefixing a glTF file.
  170. * @returns - GLTFData with glTF file data.
  171. */
  172. _generateGLTF(glTFPrefix: string): _GLTFData;
  173. /**
  174. * Creates a binary buffer for glTF
  175. * @returns - array buffer for binary data
  176. */
  177. private generateBinary();
  178. /**
  179. * Pads the number to a multiple of 4
  180. * @param num - number to pad
  181. * @returns - padded number
  182. */
  183. private _getPadding(num);
  184. /**
  185. * Generates a glb file from the json and binary data.
  186. * Returns an object with the glb file name as the key and data as the value.
  187. * @param glTFPrefix
  188. * @returns - object with glb filename as key and data as value
  189. */
  190. _generateGLB(glTFPrefix: string): _GLTFData;
  191. /**
  192. * Sets the TRS for each node
  193. * @param node - glTF Node for storing the transformation data.
  194. * @param babylonMesh - Babylon mesh used as the source for the transformation data.
  195. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  196. */
  197. private setNodeTransformation(node, babylonMesh, useRightHandedSystem);
  198. /**
  199. *
  200. * @param babylonTexture - Babylon texture to extract.
  201. * @param mimeType - Mime Type of the babylonTexture.
  202. * @return - glTF texture, or null if the texture format is not supported.
  203. */
  204. private exportTexture(babylonTexture, mimeType?);
  205. /**
  206. * Creates a bufferview based on the vertices type for the Babylon mesh
  207. * @param kind - Indicates the type of vertices data.
  208. * @param babylonMesh - The Babylon mesh to get the vertices data from.
  209. * @param byteOffset - The offset from the buffer to start indexing from.
  210. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  211. * @param dataBuffer - The buffer to write the bufferview data to.
  212. * @returns bytelength of the bufferview data.
  213. */
  214. private createBufferViewKind(kind, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  215. /**
  216. * Sets data for the primitive attributes of each submesh
  217. * @param mesh - glTF Mesh object to store the primitive attribute information.
  218. * @param babylonMesh - Babylon mesh to get the primitive attribute data from.
  219. * @param byteOffset - The offset in bytes of the buffer data.
  220. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  221. * @param dataBuffer - Buffer to write the attribute data to.
  222. * @returns - bytelength of the primitive attributes plus the passed in byteOffset.
  223. */
  224. private setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  225. /**
  226. * Creates a glTF scene based on the array of meshes.
  227. * Returns the the total byte offset.
  228. * @param babylonScene - Babylon scene to get the mesh data from.
  229. * @param byteOffset - Offset to start from in bytes.
  230. * @param dataBuffer - Buffer to write geometry data to.
  231. * @returns bytelength + byteoffset
  232. */
  233. private createScene(babylonScene, byteOffset, dataBuffer);
  234. }
  235. }
  236. declare module BABYLON {
  237. /**
  238. * Class for holding and downloading glTF file data
  239. */
  240. class _GLTFData {
  241. /**
  242. * Object which contains the file name as the key and its data as the value.
  243. */
  244. glTFFiles: {
  245. [fileName: string]: string | Blob;
  246. };
  247. /**
  248. * Initializes the glTF file object.
  249. */
  250. constructor();
  251. /**
  252. * Downloads the glTF data as files based on their names and data.
  253. */
  254. downloadFiles(): void;
  255. }
  256. }
  257. declare module BABYLON.GLTF2 {
  258. /**
  259. * Utility methods for working with glTF material conversion properties. This class should only be used internally.
  260. */
  261. class _GLTFMaterial {
  262. /**
  263. * Represents the dielectric specular values for R, G and B.
  264. */
  265. private static readonly dielectricSpecular;
  266. /**
  267. * Epsilon value, used as a small tolerance value for a numeric value.
  268. */
  269. private static readonly epsilon;
  270. /**
  271. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material.
  272. * @param babylonStandardMaterial
  273. * @returns - glTF Metallic Roughness Material representation
  274. */
  275. static ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  276. /**
  277. * Converts Specular Glossiness to Metallic Roughness. This is based on the algorithm used in the Babylon glTF 3ds Max Exporter.
  278. * {@link https://github.com/BabylonJS/Exporters/blob/master/3ds%20Max/Max2Babylon/Exporter/BabylonExporter.GLTFExporter.Material.cs}
  279. * @param babylonSpecularGlossiness - Babylon specular glossiness parameters
  280. * @returns - Babylon metallic roughness values
  281. */
  282. private static _ConvertToMetallicRoughness(babylonSpecularGlossiness);
  283. /**
  284. * Returns the perceived brightness value based on the provided color
  285. * @param color - color used in calculating the perceived brightness
  286. * @returns - perceived brightness value
  287. */
  288. private static PerceivedBrightness(color);
  289. /**
  290. * Computes the metallic factor
  291. * @param diffuse - diffused value
  292. * @param specular - specular value
  293. * @param oneMinusSpecularStrength - one minus the specular strength
  294. * @returns - metallic value
  295. */
  296. static SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  297. /**
  298. * Gets the glTF alpha mode from the Babylon Material
  299. * @param babylonMaterial - Babylon Material
  300. * @returns - The Babylon alpha mode value
  301. */
  302. static GetAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  303. }
  304. }