babylon.glTF2Serializer.d.ts 42 KB

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