babylonjs.serializers.d.ts 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. /**
  942. * Defines the module for the built-in glTF 2.0 exporter extensions.
  943. */
  944. declare module BABYLON.GLTF2.Extensions {
  945. }
  946. declare module BABYLON {
  947. /**
  948. * Interface for extending the exporter
  949. * @hidden
  950. */
  951. interface IGLTFExporterExtension {
  952. /**
  953. * The name of this extension
  954. */
  955. readonly name: string;
  956. /**
  957. * Defines whether this extension is enabled
  958. */
  959. enabled: boolean;
  960. /**
  961. * Defines whether this extension is required
  962. */
  963. required: boolean;
  964. }
  965. }
  966. declare module BABYLON.GLTF2.Exporter.Extensions {
  967. /**
  968. * @hidden
  969. */
  970. class KHR_texture_transform implements IGLTFExporterExtension {
  971. /** Name of this extension */
  972. readonly name: string;
  973. /** Defines whether this extension is enabled */
  974. enabled: boolean;
  975. /** Defines whether this extension is required */
  976. required: boolean;
  977. /** Reference to the glTF exporter */
  978. private _exporter;
  979. constructor(exporter: _Exporter);
  980. dispose(): void;
  981. preExportTextureAsync(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Nullable<Promise<Texture>>;
  982. /**
  983. * Transform the babylon texture by the offset, rotation and scale parameters using a procedural texture
  984. * @param babylonTexture
  985. * @param offset
  986. * @param rotation
  987. * @param scale
  988. * @param scene
  989. */
  990. textureTransformTextureAsync(babylonTexture: Texture, offset: Vector2, rotation: number, scale: Vector2, scene: Scene): Promise<BaseTexture>;
  991. }
  992. }