babylonjs.serializers.module.d.ts 21 KB

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