babylonjs.serializers.module.d.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. * Creates a bufferview based on the vertices type for the Babylon mesh
  448. * @param kind - Indicates the type of vertices data.
  449. * @param babylonMesh - The Babylon mesh to get the vertices data from.
  450. * @param byteOffset - The offset from the buffer to start indexing from.
  451. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  452. * @param dataBuffer - The buffer to write the bufferview data to.
  453. * @returns bytelength of the bufferview data.
  454. */
  455. private createBufferViewKind(kind, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  456. /**
  457. * Sets data for the primitive attributes of each submesh
  458. * @param mesh - glTF Mesh object to store the primitive attribute information.
  459. * @param babylonMesh - Babylon mesh to get the primitive attribute data from.
  460. * @param byteOffset - The offset in bytes of the buffer data.
  461. * @param useRightHandedSystem - Indicates whether the data should be modified for a right or left handed coordinate system.
  462. * @param dataBuffer - Buffer to write the attribute data to.
  463. * @returns - bytelength of the primitive attributes plus the passed in byteOffset.
  464. */
  465. private setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  466. /**
  467. * Creates a glTF scene based on the array of meshes.
  468. * Returns the the total byte offset.
  469. * @param babylonScene - Babylon scene to get the mesh data from.
  470. * @param byteOffset - Offset to start from in bytes.
  471. * @param dataBuffer - Buffer to write geometry data to.
  472. * @returns bytelength + byteoffset
  473. */
  474. private createScene(babylonScene, byteOffset, dataBuffer);
  475. }
  476. }
  477. declare module BABYLON {
  478. /**
  479. * Class for holding and downloading glTF file data
  480. */
  481. class _GLTFData {
  482. /**
  483. * Object which contains the file name as the key and its data as the value.
  484. */
  485. glTFFiles: {
  486. [fileName: string]: string | Blob;
  487. };
  488. /**
  489. * Initializes the glTF file object.
  490. */
  491. constructor();
  492. /**
  493. * Downloads the glTF data as files based on their names and data.
  494. */
  495. downloadFiles(): void;
  496. }
  497. }
  498. declare module BABYLON.GLTF2 {
  499. /**
  500. * Utility methods for working with glTF material conversion properties. This class should only be used internally.
  501. */
  502. class _GLTFMaterial {
  503. /**
  504. * Represents the dielectric specular values for R, G and B.
  505. */
  506. private static readonly dielectricSpecular;
  507. /**
  508. * Allows the maximum specular power to be defined for material calculations.
  509. */
  510. private static maxSpecularPower;
  511. /**
  512. * Gets the materials from a Babylon scene and converts them to glTF materials.
  513. * @param scene
  514. * @param mimeType
  515. * @param images
  516. * @param textures
  517. * @param materials
  518. * @param imageData
  519. * @param hasTextureCoords
  520. */
  521. static ConvertMaterialsToGLTF(babylonMaterials: Material[], mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  522. [fileName: string]: {
  523. data: Uint8Array;
  524. mimeType: ImageMimeType;
  525. };
  526. }, hasTextureCoords: boolean): void;
  527. /**
  528. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material.
  529. * @param babylonStandardMaterial
  530. * @returns - glTF Metallic Roughness Material representation
  531. */
  532. static ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  533. /**
  534. * Computes the metallic factor
  535. * @param diffuse - diffused value
  536. * @param specular - specular value
  537. * @param oneMinusSpecularStrength - one minus the specular strength
  538. * @returns - metallic value
  539. */
  540. static SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  541. /**
  542. * Gets the glTF alpha mode from the Babylon Material
  543. * @param babylonMaterial - Babylon Material
  544. * @returns - The Babylon alpha mode value
  545. */
  546. static GetAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  547. /**
  548. * Converts a Babylon Standard Material to a glTF Material.
  549. * @param babylonStandardMaterial - BJS Standard Material.
  550. * @param mimeType - mime type to use for the textures.
  551. * @param images - array of glTF image interfaces.
  552. * @param textures - array of glTF texture interfaces.
  553. * @param materials - array of glTF material interfaces.
  554. * @param imageData - map of image file name to data.
  555. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  556. */
  557. static ConvertStandardMaterial(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  558. [fileName: string]: {
  559. data: Uint8Array;
  560. mimeType: ImageMimeType;
  561. };
  562. }, hasTextureCoords: boolean): void;
  563. /**
  564. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
  565. * @param babylonPBRMetalRoughMaterial - BJS PBR Metallic Roughness Material.
  566. * @param mimeType - mime type to use for the textures.
  567. * @param images - array of glTF image interfaces.
  568. * @param textures - array of glTF texture interfaces.
  569. * @param materials - array of glTF material interfaces.
  570. * @param imageData - map of image file name to data.
  571. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  572. */
  573. static ConvertPBRMetallicRoughnessMaterial(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], materials: IMaterial[], imageData: {
  574. [fileName: string]: {
  575. data: Uint8Array;
  576. mimeType: ImageMimeType;
  577. };
  578. }, hasTextureCoords: boolean): void;
  579. /**
  580. * Extracts a texture from a Babylon texture into file data and glTF data.
  581. * @param babylonTexture - Babylon texture to extract.
  582. * @param mimeType - Mime Type of the babylonTexture.
  583. * @param images - Array of glTF images.
  584. * @param textures - Array of glTF textures.
  585. * @param imageData - map of image file name and data.
  586. * @return - glTF texture, or null if the texture format is not supported.
  587. */
  588. static ExportTexture(babylonTexture: BaseTexture, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], imageData: {
  589. [fileName: string]: {
  590. data: Uint8Array;
  591. mimeType: ImageMimeType;
  592. };
  593. }): Nullable<ITextureInfo>;
  594. }
  595. }