babylon.glTF2Serializer.d.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. /**
  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(babylonScene, nodes, shouldExportTransformNode, binaryWriter);
  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(babylonTransformNode, binaryWriter);
  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(byteLength);
  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(color1, color2, epsilon);
  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(buffer, width, height, mimeType);
  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(width, height, scene);
  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(texture1, texture2, scene);
  516. /**
  517. * Convert Specular Glossiness Textures to Metallic Roughness
  518. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  519. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  520. * @param diffuseTexture texture used to store diffuse information
  521. * @param specularGlossinessTexture texture used to store specular and glossiness information
  522. * @param factors specular glossiness material factors
  523. * @param mimeType the mime type to use for the texture
  524. * @returns pbr metallic roughness interface or null
  525. */
  526. private _convertSpecularGlossinessTexturesToMetallicRoughnessAsync(diffuseTexture, specularGlossinessTexture, factors, mimeType);
  527. /**
  528. * Converts specular glossiness material properties to metallic roughness
  529. * @param specularGlossiness interface with specular glossiness material properties
  530. * @returns interface with metallic roughness material properties
  531. */
  532. private _convertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  533. /**
  534. * Calculates the surface reflectance, independent of lighting conditions
  535. * @param color Color source to calculate brightness from
  536. * @returns number representing the perceived brightness, or zero if color is undefined
  537. */
  538. private _getPerceivedBrightness(color);
  539. /**
  540. * Returns the maximum color component value
  541. * @param color
  542. * @returns maximum color component value, or zero if color is null or undefined
  543. */
  544. private _getMaxComponent(color);
  545. /**
  546. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
  547. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  548. * @param mimeType mime type to use for the textures
  549. * @param images array of glTF image interfaces
  550. * @param textures array of glTF texture interfaces
  551. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  552. * @param imageData map of image file name to data
  553. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  554. * @returns glTF PBR Metallic Roughness factors
  555. */
  556. private _gonvertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  557. private _getGLTFTextureSampler(texture);
  558. private _getGLTFTextureWrapMode(wrapMode);
  559. private _getGLTFTextureWrapModesSampler(texture);
  560. /**
  561. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
  562. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  563. * @param mimeType mime type to use for the textures
  564. * @param images array of glTF image interfaces
  565. * @param textures array of glTF texture interfaces
  566. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  567. * @param imageData map of image file name to data
  568. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  569. * @returns glTF PBR Metallic Roughness factors
  570. */
  571. private _convertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  572. /**
  573. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  574. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  575. * @param mimeType mime type to use for the textures
  576. * @param images array of glTF image interfaces
  577. * @param textures array of glTF texture interfaces
  578. * @param materials array of glTF material interfaces
  579. * @param imageData map of image file name to data
  580. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  581. */
  582. _convertPBRMaterialAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  583. private setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
  584. private getPixelsFromTexture(babylonTexture);
  585. /**
  586. * Extracts a texture from a Babylon texture into file data and glTF data
  587. * @param babylonTexture Babylon texture to extract
  588. * @param mimeType Mime Type of the babylonTexture
  589. * @param images Array of glTF images
  590. * @param textures Array of glTF textures
  591. * @param imageData map of image file name and data
  592. * @return glTF texture info, or null if the texture format is not supported
  593. */
  594. private _exportTextureAsync(babylonTexture, mimeType);
  595. /**
  596. * Builds a texture from base64 string
  597. * @param base64Texture base64 texture string
  598. * @param baseTextureName Name to use for the texture
  599. * @param mimeType image mime type for the texture
  600. * @param images array of images
  601. * @param textures array of textures
  602. * @param imageData map of image data
  603. * @returns glTF texture info, or null if the texture format is not supported
  604. */
  605. private _getTextureInfoFromBase64(base64Texture, baseTextureName, mimeType, texCoordIndex, samplerIndex);
  606. }
  607. }
  608. declare module BABYLON.GLTF2 {
  609. /**
  610. * @hidden
  611. * Interface to store animation data.
  612. */
  613. interface _IAnimationData {
  614. /**
  615. * Keyframe data.
  616. */
  617. inputs: number[];
  618. /**
  619. * Value data.
  620. */
  621. outputs: number[][];
  622. /**
  623. * Animation interpolation data.
  624. */
  625. samplerInterpolation: AnimationSamplerInterpolation;
  626. /**
  627. * Minimum keyframe value.
  628. */
  629. inputsMin: number;
  630. /**
  631. * Maximum keyframe value.
  632. */
  633. inputsMax: number;
  634. }
  635. /**
  636. * @hidden
  637. */
  638. interface _IAnimationInfo {
  639. /**
  640. * The target channel for the animation
  641. */
  642. animationChannelTargetPath: AnimationChannelTargetPath;
  643. /**
  644. * The glTF accessor type for the data.
  645. */
  646. dataAccessorType: AccessorType.VEC3 | AccessorType.VEC4;
  647. /**
  648. * Specifies if quaternions should be used.
  649. */
  650. useQuaternion: boolean;
  651. }
  652. /**
  653. * @hidden
  654. * Utility class for generating glTF animation data from BabylonJS.
  655. */
  656. class _GLTFAnimation {
  657. /**
  658. * @ignore
  659. *
  660. * Creates glTF channel animation from BabylonJS animation.
  661. * @param babylonTransformNode - BabylonJS mesh.
  662. * @param animation - animation.
  663. * @param animationChannelTargetPath - The target animation channel.
  664. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  665. * @param useQuaternion - Specifies if quaternions are used.
  666. * @returns nullable IAnimationData
  667. */
  668. static _CreateNodeAnimation(babylonTransformNode: TransformNode, animation: Animation, animationChannelTargetPath: AnimationChannelTargetPath, convertToRightHandedSystem: boolean, useQuaternion: boolean, animationSampleRate: number): Nullable<_IAnimationData>;
  669. private static _DeduceAnimationInfo(animation);
  670. /**
  671. * @ignore
  672. * Create node animations from the transform node animations
  673. * @param babylonTransformNode
  674. * @param runtimeGLTFAnimation
  675. * @param idleGLTFAnimations
  676. * @param nodeMap
  677. * @param nodes
  678. * @param binaryWriter
  679. * @param bufferViews
  680. * @param accessors
  681. * @param convertToRightHandedSystem
  682. */
  683. static _CreateNodeAnimationFromTransformNodeAnimations(babylonTransformNode: TransformNode, runtimeGLTFAnimation: IAnimation, idleGLTFAnimations: IAnimation[], nodeMap: {
  684. [key: number]: number;
  685. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  686. /**
  687. * @ignore
  688. * Create node animations from the animation groups
  689. * @param babylonScene
  690. * @param glTFAnimations
  691. * @param nodeMap
  692. * @param nodes
  693. * @param binaryWriter
  694. * @param bufferViews
  695. * @param accessors
  696. * @param convertToRightHandedSystem
  697. */
  698. static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: {
  699. [key: number]: number;
  700. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  701. private static AddAnimation(name, glTFAnimation, babylonTransformNode, animation, dataAccessorType, animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, useQuaternion, animationSampleRate);
  702. /**
  703. * Create a baked animation
  704. * @param babylonTransformNode BabylonJS mesh
  705. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  706. * @param animationChannelTargetPath animation target channel
  707. * @param minFrame minimum animation frame
  708. * @param maxFrame maximum animation frame
  709. * @param fps frames per second of the animation
  710. * @param inputs input key frames of the animation
  711. * @param outputs output key frame data of the animation
  712. * @param convertToRightHandedSystem converts the values to right-handed
  713. * @param useQuaternion specifies if quaternions should be used
  714. */
  715. private static _CreateBakedAnimation(babylonTransformNode, animation, animationChannelTargetPath, minFrame, maxFrame, fps, sampleRate, inputs, outputs, minMaxFrames, convertToRightHandedSystem, useQuaternion);
  716. private static _ConvertFactorToVector3OrQuaternion(factor, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  717. private static _SetInterpolatedValue(babylonTransformNode, value, time, animation, animationChannelTargetPath, quaternionCache, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  718. /**
  719. * Creates linear animation from the animation key frames
  720. * @param babylonTransformNode BabylonJS mesh
  721. * @param animation BabylonJS animation
  722. * @param animationChannelTargetPath The target animation channel
  723. * @param frameDelta The difference between the last and first frame of the animation
  724. * @param inputs Array to store the key frame times
  725. * @param outputs Array to store the key frame data
  726. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  727. * @param useQuaternion Specifies if quaternions are used in the animation
  728. */
  729. private static _CreateLinearOrStepAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  730. /**
  731. * Creates cubic spline animation from the animation key frames
  732. * @param babylonTransformNode BabylonJS mesh
  733. * @param animation BabylonJS animation
  734. * @param animationChannelTargetPath The target animation channel
  735. * @param frameDelta The difference between the last and first frame of the animation
  736. * @param inputs Array to store the key frame times
  737. * @param outputs Array to store the key frame data
  738. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  739. * @param useQuaternion Specifies if quaternions are used in the animation
  740. */
  741. private static _CreateCubicSplineAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  742. private static _GetBasePositionRotationOrScale(babylonTransformNode, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  743. /**
  744. * Adds a key frame value
  745. * @param keyFrame
  746. * @param animation
  747. * @param outputs
  748. * @param animationChannelTargetPath
  749. * @param basePositionRotationOrScale
  750. * @param convertToRightHandedSystem
  751. * @param useQuaternion
  752. */
  753. private static _AddKeyframeValue(keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion);
  754. /**
  755. * Determine the interpolation based on the key frames
  756. * @param keyFrames
  757. * @param animationChannelTargetPath
  758. * @param useQuaternion
  759. */
  760. private static _DeduceInterpolation(keyFrames, animationChannelTargetPath, useQuaternion);
  761. /**
  762. * Adds an input tangent or output tangent to the output data
  763. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  764. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  765. * @param outputs The animation data by keyframe
  766. * @param animationChannelTargetPath The target animation channel
  767. * @param interpolation The interpolation type
  768. * @param keyFrame The key frame with the animation data
  769. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  770. * @param useQuaternion Specifies if quaternions are used
  771. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  772. */
  773. private static AddSplineTangent(babylonTransformNode, tangentType, outputs, animationChannelTargetPath, interpolation, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem);
  774. /**
  775. * Get the minimum and maximum key frames' frame values
  776. * @param keyFrames animation key frames
  777. * @returns the minimum and maximum key frame value
  778. */
  779. private static calculateMinMaxKeyFrames(keyFrames);
  780. }
  781. }
  782. declare module BABYLON.GLTF2 {
  783. /**
  784. * @hidden
  785. */
  786. class _GLTFUtilities {
  787. /**
  788. * Creates a buffer view based on the supplied arguments
  789. * @param bufferIndex index value of the specified buffer
  790. * @param byteOffset byte offset value
  791. * @param byteLength byte length of the bufferView
  792. * @param byteStride byte distance between conequential elements
  793. * @param name name of the buffer view
  794. * @returns bufferView for glTF
  795. */
  796. static CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView;
  797. /**
  798. * Creates an accessor based on the supplied arguments
  799. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  800. * @param name The name of the accessor
  801. * @param type The type of the accessor
  802. * @param componentType The datatype of components in the attribute
  803. * @param count The number of attributes referenced by this accessor
  804. * @param byteOffset The offset relative to the start of the bufferView in bytes
  805. * @param min Minimum value of each component in this attribute
  806. * @param max Maximum value of each component in this attribute
  807. * @returns accessor for glTF
  808. */
  809. static CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor;
  810. /**
  811. * Calculates the minimum and maximum values of an array of position floats
  812. * @param positions Positions array of a mesh
  813. * @param vertexStart Starting vertex offset to calculate min and max values
  814. * @param vertexCount Number of vertices to check for min and max values
  815. * @returns min number array and max number array
  816. */
  817. static CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): {
  818. min: number[];
  819. max: number[];
  820. };
  821. /**
  822. * Converts a new right-handed Vector3
  823. * @param vector vector3 array
  824. * @returns right-handed Vector3
  825. */
  826. static GetRightHandedPositionVector3(vector: Vector3): Vector3;
  827. /**
  828. * Converts a Vector3 to right-handed
  829. * @param vector Vector3 to convert to right-handed
  830. */
  831. static GetRightHandedPositionVector3FromRef(vector: Vector3): void;
  832. /**
  833. * Converts a three element number array to right-handed
  834. * @param vector number array to convert to right-handed
  835. */
  836. static GetRightHandedPositionArray3FromRef(vector: number[]): void;
  837. /**
  838. * Converts a new right-handed Vector3
  839. * @param vector vector3 array
  840. * @returns right-handed Vector3
  841. */
  842. static GetRightHandedNormalVector3(vector: Vector3): Vector3;
  843. /**
  844. * Converts a Vector3 to right-handed
  845. * @param vector Vector3 to convert to right-handed
  846. */
  847. static GetRightHandedNormalVector3FromRef(vector: Vector3): void;
  848. /**
  849. * Converts a three element number array to right-handed
  850. * @param vector number array to convert to right-handed
  851. */
  852. static GetRightHandedNormalArray3FromRef(vector: number[]): void;
  853. /**
  854. * Converts a Vector4 to right-handed
  855. * @param vector Vector4 to convert to right-handed
  856. */
  857. static GetRightHandedVector4FromRef(vector: Vector4): void;
  858. /**
  859. * Converts a Vector4 to right-handed
  860. * @param vector Vector4 to convert to right-handed
  861. */
  862. static GetRightHandedArray4FromRef(vector: number[]): void;
  863. /**
  864. * Converts a Quaternion to right-handed
  865. * @param quaternion Source quaternion to convert to right-handed
  866. */
  867. static GetRightHandedQuaternionFromRef(quaternion: Quaternion): void;
  868. /**
  869. * Converts a Quaternion to right-handed
  870. * @param quaternion Source quaternion to convert to right-handed
  871. */
  872. static GetRightHandedQuaternionArrayFromRef(quaternion: number[]): void;
  873. }
  874. }