babylon.glTF2Serializer.d.ts 42 KB

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