babylon.glTF2Serializer.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. declare module BABYLON.GLTF2 {
  2. const enum AccessorComponentType {
  3. BYTE = 5120,
  4. UNSIGNED_BYTE = 5121,
  5. SHORT = 5122,
  6. UNSIGNED_SHORT = 5123,
  7. UNSIGNED_INT = 5125,
  8. FLOAT = 5126,
  9. }
  10. const enum AccessorType {
  11. SCALAR = "SCALAR",
  12. VEC2 = "VEC2",
  13. VEC3 = "VEC3",
  14. VEC4 = "VEC4",
  15. MAT2 = "MAT2",
  16. MAT3 = "MAT3",
  17. MAT4 = "MAT4",
  18. }
  19. const enum AnimationChannelTargetPath {
  20. TRANSLATION = "translation",
  21. ROTATION = "rotation",
  22. SCALE = "scale",
  23. WEIGHTS = "weights",
  24. }
  25. const enum AnimationInterpolation {
  26. LINEAR = "LINEAR",
  27. STEP = "STEP",
  28. CUBICSPLINE = "CUBICSPLINE",
  29. }
  30. const enum CameraType {
  31. PERSPECTIVE = "perspective",
  32. ORTHOGRAPHIC = "orthographic",
  33. }
  34. const enum ImageMimeType {
  35. JPEG = "image/jpeg",
  36. PNG = "image/png",
  37. }
  38. const enum MaterialAlphaMode {
  39. OPAQUE = "OPAQUE",
  40. MASK = "MASK",
  41. BLEND = "BLEND",
  42. }
  43. const enum MeshPrimitiveMode {
  44. POINTS = 0,
  45. LINES = 1,
  46. LINE_LOOP = 2,
  47. LINE_STRIP = 3,
  48. TRIANGLES = 4,
  49. TRIANGLE_STRIP = 5,
  50. TRIANGLE_FAN = 6,
  51. }
  52. const enum TextureMagFilter {
  53. NEAREST = 9728,
  54. LINEAR = 9729,
  55. }
  56. const enum TextureMinFilter {
  57. NEAREST = 9728,
  58. LINEAR = 9729,
  59. NEAREST_MIPMAP_NEAREST = 9984,
  60. LINEAR_MIPMAP_NEAREST = 9985,
  61. NEAREST_MIPMAP_LINEAR = 9986,
  62. LINEAR_MIPMAP_LINEAR = 9987,
  63. }
  64. const enum TextureWrapMode {
  65. CLAMP_TO_EDGE = 33071,
  66. MIRRORED_REPEAT = 33648,
  67. REPEAT = 10497,
  68. }
  69. interface IProperty {
  70. extensions?: {
  71. [key: string]: any;
  72. };
  73. extras?: any;
  74. }
  75. interface IChildRootProperty extends IProperty {
  76. name?: string;
  77. }
  78. interface IAccessorSparseIndices extends IProperty {
  79. bufferView: number;
  80. byteOffset?: number;
  81. componentType: AccessorComponentType;
  82. }
  83. interface IAccessorSparseValues extends IProperty {
  84. bufferView: number;
  85. byteOffset?: number;
  86. }
  87. interface IAccessorSparse extends IProperty {
  88. count: number;
  89. indices: IAccessorSparseIndices;
  90. values: IAccessorSparseValues;
  91. }
  92. interface IAccessor extends IChildRootProperty {
  93. bufferView?: number;
  94. byteOffset?: number;
  95. componentType: AccessorComponentType;
  96. normalized?: boolean;
  97. count: number;
  98. type: AccessorType;
  99. max?: number[];
  100. min?: number[];
  101. sparse?: IAccessorSparse;
  102. }
  103. interface IAnimationChannel extends IProperty {
  104. sampler: number;
  105. target: IAnimationChannelTarget;
  106. }
  107. interface IAnimationChannelTarget extends IProperty {
  108. node: number;
  109. path: AnimationChannelTargetPath;
  110. }
  111. interface IAnimationSampler extends IProperty {
  112. input: number;
  113. interpolation?: AnimationInterpolation;
  114. output: number;
  115. }
  116. interface IAnimation extends IChildRootProperty {
  117. channels: IAnimationChannel[];
  118. samplers: IAnimationSampler[];
  119. }
  120. interface IAsset extends IChildRootProperty {
  121. copyright?: string;
  122. generator?: string;
  123. version: string;
  124. minVersion?: string;
  125. }
  126. interface IBuffer extends IChildRootProperty {
  127. uri?: string;
  128. byteLength: number;
  129. }
  130. interface IBufferView extends IChildRootProperty {
  131. buffer: number;
  132. byteOffset?: number;
  133. byteLength: number;
  134. byteStride?: number;
  135. }
  136. interface ICameraOrthographic extends IProperty {
  137. xmag: number;
  138. ymag: number;
  139. zfar: number;
  140. znear: number;
  141. }
  142. interface ICameraPerspective extends IProperty {
  143. aspectRatio: number;
  144. yfov: number;
  145. zfar: number;
  146. znear: number;
  147. }
  148. interface ICamera extends IChildRootProperty {
  149. orthographic?: ICameraOrthographic;
  150. perspective?: ICameraPerspective;
  151. type: CameraType;
  152. }
  153. interface IImage extends IChildRootProperty {
  154. uri?: string;
  155. mimeType?: ImageMimeType;
  156. bufferView?: number;
  157. }
  158. interface IMaterialNormalTextureInfo extends ITextureInfo {
  159. scale?: number;
  160. }
  161. interface IMaterialOcclusionTextureInfo extends ITextureInfo {
  162. strength?: number;
  163. }
  164. interface IMaterialPbrMetallicRoughness {
  165. baseColorFactor?: number[];
  166. baseColorTexture?: ITextureInfo;
  167. metallicFactor?: number;
  168. roughnessFactor?: number;
  169. metallicRoughnessTexture?: ITextureInfo;
  170. }
  171. interface IMaterial extends IChildRootProperty {
  172. pbrMetallicRoughness?: IMaterialPbrMetallicRoughness;
  173. normalTexture?: IMaterialNormalTextureInfo;
  174. occlusionTexture?: IMaterialOcclusionTextureInfo;
  175. emissiveTexture?: ITextureInfo;
  176. emissiveFactor?: number[];
  177. alphaMode?: MaterialAlphaMode;
  178. alphaCutoff?: number;
  179. doubleSided?: boolean;
  180. }
  181. interface IMeshPrimitive extends IProperty {
  182. attributes: {
  183. [name: string]: number;
  184. };
  185. indices?: number;
  186. material?: number;
  187. mode?: MeshPrimitiveMode;
  188. targets?: {
  189. [name: string]: number;
  190. }[];
  191. }
  192. interface IMesh extends IChildRootProperty {
  193. primitives: IMeshPrimitive[];
  194. weights?: number[];
  195. }
  196. interface INode extends IChildRootProperty {
  197. camera?: number;
  198. children?: number[];
  199. skin?: number;
  200. matrix?: number[];
  201. mesh?: number;
  202. rotation?: number[];
  203. scale?: number[];
  204. translation?: number[];
  205. weights?: number[];
  206. }
  207. interface ISampler extends IChildRootProperty {
  208. magFilter?: TextureMagFilter;
  209. minFilter?: TextureMinFilter;
  210. wrapS?: TextureWrapMode;
  211. wrapT?: TextureWrapMode;
  212. }
  213. interface IScene extends IChildRootProperty {
  214. nodes: number[];
  215. }
  216. interface ISkin extends IChildRootProperty {
  217. inverseBindMatrices?: number;
  218. skeleton?: number;
  219. joints: number[];
  220. }
  221. interface ITexture extends IChildRootProperty {
  222. sampler?: number;
  223. source: number;
  224. }
  225. interface ITextureInfo {
  226. index: number;
  227. texCoord?: number;
  228. }
  229. interface IGLTF extends IProperty {
  230. accessors?: IAccessor[];
  231. animations?: IAnimation[];
  232. asset: IAsset;
  233. buffers?: IBuffer[];
  234. bufferViews?: IBufferView[];
  235. cameras?: ICamera[];
  236. extensionsUsed?: string[];
  237. extensionsRequired?: string[];
  238. images?: IImage[];
  239. materials?: IMaterial[];
  240. meshes?: IMesh[];
  241. nodes?: INode[];
  242. samplers?: ISampler[];
  243. scene?: number;
  244. scenes?: IScene[];
  245. skins?: ISkin[];
  246. textures?: ITexture[];
  247. }
  248. }
  249. declare module BABYLON {
  250. /**
  251. * Holds a collection of exporter options and parameters
  252. */
  253. interface IExporterOptions {
  254. /**
  255. * Function which indicates whether a babylon mesh should be exported or not.
  256. * @param mesh - source Babylon mesh. It is used to check whether it should be
  257. * exported to glTF or not.
  258. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  259. */
  260. shouldExportMesh?(mesh: AbstractMesh): boolean;
  261. }
  262. /**
  263. * Class for generating glTF data from a Babylon scene.
  264. */
  265. class GLTF2Export {
  266. /**
  267. * Exports the geometry of the scene to .gltf file format.
  268. * @param scene - Babylon scene with scene hierarchy information.
  269. * @param filePrefix - File prefix to use when generating the glTF file.
  270. * @param options - Exporter options.
  271. * @returns - Returns an object with a .gltf file and associates texture names
  272. * as keys and their data and paths as values.
  273. */
  274. static GLTF(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData;
  275. /**
  276. * Exports the geometry of the scene to .glb file format.
  277. * @param scene - Babylon scene with scene hierarchy information.
  278. * @param filePrefix - File prefix to use when generating glb file.
  279. * @param options - Exporter options.
  280. * @returns - Returns an object with a .glb filename as key and data as value
  281. */
  282. static GLB(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData;
  283. }
  284. }
  285. /**
  286. * Module for the Babylon glTF 2.0 exporter. Should ONLY be used internally.
  287. * @ignore - capitalization of GLTF2 module.
  288. */
  289. declare module BABYLON.GLTF2 {
  290. /**
  291. * Converts Babylon Scene into glTF 2.0.
  292. */
  293. class _Exporter {
  294. /**
  295. * Stores all generated buffer views, which represents views into the main glTF buffer data.
  296. */
  297. private bufferViews;
  298. /**
  299. * Stores all the generated accessors, which is used for accessing the data within the buffer views in glTF.
  300. */
  301. private accessors;
  302. /**
  303. * Stores all the generated nodes, which contains transform and/or mesh information per node.
  304. */
  305. private nodes;
  306. /**
  307. * Stores the glTF asset information, which represents the glTF version and this file generator.
  308. */
  309. private asset;
  310. /**
  311. * Stores all the generated glTF scenes, which stores multiple node hierarchies.
  312. */
  313. private scenes;
  314. /**
  315. * Stores all the generated mesh information, each containing a set of primitives to render in glTF.
  316. */
  317. private meshes;
  318. /**
  319. * Stores all the generated material information, which represents the appearance of each primitive.
  320. */
  321. private materials;
  322. /**
  323. * Stores all the generated texture information, which is referenced by glTF materials.
  324. */
  325. private textures;
  326. /**
  327. * Stores all the generated image information, which is referenced by glTF textures.
  328. */
  329. private images;
  330. /**
  331. * Stores the total amount of bytes stored in the glTF buffer.
  332. */
  333. private totalByteLength;
  334. /**
  335. * Stores a reference to the Babylon scene containing the source geometry and material information.
  336. */
  337. private babylonScene;
  338. /**
  339. * Stores the exporter options, which are optionally passed in from the glTF serializer.
  340. */
  341. private options?;
  342. /**
  343. * Stores a map of the image data, where the key is the file name and the value
  344. * is the image data.
  345. */
  346. private imageData;
  347. /**
  348. * Creates a glTF Exporter instance, which can accept optional exporter options.
  349. * @param babylonScene - Babylon scene object
  350. * @param options - Options to modify the behavior of the exporter.
  351. */
  352. constructor(babylonScene: Scene, options?: IExporterOptions);
  353. /**
  354. * Creates a buffer view based on teh supplied arguments
  355. * @param bufferIndex - index value of the specified buffer
  356. * @param byteOffset - byte offset value
  357. * @param byteLength - byte length of the bufferView
  358. * @param byteStride - byte distance between conequential elements.
  359. * @param name - name of the buffer view
  360. * @returns - bufferView for glTF
  361. */
  362. private createBufferView(bufferIndex, byteOffset, byteLength, byteStride?, name?);
  363. /**
  364. * Creates an accessor based on the supplied arguments
  365. * @param bufferviewIndex
  366. * @param name
  367. * @param type
  368. * @param componentType
  369. * @param count
  370. * @param min
  371. * @param max
  372. * @returns - accessor for glTF
  373. */
  374. private createAccessor(bufferviewIndex, name, type, componentType, count, byteOffset?, min?, max?);
  375. /**
  376. * Calculates the minimum and maximum values of an array of floats, based on stride
  377. * @param buff - Data to check for min and max values.
  378. * @param vertexStart - Start offset to calculate min and max values.
  379. * @param vertexCount - Number of vertices to check for min and max values.
  380. * @param stride - Offset between consecutive attributes.
  381. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  382. * @returns - min number array and max number array.
  383. */
  384. private calculateMinMax(buff, vertexStart, vertexCount, stride, useRightHandedSystem);
  385. /**
  386. * Writes mesh attribute data to a data buffer.
  387. * Returns the bytelength of the data.
  388. * @param vertexBufferKind - Indicates what kind of vertex data is being passed in.
  389. * @param meshAttributeArray - Array containing the attribute data.
  390. * @param strideSize - Represents the offset between consecutive attributes
  391. * @param byteOffset - The offset to start counting bytes from.
  392. * @param dataBuffer - The buffer to write the binary data to.
  393. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  394. * @returns - Byte length of the attribute data.
  395. */
  396. private writeAttributeData(vertexBufferKind, meshAttributeArray, strideSize, vertexBufferOffset, byteOffset, dataBuffer, useRightHandedSystem);
  397. /**
  398. * Generates glTF json data
  399. * @param shouldUseGlb - Indicates whether the json should be written for a glb file.
  400. * @param glTFPrefix - Text to use when prefixing a glTF file.
  401. * @param prettyPrint - Indicates whether the json file should be pretty printed (true) or not (false).
  402. * @returns - json data as string
  403. */
  404. private generateJSON(shouldUseGlb, glTFPrefix?, prettyPrint?);
  405. /**
  406. * Generates data for .gltf and .bin files based on the glTF prefix string
  407. * @param glTFPrefix - Text to use when prefixing a glTF file.
  408. * @returns - GLTFData with glTF file data.
  409. */
  410. _generateGLTF(glTFPrefix: string): _GLTFData;
  411. /**
  412. * Creates a binary buffer for glTF
  413. * @returns - array buffer for binary data
  414. */
  415. private generateBinary();
  416. /**
  417. * Pads the number to a multiple of 4
  418. * @param num - number to pad
  419. * @returns - padded number
  420. */
  421. private _getPadding(num);
  422. /**
  423. * Generates a glb file from the json and binary data.
  424. * Returns an object with the glb file name as the key and data as the value.
  425. * @param glTFPrefix
  426. * @returns - object with glb filename as key and data as value
  427. */
  428. _generateGLB(glTFPrefix: string): _GLTFData;
  429. /**
  430. * Sets the TRS for each node
  431. * @param node - glTF Node for storing the transformation data.
  432. * @param babylonMesh - Babylon mesh used as the source for the transformation data.
  433. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  434. */
  435. private setNodeTransformation(node, babylonMesh, useRightHandedSystem);
  436. /**
  437. *
  438. * @param babylonTexture - Babylon texture to extract.
  439. * @param mimeType - Mime Type of the babylonTexture.
  440. * @return - glTF texture, or null if the texture format is not supported.
  441. */
  442. private exportTexture(babylonTexture, mimeType?);
  443. /**
  444. * Creates a bufferview based on the vertices type for the Babylon mesh
  445. * @param kind - Indicates the type of vertices data.
  446. * @param babylonMesh - The Babylon mesh to get the vertices data from.
  447. * @param byteOffset - The offset from the buffer to start indexing from.
  448. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  449. * @param dataBuffer - The buffer to write the bufferview data to.
  450. * @returns bytelength of the bufferview data.
  451. */
  452. private createBufferViewKind(kind, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  453. /**
  454. * Sets data for the primitive attributes of each submesh
  455. * @param mesh - glTF Mesh object to store the primitive attribute information.
  456. * @param babylonMesh - Babylon mesh to get the primitive attribute data from.
  457. * @param byteOffset - The offset in bytes of the buffer data.
  458. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  459. * @param dataBuffer - Buffer to write the attribute data to.
  460. * @returns - bytelength of the primitive attributes plus the passed in byteOffset.
  461. */
  462. private setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  463. /**
  464. * Creates a glTF scene based on the array of meshes.
  465. * Returns the the total byte offset.
  466. * @param babylonScene - Babylon scene to get the mesh data from.
  467. * @param byteOffset - Offset to start from in bytes.
  468. * @param dataBuffer - Buffer to write geometry data to.
  469. * @returns bytelength + byteoffset
  470. */
  471. private createScene(babylonScene, byteOffset, dataBuffer);
  472. }
  473. }
  474. declare module BABYLON {
  475. /**
  476. * Class for holding and downloading glTF file data
  477. */
  478. class _GLTFData {
  479. /**
  480. * Object which contains the file name as the key and its data as the value.
  481. */
  482. glTFFiles: {
  483. [fileName: string]: string | Blob;
  484. };
  485. /**
  486. * Initializes the glTF file object.
  487. */
  488. constructor();
  489. /**
  490. * Downloads the glTF data as files based on their names and data.
  491. */
  492. downloadFiles(): void;
  493. }
  494. }
  495. declare module BABYLON.GLTF2 {
  496. /**
  497. * Utility methods for working with glTF material conversion properties. This class should only be used internally.
  498. */
  499. class _GLTFMaterial {
  500. /**
  501. * Represents the dielectric specular values for R, G and B.
  502. */
  503. private static readonly dielectricSpecular;
  504. /**
  505. * Epsilon value, used as a small tolerance value for a numeric value.
  506. */
  507. private static readonly epsilon;
  508. /**
  509. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material.
  510. * @param babylonStandardMaterial
  511. * @returns - glTF Metallic Roughness Material representation
  512. */
  513. static ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  514. /**
  515. * Converts Specular Glossiness to Metallic Roughness. This is based on the algorithm used in the Babylon glTF 3ds Max Exporter.
  516. * {@link https://github.com/BabylonJS/Exporters/blob/master/3ds%20Max/Max2Babylon/Exporter/BabylonExporter.GLTFExporter.Material.cs}
  517. * @param babylonSpecularGlossiness - Babylon specular glossiness parameters
  518. * @returns - Babylon metallic roughness values
  519. */
  520. private static _ConvertToMetallicRoughness(babylonSpecularGlossiness);
  521. /**
  522. * Returns the perceived brightness value based on the provided color
  523. * @param color - color used in calculating the perceived brightness
  524. * @returns - perceived brightness value
  525. */
  526. private static PerceivedBrightness(color);
  527. /**
  528. * Computes the metallic factor
  529. * @param diffuse - diffused value
  530. * @param specular - specular value
  531. * @param oneMinusSpecularStrength - one minus the specular strength
  532. * @returns - metallic value
  533. */
  534. static SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  535. /**
  536. * Gets the glTF alpha mode from the Babylon Material
  537. * @param babylonMaterial - Babylon Material
  538. * @returns - The Babylon alpha mode value
  539. */
  540. static GetAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  541. }
  542. }