Kaynağa Gözat

load script from code instead of from html

Trevor Baron 6 yıl önce
ebeveyn
işleme
7de87f36d0

+ 0 - 1
Playground/index-local.html

@@ -22,7 +22,6 @@
     <script src="../dist/preview%20release/Oimo.js"></script>
     <script src="../dist/preview%20release/gltf_validator.js"></script>
     <script src="../dist/preview%20release/earcut.min.js"></script>
-    <script src="../dist/preview%20release/basisTranscoder/basis_transcoder.js"></script>
     <!-- Monaco -->
 
     <!-- Babylon.js -->

+ 23 - 22
src/Materials/Textures/Loaders/basisTextureLoader.ts

@@ -69,31 +69,32 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
     public loadData(data: ArrayBuffer, texture: InternalTexture,
         callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
         // Verify Basis Module is loaded and detect file info and format
-        BasisTools.VerifyBasisModule();
-        var loadedFile = BasisTools.LoadBasisFile(data);
-        var fileInfo = BasisTools.GetFileInfo(loadedFile);
-        var format = BasisTools.GetSupportedTranscodeFormat(texture.getEngine(), fileInfo);
+        BasisTools.VerifyBasisModuleAsync().then(()=>{
+            var loadedFile = BasisTools.LoadBasisFile(data);
+            var fileInfo = BasisTools.GetFileInfo(loadedFile);
+            var format = BasisTools.GetSupportedTranscodeFormat(texture.getEngine(), fileInfo);
 
-        // TODO this should be done in web worker
-        var transcodeResult = BasisTools.TranscodeFile(format, fileInfo, loadedFile);
+            // TODO this should be done in web worker
+            var transcodeResult = BasisTools.TranscodeFile(format, fileInfo, loadedFile);
 
-        // Upload data to texture
-        callback(fileInfo.width, fileInfo.height, false, true, () => {
-            if (transcodeResult.fallbackToRgb565) {
-                // Load rgb565Data to texture if conversion is needed
-                texture.getEngine()._unpackFlipY(texture.invertY);
-                var gl = texture.getEngine()._gl;
-                gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, fileInfo.alignedWidth, fileInfo.alignedHeight, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, transcodeResult.pixels);
+            // Upload data to texture
+            callback(fileInfo.width, fileInfo.height, false, true, () => {
+                if (transcodeResult.fallbackToRgb565) {
+                    // Load rgb565Data to texture if conversion is needed
+                    texture.getEngine()._unpackFlipY(texture.invertY);
+                    var gl = texture.getEngine()._gl;
+                    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, fileInfo.alignedWidth, fileInfo.alignedHeight, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, transcodeResult.pixels);
 
-                // TODO this is not working because computed power of 2 image size is larger than rgb565Data causing an error, working around this with below code
-                // texture.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
-                // texture.format = Engine.TEXTUREFORMAT_RGB;
-                // texture.getEngine()._uploadDataToTextureDirectly(texture, rgb565Data, 0,0)
-            }else {
-                // compress texture needs to be flipped
-                texture._invertVScale = true;
-                texture.getEngine()._uploadCompressedDataToTextureDirectly(texture, BasisTools.GetInternalFormatFromBasisFormat(format!), fileInfo.width, fileInfo.height, transcodeResult.pixels, 0, 0);
-            }
+                    // TODO this is not working because computed power of 2 image size is larger than rgb565Data causing an error, working around this with below code
+                    // texture.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
+                    // texture.format = Engine.TEXTUREFORMAT_RGB;
+                    // texture.getEngine()._uploadDataToTextureDirectly(texture, rgb565Data, 0,0)
+                }else {
+                    // compress texture needs to be flipped
+                    texture._invertVScale = true;
+                    texture.getEngine()._uploadCompressedDataToTextureDirectly(texture, BasisTools.GetInternalFormatFromBasisFormat(format!), fileInfo.width, fileInfo.height, transcodeResult.pixels, 0, 0);
+                }
+            });
         });
     }
 }

+ 24 - 15
src/Misc/basis.ts

@@ -1,6 +1,6 @@
-import { Logger } from "../Misc/logger";
 import { Nullable } from '../types';
 import { Engine } from '../Engines/engine';
+import { Tools } from './tools';
 
 /**
  * Info about the .basis files
@@ -33,8 +33,10 @@ class BasisFileInfo {
  * See https://github.com/BinomialLLC/basis_universal/tree/master/webgl
  */
 export class BasisTools {
-    private static _Initialized = false;
     private static _IgnoreSupportedFormats = false;
+    private static LoadScriptPromise:any = null;
+    // TODO should load from cdn location as fallback once it exists
+    private static _FallbackURL = "../dist/preview%20release/basisTranscoder/basis_transcoder.js";
     private static _BASIS_FORMAT = {
         cTFETC1: 0,
         cTFBC1: 1,
@@ -54,21 +56,29 @@ export class BasisTools {
     /**
      * Verifies that the BasisModule has been populated and falls back to loading from the web if not availible
      */
-    public static VerifyBasisModule() {
-        if (!BasisTools.BasisModule) {
-            if ((window as any).Module && (window as any).Module.BasisFile) {
-                Logger.Warn("BasisTools.BasisModule not populated, falling back to window.Module");
-                BasisTools.BasisModule = (window as any).Module;
-            }else {
-                // TODO should load from cdn location as fallback
-                throw "Unable to load .basis texture, BasisTools.BasisModule should be populated";
-            }
+    public static VerifyBasisModuleAsync() {
+        // Complete if module has been populated
+        if(BasisTools.BasisModule){
+            return Promise.resolve();
         }
 
-        if (!BasisTools._Initialized && BasisTools.BasisModule) {
-            BasisTools.BasisModule.initializeBasis();
-            BasisTools._Initialized = true;
+        // Otherwise load script from fallback url
+        if(!this.LoadScriptPromise){
+            this.LoadScriptPromise = Tools.LoadScriptAsync(BasisTools._FallbackURL, "basis_transcoder").then((success)=>{
+                return new Promise((res, rej)=>{
+                    if ((window as any).Module) {
+                        (window as any).Module.onRuntimeInitialized = () => {
+                            BasisTools.BasisModule = (window as any).Module;
+                            BasisTools.BasisModule.initializeBasis();
+                            res();
+                        }
+                    }else {
+                        rej("Unable to load .basis texture, BasisTools.BasisModule should be populated");
+                    }
+                })
+            })
         }
+        return this.LoadScriptPromise;
     }
 
     /**
@@ -77,7 +87,6 @@ export class BasisTools {
      * @returns the Basis file
      */
     public static LoadBasisFile(data: ArrayBuffer) {
-        BasisTools.VerifyBasisModule();
         return new BasisTools.BasisModule.BasisFile(new Uint8Array(data));
     }
 

+ 1 - 1
src/Misc/tools.ts

@@ -1031,7 +1031,7 @@ export class Tools {
      * @param scriptId defines the id of the script element
      * @returns a promise request object
      */
-    public static LoadScriptAsync(scriptUrl: string, scriptId?: string): Nullable<Promise<boolean>> {
+    public static LoadScriptAsync(scriptUrl: string, scriptId?: string): Promise<boolean> {
         return new Promise<boolean>((resolve, reject) => {
             if (!DomManagement.IsWindowObjectExist()) {
                 resolve(false);