babylon.glTF2Serializer.d.ts 39 KB

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