babylon.glTF2Serializer.d.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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(submesh, primitiveMode, babylonIndices, byteOffset, binaryWriter);
  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(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  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(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  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(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  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(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  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(vertices, byteOffset, vertexAttributeKind, meshAttributeArray, binaryWriter);
  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(vertexBufferKind, meshAttributeArray, byteStride, binaryWriter);
  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(shouldUseGlb, glTFPrefix?, prettyPrint?);
  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(num);
  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(node, babylonTransformNode);
  247. private getVertexBufferFromMesh(attributeKind, bufferMesh);
  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(kind, babylonTransformNode, binaryWriter, byteStride);
  255. /**
  256. * The primitive mode of the Babylon mesh
  257. * @param babylonMesh The BabylonJS mesh
  258. */
  259. private getMeshPrimitiveMode(babylonMesh);
  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(meshPrimitive, primitiveMode);
  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(meshPrimitive, attributeKind);
  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(mesh, babylonTransformNode, binaryWriter);
  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(babylonScene, binaryWriter);
  287. private getRootNodes(babylonScene, nodes, shouldExportTransformNode);
  288. /**
  289. * Creates a mapping of Node unique id to node index and handles animations
  290. * @param babylonScene Babylon Scene
  291. * @param nodes Babylon transform nodes
  292. * @param shouldExportTransformNode Callback specifying if a transform node should be exported
  293. * @param binaryWriter Buffer to write binary data to
  294. * @returns Node mapping of unique id to index
  295. */
  296. private createNodeMapAndAnimations(babylonScene, nodes, shouldExportTransformNode, binaryWriter);
  297. /**
  298. * Creates a glTF node from a Babylon mesh
  299. * @param babylonMesh Source Babylon mesh
  300. * @param binaryWriter Buffer for storing geometry data
  301. * @returns glTF node
  302. */
  303. private createNode(babylonTransformNode, binaryWriter);
  304. }
  305. /**
  306. * @hidden
  307. *
  308. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  309. */
  310. class _BinaryWriter {
  311. /**
  312. * Array buffer which stores all binary data
  313. */
  314. private _arrayBuffer;
  315. /**
  316. * View of the array buffer
  317. */
  318. private _dataView;
  319. /**
  320. * byte offset of data in array buffer
  321. */
  322. private _byteOffset;
  323. /**
  324. * Initialize binary writer with an initial byte length
  325. * @param byteLength Initial byte length of the array buffer
  326. */
  327. constructor(byteLength: number);
  328. /**
  329. * Resize the array buffer to the specified byte length
  330. * @param byteLength
  331. */
  332. private resizeBuffer(byteLength);
  333. /**
  334. * Get an array buffer with the length of the byte offset
  335. * @returns ArrayBuffer resized to the byte offset
  336. */
  337. getArrayBuffer(): ArrayBuffer;
  338. /**
  339. * Get the byte offset of the array buffer
  340. * @returns byte offset
  341. */
  342. getByteOffset(): number;
  343. /**
  344. * Stores an UInt8 in the array buffer
  345. * @param entry
  346. * @param byteOffset If defined, specifies where to set the value as an offset.
  347. */
  348. setUInt8(entry: number, byteOffset?: number): void;
  349. /**
  350. * Gets an UInt32 in the array buffer
  351. * @param entry
  352. * @param byteOffset If defined, specifies where to set the value as an offset.
  353. */
  354. getUInt32(byteOffset: number): number;
  355. getVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  356. setVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  357. getVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  358. setVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  359. /**
  360. * Stores a Float32 in the array buffer
  361. * @param entry
  362. */
  363. setFloat32(entry: number, byteOffset?: number): void;
  364. /**
  365. * Stores an UInt32 in the array buffer
  366. * @param entry
  367. * @param byteOffset If defined, specifies where to set the value as an offset.
  368. */
  369. setUInt32(entry: number, byteOffset?: number): void;
  370. }
  371. }
  372. declare module BABYLON {
  373. /**
  374. * Class for holding and downloading glTF file data
  375. */
  376. class GLTFData {
  377. /**
  378. * Object which contains the file name as the key and its data as the value
  379. */
  380. glTFFiles: {
  381. [fileName: string]: string | Blob;
  382. };
  383. /**
  384. * Initializes the glTF file object
  385. */
  386. constructor();
  387. /**
  388. * Downloads the glTF data as files based on their names and data
  389. */
  390. downloadFiles(): void;
  391. }
  392. }
  393. declare module BABYLON.GLTF2 {
  394. /**
  395. * Utility methods for working with glTF material conversion properties. This class should only be used internally
  396. * @hidden
  397. */
  398. class _GLTFMaterialExporter {
  399. /**
  400. * Represents the dielectric specular values for R, G and B
  401. */
  402. private static readonly _DielectricSpecular;
  403. /**
  404. * Allows the maximum specular power to be defined for material calculations
  405. */
  406. private static readonly _MaxSpecularPower;
  407. /**
  408. * Mapping to store textures
  409. */
  410. private _textureMap;
  411. /**
  412. * Numeric tolerance value
  413. */
  414. private static readonly _Epsilon;
  415. /**
  416. * Reference to the glTF Exporter
  417. */
  418. private _exporter;
  419. constructor(exporter: _Exporter);
  420. /**
  421. * Specifies if two colors are approximately equal in value
  422. * @param color1 first color to compare to
  423. * @param color2 second color to compare to
  424. * @param epsilon threshold value
  425. */
  426. private static FuzzyEquals(color1, color2, epsilon);
  427. /**
  428. * Gets the materials from a Babylon scene and converts them to glTF materials
  429. * @param scene babylonjs scene
  430. * @param mimeType texture mime type
  431. * @param images array of images
  432. * @param textures array of textures
  433. * @param materials array of materials
  434. * @param imageData mapping of texture names to base64 textures
  435. * @param hasTextureCoords specifies if texture coordinates are present on the material
  436. */
  437. _convertMaterialsToGLTFAsync(babylonMaterials: Material[], mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  438. /**
  439. * Makes a copy of the glTF material without the texture parameters
  440. * @param originalMaterial original glTF material
  441. * @returns glTF material without texture parameters
  442. */
  443. _stripTexturesFromMaterial(originalMaterial: IMaterial): IMaterial;
  444. /**
  445. * Specifies if the material has any texture parameters present
  446. * @param material glTF Material
  447. * @returns boolean specifying if texture parameters are present
  448. */
  449. _hasTexturesPresent(material: IMaterial): boolean;
  450. /**
  451. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
  452. * @param babylonStandardMaterial
  453. * @returns glTF Metallic Roughness Material representation
  454. */
  455. _convertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  456. /**
  457. * Computes the metallic factor
  458. * @param diffuse diffused value
  459. * @param specular specular value
  460. * @param oneMinusSpecularStrength one minus the specular strength
  461. * @returns metallic value
  462. */
  463. static _SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  464. /**
  465. * Gets the glTF alpha mode from the Babylon Material
  466. * @param babylonMaterial Babylon Material
  467. * @returns The Babylon alpha mode value
  468. */
  469. _getAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  470. /**
  471. * Converts a Babylon Standard Material to a glTF Material
  472. * @param babylonStandardMaterial BJS Standard Material
  473. * @param mimeType mime type to use for the textures
  474. * @param images array of glTF image interfaces
  475. * @param textures array of glTF texture interfaces
  476. * @param materials array of glTF material interfaces
  477. * @param imageData map of image file name to data
  478. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  479. */
  480. _convertStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  481. /**
  482. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  483. * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
  484. * @param mimeType mime type to use for the textures
  485. * @param images array of glTF image interfaces
  486. * @param textures array of glTF texture interfaces
  487. * @param materials array of glTF material interfaces
  488. * @param imageData map of image file name to data
  489. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  490. */
  491. _convertPBRMetallicRoughnessMaterialAsync(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  492. /**
  493. * Converts an image typed array buffer to a base64 image
  494. * @param buffer typed array buffer
  495. * @param width width of the image
  496. * @param height height of the image
  497. * @param mimeType mimetype of the image
  498. * @returns base64 image string
  499. */
  500. private _createBase64FromCanvasAsync(buffer, width, height, mimeType);
  501. /**
  502. * Generates a white texture based on the specified width and height
  503. * @param width width of the texture in pixels
  504. * @param height height of the texture in pixels
  505. * @param scene babylonjs scene
  506. * @returns white texture
  507. */
  508. private _createWhiteTexture(width, height, scene);
  509. /**
  510. * 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
  511. * @param texture1 first texture to resize
  512. * @param texture2 second texture to resize
  513. * @param scene babylonjs scene
  514. * @returns resized textures or null
  515. */
  516. private _resizeTexturesToSameDimensions(texture1, texture2, scene);
  517. /**
  518. * Convert Specular Glossiness Textures to Metallic Roughness
  519. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  520. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  521. * @param diffuseTexture texture used to store diffuse information
  522. * @param specularGlossinessTexture texture used to store specular and glossiness information
  523. * @param factors specular glossiness material factors
  524. * @param mimeType the mime type to use for the texture
  525. * @returns pbr metallic roughness interface or null
  526. */
  527. private _convertSpecularGlossinessTexturesToMetallicRoughnessAsync(diffuseTexture, specularGlossinessTexture, factors, mimeType);
  528. /**
  529. * Converts specular glossiness material properties to metallic roughness
  530. * @param specularGlossiness interface with specular glossiness material properties
  531. * @returns interface with metallic roughness material properties
  532. */
  533. private _convertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  534. /**
  535. * Calculates the surface reflectance, independent of lighting conditions
  536. * @param color Color source to calculate brightness from
  537. * @returns number representing the perceived brightness, or zero if color is undefined
  538. */
  539. private _getPerceivedBrightness(color);
  540. /**
  541. * Returns the maximum color component value
  542. * @param color
  543. * @returns maximum color component value, or zero if color is null or undefined
  544. */
  545. private _getMaxComponent(color);
  546. /**
  547. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
  548. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  549. * @param mimeType mime type to use for the textures
  550. * @param images array of glTF image interfaces
  551. * @param textures array of glTF texture interfaces
  552. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  553. * @param imageData map of image file name to data
  554. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  555. * @returns glTF PBR Metallic Roughness factors
  556. */
  557. private _convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  558. private _getGLTFTextureSampler(texture);
  559. private _getGLTFTextureWrapMode(wrapMode);
  560. private _getGLTFTextureWrapModesSampler(texture);
  561. /**
  562. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
  563. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  564. * @param mimeType mime type to use for the textures
  565. * @param images array of glTF image interfaces
  566. * @param textures array of glTF texture interfaces
  567. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  568. * @param imageData map of image file name to data
  569. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  570. * @returns glTF PBR Metallic Roughness factors
  571. */
  572. private _convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  573. /**
  574. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  575. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  576. * @param mimeType mime type to use for the textures
  577. * @param images array of glTF image interfaces
  578. * @param textures array of glTF texture interfaces
  579. * @param materials array of glTF material interfaces
  580. * @param imageData map of image file name to data
  581. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  582. */
  583. _convertPBRMaterialAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  584. private setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
  585. private getPixelsFromTexture(babylonTexture);
  586. /**
  587. * Extracts a texture from a Babylon texture into file data and glTF data
  588. * @param babylonTexture Babylon texture to extract
  589. * @param mimeType Mime Type of the babylonTexture
  590. * @param images Array of glTF images
  591. * @param textures Array of glTF textures
  592. * @param imageData map of image file name and data
  593. * @return glTF texture info, or null if the texture format is not supported
  594. */
  595. private _exportTextureAsync(babylonTexture, mimeType);
  596. /**
  597. * Builds a texture from base64 string
  598. * @param base64Texture base64 texture string
  599. * @param baseTextureName Name to use for the texture
  600. * @param mimeType image mime type for the texture
  601. * @param images array of images
  602. * @param textures array of textures
  603. * @param imageData map of image data
  604. * @returns glTF texture info, or null if the texture format is not supported
  605. */
  606. private _getTextureInfoFromBase64(base64Texture, baseTextureName, mimeType, texCoordIndex, samplerIndex);
  607. }
  608. }
  609. declare module BABYLON.GLTF2 {
  610. /**
  611. * @hidden
  612. * Interface to store animation data.
  613. */
  614. interface _IAnimationData {
  615. /**
  616. * Keyframe data.
  617. */
  618. inputs: number[];
  619. /**
  620. * Value data.
  621. */
  622. outputs: number[][];
  623. /**
  624. * Animation interpolation data.
  625. */
  626. samplerInterpolation: AnimationSamplerInterpolation;
  627. /**
  628. * Minimum keyframe value.
  629. */
  630. inputsMin: number;
  631. /**
  632. * Maximum keyframe value.
  633. */
  634. inputsMax: number;
  635. }
  636. /**
  637. * @hidden
  638. */
  639. interface _IAnimationInfo {
  640. /**
  641. * The target channel for the animation
  642. */
  643. animationChannelTargetPath: AnimationChannelTargetPath;
  644. /**
  645. * The glTF accessor type for the data.
  646. */
  647. dataAccessorType: AccessorType.VEC3 | AccessorType.VEC4;
  648. /**
  649. * Specifies if quaternions should be used.
  650. */
  651. useQuaternion: boolean;
  652. }
  653. /**
  654. * @hidden
  655. * Utility class for generating glTF animation data from BabylonJS.
  656. */
  657. class _GLTFAnimation {
  658. /**
  659. * @ignore
  660. *
  661. * Creates glTF channel animation from BabylonJS animation.
  662. * @param babylonTransformNode - BabylonJS mesh.
  663. * @param animation - animation.
  664. * @param animationChannelTargetPath - The target animation channel.
  665. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  666. * @param useQuaternion - Specifies if quaternions are used.
  667. * @returns nullable IAnimationData
  668. */
  669. static _CreateNodeAnimation(babylonTransformNode: TransformNode, animation: Animation, animationChannelTargetPath: AnimationChannelTargetPath, convertToRightHandedSystem: boolean, useQuaternion: boolean, animationSampleRate: number): Nullable<_IAnimationData>;
  670. private static _DeduceAnimationInfo(animation);
  671. /**
  672. * @ignore
  673. * Create node animations from the transform node animations
  674. * @param babylonTransformNode
  675. * @param runtimeGLTFAnimation
  676. * @param idleGLTFAnimations
  677. * @param nodeMap
  678. * @param nodes
  679. * @param binaryWriter
  680. * @param bufferViews
  681. * @param accessors
  682. * @param convertToRightHandedSystem
  683. */
  684. static _CreateNodeAnimationFromTransformNodeAnimations(babylonTransformNode: TransformNode, runtimeGLTFAnimation: IAnimation, idleGLTFAnimations: IAnimation[], nodeMap: {
  685. [key: number]: number;
  686. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  687. /**
  688. * @ignore
  689. * Create node animations from the animation groups
  690. * @param babylonScene
  691. * @param glTFAnimations
  692. * @param nodeMap
  693. * @param nodes
  694. * @param binaryWriter
  695. * @param bufferViews
  696. * @param accessors
  697. * @param convertToRightHandedSystem
  698. */
  699. static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: {
  700. [key: number]: number;
  701. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  702. private static AddAnimation(name, glTFAnimation, babylonTransformNode, animation, dataAccessorType, animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, useQuaternion, animationSampleRate);
  703. /**
  704. * Create a baked animation
  705. * @param babylonTransformNode BabylonJS mesh
  706. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  707. * @param animationChannelTargetPath animation target channel
  708. * @param minFrame minimum animation frame
  709. * @param maxFrame maximum animation frame
  710. * @param fps frames per second of the animation
  711. * @param inputs input key frames of the animation
  712. * @param outputs output key frame data of the animation
  713. * @param convertToRightHandedSystem converts the values to right-handed
  714. * @param useQuaternion specifies if quaternions should be used
  715. */
  716. private static _CreateBakedAnimation(babylonTransformNode, animation, animationChannelTargetPath, minFrame, maxFrame, fps, sampleRate, inputs, outputs, minMaxFrames, convertToRightHandedSystem, useQuaternion);
  717. private static _ConvertFactorToVector3OrQuaternion(factor, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  718. private static _SetInterpolatedValue(babylonTransformNode, value, time, animation, animationChannelTargetPath, quaternionCache, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  719. /**
  720. * Creates linear animation from the animation key frames
  721. * @param babylonTransformNode BabylonJS mesh
  722. * @param animation BabylonJS animation
  723. * @param animationChannelTargetPath The target animation channel
  724. * @param frameDelta The difference between the last and first frame of the animation
  725. * @param inputs Array to store the key frame times
  726. * @param outputs Array to store the key frame data
  727. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  728. * @param useQuaternion Specifies if quaternions are used in the animation
  729. */
  730. private static _CreateLinearOrStepAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  731. /**
  732. * Creates cubic spline animation from the animation key frames
  733. * @param babylonTransformNode BabylonJS mesh
  734. * @param animation BabylonJS animation
  735. * @param animationChannelTargetPath The target animation channel
  736. * @param frameDelta The difference between the last and first frame of the animation
  737. * @param inputs Array to store the key frame times
  738. * @param outputs Array to store the key frame data
  739. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  740. * @param useQuaternion Specifies if quaternions are used in the animation
  741. */
  742. private static _CreateCubicSplineAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  743. private static _GetBasePositionRotationOrScale(babylonTransformNode, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  744. /**
  745. * Adds a key frame value
  746. * @param keyFrame
  747. * @param animation
  748. * @param outputs
  749. * @param animationChannelTargetPath
  750. * @param basePositionRotationOrScale
  751. * @param convertToRightHandedSystem
  752. * @param useQuaternion
  753. */
  754. private static _AddKeyframeValue(keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion);
  755. /**
  756. * Determine the interpolation based on the key frames
  757. * @param keyFrames
  758. * @param animationChannelTargetPath
  759. * @param useQuaternion
  760. */
  761. private static _DeduceInterpolation(keyFrames, animationChannelTargetPath, useQuaternion);
  762. /**
  763. * Adds an input tangent or output tangent to the output data
  764. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  765. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  766. * @param outputs The animation data by keyframe
  767. * @param animationChannelTargetPath The target animation channel
  768. * @param interpolation The interpolation type
  769. * @param keyFrame The key frame with the animation data
  770. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  771. * @param useQuaternion Specifies if quaternions are used
  772. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  773. */
  774. private static AddSplineTangent(babylonTransformNode, tangentType, outputs, animationChannelTargetPath, interpolation, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem);
  775. /**
  776. * Get the minimum and maximum key frames' frame values
  777. * @param keyFrames animation key frames
  778. * @returns the minimum and maximum key frame value
  779. */
  780. private static calculateMinMaxKeyFrames(keyFrames);
  781. }
  782. }
  783. declare module BABYLON.GLTF2 {
  784. /**
  785. * @hidden
  786. */
  787. class _GLTFUtilities {
  788. /**
  789. * Creates a buffer view based on the supplied arguments
  790. * @param bufferIndex index value of the specified buffer
  791. * @param byteOffset byte offset value
  792. * @param byteLength byte length of the bufferView
  793. * @param byteStride byte distance between conequential elements
  794. * @param name name of the buffer view
  795. * @returns bufferView for glTF
  796. */
  797. static _CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView;
  798. /**
  799. * Creates an accessor based on the supplied arguments
  800. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  801. * @param name The name of the accessor
  802. * @param type The type of the accessor
  803. * @param componentType The datatype of components in the attribute
  804. * @param count The number of attributes referenced by this accessor
  805. * @param byteOffset The offset relative to the start of the bufferView in bytes
  806. * @param min Minimum value of each component in this attribute
  807. * @param max Maximum value of each component in this attribute
  808. * @returns accessor for glTF
  809. */
  810. static _CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor;
  811. /**
  812. * Calculates the minimum and maximum values of an array of position floats
  813. * @param positions Positions array of a mesh
  814. * @param vertexStart Starting vertex offset to calculate min and max values
  815. * @param vertexCount Number of vertices to check for min and max values
  816. * @returns min number array and max number array
  817. */
  818. static _CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): {
  819. min: number[];
  820. max: number[];
  821. };
  822. /**
  823. * Converts a new right-handed Vector3
  824. * @param vector vector3 array
  825. * @returns right-handed Vector3
  826. */
  827. static _GetRightHandedPositionVector3(vector: Vector3): Vector3;
  828. /**
  829. * Converts a Vector3 to right-handed
  830. * @param vector Vector3 to convert to right-handed
  831. */
  832. static _GetRightHandedPositionVector3FromRef(vector: Vector3): void;
  833. /**
  834. * Converts a three element number array to right-handed
  835. * @param vector number array to convert to right-handed
  836. */
  837. static _GetRightHandedPositionArray3FromRef(vector: number[]): void;
  838. /**
  839. * Converts a new right-handed Vector3
  840. * @param vector vector3 array
  841. * @returns right-handed Vector3
  842. */
  843. static _GetRightHandedNormalVector3(vector: Vector3): Vector3;
  844. /**
  845. * Converts a Vector3 to right-handed
  846. * @param vector Vector3 to convert to right-handed
  847. */
  848. static _GetRightHandedNormalVector3FromRef(vector: Vector3): void;
  849. /**
  850. * Converts a three element number array to right-handed
  851. * @param vector number array to convert to right-handed
  852. */
  853. static _GetRightHandedNormalArray3FromRef(vector: number[]): void;
  854. /**
  855. * Converts a Vector4 to right-handed
  856. * @param vector Vector4 to convert to right-handed
  857. */
  858. static _GetRightHandedVector4FromRef(vector: Vector4): void;
  859. /**
  860. * Converts a Vector4 to right-handed
  861. * @param vector Vector4 to convert to right-handed
  862. */
  863. static _GetRightHandedArray4FromRef(vector: number[]): void;
  864. /**
  865. * Converts a Quaternion to right-handed
  866. * @param quaternion Source quaternion to convert to right-handed
  867. */
  868. static _GetRightHandedQuaternionFromRef(quaternion: Quaternion): void;
  869. /**
  870. * Converts a Quaternion to right-handed
  871. * @param quaternion Source quaternion to convert to right-handed
  872. */
  873. static _GetRightHandedQuaternionArrayFromRef(quaternion: number[]): void;
  874. static _NormalizeTangentFromRef(tangent: Vector4): void;
  875. }
  876. }