babylon.glTF2Serializer.d.ts 42 KB

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