babylon.glTF2Serializer.d.ts 43 KB

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