123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /// <reference path="../../../dist/preview release/babylon.d.ts"/>
- module BABYLON {
- export abstract class GLTFFileLoaderExtension {
- private _name: string;
- public constructor(name: string) {
- this._name = name;
- }
- public get name(): string {
- return this._name;
- }
- /**
- * Defines an override for loading the runtime
- * Return true to stop further extensions from loading the runtime
- */
- public loadRuntimeAsync(scene: Scene, data: string, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): boolean {
- return false;
- }
- /**
- * Defines an onverride for creating gltf runtime
- * Return true to stop further extensions from creating the runtime
- */
- public loadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): boolean {
- return false;
- }
- /**
- * Defines an override for loading buffers
- * Return true to stop further extensions from loading this buffer
- */
- public loadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void, onProgress?: () => void): boolean {
- return false;
- }
- /**
- * Defines an override for loading texture buffers
- * Return true to stop further extensions from loading this texture data
- */
- public loadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): boolean {
- return false;
- }
- /**
- * Defines an override for creating textures
- * Return true to stop further extensions from loading this texture
- */
- public createTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): boolean {
- return false;
- }
- /**
- * Defines an override for loading shader strings
- * Return true to stop further extensions from loading this shader data
- */
- public loadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderString: string) => void, onError: () => void): boolean {
- return false;
- }
- /**
- * Defines an override for loading materials
- * Return true to stop further extensions from loading this material
- */
- public loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): boolean {
- return false;
- }
- // ---------
- // Utilities
- // ---------
- public static LoadRuntimeAsync(scene: Scene, data: string, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
- }, () => {
- setTimeout(() => {
- onSuccess(GLTFFileLoaderBase.CreateRuntime(JSON.parse(data), scene, rootUrl));
- });
- });
- }
- public static LoadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.loadRuntimeExtensionsAsync(gltfRuntime, onSuccess, onError);
- }, () => {
- setTimeout(() => {
- onSuccess();
- });
- });
- }
- public static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void, onProgress?: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
- }, () => {
- GLTFFileLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
- });
- }
- public static LoadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void {
- GLTFFileLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id,
- buffer => GLTFFileLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError),
- onError);
- }
- public static LoadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
- }, () => {
- GLTFFileLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
- });
- }
- public static LoadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
- }, () => {
- GLTFFileLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
- });
- }
- private static LoadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
- }, () => {
- GLTFFileLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
- });
- }
- private static CreateTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): void {
- GLTFFileLoaderExtension.ApplyExtensions(loaderExtension => {
- return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
- }, () => {
- GLTFFileLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
- });
- }
- private static ApplyExtensions(func: (loaderExtension: GLTFFileLoaderExtension) => boolean, defaultFunc: () => void): void {
- for (var extensionName in GLTFFileLoader.Extensions) {
- var loaderExtension = GLTFFileLoader.Extensions[extensionName];
- if (func(loaderExtension)) {
- return;
- }
- }
- defaultFunc();
- }
- }
- }
|