glTFSerializer.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Node } from "babylonjs/node";
  2. import { Scene } from "babylonjs/scene";
  3. import { GLTFData } from "./glTFData";
  4. import { _Exporter } from "./glTFExporter";
  5. /**
  6. * Holds a collection of exporter options and parameters
  7. */
  8. export interface IExportOptions {
  9. /**
  10. * Function which indicates whether a babylon node should be exported or not
  11. * @param node source Babylon node. It is used to check whether it should be exported to glTF or not
  12. * @returns boolean, which indicates whether the node should be exported (true) or not (false)
  13. */
  14. shouldExportNode?(node: Node): boolean;
  15. /**
  16. * Function used to extract the part of node's metadata that will be exported into glTF node extras
  17. * @param metadata source metadata to read from
  18. * @returns the data to store to glTF node extras
  19. */
  20. metadataSelector?(metadata: any): any;
  21. /**
  22. * The sample rate to bake animation curves
  23. */
  24. animationSampleRate?: number;
  25. /**
  26. * Begin serialization without waiting for the scene to be ready
  27. */
  28. exportWithoutWaitingForScene?: boolean;
  29. /**
  30. * Indicates if coordinate system swapping root nodes should be included in export
  31. */
  32. includeCoordinateSystemConversionNodes?: boolean;
  33. }
  34. /**
  35. * Class for generating glTF data from a Babylon scene.
  36. */
  37. export class GLTF2Export {
  38. /**
  39. * Exports the geometry of the scene to .gltf file format asynchronously
  40. * @param scene Babylon scene with scene hierarchy information
  41. * @param filePrefix File prefix to use when generating the glTF file
  42. * @param options Exporter options
  43. * @returns Returns an object with a .gltf file and associates texture names
  44. * as keys and their data and paths as values
  45. */
  46. public static GLTFAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData> {
  47. return scene.whenReadyAsync().then(() => {
  48. const glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  49. const gltfGenerator = new _Exporter(scene, options);
  50. return gltfGenerator._generateGLTFAsync(glTFPrefix);
  51. });
  52. }
  53. private static _PreExportAsync(scene: Scene, options?: IExportOptions): Promise<void> {
  54. return Promise.resolve().then(() => {
  55. if (options && options.exportWithoutWaitingForScene) {
  56. return Promise.resolve();
  57. }
  58. else {
  59. return scene.whenReadyAsync();
  60. }
  61. });
  62. }
  63. private static _PostExportAsync(scene: Scene, glTFData: GLTFData, options?: IExportOptions): Promise<GLTFData> {
  64. return Promise.resolve().then(() => {
  65. if (options && options.exportWithoutWaitingForScene) {
  66. return glTFData;
  67. }
  68. else {
  69. return glTFData;
  70. }
  71. });
  72. }
  73. /**
  74. * Exports the geometry of the scene to .glb file format asychronously
  75. * @param scene Babylon scene with scene hierarchy information
  76. * @param filePrefix File prefix to use when generating glb file
  77. * @param options Exporter options
  78. * @returns Returns an object with a .glb filename as key and data as value
  79. */
  80. public static GLBAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData> {
  81. return this._PreExportAsync(scene, options).then(() => {
  82. const glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  83. const gltfGenerator = new _Exporter(scene, options);
  84. return gltfGenerator._generateGLBAsync(glTFPrefix).then((glTFData) => {
  85. return this._PostExportAsync(scene, glTFData, options);
  86. });
  87. });
  88. }
  89. }