babylonjs.serializers.d.ts 42 KB

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