babylonjs.serializers.d.ts 43 KB

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