babylon.glTFFileLoaderExtension.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. export abstract class GLTFFileLoaderExtension {
  4. private _name: string;
  5. public constructor(name: string) {
  6. this._name = name;
  7. }
  8. public get name(): string {
  9. return this._name;
  10. }
  11. /**
  12. * Defines an override for loading the runtime
  13. * Return true to stop further extensions from loading the runtime
  14. */
  15. public loadRuntimeAsync(scene: Scene, data: string, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): boolean {
  16. return false;
  17. }
  18. /**
  19. * Defines an onverride for creating gltf runtime
  20. * Return true to stop further extensions from creating the runtime
  21. */
  22. public loadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): boolean {
  23. return false;
  24. }
  25. /**
  26. * Defines an override for loading buffers
  27. * Return true to stop further extensions from loading this buffer
  28. */
  29. public loadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void, onProgress?: () => void): boolean {
  30. return false;
  31. }
  32. /**
  33. * Defines an override for loading texture buffers
  34. * Return true to stop further extensions from loading this texture data
  35. */
  36. public loadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): boolean {
  37. return false;
  38. }
  39. /**
  40. * Defines an override for creating textures
  41. * Return true to stop further extensions from loading this texture
  42. */
  43. public createTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): boolean {
  44. return false;
  45. }
  46. /**
  47. * Defines an override for loading shader strings
  48. * Return true to stop further extensions from loading this shader data
  49. */
  50. public loadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderString: string) => void, onError: () => void): boolean {
  51. return false;
  52. }
  53. /**
  54. * Defines an override for loading materials
  55. * Return true to stop further extensions from loading this material
  56. */
  57. public loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): boolean {
  58. return false;
  59. }
  60. // ---------
  61. // Utilities
  62. // ---------
  63. public static LoadRuntimeAsync(scene: Scene, data: string, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): void {
  64. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  65. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  66. }, () => {
  67. setTimeout(() => {
  68. onSuccess(GLTFFileLoaderBase.CreateRuntime(JSON.parse(data), scene, rootUrl));
  69. });
  70. });
  71. }
  72. public static LoadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): void {
  73. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  74. return loaderExtension.loadRuntimeExtensionsAsync(gltfRuntime, onSuccess, onError);
  75. }, () => {
  76. setTimeout(() => {
  77. onSuccess();
  78. });
  79. });
  80. }
  81. public static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void, onProgress?: () => void): void {
  82. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  83. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  84. }, () => {
  85. GLTFFileLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  86. });
  87. }
  88. public static LoadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void {
  89. GLTFFileLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id,
  90. buffer => GLTFFileLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError),
  91. onError);
  92. }
  93. public static LoadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void {
  94. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  95. return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  96. }, () => {
  97. GLTFFileLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  98. });
  99. }
  100. public static LoadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
  101. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  102. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  103. }, () => {
  104. GLTFFileLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  105. });
  106. }
  107. private static LoadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): void {
  108. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  109. return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  110. }, () => {
  111. GLTFFileLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  112. });
  113. }
  114. private static CreateTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): void {
  115. GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
  116. return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  117. }, () => {
  118. GLTFFileLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  119. });
  120. }
  121. private static ApplyExtensions(func: (loaderExtension: GLTFFileLoaderExtension) => boolean, defaultFunc: () => void): void {
  122. for (var extensionName in GLTFFileLoader.Extensions) {
  123. var loaderExtension = GLTFFileLoader.Extensions[extensionName];
  124. if (func(loaderExtension)) {
  125. return;
  126. }
  127. }
  128. defaultFunc();
  129. }
  130. }
  131. }