babylonjs.serializers.d.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. /**
  294. * Creates a mapping of Node unique id to node index and handles animations
  295. * @param babylonScene Babylon Scene
  296. * @param nodes Babylon transform nodes
  297. * @param shouldExportTransformNode Callback specifying if a transform node should be exported
  298. * @param binaryWriter Buffer to write binary data to
  299. * @returns Node mapping of unique id to index
  300. */
  301. private createNodeMapAndAnimations(babylonScene, nodes, shouldExportTransformNode, binaryWriter);
  302. /**
  303. * Creates a glTF node from a Babylon mesh
  304. * @param babylonMesh Source Babylon mesh
  305. * @param binaryWriter Buffer for storing geometry data
  306. * @returns glTF node
  307. */
  308. private createNode(babylonTransformNode, binaryWriter);
  309. }
  310. /**
  311. * @hidden
  312. *
  313. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  314. */
  315. class _BinaryWriter {
  316. /**
  317. * Array buffer which stores all binary data
  318. */
  319. private _arrayBuffer;
  320. /**
  321. * View of the array buffer
  322. */
  323. private _dataView;
  324. /**
  325. * byte offset of data in array buffer
  326. */
  327. private _byteOffset;
  328. /**
  329. * Initialize binary writer with an initial byte length
  330. * @param byteLength Initial byte length of the array buffer
  331. */
  332. constructor(byteLength: number);
  333. /**
  334. * Resize the array buffer to the specified byte length
  335. * @param byteLength
  336. */
  337. private resizeBuffer(byteLength);
  338. /**
  339. * Get an array buffer with the length of the byte offset
  340. * @returns ArrayBuffer resized to the byte offset
  341. */
  342. getArrayBuffer(): ArrayBuffer;
  343. /**
  344. * Get the byte offset of the array buffer
  345. * @returns byte offset
  346. */
  347. getByteOffset(): number;
  348. /**
  349. * Stores an UInt8 in the array buffer
  350. * @param entry
  351. * @param byteOffset If defined, specifies where to set the value as an offset.
  352. */
  353. setUInt8(entry: number, byteOffset?: number): void;
  354. /**
  355. * Gets an UInt32 in the array buffer
  356. * @param entry
  357. * @param byteOffset If defined, specifies where to set the value as an offset.
  358. */
  359. getUInt32(byteOffset: number): number;
  360. getVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  361. setVector3Float32FromRef(vector3: Vector3, byteOffset: number): void;
  362. getVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  363. setVector4Float32FromRef(vector4: Vector4, byteOffset: number): void;
  364. /**
  365. * Stores a Float32 in the array buffer
  366. * @param entry
  367. */
  368. setFloat32(entry: number, byteOffset?: number): void;
  369. /**
  370. * Stores an UInt32 in the array buffer
  371. * @param entry
  372. * @param byteOffset If defined, specifies where to set the value as an offset.
  373. */
  374. setUInt32(entry: number, byteOffset?: number): void;
  375. }
  376. }
  377. declare module BABYLON {
  378. /**
  379. * Class for holding and downloading glTF file data
  380. */
  381. class GLTFData {
  382. /**
  383. * Object which contains the file name as the key and its data as the value
  384. */
  385. glTFFiles: {
  386. [fileName: string]: string | Blob;
  387. };
  388. /**
  389. * Initializes the glTF file object
  390. */
  391. constructor();
  392. /**
  393. * Downloads the glTF data as files based on their names and data
  394. */
  395. downloadFiles(): void;
  396. }
  397. }
  398. declare module BABYLON.GLTF2 {
  399. /**
  400. * Utility methods for working with glTF material conversion properties. This class should only be used internally
  401. * @hidden
  402. */
  403. class _GLTFMaterialExporter {
  404. /**
  405. * Represents the dielectric specular values for R, G and B
  406. */
  407. private static readonly _DielectricSpecular;
  408. /**
  409. * Allows the maximum specular power to be defined for material calculations
  410. */
  411. private static readonly _MaxSpecularPower;
  412. /**
  413. * Mapping to store textures
  414. */
  415. private _textureMap;
  416. /**
  417. * Numeric tolerance value
  418. */
  419. private static readonly _Epsilon;
  420. /**
  421. * Reference to the glTF Exporter
  422. */
  423. private _exporter;
  424. constructor(exporter: _Exporter);
  425. /**
  426. * Specifies if two colors are approximately equal in value
  427. * @param color1 first color to compare to
  428. * @param color2 second color to compare to
  429. * @param epsilon threshold value
  430. */
  431. private static FuzzyEquals(color1, color2, epsilon);
  432. /**
  433. * Gets the materials from a Babylon scene and converts them to glTF materials
  434. * @param scene babylonjs scene
  435. * @param mimeType texture mime type
  436. * @param images array of images
  437. * @param textures array of textures
  438. * @param materials array of materials
  439. * @param imageData mapping of texture names to base64 textures
  440. * @param hasTextureCoords specifies if texture coordinates are present on the material
  441. */
  442. _convertMaterialsToGLTFAsync(babylonMaterials: Material[], mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  443. /**
  444. * Makes a copy of the glTF material without the texture parameters
  445. * @param originalMaterial original glTF material
  446. * @returns glTF material without texture parameters
  447. */
  448. _stripTexturesFromMaterial(originalMaterial: IMaterial): IMaterial;
  449. /**
  450. * Specifies if the material has any texture parameters present
  451. * @param material glTF Material
  452. * @returns boolean specifying if texture parameters are present
  453. */
  454. _hasTexturesPresent(material: IMaterial): boolean;
  455. /**
  456. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
  457. * @param babylonStandardMaterial
  458. * @returns glTF Metallic Roughness Material representation
  459. */
  460. _convertToGLTFPBRMetallicRoughness(babylonStandardMaterial: StandardMaterial): IMaterialPbrMetallicRoughness;
  461. /**
  462. * Computes the metallic factor
  463. * @param diffuse diffused value
  464. * @param specular specular value
  465. * @param oneMinusSpecularStrength one minus the specular strength
  466. * @returns metallic value
  467. */
  468. static _SolveMetallic(diffuse: number, specular: number, oneMinusSpecularStrength: number): number;
  469. /**
  470. * Gets the glTF alpha mode from the Babylon Material
  471. * @param babylonMaterial Babylon Material
  472. * @returns The Babylon alpha mode value
  473. */
  474. _getAlphaMode(babylonMaterial: Material): MaterialAlphaMode;
  475. /**
  476. * Converts a Babylon Standard Material to a glTF Material
  477. * @param babylonStandardMaterial BJS Standard Material
  478. * @param mimeType mime type to use for the textures
  479. * @param images array of glTF image interfaces
  480. * @param textures array of glTF texture interfaces
  481. * @param materials array of glTF material interfaces
  482. * @param imageData map of image file name to data
  483. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  484. */
  485. _convertStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  486. /**
  487. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  488. * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
  489. * @param mimeType mime type to use for the textures
  490. * @param images array of glTF image interfaces
  491. * @param textures array of glTF texture interfaces
  492. * @param materials array of glTF material interfaces
  493. * @param imageData map of image file name to data
  494. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  495. */
  496. _convertPBRMetallicRoughnessMaterialAsync(babylonPBRMetalRoughMaterial: PBRMetallicRoughnessMaterial, mimeType: ImageMimeType, 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 _createBase64FromCanvasAsync(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 _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 _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 _convertSpecularGlossinessTexturesToMetallicRoughnessAsync(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 _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 _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 _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 _gonvertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords);
  563. private _getGLTFTextureSampler(texture);
  564. private _getGLTFTextureWrapMode(wrapMode);
  565. private _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 _convertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, 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. _convertPBRMaterialAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, hasTextureCoords: boolean): Promise<void>;
  589. private setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
  590. private getPixelsFromTexture(babylonTexture);
  591. /**
  592. * Extracts a texture from a Babylon texture into file data and glTF data
  593. * @param babylonTexture Babylon texture to extract
  594. * @param mimeType Mime Type of the babylonTexture
  595. * @param images Array of glTF images
  596. * @param textures Array of glTF textures
  597. * @param imageData map of image file name and data
  598. * @return glTF texture info, or null if the texture format is not supported
  599. */
  600. private _exportTextureAsync(babylonTexture, mimeType);
  601. /**
  602. * Builds a texture from base64 string
  603. * @param base64Texture base64 texture string
  604. * @param baseTextureName Name to use for the texture
  605. * @param mimeType image mime type for the texture
  606. * @param images array of images
  607. * @param textures array of textures
  608. * @param imageData map of image data
  609. * @returns glTF texture info, or null if the texture format is not supported
  610. */
  611. private _getTextureInfoFromBase64(base64Texture, baseTextureName, mimeType, texCoordIndex, samplerIndex);
  612. }
  613. }
  614. declare module BABYLON.GLTF2 {
  615. /**
  616. * @hidden
  617. * Interface to store animation data.
  618. */
  619. interface _IAnimationData {
  620. /**
  621. * Keyframe data.
  622. */
  623. inputs: number[];
  624. /**
  625. * Value data.
  626. */
  627. outputs: number[][];
  628. /**
  629. * Animation interpolation data.
  630. */
  631. samplerInterpolation: AnimationSamplerInterpolation;
  632. /**
  633. * Minimum keyframe value.
  634. */
  635. inputsMin: number;
  636. /**
  637. * Maximum keyframe value.
  638. */
  639. inputsMax: number;
  640. }
  641. /**
  642. * @hidden
  643. */
  644. interface _IAnimationInfo {
  645. /**
  646. * The target channel for the animation
  647. */
  648. animationChannelTargetPath: AnimationChannelTargetPath;
  649. /**
  650. * The glTF accessor type for the data.
  651. */
  652. dataAccessorType: AccessorType.VEC3 | AccessorType.VEC4;
  653. /**
  654. * Specifies if quaternions should be used.
  655. */
  656. useQuaternion: boolean;
  657. }
  658. /**
  659. * @hidden
  660. * Utility class for generating glTF animation data from BabylonJS.
  661. */
  662. class _GLTFAnimation {
  663. /**
  664. * @ignore
  665. *
  666. * Creates glTF channel animation from BabylonJS animation.
  667. * @param babylonTransformNode - BabylonJS mesh.
  668. * @param animation - animation.
  669. * @param animationChannelTargetPath - The target animation channel.
  670. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  671. * @param useQuaternion - Specifies if quaternions are used.
  672. * @returns nullable IAnimationData
  673. */
  674. static _CreateNodeAnimation(babylonTransformNode: TransformNode, animation: Animation, animationChannelTargetPath: AnimationChannelTargetPath, convertToRightHandedSystem: boolean, useQuaternion: boolean, animationSampleRate: number): Nullable<_IAnimationData>;
  675. private static _DeduceAnimationInfo(animation);
  676. /**
  677. * @ignore
  678. * Create node animations from the transform node animations
  679. * @param babylonTransformNode
  680. * @param runtimeGLTFAnimation
  681. * @param idleGLTFAnimations
  682. * @param nodeMap
  683. * @param nodes
  684. * @param binaryWriter
  685. * @param bufferViews
  686. * @param accessors
  687. * @param convertToRightHandedSystem
  688. */
  689. static _CreateNodeAnimationFromTransformNodeAnimations(babylonTransformNode: TransformNode, runtimeGLTFAnimation: IAnimation, idleGLTFAnimations: IAnimation[], nodeMap: {
  690. [key: number]: number;
  691. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  692. /**
  693. * @ignore
  694. * Create node animations from the animation groups
  695. * @param babylonScene
  696. * @param glTFAnimations
  697. * @param nodeMap
  698. * @param nodes
  699. * @param binaryWriter
  700. * @param bufferViews
  701. * @param accessors
  702. * @param convertToRightHandedSystem
  703. */
  704. static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: {
  705. [key: number]: number;
  706. }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystem: boolean, animationSampleRate: number): void;
  707. private static AddAnimation(name, glTFAnimation, babylonTransformNode, animation, dataAccessorType, animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, useQuaternion, animationSampleRate);
  708. /**
  709. * Create a baked animation
  710. * @param babylonTransformNode BabylonJS mesh
  711. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  712. * @param animationChannelTargetPath animation target channel
  713. * @param minFrame minimum animation frame
  714. * @param maxFrame maximum animation frame
  715. * @param fps frames per second of the animation
  716. * @param inputs input key frames of the animation
  717. * @param outputs output key frame data of the animation
  718. * @param convertToRightHandedSystem converts the values to right-handed
  719. * @param useQuaternion specifies if quaternions should be used
  720. */
  721. private static _CreateBakedAnimation(babylonTransformNode, animation, animationChannelTargetPath, minFrame, maxFrame, fps, sampleRate, inputs, outputs, minMaxFrames, convertToRightHandedSystem, useQuaternion);
  722. private static _ConvertFactorToVector3OrQuaternion(factor, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  723. private static _SetInterpolatedValue(babylonTransformNode, value, time, animation, animationChannelTargetPath, quaternionCache, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  724. /**
  725. * Creates linear animation from the animation key frames
  726. * @param babylonTransformNode BabylonJS mesh
  727. * @param animation BabylonJS animation
  728. * @param animationChannelTargetPath The target animation channel
  729. * @param frameDelta The difference between the last and first frame of the animation
  730. * @param inputs Array to store the key frame times
  731. * @param outputs Array to store the key frame data
  732. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  733. * @param useQuaternion Specifies if quaternions are used in the animation
  734. */
  735. private static _CreateLinearOrStepAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  736. /**
  737. * Creates cubic spline animation from the animation key frames
  738. * @param babylonTransformNode BabylonJS mesh
  739. * @param animation BabylonJS animation
  740. * @param animationChannelTargetPath The target animation channel
  741. * @param frameDelta The difference between the last and first frame of the animation
  742. * @param inputs Array to store the key frame times
  743. * @param outputs Array to store the key frame data
  744. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  745. * @param useQuaternion Specifies if quaternions are used in the animation
  746. */
  747. private static _CreateCubicSplineAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  748. private static _GetBasePositionRotationOrScale(babylonTransformNode, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  749. /**
  750. * Adds a key frame value
  751. * @param keyFrame
  752. * @param animation
  753. * @param outputs
  754. * @param animationChannelTargetPath
  755. * @param basePositionRotationOrScale
  756. * @param convertToRightHandedSystem
  757. * @param useQuaternion
  758. */
  759. private static _AddKeyframeValue(keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion);
  760. /**
  761. * Determine the interpolation based on the key frames
  762. * @param keyFrames
  763. * @param animationChannelTargetPath
  764. * @param useQuaternion
  765. */
  766. private static _DeduceInterpolation(keyFrames, animationChannelTargetPath, useQuaternion);
  767. /**
  768. * Adds an input tangent or output tangent to the output data
  769. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  770. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  771. * @param outputs The animation data by keyframe
  772. * @param animationChannelTargetPath The target animation channel
  773. * @param interpolation The interpolation type
  774. * @param keyFrame The key frame with the animation data
  775. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  776. * @param useQuaternion Specifies if quaternions are used
  777. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  778. */
  779. private static AddSplineTangent(babylonTransformNode, tangentType, outputs, animationChannelTargetPath, interpolation, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem);
  780. /**
  781. * Get the minimum and maximum key frames' frame values
  782. * @param keyFrames animation key frames
  783. * @returns the minimum and maximum key frame value
  784. */
  785. private static calculateMinMaxKeyFrames(keyFrames);
  786. }
  787. }
  788. declare module BABYLON.GLTF2 {
  789. /**
  790. * @hidden
  791. */
  792. class _GLTFUtilities {
  793. /**
  794. * Creates a buffer view based on the supplied arguments
  795. * @param bufferIndex index value of the specified buffer
  796. * @param byteOffset byte offset value
  797. * @param byteLength byte length of the bufferView
  798. * @param byteStride byte distance between conequential elements
  799. * @param name name of the buffer view
  800. * @returns bufferView for glTF
  801. */
  802. static CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView;
  803. /**
  804. * Creates an accessor based on the supplied arguments
  805. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  806. * @param name The name of the accessor
  807. * @param type The type of the accessor
  808. * @param componentType The datatype of components in the attribute
  809. * @param count The number of attributes referenced by this accessor
  810. * @param byteOffset The offset relative to the start of the bufferView in bytes
  811. * @param min Minimum value of each component in this attribute
  812. * @param max Maximum value of each component in this attribute
  813. * @returns accessor for glTF
  814. */
  815. static CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor;
  816. /**
  817. * Calculates the minimum and maximum values of an array of position floats
  818. * @param positions Positions array of a mesh
  819. * @param vertexStart Starting vertex offset to calculate min and max values
  820. * @param vertexCount Number of vertices to check for min and max values
  821. * @returns min number array and max number array
  822. */
  823. static CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): {
  824. min: number[];
  825. max: number[];
  826. };
  827. /**
  828. * Converts a new right-handed Vector3
  829. * @param vector vector3 array
  830. * @returns right-handed Vector3
  831. */
  832. static GetRightHandedPositionVector3(vector: Vector3): Vector3;
  833. /**
  834. * Converts a Vector3 to right-handed
  835. * @param vector Vector3 to convert to right-handed
  836. */
  837. static GetRightHandedPositionVector3FromRef(vector: Vector3): void;
  838. /**
  839. * Converts a three element number array to right-handed
  840. * @param vector number array to convert to right-handed
  841. */
  842. static GetRightHandedPositionArray3FromRef(vector: number[]): void;
  843. /**
  844. * Converts a new right-handed Vector3
  845. * @param vector vector3 array
  846. * @returns right-handed Vector3
  847. */
  848. static GetRightHandedNormalVector3(vector: Vector3): Vector3;
  849. /**
  850. * Converts a Vector3 to right-handed
  851. * @param vector Vector3 to convert to right-handed
  852. */
  853. static GetRightHandedNormalVector3FromRef(vector: Vector3): void;
  854. /**
  855. * Converts a three element number array to right-handed
  856. * @param vector number array to convert to right-handed
  857. */
  858. static GetRightHandedNormalArray3FromRef(vector: number[]): void;
  859. /**
  860. * Converts a Vector4 to right-handed
  861. * @param vector Vector4 to convert to right-handed
  862. */
  863. static GetRightHandedVector4FromRef(vector: Vector4): void;
  864. /**
  865. * Converts a Vector4 to right-handed
  866. * @param vector Vector4 to convert to right-handed
  867. */
  868. static GetRightHandedArray4FromRef(vector: number[]): void;
  869. /**
  870. * Converts a Quaternion to right-handed
  871. * @param quaternion Source quaternion to convert to right-handed
  872. */
  873. static GetRightHandedQuaternionFromRef(quaternion: Quaternion): void;
  874. /**
  875. * Converts a Quaternion to right-handed
  876. * @param quaternion Source quaternion to convert to right-handed
  877. */
  878. static GetRightHandedQuaternionArrayFromRef(quaternion: number[]): void;
  879. }
  880. }