babylonjs.serializers.module.d.ts 42 KB

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