babylon.glTFFileLoaderExtension.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: (buffer: ArrayBufferView) => void, onError: () => void): boolean {
  22. return false;
  23. }
  24. /**
  25. * Defines an override for loading texture buffers
  26. * Return true to stop further extensions from loading this texture data
  27. */
  28. public loadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): boolean {
  29. return false;
  30. }
  31. /**
  32. * Defines an override for creating textures
  33. * Return true to stop further extensions from loading this texture
  34. */
  35. public createTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): boolean {
  36. return false;
  37. }
  38. /**
  39. * Defines an override for loading shader strings
  40. * Return true to stop further extensions from loading this shader data
  41. */
  42. public loadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderString: string) => void, onError: () => void): boolean {
  43. return false;
  44. }
  45. /**
  46. * Defines an override for loading materials
  47. * Return true to stop further extensions from loading this material
  48. */
  49. public loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): boolean {
  50. return false;
  51. }
  52. // ---------
  53. // Utilities
  54. // ---------
  55. public static LoadRuntimeAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): void {
  56. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  57. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  58. }, () => {
  59. setTimeout(() => {
  60. onSuccess(GLTFFileLoaderBase.CreateRuntime(JSON.parse(<string>data), scene, rootUrl));
  61. });
  62. });
  63. }
  64. public static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void): void {
  65. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  66. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  67. }, () => {
  68. GLTFFileLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError);
  69. });
  70. }
  71. public static LoadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void {
  72. GLTFFileLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id,
  73. buffer => GLTFFileLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError),
  74. onError);
  75. }
  76. public static LoadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void {
  77. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  78. return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  79. }, () => {
  80. GLTFFileLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  81. });
  82. }
  83. public static LoadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
  84. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  85. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  86. }, () => {
  87. GLTFFileLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  88. });
  89. }
  90. private static LoadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): void {
  91. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  92. return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  93. }, () => {
  94. GLTFFileLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  95. });
  96. }
  97. private static CreateTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): void {
  98. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  99. return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  100. }, () => {
  101. GLTFFileLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  102. });
  103. }
  104. private static ApplyExtensions(func: (loaderExtension: GLTFFileLoaderExtension) => boolean, defaultFunc: () => void): void {
  105. for (var extensionName in GLTFFileLoader.Extensions) {
  106. var loaderExtension = GLTFFileLoader.Extensions[extensionName];
  107. if (func(loaderExtension)) {
  108. return;
  109. }
  110. }
  111. defaultFunc();
  112. }
  113. }
  114. }