Quellcode durchsuchen

Loader And Tools Update

MackeyK24 vor 6 Jahren
Ursprung
Commit
1dad00eae1
2 geänderte Dateien mit 75 neuen und 11 gelöschten Zeilen
  1. 25 8
      src/Loading/Plugins/babylonFileLoader.ts
  2. 50 3
      src/Misc/tools.ts

+ 25 - 8
src/Loading/Plugins/babylonFileLoader.ts

@@ -28,6 +28,7 @@ import { OimoJSPlugin } from "../../Physics/Plugins/oimoJSPlugin";
 import { AmmoJSPlugin } from "../../Physics/Plugins/ammoJSPlugin";
 import { AmmoJSPlugin } from "../../Physics/Plugins/ammoJSPlugin";
 import { ReflectionProbe } from "../../Probes/reflectionProbe";
 import { ReflectionProbe } from "../../Probes/reflectionProbe";
 import { _TypeStore } from '../../Misc/typeStore';
 import { _TypeStore } from '../../Misc/typeStore';
+import { Tools } from '../../Misc/tools';
 
 
 /** @hidden */
 /** @hidden */
 export var _BabylonLoaderRegistered = true;
 export var _BabylonLoaderRegistered = true;
@@ -88,11 +89,19 @@ var loadAssetContainer = (scene: Scene, data: string, rootUrl: string, onError?:
                 }
                 }
                 scene.environmentTexture = hdrTexture;
                 scene.environmentTexture = hdrTexture;
             } else {
             } else {
-                var cubeTexture = CubeTexture.CreateFromPrefilteredData((parsedData.environmentTexture.match(/https?:\/\//g) ? "" : rootUrl) + parsedData.environmentTexture, scene);
-                if (parsedData.environmentTextureRotationY) {
-                    cubeTexture.rotationY = parsedData.environmentTextureRotationY;
+                if (Tools.EndsWith(parsedData.environmentTexture, ".env")) {
+                    var compressedTexture = new CubeTexture((parsedData.environmentTexture.match(/https?:\/\//g) ? "" : rootUrl) + parsedData.environmentTexture, scene);
+                    if (parsedData.environmentTextureRotationY) {
+                        compressedTexture.rotationY = parsedData.environmentTextureRotationY;
+                    }
+                    scene.environmentTexture = compressedTexture;
+                } else {
+                    var cubeTexture = CubeTexture.CreateFromPrefilteredData((parsedData.environmentTexture.match(/https?:\/\//g) ? "" : rootUrl) + parsedData.environmentTexture, scene);
+                    if (parsedData.environmentTextureRotationY) {
+                        cubeTexture.rotationY = parsedData.environmentTextureRotationY;
+                    }
+                    scene.environmentTexture = cubeTexture;
                 }
                 }
-                scene.environmentTexture = cubeTexture;
             }
             }
             if (parsedData.createDefaultSkybox === true) {
             if (parsedData.createDefaultSkybox === true) {
                 var skyboxScale = (scene.activeCamera !== undefined && scene.activeCamera !== null) ? (scene.activeCamera.maxZ - scene.activeCamera.minZ) / 2 : 1000;
                 var skyboxScale = (scene.activeCamera !== undefined && scene.activeCamera !== null) ? (scene.activeCamera.maxZ - scene.activeCamera.minZ) / 2 : 1000;
@@ -636,11 +645,19 @@ SceneLoader.RegisterPlugin({
                     }
                     }
                     scene.environmentTexture = hdrTexture;
                     scene.environmentTexture = hdrTexture;
                 } else {
                 } else {
-                    var cubeTexture = CubeTexture.CreateFromPrefilteredData(rootUrl + parsedData.environmentTexture, scene);
-                    if (parsedData.environmentTextureRotationY) {
-                        cubeTexture.rotationY = parsedData.environmentTextureRotationY;
+                    if (Tools.EndsWith(parsedData.environmentTexture, ".env")) {
+                        var compressedTexture = new CubeTexture(rootUrl + parsedData.environmentTexture, scene);
+                        if (parsedData.environmentTextureRotationY) {
+                            compressedTexture.rotationY = parsedData.environmentTextureRotationY;
+                        }
+                        scene.environmentTexture = compressedTexture;
+                    } else {
+                        var cubeTexture = CubeTexture.CreateFromPrefilteredData(rootUrl + parsedData.environmentTexture, scene);
+                        if (parsedData.environmentTextureRotationY) {
+                            cubeTexture.rotationY = parsedData.environmentTextureRotationY;
+                        }
+                        scene.environmentTexture = cubeTexture;
                     }
                     }
-                    scene.environmentTexture = cubeTexture;
                 }
                 }
                 if (parsedData.createDefaultSkybox === true) {
                 if (parsedData.createDefaultSkybox === true) {
                     var skyboxScale = (scene.activeCamera !== undefined && scene.activeCamera !== null) ? (scene.activeCamera.maxZ - scene.activeCamera.minZ) / 2 : 1000;
                     var skyboxScale = (scene.activeCamera !== undefined && scene.activeCamera !== null) ? (scene.activeCamera.maxZ - scene.activeCamera.minZ) / 2 : 1000;

+ 50 - 3
src/Misc/tools.ts

@@ -995,15 +995,19 @@ export class Tools {
      * @param scriptUrl defines the url of the script to laod
      * @param scriptUrl defines the url of the script to laod
      * @param onSuccess defines the callback called when the script is loaded
      * @param onSuccess defines the callback called when the script is loaded
      * @param onError defines the callback to call if an error occurs
      * @param onError defines the callback to call if an error occurs
+     * @param scriptId defines the id of the script element
      */
      */
-    public static LoadScript(scriptUrl: string, onSuccess: () => void, onError?: (message?: string, exception?: any) => void) {
+    public static LoadScript(scriptUrl: string, onSuccess: () => void, onError?: (message?: string, exception?: any) => void, scriptId?: string) {
         if (!DomManagement.IsWindowObjectExist()) {
         if (!DomManagement.IsWindowObjectExist()) {
             return;
             return;
         }
         }
         var head = document.getElementsByTagName('head')[0];
         var head = document.getElementsByTagName('head')[0];
         var script = document.createElement('script');
         var script = document.createElement('script');
-        script.type = 'text/javascript';
-        script.src = scriptUrl;
+        script.setAttribute('type', 'text/javascript');
+        script.setAttribute('src', scriptUrl);
+        if (scriptId) {
+            script.id = scriptId;
+        }
 
 
         script.onload = () => {
         script.onload = () => {
             if (onSuccess) {
             if (onSuccess) {
@@ -1021,6 +1025,39 @@ export class Tools {
     }
     }
 
 
     /**
     /**
+     * Load an asynchronous script (identified by an url). When the url returns, the
+     * content of this file is added into a new script element, attached to the DOM (body element)
+     * @param scriptUrl defines the url of the script to laod
+     * @param scriptId defines the id of the script element
+     * @returns a promise request object
+     */
+    public static LoadScriptAsync(scriptUrl: string, scriptId?: string): Nullable<Promise<boolean>> {
+        return new Promise<boolean>((resolve, reject) => {
+            if (!DomManagement.IsWindowObjectExist()) {
+                resolve(false);
+                return;
+            }
+            var head = document.getElementsByTagName('head')[0];
+            var script = document.createElement('script');
+            script.setAttribute('type', 'text/javascript');
+            script.setAttribute('src', scriptUrl);
+            if (scriptId) {
+                script.id = scriptId;
+            }
+
+            script.onload = () => {
+                resolve(true);
+            };
+
+            script.onerror = (e) => {
+                resolve(false);
+            };
+
+            head.appendChild(script);
+        });
+    }
+
+    /**
      * Loads a file from a blob
      * Loads a file from a blob
      * @param fileToLoad defines the blob to use
      * @param fileToLoad defines the blob to use
      * @param callback defines the callback to call when data is loaded
      * @param callback defines the callback to call when data is loaded
@@ -1148,6 +1185,16 @@ export class Tools {
     }
     }
 
 
     /**
     /**
+     * Checks for a matching suffix at the end of a string (for ES5 and lower)
+     * @param str Source string
+     * @param suffix Suffix to search for in the source string
+     * @returns Boolean indicating whether the suffix was found (true) or not (false)
+     */
+    public static EndsWith(str: string, suffix: string): boolean {
+        return str.indexOf(suffix, str.length - suffix.length) !== -1;
+    }
+
+    /**
      * Function used to register events at window level
      * Function used to register events at window level
      * @param events defines the events to register
      * @param events defines the events to register
      */
      */