babylon.glTF2Serializer.d.ts 42 KB

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