babylon.glTF2Serializer.d.ts 43 KB

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