babylon.glTFFileLoaderExtension.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. module BABYLON {
  2. export abstract class GLTFFileLoaderExtension {
  3. private _name: string;
  4. public constructor(name: string) {
  5. this._name = name;
  6. }
  7. public get name(): string {
  8. return this._name;
  9. }
  10. /**
  11. * Defines an override for loading the runtime
  12. * Return true to stop further extensions from loading the runtime
  13. */
  14. public loadRuntimeAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): boolean {
  15. return false;
  16. }
  17. /**
  18. * Defines an override for loading buffers
  19. * Return true to stop further extensions from loading this buffer
  20. */
  21. public loadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void): boolean {
  22. return false;
  23. }
  24. /**
  25. * Defines an override for loading textures
  26. * Return true to stop further extensions from loading this texture
  27. */
  28. public loadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): boolean {
  29. return false;
  30. }
  31. /**
  32. * Defines an override for loading shader data
  33. * Return true to stop further extensions from loading this shader data
  34. */
  35. public loadShaderDataAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): boolean {
  36. return false;
  37. }
  38. /**
  39. * Defines an override for loading materials
  40. * Return true to stop further extensions from loading this material
  41. */
  42. public loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): boolean {
  43. return false;
  44. }
  45. // ---------
  46. // Utilities
  47. // ---------
  48. public static loadRuntimeAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): void {
  49. GLTFFileLoaderExtension.applyExtensions<IGLTFRuntime>(loaderExtension => {
  50. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  51. }, () => {
  52. onSuccess(GLTFFileLoaderBase.createRuntime(JSON.parse(<string>data), scene, rootUrl));
  53. });
  54. }
  55. public static loadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void): void {
  56. GLTFFileLoaderExtension.applyExtensions<Texture>(loaderExtension => {
  57. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  58. }, () => {
  59. GLTFFileLoaderBase.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  60. });
  61. }
  62. public static loadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void {
  63. GLTFFileLoaderExtension.applyExtensions<Texture>(loaderExtension => {
  64. return loaderExtension.loadTextureAsync(gltfRuntime, id, onSuccess, onError);
  65. }, () => {
  66. GLTFFileLoaderBase.loadTextureAsync(gltfRuntime, id, onSuccess, onError);
  67. });
  68. }
  69. public static loadShaderDataAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void {
  70. GLTFFileLoaderExtension.applyExtensions<Texture>(loaderExtension => {
  71. return loaderExtension.loadShaderDataAsync(gltfRuntime, id, onSuccess, onError);
  72. }, () => {
  73. GLTFFileLoaderBase.loadShaderDataAsync(gltfRuntime, id, onSuccess, onError);
  74. });
  75. }
  76. public static loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
  77. GLTFFileLoaderExtension.applyExtensions<Texture>(loaderExtension => {
  78. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  79. }, () => {
  80. GLTFFileLoaderBase.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  81. });
  82. }
  83. private static applyExtensions<ObjectT extends Object>(func: (loaderExtension: GLTFFileLoaderExtension) => boolean, defaultFunc: () => void): void {
  84. for (var extensionName in GLTFFileLoader.Extensions) {
  85. var loaderExtension = GLTFFileLoader.Extensions[extensionName];
  86. if (func(loaderExtension)) {
  87. return;
  88. }
  89. }
  90. defaultFunc();
  91. }
  92. }
  93. }