babylon.glTF2Serializer.d.ts 13 KB

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