babylonjs.serializers.d.ts 43 KB

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