babylon.glTF2Serializer.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. declare module BABYLON {
  2. interface IGLTFExporterOptions {
  3. /**
  4. * Interface function which indicates whether a babylon mesh should be exported or not.
  5. * @param mesh
  6. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  7. */
  8. shouldExportMesh?(mesh: AbstractMesh): boolean;
  9. }
  10. class GLTF2Export {
  11. /**
  12. * Exports the geometry of a Mesh array in .gltf file format.
  13. * @param meshes
  14. * @param materials
  15. * @param options
  16. *
  17. * @returns - Returns an object with a .gltf, .glb and associates textures
  18. * as keys and their data and paths as values.
  19. */
  20. static GLTF(scene: Scene, filename: string, options?: IGLTFExporterOptions): _GLTFData;
  21. /**
  22. *
  23. * @param meshes
  24. * @param filename
  25. *
  26. * @returns - Returns an object with a .glb filename as key and data as value
  27. */
  28. static GLB(scene: Scene, filename: string, options?: IGLTFExporterOptions): _GLTFData;
  29. }
  30. }
  31. declare module BABYLON {
  32. /**
  33. * glTF Alpha Mode Enum
  34. */
  35. enum _EGLTFAlphaModeEnum {
  36. OPAQUE = "OPAQUE",
  37. MASK = "MASK",
  38. BLEND = "BLEND",
  39. }
  40. /**
  41. * Babylon Specular Glossiness interface
  42. */
  43. interface _IBabylonSpecularGlossiness {
  44. diffuse: Color3;
  45. opacity: number;
  46. specular: Color3;
  47. glossiness: number;
  48. }
  49. /**
  50. * Babylon Metallic Roughness interface
  51. */
  52. interface _IBabylonMetallicRoughness {
  53. baseColor: Color3;
  54. opacity: number;
  55. metallic: number;
  56. roughness: number;
  57. }
  58. /**
  59. * Converts Babylon Scene into glTF 2.0
  60. */
  61. class _GLTF2Exporter {
  62. private bufferViews;
  63. private accessors;
  64. private nodes;
  65. private asset;
  66. private scenes;
  67. private meshes;
  68. private materials;
  69. private textures;
  70. private images;
  71. private totalByteLength;
  72. private babylonScene;
  73. private options?;
  74. private imageData;
  75. constructor(babylonScene: Scene, options?: IGLTFExporterOptions);
  76. /**
  77. * Creates a buffer view based on teh supplied arguments
  78. * @param {number} bufferIndex - index value of the specified buffer
  79. * @param {number} byteOffset - byte offset value
  80. * @param {number} byteLength - byte length of the bufferView
  81. * @returns - bufferView for glTF
  82. */
  83. private createBufferView(bufferIndex, byteOffset, byteLength, name?);
  84. /**
  85. * Creates an accessor based on the supplied arguments
  86. * @param bufferviewIndex
  87. * @param name
  88. * @param type
  89. * @param componentType
  90. * @param count
  91. * @param min
  92. * @param max
  93. * @returns - accessor for glTF
  94. */
  95. private createAccessor(bufferviewIndex, name, type, componentType, count, min?, max?);
  96. /**
  97. * Calculates the minimum and maximum values of an array of floats, based on stride
  98. * @param buff
  99. * @param vertexStart
  100. * @param vertexCount
  101. * @param arrayOffset
  102. * @param stride
  103. * @returns - min number array and max number array
  104. */
  105. private calculateMinMax(buff, vertexStart, vertexCount, arrayOffset, stride);
  106. /**
  107. * Write mesh attribute data to buffer.
  108. * Returns the bytelength of the data.
  109. * @param vertexBufferType
  110. * @param submesh
  111. * @param meshAttributeArray
  112. * @param strideSize
  113. * @param byteOffset
  114. * @param dataBuffer
  115. * @param useRightHandedSystem
  116. * @returns - byte length
  117. */
  118. private writeAttributeData(vertexBufferType, submesh, meshAttributeArray, strideSize, byteOffset, dataBuffer, useRightHandedSystem);
  119. /**
  120. * Generates glTF json data
  121. * @param glb
  122. * @param glTFPrefix
  123. * @param prettyPrint
  124. * @returns - json data as string
  125. */
  126. private generateJSON(glb, glTFPrefix?, prettyPrint?);
  127. /**
  128. * Generates data for .gltf and .bin files based on the glTF prefix string
  129. * @param glTFPrefix
  130. * @returns - object with glTF json tex filename
  131. * and binary file name as keys and their data as values
  132. */
  133. _generateGLTF(glTFPrefix: string): _GLTFData;
  134. /**
  135. * Creates a binary buffer for glTF
  136. * @returns - array buffer for binary data
  137. */
  138. private generateBinary();
  139. /**
  140. * Pads the number to a power of 4
  141. * @param num - number to pad
  142. * @returns - padded number
  143. */
  144. private _getPadding(num);
  145. /**
  146. * Generates a glb file from the json and binary data.
  147. * Returns an object with the glb file name as the key and data as the value.
  148. * @param jsonText
  149. * @param binaryBuffer
  150. * @param glTFPrefix
  151. * @returns - object with glb filename as key and data as value
  152. */
  153. _generateGLB(glTFPrefix: string): _GLTFData;
  154. /**
  155. * Sets the TRS for each node
  156. * @param node
  157. * @param babylonMesh
  158. * @param useRightHandedSystem
  159. */
  160. private setNodeTransformation(node, babylonMesh, useRightHandedSystem);
  161. /**
  162. *
  163. * @param babylonTexture
  164. * @return - glTF texture, or null if the texture format is not supported
  165. */
  166. private exportTexture(babylonTexture, mimeType?);
  167. /**
  168. * Sets data for the primitive attributes of each submesh
  169. * @param mesh
  170. * @param babylonMesh
  171. * @param byteOffset
  172. * @param useRightHandedSystem
  173. * @param dataBuffer
  174. * @returns - bytelength of the primitive attributes plus the passed in byteOffset
  175. */
  176. private setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer?);
  177. /**
  178. * Creates a glTF scene based on the array of meshes.
  179. * Returns the the total byte offset.
  180. * @param gltf
  181. * @param byteOffset
  182. * @param buffer
  183. * @param dataBuffer
  184. * @returns bytelength + byteoffset
  185. */
  186. private createScene(babylonScene, byteOffset, dataBuffer?);
  187. }
  188. }
  189. declare module BABYLON {
  190. /**
  191. * Class for holding and downloading glTF file data
  192. */
  193. class _GLTFData {
  194. glTFFiles: {
  195. [fileName: string]: string | Blob;
  196. };
  197. constructor();
  198. /**
  199. * Downloads glTF data.
  200. */
  201. downloadFiles(): void;
  202. }
  203. }
  204. declare module BABYLON {
  205. /**
  206. * Utility methods for working with glTF material conversion properties
  207. */
  208. class _GLTFMaterial {
  209. private static dielectricSpecular;
  210. private static epsilon;
  211. /**
  212. * Converts Specular Glossiness to Metallic Roughness
  213. * @param babylonSpecularGlossiness - Babylon specular glossiness parameters
  214. * @returns - Babylon metallic roughness values
  215. */
  216. static ConvertToMetallicRoughness(babylonSpecularGlossiness: _IBabylonSpecularGlossiness): _IBabylonMetallicRoughness;
  217. /**
  218. * Returns the perceived brightness value based on the provided color
  219. * @param color - color used in calculating the perceived brightness
  220. * @returns - perceived brightness value
  221. */
  222. private static PerceivedBrightness(color);
  223. /**
  224. * Computes the metallic factor
  225. * @param diffuse - diffused value
  226. * @param specular - specular value
  227. * @param oneMinusSpecularStrength - one minus the specular strength
  228. * @returns - metallic value
  229. */
  230. static SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  231. /**
  232. * Gets the glTF alpha mode from the Babylon Material
  233. * @param babylonMaterial - Babylon Material
  234. * @returns - The Babylon alpha mode value
  235. */
  236. static GetAlphaMode(babylonMaterial: Material): string;
  237. }
  238. }