babylonjs.serializers.module.d.ts 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. private getRootNodes(babylonScene, nodes, shouldExportTransformNode);
  299. /**
  300. * Creates a mapping of Node unique id to node index and handles animations
  301. * @param babylonScene Babylon Scene
  302. * @param nodes Babylon transform nodes
  303. * @param shouldExportTransformNode Callback specifying if a transform node should be exported
  304. * @param binaryWriter Buffer to write binary data to
  305. * @returns Node mapping of unique id to index
  306. */
  307. private createNodeMapAndAnimations(babylonScene, nodes, shouldExportTransformNode, binaryWriter);
  308. /**
  309. * Creates a glTF node from a Babylon mesh
  310. * @param babylonMesh Source Babylon mesh
  311. * @param binaryWriter Buffer for storing geometry data
  312. * @returns glTF node
  313. */
  314. private createNode(babylonTransformNode, binaryWriter);
  315. }
  316. /**
  317. * @hidden
  318. *
  319. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  320. */
  321. class _BinaryWriter {
  322. /**
  323. * Array buffer which stores all binary data
  324. */
  325. private _arrayBuffer;
  326. /**
  327. * View of the array buffer
  328. */
  329. private _dataView;
  330. /**
  331. * byte offset of data in array buffer
  332. */
  333. private _byteOffset;
  334. /**
  335. * Initialize binary writer with an initial byte length
  336. * @param byteLength Initial byte length of the array buffer
  337. */
  338. constructor(byteLength: number);
  339. /**
  340. * Resize the array buffer to the specified byte length
  341. * @param byteLength
  342. */
  343. private resizeBuffer(byteLength);
  344. /**
  345. * Get an array buffer with the length of the byte offset
  346. * @returns ArrayBuffer resized to the byte offset
  347. */
  348. getArrayBuffer(): ArrayBuffer;
  349. /**
  350. * Get the byte offset of the array buffer
  351. * @returns byte offset
  352. */
  353. getByteOffset(): number;
  354. /**
  355. * Stores an UInt8 in the array buffer
  356. * @param entry
  357. * @param byteOffset If defined, specifies where to set the value as an offset.
  358. */
  359. setUInt8(entry: number, byteOffset?: number): void;
  360. /**
  361. * Gets an UInt32 in the array buffer
  362. * @param entry
  363. * @param byteOffset If defined, specifies where to set the value as an offset.
  364. */
  365. getUInt32(byteOffset: number): number;
  366. getVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  367. setVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  368. getVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  369. setVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  370. /**
  371. * Stores a Float32 in the array buffer
  372. * @param entry
  373. */
  374. setFloat32(entry: number, byteOffset?: number): void;
  375. /**
  376. * Stores an UInt32 in the array buffer
  377. * @param entry
  378. * @param byteOffset If defined, specifies where to set the value as an offset.
  379. */
  380. setUInt32(entry: number, byteOffset?: number): void;
  381. }
  382. }
  383. declare module BABYLON {
  384. /**
  385. * Class for holding and downloading glTF file data
  386. */
  387. class GLTFData {
  388. /**
  389. * Object which contains the file name as the key and its data as the value
  390. */
  391. glTFFiles: {
  392. [fileName: string]: string | Blob;
  393. };
  394. /**
  395. * Initializes the glTF file object
  396. */
  397. constructor();
  398. /**
  399. * Downloads the glTF data as files based on their names and data
  400. */
  401. downloadFiles(): void;
  402. }
  403. }
  404. declare module BABYLON.GLTF2 {
  405. /**
  406. * Utility methods for working with glTF material conversion properties. This class should only be used internally
  407. * @hidden
  408. */
  409. class _GLTFMaterialExporter {
  410. /**
  411. * Represents the dielectric specular values for R, G and B
  412. */
  413. private static readonly _DielectricSpecular;
  414. /**
  415. * Allows the maximum specular power to be defined for material calculations
  416. */
  417. private static readonly _MaxSpecularPower;
  418. /**
  419. * Mapping to store textures
  420. */
  421. private _textureMap;
  422. /**
  423. * Numeric tolerance value
  424. */
  425. private static readonly _Epsilon;
  426. /**
  427. * Reference to the glTF Exporter
  428. */
  429. private _exporter;
  430. constructor(exporter: _Exporter);
  431. /**
  432. * Specifies if two colors are approximately equal in value
  433. * @param color1 first color to compare to
  434. * @param color2 second color to compare to
  435. * @param epsilon threshold value
  436. */
  437. private static FuzzyEquals(color1, color2, epsilon);
  438. /**
  439. * Gets the materials from a Babylon scene and converts them to glTF materials
  440. * @param scene babylonjs scene
  441. * @param mimeType texture mime type
  442. * @param images array of images
  443. * @param textures array of textures
  444. * @param materials array of materials
  445. * @param imageData mapping of texture names to base64 textures
  446. * @param hasTextureCoords specifies if texture coordinates are present on the material
  447. */
  448. _convertMaterialsToGLTFAsync(babylonMaterials: Material[], mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  449. /**
  450. * Makes a copy of the glTF material without the texture parameters
  451. * @param originalMaterial original glTF material
  452. * @returns glTF material without texture parameters
  453. */
  454. _stripTexturesFromMaterial(originalMaterial: IMaterial): IMaterial;
  455. /**
  456. * Specifies if the material has any texture parameters present
  457. * @param material glTF Material
  458. * @returns boolean specifying if texture parameters are present
  459. */
  460. _hasTexturesPresent(material: IMaterial): boolean;
  461. /**
  462. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
  463. * @param babylonStandardMaterial
  464. * @returns glTF Metallic Roughness Material representation
  465. */
  466. _convertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  467. /**
  468. * Computes the metallic factor
  469. * @param diffuse diffused value
  470. * @param specular specular value
  471. * @param oneMinusSpecularStrength one minus the specular strength
  472. * @returns metallic value
  473. */
  474. static _SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  475. /**
  476. * Gets the glTF alpha mode from the Babylon Material
  477. * @param babylonMaterial Babylon Material
  478. * @returns The Babylon alpha mode value
  479. */
  480. _getAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  481. /**
  482. * Converts a Babylon Standard Material to a glTF Material
  483. * @param babylonStandardMaterial BJS Standard Material
  484. * @param mimeType mime type to use for the textures
  485. * @param images array of glTF image interfaces
  486. * @param textures array of glTF texture interfaces
  487. * @param materials array of glTF material interfaces
  488. * @param imageData map of image file name to data
  489. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  490. */
  491. _convertStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  492. /**
  493. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  494. * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
  495. * @param mimeType mime type to use for the textures
  496. * @param images array of glTF image interfaces
  497. * @param textures array of glTF texture interfaces
  498. * @param materials array of glTF material interfaces
  499. * @param imageData map of image file name to data
  500. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  501. */
  502. _convertPBRMetallicRoughnessMaterialAsync(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  503. /**
  504. * Converts an image typed array buffer to a base64 image
  505. * @param buffer typed array buffer
  506. * @param width width of the image
  507. * @param height height of the image
  508. * @param mimeType mimetype of the image
  509. * @returns base64 image string
  510. */
  511. private _createBase64FromCanvasAsync(buffer, width, height, mimeType);
  512. /**
  513. * Generates a white texture based on the specified width and height
  514. * @param width width of the texture in pixels
  515. * @param height height of the texture in pixels
  516. * @param scene babylonjs scene
  517. * @returns white texture
  518. */
  519. private _createWhiteTexture(width, height, scene);
  520. /**
  521. * 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
  522. * @param texture1 first texture to resize
  523. * @param texture2 second texture to resize
  524. * @param scene babylonjs scene
  525. * @returns resized textures or null
  526. */
  527. private _resizeTexturesToSameDimensions(texture1, texture2, scene);
  528. /**
  529. * Convert Specular Glossiness Textures to Metallic Roughness
  530. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  531. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  532. * @param diffuseTexture texture used to store diffuse information
  533. * @param specularGlossinessTexture texture used to store specular and glossiness information
  534. * @param factors specular glossiness material factors
  535. * @param mimeType the mime type to use for the texture
  536. * @returns pbr metallic roughness interface or null
  537. */
  538. private _convertSpecularGlossinessTexturesToMetallicRoughnessAsync(diffuseTexture, specularGlossinessTexture, factors, mimeType);
  539. /**
  540. * Converts specular glossiness material properties to metallic roughness
  541. * @param specularGlossiness interface with specular glossiness material properties
  542. * @returns interface with metallic roughness material properties
  543. */
  544. private _convertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  545. /**
  546. * Calculates the surface reflectance, independent of lighting conditions
  547. * @param color Color source to calculate brightness from
  548. * @returns number representing the perceived brightness, or zero if color is undefined
  549. */
  550. private _getPerceivedBrightness(color);
  551. /**
  552. * Returns the maximum color component value
  553. * @param color
  554. * @returns maximum color component value, or zero if color is null or undefined
  555. */
  556. private _getMaxComponent(color);
  557. /**
  558. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
  559. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  560. * @param mimeType mime type to use for the textures
  561. * @param images array of glTF image interfaces
  562. * @param textures array of glTF texture interfaces
  563. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  564. * @param imageData map of image file name to data
  565. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  566. * @returns glTF PBR Metallic Roughness factors
  567. */
  568. private _convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  569. private _getGLTFTextureSampler(texture);
  570. private _getGLTFTextureWrapMode(wrapMode);
  571. private _getGLTFTextureWrapModesSampler(texture);
  572. /**
  573. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
  574. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  575. * @param mimeType mime type to use for the textures
  576. * @param images array of glTF image interfaces
  577. * @param textures array of glTF texture interfaces
  578. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  579. * @param imageData map of image file name to data
  580. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  581. * @returns glTF PBR Metallic Roughness factors
  582. */
  583. private _convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  584. /**
  585. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  586. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  587. * @param mimeType mime type to use for the textures
  588. * @param images array of glTF image interfaces
  589. * @param textures array of glTF texture interfaces
  590. * @param materials array of glTF material interfaces
  591. * @param imageData map of image file name to data
  592. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  593. */
  594. _convertPBRMaterialAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  595. private setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
  596. private 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 _exportTextureAsync(babylonTexture, mimeType);
  607. /**
  608. * Builds a texture from base64 string
  609. * @param base64Texture base64 texture string
  610. * @param baseTextureName 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 _getTextureInfoFromBase64(base64Texture, baseTextureName, mimeType, texCoordIndex, samplerIndex);
  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. static _NormalizeTangentFromRef(tangent: Vector4): void;
  886. }
  887. }