babylon.glTFSerializer.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 IExporterOptions {
  7. /**
  8. * Function which indicates whether a babylon mesh should be exported or not.
  9. * @param mesh - source Babylon mesh. It is used to check whether it should be
  10. * exported to glTF or not.
  11. * @returns boolean, which indicates whether the mesh should be exported (true) or not (false)
  12. */
  13. shouldExportMesh?(mesh: AbstractMesh): boolean;
  14. };
  15. /**
  16. * Class for generating glTF data from a Babylon scene.
  17. */
  18. export class GLTF2Export {
  19. /**
  20. * Exports the geometry of the scene to .gltf file format.
  21. * @param scene - Babylon scene with scene hierarchy information.
  22. * @param filePrefix - File prefix to use when generating the glTF file.
  23. * @param options - Exporter options.
  24. * @returns - Returns an object with a .gltf file and associates texture names
  25. * as keys and their data and paths as values.
  26. */
  27. public static GLTF(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData {
  28. const glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  29. const gltfGenerator = new GLTF2._Exporter(scene, options);
  30. if (scene.isReady) {
  31. return gltfGenerator._generateGLTF(glTFPrefix);
  32. }
  33. else {
  34. throw new Error("glTF Serializer: Scene is not ready!");
  35. }
  36. }
  37. /**
  38. * Exports the geometry of the scene to .glb file format.
  39. * @param scene - Babylon scene with scene hierarchy information.
  40. * @param filePrefix - File prefix to use when generating glb file.
  41. * @param options - Exporter options.
  42. * @returns - Returns an object with a .glb filename as key and data as value
  43. */
  44. public static GLB(scene: Scene, filePrefix: string, options?: IExporterOptions): _GLTFData {
  45. const glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  46. const gltfGenerator = new GLTF2._Exporter(scene, options);
  47. if (scene.isReady) {
  48. return gltfGenerator._generateGLB(glTFPrefix);
  49. }
  50. else {
  51. throw new Error("glTF Serializer: Scene is not ready!");
  52. }
  53. }
  54. }
  55. }