babylonjs.serializers.module.d.ts 43 KB

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