babylonjs.serializers.module.d.ts 43 KB

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