babylonjs.serializers.module.d.ts 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /// <reference types="babylonjs"/>
  2. /// <reference types="babylonjs-gltf2interface"/>
  3. declare module 'babylonjs-serializers' {
  4. export = BABYLON;
  5. }
  6. declare module BABYLON {
  7. class OBJExport {
  8. static OBJ(mesh: Mesh[], materials?: boolean, matlibname?: string, globalposition?: boolean): string;
  9. static MTL(mesh: Mesh): string;
  10. }
  11. }
  12. declare module BABYLON {
  13. /**
  14. * Holds a collection of exporter options and parameters
  15. */
  16. interface IExportOptions {
  17. /**
  18. * Function which indicates whether a babylon mesh should be exported or not
  19. * @param transformNode source Babylon transform node. It is used to check whether it should be exported to glTF or not
  20. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  21. */
  22. shouldExportTransformNode?(transformNode: TransformNode): boolean;
  23. /**
  24. * The sample rate to bake animation curves
  25. */
  26. animationSampleRate?: number;
  27. }
  28. /**
  29. * Class for generating glTF data from a Babylon scene.
  30. */
  31. class GLTF2Export {
  32. /**
  33. * Exports the geometry of the scene to .gltf file format asynchronously
  34. * @param scene Babylon scene with scene hierarchy information
  35. * @param filePrefix File prefix to use when generating the glTF file
  36. * @param options Exporter options
  37. * @returns Returns an object with a .gltf file and associates texture names
  38. * as keys and their data and paths as values
  39. */
  40. static GLTFAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData>;
  41. /**
  42. * Exports the geometry of the scene to .glb file format asychronously
  43. * @param scene Babylon scene with scene hierarchy information
  44. * @param filePrefix File prefix to use when generating glb file
  45. * @param options Exporter options
  46. * @returns Returns an object with a .glb filename as key and data as value
  47. */
  48. static GLBAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData>;
  49. }
  50. }
  51. declare module BABYLON.GLTF2 {
  52. /**
  53. * Converts Babylon Scene into glTF 2.0.
  54. * @hidden
  55. */
  56. class _Exporter {
  57. /**
  58. * Stores all generated buffer views, which represents views into the main glTF buffer data
  59. */
  60. private bufferViews;
  61. /**
  62. * Stores all the generated accessors, which is used for accessing the data within the buffer views in glTF
  63. */
  64. private accessors;
  65. /**
  66. * Stores all the generated nodes, which contains transform and/or mesh information per node
  67. */
  68. private nodes;
  69. /**
  70. * Stores the glTF asset information, which represents the glTF version and this file generator
  71. */
  72. private asset;
  73. /**
  74. * Stores all the generated glTF scenes, which stores multiple node hierarchies
  75. */
  76. private scenes;
  77. /**
  78. * Stores all the generated mesh information, each containing a set of primitives to render in glTF
  79. */
  80. private meshes;
  81. /**
  82. * Stores all the generated material information, which represents the appearance of each primitive
  83. */
  84. private materials;
  85. private materialMap;
  86. /**
  87. * Stores all the generated texture information, which is referenced by glTF materials
  88. */
  89. private textures;
  90. /**
  91. * Stores all the generated image information, which is referenced by glTF textures
  92. */
  93. private images;
  94. /**
  95. * Stores all the texture samplers
  96. */
  97. private samplers;
  98. /**
  99. * Stores all the generated animation samplers, which is referenced by glTF animations
  100. */
  101. /**
  102. * Stores the animations for glTF models
  103. */
  104. private animations;
  105. /**
  106. * Stores the total amount of bytes stored in the glTF buffer
  107. */
  108. private totalByteLength;
  109. /**
  110. * Stores a reference to the Babylon scene containing the source geometry and material information
  111. */
  112. private babylonScene;
  113. /**
  114. * Stores a map of the image data, where the key is the file name and the value
  115. * is the image data
  116. */
  117. private imageData;
  118. /**
  119. * Stores a map of the unique id of a node to its index in the node array
  120. */
  121. private nodeMap;
  122. /**
  123. * Specifies if the Babylon scene should be converted to right-handed on export
  124. */
  125. private convertToRightHandedSystem;
  126. /**
  127. * Baked animation sample rate
  128. */
  129. private animationSampleRate;
  130. /**
  131. * Callback which specifies if a transform node should be exported or not
  132. */
  133. private shouldExportTransformNode;
  134. /**
  135. * Creates a glTF Exporter instance, which can accept optional exporter options
  136. * @param babylonScene Babylon scene object
  137. * @param options Options to modify the behavior of the exporter
  138. */
  139. constructor(babylonScene: Scene, options?: IExportOptions);
  140. private reorderIndicesBasedOnPrimitiveMode(submesh, primitiveMode, babylonIndices, byteOffset, binaryWriter);
  141. /**
  142. * Reorders the vertex attribute data based on the primitive mode. This is necessary when indices are not available and the winding order is
  143. * clock-wise during export to glTF
  144. * @param submesh BabylonJS submesh
  145. * @param primitiveMode Primitive mode of the mesh
  146. * @param sideOrientation the winding order of the submesh
  147. * @param vertexBufferKind The type of vertex attribute
  148. * @param meshAttributeArray The vertex attribute data
  149. * @param byteOffset The offset to the binary data
  150. * @param binaryWriter The binary data for the glTF file
  151. */
  152. private reorderVertexAttributeDataBasedOnPrimitiveMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  153. /**
  154. * Reorders the vertex attributes in the correct triangle mode order . This is necessary when indices are not available and the winding order is
  155. * clock-wise during export to glTF
  156. * @param submesh BabylonJS submesh
  157. * @param primitiveMode Primitive mode of the mesh
  158. * @param sideOrientation the winding order of the submesh
  159. * @param vertexBufferKind The type of vertex attribute
  160. * @param meshAttributeArray The vertex attribute data
  161. * @param byteOffset The offset to the binary data
  162. * @param binaryWriter The binary data for the glTF file
  163. */
  164. private reorderTriangleFillMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  165. /**
  166. * Reorders the vertex attributes in the correct triangle strip order. This is necessary when indices are not available and the winding order is
  167. * clock-wise during export to glTF
  168. * @param submesh BabylonJS submesh
  169. * @param primitiveMode Primitive mode of the mesh
  170. * @param sideOrientation the winding order of the submesh
  171. * @param vertexBufferKind The type of vertex attribute
  172. * @param meshAttributeArray The vertex attribute data
  173. * @param byteOffset The offset to the binary data
  174. * @param binaryWriter The binary data for the glTF file
  175. */
  176. private reorderTriangleStripDrawMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  177. /**
  178. * Reorders the vertex attributes in the correct triangle fan order. This is necessary when indices are not available and the winding order is
  179. * clock-wise during export to glTF
  180. * @param submesh BabylonJS submesh
  181. * @param primitiveMode Primitive mode of the mesh
  182. * @param sideOrientation the winding order of the submesh
  183. * @param vertexBufferKind The type of vertex attribute
  184. * @param meshAttributeArray The vertex attribute data
  185. * @param byteOffset The offset to the binary data
  186. * @param binaryWriter The binary data for the glTF file
  187. */
  188. private reorderTriangleFanMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  189. /**
  190. * Writes the vertex attribute data to binary
  191. * @param vertices The vertices to write to the binary writer
  192. * @param byteOffset The offset into the binary writer to overwrite binary data
  193. * @param vertexAttributeKind The vertex attribute type
  194. * @param meshAttributeArray The vertex attribute data
  195. * @param binaryWriter The writer containing the binary data
  196. */
  197. private writeVertexAttributeData(vertices, byteOffset, vertexAttributeKind, meshAttributeArray, binaryWriter);
  198. /**
  199. * Writes mesh attribute data to a data buffer
  200. * Returns the bytelength of the data
  201. * @param vertexBufferKind Indicates what kind of vertex data is being passed in
  202. * @param meshAttributeArray Array containing the attribute data
  203. * @param binaryWriter The buffer to write the binary data to
  204. * @param indices Used to specify the order of the vertex data
  205. */
  206. private writeAttributeData(vertexBufferKind, meshAttributeArray, byteStride, binaryWriter);
  207. /**
  208. * Generates glTF json data
  209. * @param shouldUseGlb Indicates whether the json should be written for a glb file
  210. * @param glTFPrefix Text to use when prefixing a glTF file
  211. * @param prettyPrint Indicates whether the json file should be pretty printed (true) or not (false)
  212. * @returns json data as string
  213. */
  214. private generateJSON(shouldUseGlb, glTFPrefix?, prettyPrint?);
  215. /**
  216. * Generates data for .gltf and .bin files based on the glTF prefix string
  217. * @param glTFPrefix Text to use when prefixing a glTF file
  218. * @returns GLTFData with glTF file data
  219. */
  220. _generateGLTFAsync(glTFPrefix: string): Promise<GLTFData>;
  221. /**
  222. * Creates a binary buffer for glTF
  223. * @returns array buffer for binary data
  224. */
  225. private _generateBinaryAsync();
  226. /**
  227. * Pads the number to a multiple of 4
  228. * @param num number to pad
  229. * @returns padded number
  230. */
  231. private _getPadding(num);
  232. /**
  233. * Generates a glb file from the json and binary data
  234. * Returns an object with the glb file name as the key and data as the value
  235. * @param glTFPrefix
  236. * @returns object with glb filename as key and data as value
  237. */
  238. _generateGLBAsync(glTFPrefix: string): Promise<GLTFData>;
  239. /**
  240. * Sets the TRS for each node
  241. * @param node glTF Node for storing the transformation data
  242. * @param babylonTransformNode Babylon mesh used as the source for the transformation data
  243. */
  244. private setNodeTransformation(node, babylonTransformNode);
  245. private getVertexBufferFromMesh(attributeKind, bufferMesh);
  246. /**
  247. * Creates a bufferview based on the vertices type for the Babylon mesh
  248. * @param kind Indicates the type of vertices data
  249. * @param babylonTransformNode The Babylon mesh to get the vertices data from
  250. * @param binaryWriter The buffer to write the bufferview data to
  251. */
  252. private createBufferViewKind(kind, babylonTransformNode, binaryWriter, byteStride);
  253. /**
  254. * The primitive mode of the Babylon mesh
  255. * @param babylonMesh The BabylonJS mesh
  256. */
  257. private getMeshPrimitiveMode(babylonMesh);
  258. /**
  259. * Sets the primitive mode of the glTF mesh primitive
  260. * @param meshPrimitive glTF mesh primitive
  261. * @param primitiveMode The primitive mode
  262. */
  263. private setPrimitiveMode(meshPrimitive, primitiveMode);
  264. /**
  265. * Sets the vertex attribute accessor based of the glTF mesh primitive
  266. * @param meshPrimitive glTF mesh primitive
  267. * @param attributeKind vertex attribute
  268. * @returns boolean specifying if uv coordinates are present
  269. */
  270. private setAttributeKind(meshPrimitive, attributeKind);
  271. /**
  272. * Sets data for the primitive attributes of each submesh
  273. * @param mesh glTF Mesh object to store the primitive attribute information
  274. * @param babylonTransformNode Babylon mesh to get the primitive attribute data from
  275. * @param binaryWriter Buffer to write the attribute data to
  276. */
  277. private setPrimitiveAttributes(mesh, babylonTransformNode, binaryWriter);
  278. /**
  279. * Creates a glTF scene based on the array of meshes
  280. * Returns the the total byte offset
  281. * @param babylonScene Babylon scene to get the mesh data from
  282. * @param binaryWriter Buffer to write binary data to
  283. */
  284. private createSceneAsync(babylonScene, binaryWriter);
  285. /**
  286. * Creates a mapping of Node unique id to node index and handles animations
  287. * @param babylonScene Babylon Scene
  288. * @param nodes Babylon transform nodes
  289. * @param shouldExportTransformNode Callback specifying if a transform node should be exported
  290. * @param binaryWriter Buffer to write binary data to
  291. * @returns Node mapping of unique id to index
  292. */
  293. private createNodeMapAndAnimations(babylonScene, nodes, shouldExportTransformNode, binaryWriter);
  294. /**
  295. * Creates a glTF node from a Babylon mesh
  296. * @param babylonMesh Source Babylon mesh
  297. * @param binaryWriter Buffer for storing geometry data
  298. * @returns glTF node
  299. */
  300. private createNode(babylonTransformNode, binaryWriter);
  301. }
  302. /**
  303. * @hidden
  304. *
  305. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  306. */
  307. class _BinaryWriter {
  308. /**
  309. * Array buffer which stores all binary data
  310. */
  311. private _arrayBuffer;
  312. /**
  313. * View of the array buffer
  314. */
  315. private _dataView;
  316. /**
  317. * byte offset of data in array buffer
  318. */
  319. private _byteOffset;
  320. /**
  321. * Initialize binary writer with an initial byte length
  322. * @param byteLength Initial byte length of the array buffer
  323. */
  324. constructor(byteLength: number);
  325. /**
  326. * Resize the array buffer to the specified byte length
  327. * @param byteLength
  328. */
  329. private resizeBuffer(byteLength);
  330. /**
  331. * Get an array buffer with the length of the byte offset
  332. * @returns ArrayBuffer resized to the byte offset
  333. */
  334. getArrayBuffer(): ArrayBuffer;
  335. /**
  336. * Get the byte offset of the array buffer
  337. * @returns byte offset
  338. */
  339. getByteOffset(): number;
  340. /**
  341. * Stores an UInt8 in the array buffer
  342. * @param entry
  343. * @param byteOffset If defined, specifies where to set the value as an offset.
  344. */
  345. setUInt8(entry: number, byteOffset?: number): void;
  346. /**
  347. * Gets an UInt32 in the array buffer
  348. * @param entry
  349. * @param byteOffset If defined, specifies where to set the value as an offset.
  350. */
  351. getUInt32(byteOffset: number): number;
  352. getVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  353. setVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  354. getVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  355. setVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  356. /**
  357. * Stores a Float32 in the array buffer
  358. * @param entry
  359. */
  360. setFloat32(entry: number, byteOffset?: number): void;
  361. /**
  362. * Stores an UInt32 in the array buffer
  363. * @param entry
  364. * @param byteOffset If defined, specifies where to set the value as an offset.
  365. */
  366. setUInt32(entry: number, byteOffset?: number): void;
  367. }
  368. }
  369. declare module BABYLON {
  370. /**
  371. * Class for holding and downloading glTF file data
  372. */
  373. class GLTFData {
  374. /**
  375. * Object which contains the file name as the key and its data as the value
  376. */
  377. glTFFiles: {
  378. [fileName: string]: string | Blob;
  379. };
  380. /**
  381. * Initializes the glTF file object
  382. */
  383. constructor();
  384. /**
  385. * Downloads the glTF data as files based on their names and data
  386. */
  387. downloadFiles(): void;
  388. }
  389. }
  390. declare module BABYLON.GLTF2 {
  391. /**
  392. * Utility methods for working with glTF material conversion properties. This class should only be used internally
  393. * @hidden
  394. */
  395. class _GLTFMaterial {
  396. /**
  397. * Represents the dielectric specular values for R, G and B
  398. */
  399. private static readonly _dielectricSpecular;
  400. /**
  401. * Allows the maximum specular power to be defined for material calculations
  402. */
  403. private static _maxSpecularPower;
  404. /**
  405. * Numeric tolerance value
  406. */
  407. private static _epsilon;
  408. /**
  409. * Specifies if two colors are approximately equal in value
  410. * @param color1 first color to compare to
  411. * @param color2 second color to compare to
  412. * @param epsilon threshold value
  413. */
  414. private static FuzzyEquals(color1, color2, epsilon);
  415. /**
  416. * Gets the materials from a Babylon scene and converts them to glTF materials
  417. * @param scene babylonjs scene
  418. * @param mimeType texture mime type
  419. * @param images array of images
  420. * @param textures array of textures
  421. * @param materials array of materials
  422. * @param imageData mapping of texture names to base64 textures
  423. * @param hasTextureCoords specifies if texture coordinates are present on the material
  424. */
  425. static _ConvertMaterialsToGLTFAsync(babylonMaterials: Material[], mimeType: ImageMimeType, images: IImage[], textures: ITexture[], samplers: ISampler[], materials: IMaterial[], materialMap: {
  426. [materialID: number]: number;
  427. }, imageData: {
  428. [fileName: string]: {
  429. data: Uint8Array;
  430. mimeType: ImageMimeType;
  431. };
  432. }, hasTextureCoords: boolean): Promise<void>;
  433. /**
  434. * Makes a copy of the glTF material without the texture parameters
  435. * @param originalMaterial original glTF material
  436. * @returns glTF material without texture parameters
  437. */
  438. static _StripTexturesFromMaterial(originalMaterial: IMaterial): IMaterial;
  439. /**
  440. * Specifies if the material has any texture parameters present
  441. * @param material glTF Material
  442. * @returns boolean specifying if texture parameters are present
  443. */
  444. static _HasTexturesPresent(material: IMaterial): boolean;
  445. /**
  446. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
  447. * @param babylonStandardMaterial
  448. * @returns glTF Metallic Roughness Material representation
  449. */
  450. static _ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  451. /**
  452. * Computes the metallic factor
  453. * @param diffuse diffused value
  454. * @param specular specular value
  455. * @param oneMinusSpecularStrength one minus the specular strength
  456. * @returns metallic value
  457. */
  458. static _SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  459. /**
  460. * Gets the glTF alpha mode from the Babylon Material
  461. * @param babylonMaterial Babylon Material
  462. * @returns The Babylon alpha mode value
  463. */
  464. static _GetAlphaMode(babylonMaterial: Material): Nullable<MaterialAlphaMode>;
  465. /**
  466. * Converts a Babylon Standard Material to a glTF Material
  467. * @param babylonStandardMaterial BJS Standard Material
  468. * @param mimeType mime type to use for the textures
  469. * @param images array of glTF image interfaces
  470. * @param textures array of glTF texture interfaces
  471. * @param materials array of glTF material interfaces
  472. * @param imageData map of image file name to data
  473. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  474. */
  475. static _ConvertStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], samplers: ISampler[], materials: IMaterial[], materialMap: {
  476. [materialID: number]: number;
  477. }, imageData: {
  478. [fileName: string]: {
  479. data: Uint8Array;
  480. mimeType: ImageMimeType;
  481. };
  482. }, hasTextureCoords: boolean): Promise<void>;
  483. /**
  484. *
  485. * @param texture Texture with alpha to overwrite to one
  486. * @param useAlpha Specifies if alpha should be preserved or not
  487. * @returns Promise with texture
  488. */
  489. static _SetAlphaToOneAsync(texture: BaseTexture, useAlpha: boolean): Promise<Texture>;
  490. /**
  491. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  492. * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
  493. * @param mimeType mime type to use for the textures
  494. * @param images array of glTF image interfaces
  495. * @param textures array of glTF texture interfaces
  496. * @param materials array of glTF material interfaces
  497. * @param imageData map of image file name to data
  498. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  499. */
  500. static _ConvertPBRMetallicRoughnessMaterialAsync(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], samplers: ISampler[], materials: IMaterial[], materialMap: {
  501. [materialID: number]: number;
  502. }, imageData: {
  503. [fileName: string]: {
  504. data: Uint8Array;
  505. mimeType: ImageMimeType;
  506. };
  507. }, hasTextureCoords: boolean): Promise<void>;
  508. /**
  509. * Converts an image typed array buffer to a base64 image
  510. * @param buffer typed array buffer
  511. * @param width width of the image
  512. * @param height height of the image
  513. * @param mimeType mimetype of the image
  514. * @returns base64 image string
  515. */
  516. private static _CreateBase64FromCanvas(buffer, width, height, mimeType);
  517. /**
  518. * Generates a white texture based on the specified width and height
  519. * @param width width of the texture in pixels
  520. * @param height height of the texture in pixels
  521. * @param scene babylonjs scene
  522. * @returns white texture
  523. */
  524. private static _CreateWhiteTexture(width, height, scene);
  525. /**
  526. * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null
  527. * @param texture1 first texture to resize
  528. * @param texture2 second texture to resize
  529. * @param scene babylonjs scene
  530. * @returns resized textures or null
  531. */
  532. private static _ResizeTexturesToSameDimensions(texture1, texture2, scene);
  533. /**
  534. * Convert Specular Glossiness Textures to Metallic Roughness
  535. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  536. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  537. * @param diffuseTexture texture used to store diffuse information
  538. * @param specularGlossinessTexture texture used to store specular and glossiness information
  539. * @param factors specular glossiness material factors
  540. * @param mimeType the mime type to use for the texture
  541. * @returns pbr metallic roughness interface or null
  542. */
  543. private static _ConvertSpecularGlossinessTexturesToMetallicRoughness(diffuseTexture, specularGlossinessTexture, factors, mimeType);
  544. /**
  545. * Converts specular glossiness material properties to metallic roughness
  546. * @param specularGlossiness interface with specular glossiness material properties
  547. * @returns interface with metallic roughness material properties
  548. */
  549. private static _ConvertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  550. /**
  551. * Calculates the surface reflectance, independent of lighting conditions
  552. * @param color Color source to calculate brightness from
  553. * @returns number representing the perceived brightness, or zero if color is undefined
  554. */
  555. private static _GetPerceivedBrightness(color);
  556. /**
  557. * Returns the maximum color component value
  558. * @param color
  559. * @returns maximum color component value, or zero if color is null or undefined
  560. */
  561. private static _GetMaxComponent(color);
  562. /**
  563. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
  564. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  565. * @param mimeType mime type to use for the textures
  566. * @param images array of glTF image interfaces
  567. * @param textures array of glTF texture interfaces
  568. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  569. * @param imageData map of image file name to data
  570. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  571. * @returns glTF PBR Metallic Roughness factors
  572. */
  573. private static _ConvertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, images, textures, samplers, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
  574. private static _GetGLTFTextureSampler(texture);
  575. private static _GetGLTFTextureWrapMode(wrapMode);
  576. private static _GetGLTFTextureWrapModesSampler(texture);
  577. /**
  578. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
  579. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  580. * @param mimeType mime type to use for the textures
  581. * @param images array of glTF image interfaces
  582. * @param textures array of glTF texture interfaces
  583. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  584. * @param imageData map of image file name to data
  585. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  586. * @returns glTF PBR Metallic Roughness factors
  587. */
  588. private static _ConvertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, samplers, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
  589. /**
  590. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  591. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  592. * @param mimeType mime type to use for the textures
  593. * @param images array of glTF image interfaces
  594. * @param textures array of glTF texture interfaces
  595. * @param materials array of glTF material interfaces
  596. * @param imageData map of image file name to data
  597. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  598. */
  599. static _ConvertPBRMaterialAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, images: IImage[], textures: ITexture[], samplers: ISampler[], materials: IMaterial[], materialMap: {
  600. [materialID: number]: number;
  601. }, imageData: {
  602. [fileName: string]: {
  603. data: Uint8Array;
  604. mimeType: ImageMimeType;
  605. };
  606. }, hasTextureCoords: boolean): Promise<void>;
  607. private static SetMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, images, textures, samplers, materials, materialMap, imageData, hasTextureCoords);
  608. private static GetPixelsFromTexture(babylonTexture);
  609. /**
  610. * Extracts a texture from a Babylon texture into file data and glTF data
  611. * @param babylonTexture Babylon texture to extract
  612. * @param mimeType Mime Type of the babylonTexture
  613. * @param images Array of glTF images
  614. * @param textures Array of glTF textures
  615. * @param imageData map of image file name and data
  616. * @return glTF texture info, or null if the texture format is not supported
  617. */
  618. private static _ExportTextureAsync(babylonTexture, mimeType, images, textures, samplers, imageData, useAlpha);
  619. /**
  620. * Builds a texture from base64 string
  621. * @param base64Texture base64 texture string
  622. * @param baseTextureName Name to use for the texture
  623. * @param mimeType image mime type for the texture
  624. * @param images array of images
  625. * @param textures array of textures
  626. * @param imageData map of image data
  627. * @returns glTF texture info, or null if the texture format is not supported
  628. */
  629. private static _GetTextureInfoFromBase64(base64Texture, baseTextureName, mimeType, images, textures, texCoordIndex, samplerIndex, imageData);
  630. }
  631. }
  632. declare module BABYLON.GLTF2 {
  633. /**
  634. * @hidden
  635. * Interface to store animation data.
  636. */
  637. interface _IAnimationData {
  638. /**
  639. * Keyframe data.
  640. */
  641. inputs: number[];
  642. /**
  643. * Value data.
  644. */
  645. outputs: number[][];
  646. /**
  647. * Animation interpolation data.
  648. */
  649. samplerInterpolation: AnimationSamplerInterpolation;
  650. /**
  651. * Minimum keyframe value.
  652. */
  653. inputsMin: number;
  654. /**
  655. * Maximum keyframe value.
  656. */
  657. inputsMax: number;
  658. }
  659. /**
  660. * @hidden
  661. */
  662. interface _IAnimationInfo {
  663. /**
  664. * The target channel for the animation
  665. */
  666. animationChannelTargetPath: AnimationChannelTargetPath;
  667. /**
  668. * The glTF accessor type for the data.
  669. */
  670. dataAccessorType: AccessorType.VEC3 | AccessorType.VEC4;
  671. /**
  672. * Specifies if quaternions should be used.
  673. */
  674. useQuaternion: boolean;
  675. }
  676. /**
  677. * @hidden
  678. * Utility class for generating glTF animation data from BabylonJS.
  679. */
  680. class _GLTFAnimation {
  681. /**
  682. * @ignore
  683. *
  684. * Creates glTF channel animation from BabylonJS animation.
  685. * @param babylonTransformNode - BabylonJS mesh.
  686. * @param animation - animation.
  687. * @param animationChannelTargetPath - The target animation channel.
  688. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  689. * @param useQuaternion - Specifies if quaternions are used.
  690. * @returns nullable IAnimationData
  691. */
  692. static _CreateNodeAnimation(babylonTransformNode: TransformNode, animation: Animation, animationChannelTargetPath: AnimationChannelTargetPath, convertToRightHandedSystem: boolean, useQuaternion: boolean, animationSampleRate: number): Nullable<_IAnimationData>;
  693. private static _DeduceAnimationInfo(animation);
  694. /**
  695. * @ignore
  696. * Create node animations from the transform node animations
  697. * @param babylonTransformNode
  698. * @param runtimeGLTFAnimation
  699. * @param idleGLTFAnimations
  700. * @param nodeMap
  701. * @param nodes
  702. * @param binaryWriter
  703. * @param bufferViews
  704. * @param accessors
  705. * @param convertToRightHandedSystem
  706. */
  707. static _CreateNodeAnimationFromTransformNodeAnimations(babylonTransformNode: TransformNode, runtimeGLTFAnimation: IAnimation, idleGLTFAnimations: IAnimation[], nodeMap: {
  708. [key: number]: number;
  709. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  710. /**
  711. * @ignore
  712. * Create node animations from the animation groups
  713. * @param babylonScene
  714. * @param glTFAnimations
  715. * @param nodeMap
  716. * @param nodes
  717. * @param binaryWriter
  718. * @param bufferViews
  719. * @param accessors
  720. * @param convertToRightHandedSystem
  721. */
  722. static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: {
  723. [key: number]: number;
  724. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  725. private static AddAnimation(name, glTFAnimation, babylonTransformNode, animation, dataAccessorType, animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, useQuaternion, animationSampleRate);
  726. /**
  727. * Create a baked animation
  728. * @param babylonTransformNode BabylonJS mesh
  729. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  730. * @param animationChannelTargetPath animation target channel
  731. * @param minFrame minimum animation frame
  732. * @param maxFrame maximum animation frame
  733. * @param fps frames per second of the animation
  734. * @param inputs input key frames of the animation
  735. * @param outputs output key frame data of the animation
  736. * @param convertToRightHandedSystem converts the values to right-handed
  737. * @param useQuaternion specifies if quaternions should be used
  738. */
  739. private static _CreateBakedAnimation(babylonTransformNode, animation, animationChannelTargetPath, minFrame, maxFrame, fps, sampleRate, inputs, outputs, minMaxFrames, convertToRightHandedSystem, useQuaternion);
  740. private static _ConvertFactorToVector3OrQuaternion(factor, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  741. private static _SetInterpolatedValue(babylonTransformNode, value, time, animation, animationChannelTargetPath, quaternionCache, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  742. /**
  743. * Creates linear animation from the animation key frames
  744. * @param babylonTransformNode BabylonJS mesh
  745. * @param animation BabylonJS animation
  746. * @param animationChannelTargetPath The target animation channel
  747. * @param frameDelta The difference between the last and first frame of the animation
  748. * @param inputs Array to store the key frame times
  749. * @param outputs Array to store the key frame data
  750. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  751. * @param useQuaternion Specifies if quaternions are used in the animation
  752. */
  753. private static _CreateLinearOrStepAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  754. /**
  755. * Creates cubic spline animation from the animation key frames
  756. * @param babylonTransformNode BabylonJS mesh
  757. * @param animation BabylonJS animation
  758. * @param animationChannelTargetPath The target animation channel
  759. * @param frameDelta The difference between the last and first frame of the animation
  760. * @param inputs Array to store the key frame times
  761. * @param outputs Array to store the key frame data
  762. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  763. * @param useQuaternion Specifies if quaternions are used in the animation
  764. */
  765. private static _CreateCubicSplineAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  766. private static _GetBasePositionRotationOrScale(babylonTransformNode, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  767. /**
  768. * Adds a key frame value
  769. * @param keyFrame
  770. * @param animation
  771. * @param outputs
  772. * @param animationChannelTargetPath
  773. * @param basePositionRotationOrScale
  774. * @param convertToRightHandedSystem
  775. * @param useQuaternion
  776. */
  777. private static _AddKeyframeValue(keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion);
  778. /**
  779. * Determine the interpolation based on the key frames
  780. * @param keyFrames
  781. * @param animationChannelTargetPath
  782. * @param useQuaternion
  783. */
  784. private static _DeduceInterpolation(keyFrames, animationChannelTargetPath, useQuaternion);
  785. /**
  786. * Adds an input tangent or output tangent to the output data
  787. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  788. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  789. * @param outputs The animation data by keyframe
  790. * @param animationChannelTargetPath The target animation channel
  791. * @param interpolation The interpolation type
  792. * @param keyFrame The key frame with the animation data
  793. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  794. * @param useQuaternion Specifies if quaternions are used
  795. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  796. */
  797. private static AddSplineTangent(babylonTransformNode, tangentType, outputs, animationChannelTargetPath, interpolation, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem);
  798. /**
  799. * Get the minimum and maximum key frames' frame values
  800. * @param keyFrames animation key frames
  801. * @returns the minimum and maximum key frame value
  802. */
  803. private static calculateMinMaxKeyFrames(keyFrames);
  804. }
  805. }
  806. declare module BABYLON.GLTF2 {
  807. /**
  808. * @hidden
  809. */
  810. class _GLTFUtilities {
  811. /**
  812. * Creates a buffer view based on the supplied arguments
  813. * @param bufferIndex index value of the specified buffer
  814. * @param byteOffset byte offset value
  815. * @param byteLength byte length of the bufferView
  816. * @param byteStride byte distance between conequential elements
  817. * @param name name of the buffer view
  818. * @returns bufferView for glTF
  819. */
  820. static CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView;
  821. /**
  822. * Creates an accessor based on the supplied arguments
  823. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  824. * @param name The name of the accessor
  825. * @param type The type of the accessor
  826. * @param componentType The datatype of components in the attribute
  827. * @param count The number of attributes referenced by this accessor
  828. * @param byteOffset The offset relative to the start of the bufferView in bytes
  829. * @param min Minimum value of each component in this attribute
  830. * @param max Maximum value of each component in this attribute
  831. * @returns accessor for glTF
  832. */
  833. static CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor;
  834. /**
  835. * Calculates the minimum and maximum values of an array of position floats
  836. * @param positions Positions array of a mesh
  837. * @param vertexStart Starting vertex offset to calculate min and max values
  838. * @param vertexCount Number of vertices to check for min and max values
  839. * @returns min number array and max number array
  840. */
  841. static CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): {
  842. min: number[];
  843. max: number[];
  844. };
  845. /**
  846. * Converts a new right-handed Vector3
  847. * @param vector vector3 array
  848. * @returns right-handed Vector3
  849. */
  850. static GetRightHandedPositionVector3(vector: Vector3): Vector3;
  851. /**
  852. * Converts a Vector3 to right-handed
  853. * @param vector Vector3 to convert to right-handed
  854. */
  855. static GetRightHandedPositionVector3FromRef(vector: Vector3): void;
  856. /**
  857. * Converts a three element number array to right-handed
  858. * @param vector number array to convert to right-handed
  859. */
  860. static GetRightHandedPositionArray3FromRef(vector: number[]): void;
  861. /**
  862. * Converts a new right-handed Vector3
  863. * @param vector vector3 array
  864. * @returns right-handed Vector3
  865. */
  866. static GetRightHandedNormalVector3(vector: Vector3): Vector3;
  867. /**
  868. * Converts a Vector3 to right-handed
  869. * @param vector Vector3 to convert to right-handed
  870. */
  871. static GetRightHandedNormalVector3FromRef(vector: Vector3): void;
  872. /**
  873. * Converts a three element number array to right-handed
  874. * @param vector number array to convert to right-handed
  875. */
  876. static GetRightHandedNormalArray3FromRef(vector: number[]): void;
  877. /**
  878. * Converts a Vector4 to right-handed
  879. * @param vector Vector4 to convert to right-handed
  880. */
  881. static GetRightHandedVector4FromRef(vector: Vector4): void;
  882. /**
  883. * Converts a Vector4 to right-handed
  884. * @param vector Vector4 to convert to right-handed
  885. */
  886. static GetRightHandedArray4FromRef(vector: number[]): void;
  887. /**
  888. * Converts a Quaternion to right-handed
  889. * @param quaternion Source quaternion to convert to right-handed
  890. */
  891. static GetRightHandedQuaternionFromRef(quaternion: Quaternion): void;
  892. /**
  893. * Converts a Quaternion to right-handed
  894. * @param quaternion Source quaternion to convert to right-handed
  895. */
  896. static GetRightHandedQuaternionArrayFromRef(quaternion: number[]): void;
  897. }
  898. }