babylon.glTFSerializer.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. /**
  4. * Holds a collection of exporter options and parameters
  5. */
  6. export interface IExportOptions {
  7. /**
  8. * Function which indicates whether a babylon mesh should be exported or not
  9. * @param transformNode source Babylon transform node. It is used to check whether it should be exported to glTF or not
  10. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  11. */
  12. shouldExportTransformNode?(transformNode: TransformNode): boolean;
  13. /**
  14. * The sample rate to bake animation curves
  15. */
  16. animationSampleRate?: number;
  17. /**
  18. * Begin serialization without waiting for the scene to be ready
  19. */
  20. exportWithoutWaitingForScene?: boolean;
  21. }
  22. /**
  23. * Class for generating glTF data from a Babylon scene.
  24. */
  25. export class GLTF2Export {
  26. /**
  27. * Exports the geometry of the scene to .gltf file format asynchronously
  28. * @param scene Babylon scene with scene hierarchy information
  29. * @param filePrefix File prefix to use when generating the glTF file
  30. * @param options Exporter options
  31. * @returns Returns an object with a .gltf file and associates texture names
  32. * as keys and their data and paths as values
  33. */
  34. public static GLTFAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData> {
  35. return scene.whenReadyAsync().then(() => {
  36. const glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  37. const gltfGenerator = new GLTF2.Exporter._Exporter(scene, options);
  38. return gltfGenerator._generateGLTFAsync(glTFPrefix);
  39. });
  40. }
  41. private static _PreExportAsync(scene: Scene, options?: IExportOptions): Promise<void> {
  42. return Promise.resolve().then(() => {
  43. if (options && options.exportWithoutWaitingForScene) {
  44. return Promise.resolve();
  45. }
  46. else {
  47. return scene.whenReadyAsync();
  48. }
  49. });
  50. }
  51. private static _PostExportAsync(scene: Scene, glTFData: GLTFData, options?: IExportOptions): Promise<GLTFData> {
  52. return Promise.resolve().then(() => {
  53. if (options && options.exportWithoutWaitingForScene) {
  54. return glTFData;
  55. }
  56. else {
  57. return glTFData;
  58. }
  59. });
  60. }
  61. /**
  62. * Exports the geometry of the scene to .glb file format asychronously
  63. * @param scene Babylon scene with scene hierarchy information
  64. * @param filePrefix File prefix to use when generating glb file
  65. * @param options Exporter options
  66. * @returns Returns an object with a .glb filename as key and data as value
  67. */
  68. public static GLBAsync(scene: Scene, filePrefix: string, options?: IExportOptions): Promise<GLTFData> {
  69. return this._PreExportAsync(scene, options).then(() => {
  70. const glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  71. const gltfGenerator = new GLTF2.Exporter._Exporter(scene, options);
  72. return gltfGenerator._generateGLBAsync(glTFPrefix).then((glTFData) => {
  73. return this._PostExportAsync(scene, glTFData, options);
  74. });
  75. });
  76. }
  77. }
  78. }