babylonjs.serializers.module.d.ts 43 KB

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