Browse Source

Make UI blocking functions asychronous

Gary Hsu 8 years ago
parent
commit
a6ce5d7854

+ 20 - 13
loaders/glTF/babylon.glTFBinaryExtension.ts

@@ -33,20 +33,23 @@
                 return false;
             }
 
-            this._binary = this._parseBinary(<ArrayBuffer>data);
-            if (!this._binary) {
-                onError();
-                return true;
-            }
+            setTimeout(() => {
+                this._binary = this._parseBinary(<ArrayBuffer>data);
+                if (!this._binary) {
+                    onError();
+                    return true;
+                }
 
-            var gltfRuntime = GLTFFileLoaderBase.CreateRuntime(this._binary.content, scene, rootUrl);
+                var gltfRuntime = GLTFFileLoaderBase.CreateRuntime(this._binary.content, scene, rootUrl);
 
-            if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
-                Tools.Warn("glTF binary file does not have " + this.name + " specified in extensionsUsed");
-                gltfRuntime.extensionsUsed.push(this.name);
-            }
+                if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
+                    Tools.Warn("glTF binary file does not have " + this.name + " specified in extensionsUsed");
+                    gltfRuntime.extensionsUsed.push(this.name);
+                }
+
+                onSuccess(gltfRuntime);
+            });
 
-            onSuccess(gltfRuntime);
             return true;
         }
 
@@ -86,8 +89,12 @@
             var binaryExtensionShader: IGLTFBinaryExtensionShader = shader.extensions[this.name];
             var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
             var shaderBytes = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
-            var shaderString = GLTFUtils.DecodeBufferToText(shaderBytes);
-            onSuccess(shaderString);
+
+            setTimeout(() => {
+                var shaderString = GLTFUtils.DecodeBufferToText(shaderBytes);
+                onSuccess(shaderString);
+            });
+
             return true;
         }
 

+ 23 - 16
loaders/glTF/babylon.glTFFileLoader.js

@@ -1133,7 +1133,7 @@ var BABYLON;
         GLTFFileLoaderBase.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
             var buffer = gltfRuntime.buffers[id];
             if (BABYLON.GLTFUtils.IsBase64(buffer.uri)) {
-                onSuccess(new Uint8Array(BABYLON.GLTFUtils.DecodeBase64(buffer.uri)));
+                setTimeout(function () { return onSuccess(new Uint8Array(BABYLON.GLTFUtils.DecodeBase64(buffer.uri))); });
             }
             else {
                 BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, function (data) { return onSuccess(new Uint8Array(data)); }, null, null, true, onError);
@@ -1151,7 +1151,7 @@ var BABYLON;
             }
             var source = gltfRuntime.images[texture.source];
             if (BABYLON.GLTFUtils.IsBase64(source.uri)) {
-                onSuccess(new Uint8Array(BABYLON.GLTFUtils.DecodeBase64(source.uri)));
+                setTimeout(onSuccess(new Uint8Array(BABYLON.GLTFUtils.DecodeBase64(source.uri))));
             }
             else {
                 BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + source.uri, function (data) { return onSuccess(new Uint8Array(data)); }, null, null, true, onError);
@@ -1758,7 +1758,9 @@ var BABYLON;
             GLTFFileLoaderExtension.ApplyExtensions(function (loaderExtension) {
                 return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
             }, function () {
-                onSuccess(BABYLON.GLTFFileLoaderBase.CreateRuntime(JSON.parse(data), scene, rootUrl));
+                setTimeout(function () {
+                    onSuccess(BABYLON.GLTFFileLoaderBase.CreateRuntime(JSON.parse(data), scene, rootUrl));
+                });
             });
         };
         GLTFFileLoaderExtension.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
@@ -1835,20 +1837,23 @@ var BABYLON;
             _super.call(this, "KHR_binary_glTF");
         }
         GLTFBinaryExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
+            var _this = this;
             if (!(data instanceof ArrayBuffer)) {
                 return false;
             }
-            this._binary = this._parseBinary(data);
-            if (!this._binary) {
-                onError();
-                return true;
-            }
-            var gltfRuntime = BABYLON.GLTFFileLoaderBase.CreateRuntime(this._binary.content, scene, rootUrl);
-            if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
-                BABYLON.Tools.Warn("glTF binary file does not have " + this.name + " specified in extensionsUsed");
-                gltfRuntime.extensionsUsed.push(this.name);
-            }
-            onSuccess(gltfRuntime);
+            setTimeout(function () {
+                _this._binary = _this._parseBinary(data);
+                if (!_this._binary) {
+                    onError();
+                    return true;
+                }
+                var gltfRuntime = BABYLON.GLTFFileLoaderBase.CreateRuntime(_this._binary.content, scene, rootUrl);
+                if (gltfRuntime.extensionsUsed.indexOf(_this.name) === -1) {
+                    BABYLON.Tools.Warn("glTF binary file does not have " + _this.name + " specified in extensionsUsed");
+                    gltfRuntime.extensionsUsed.push(_this.name);
+                }
+                onSuccess(gltfRuntime);
+            });
             return true;
         };
         GLTFBinaryExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
@@ -1881,8 +1886,10 @@ var BABYLON;
             var binaryExtensionShader = shader.extensions[this.name];
             var bufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
             var shaderBytes = BABYLON.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, BABYLON.EComponentType.UNSIGNED_BYTE);
-            var shaderString = BABYLON.GLTFUtils.DecodeBufferToText(shaderBytes);
-            onSuccess(shaderString);
+            setTimeout(function () {
+                var shaderString = BABYLON.GLTFUtils.DecodeBufferToText(shaderBytes);
+                onSuccess(shaderString);
+            });
             return true;
         };
         // Parses a glTF binary array buffer into its content and body

+ 2 - 2
loaders/glTF/babylon.glTFFileLoader.ts

@@ -1274,7 +1274,7 @@
             var buffer: IGLTFBuffer = gltfRuntime.buffers[id];
 
             if (GLTFUtils.IsBase64(buffer.uri)) {
-                onSuccess(new Uint8Array(GLTFUtils.DecodeBase64(buffer.uri)));
+                setTimeout(() => onSuccess(new Uint8Array(GLTFUtils.DecodeBase64(buffer.uri))));
             }
             else {
                 Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, data => onSuccess(new Uint8Array(data)), null, null, true, onError);
@@ -1297,7 +1297,7 @@
             var source: IGLTFImage = gltfRuntime.images[texture.source];
 
             if (GLTFUtils.IsBase64(source.uri)) {
-                onSuccess(new Uint8Array(GLTFUtils.DecodeBase64(source.uri)));
+                setTimeout(onSuccess(new Uint8Array(GLTFUtils.DecodeBase64(source.uri))));
             }
             else {
                 Tools.LoadFile(gltfRuntime.rootUrl + source.uri, data => onSuccess(new Uint8Array(data)), null, null, true, onError);

+ 3 - 1
loaders/glTF/babylon.glTFFileLoaderExtension.ts

@@ -66,7 +66,9 @@
             GLTFFileLoaderExtension.ApplyExtensions<IGLTFRuntime>(loaderExtension => {
                 return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
             }, () => {
-                onSuccess(GLTFFileLoaderBase.CreateRuntime(JSON.parse(<string>data), scene, rootUrl));
+                setTimeout(() => {
+                    onSuccess(GLTFFileLoaderBase.CreateRuntime(JSON.parse(<string>data), scene, rootUrl));
+                });
             });
         }