babylon.glTFFileLoaderExtension.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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<IGLTFRuntime>(loaderExtension => {
  57. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  58. }, () => {
  59. onSuccess(GLTFFileLoaderBase.CreateRuntime(JSON.parse(<string>data), scene, rootUrl));
  60. });
  61. }
  62. public static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void): void {
  63. GLTFFileLoaderExtension.ApplyExtensions<Texture>(loaderExtension => {
  64. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  65. }, () => {
  66. GLTFFileLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError);
  67. });
  68. }
  69. public static LoadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void {
  70. GLTFFileLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id,
  71. buffer => GLTFFileLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError),
  72. onError);
  73. }
  74. public static LoadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void {
  75. GLTFFileLoaderExtension.ApplyExtensions<Texture>(loaderExtension => {
  76. return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  77. }, () => {
  78. GLTFFileLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  79. });
  80. }
  81. public static LoadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
  82. GLTFFileLoaderExtension.ApplyExtensions<Texture>(loaderExtension => {
  83. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  84. }, () => {
  85. GLTFFileLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  86. });
  87. }
  88. private static LoadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): void {
  89. GLTFFileLoaderExtension.ApplyExtensions<Texture>(loaderExtension => {
  90. return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  91. }, () => {
  92. GLTFFileLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  93. });
  94. }
  95. private static CreateTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): void {
  96. GLTFFileLoaderExtension.ApplyExtensions<Texture>(loaderExtension => {
  97. return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  98. }, () => {
  99. GLTFFileLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  100. });
  101. }
  102. private static ApplyExtensions<ObjectT extends Object>(func: (loaderExtension: GLTFFileLoaderExtension) => boolean, defaultFunc: () => void): void {
  103. for (var extensionName in GLTFFileLoader.Extensions) {
  104. var loaderExtension = GLTFFileLoader.Extensions[extensionName];
  105. if (func(loaderExtension)) {
  106. return;
  107. }
  108. }
  109. defaultFunc();
  110. }
  111. }
  112. }