瀏覽代碼

Fixing bug with multimats shadows

David Catuhe 11 年之前
父節點
當前提交
c165c5a387

+ 1 - 0
Babylon/Cameras/babylon.gamepadCamera.js

@@ -50,6 +50,7 @@ var BABYLON;
         };
 
         GamepadCamera.prototype.dispose = function () {
+            this._gamepads.dispose();
         };
         return GamepadCamera;
     })(BABYLON.FreeCamera);

+ 1 - 1
Babylon/Cameras/babylon.gamepadCamera.ts

@@ -42,7 +42,7 @@ module BABYLON {
         }
 
         public dispose(): void {
-            
+            this._gamepads.dispose();
         }
     }
 }

+ 9 - 5
Babylon/Lights/Shadows/babylon.shadowGenerator.js

@@ -40,15 +40,16 @@
 
                 var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances !== null);
 
-                if (_this.isReady(mesh, hardwareInstancedRendering)) {
+                if (_this.isReady(subMesh, hardwareInstancedRendering)) {
                     engine.enableEffect(_this._effect);
                     mesh._bind(subMesh, _this._effect, false);
+                    var material = subMesh.getMaterial();
 
                     _this._effect.setMatrix("viewProjection", _this.getTransformMatrix());
 
                     // Alpha test
-                    if (mesh.material && mesh.material.needAlphaTesting()) {
-                        var alphaTexture = mesh.material.getAlphaTestTexture();
+                    if (material && material.needAlphaTesting()) {
+                        var alphaTexture = material.getAlphaTestTexture();
                         _this._effect.setTexture("diffuseSampler", alphaTexture);
                         _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                     }
@@ -152,7 +153,7 @@
             configurable: true
         });
 
-        ShadowGenerator.prototype.isReady = function (mesh, useInstances) {
+        ShadowGenerator.prototype.isReady = function (subMesh, useInstances) {
             var defines = [];
 
             if (this.useVarianceShadowMap) {
@@ -161,8 +162,11 @@
 
             var attribs = [BABYLON.VertexBuffer.PositionKind];
 
+            var mesh = subMesh.getMesh();
+            var material = subMesh.getMaterial();
+
             // Alpha test
-            if (mesh.material && mesh.material.needAlphaTesting()) {
+            if (material && material.needAlphaTesting()) {
                 defines.push("#define ALPHATEST");
                 if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
                     attribs.push(BABYLON.VertexBuffer.UVKind);

+ 9 - 5
Babylon/Lights/Shadows/babylon.shadowGenerator.ts

@@ -79,15 +79,16 @@
 
                 var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances !== null);
 
-                if (this.isReady(mesh, hardwareInstancedRendering)) {
+                if (this.isReady(subMesh, hardwareInstancedRendering)) {
                     engine.enableEffect(this._effect);
                     mesh._bind(subMesh, this._effect, false);
+                    var material = subMesh.getMaterial();
 
                     this._effect.setMatrix("viewProjection", this.getTransformMatrix());
 
                     // Alpha test
-                    if (mesh.material && mesh.material.needAlphaTesting()) {
-                        var alphaTexture = mesh.material.getAlphaTestTexture();
+                    if (material && material.needAlphaTesting()) {
+                        var alphaTexture = material.getAlphaTestTexture();
                         this._effect.setTexture("diffuseSampler", alphaTexture);
                         this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                     }
@@ -146,7 +147,7 @@
 
         }
 
-        public isReady(mesh: Mesh, useInstances: boolean): boolean {
+        public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
             var defines = [];
 
             if (this.useVarianceShadowMap) {
@@ -155,8 +156,11 @@
 
             var attribs = [BABYLON.VertexBuffer.PositionKind];
 
+            var mesh = subMesh.getMesh();
+            var material = subMesh.getMaterial();
+
             // Alpha test
-            if (mesh.material && mesh.material.needAlphaTesting()) {
+            if (material && material.needAlphaTesting()) {
                 defines.push("#define ALPHATEST");
                 if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
                     attribs.push(BABYLON.VertexBuffer.UVKind);

+ 49 - 25
Babylon/Tools/babylon.database.js

@@ -366,21 +366,21 @@ var BABYLON;
             }
         };
 
-        Database.prototype.loadSceneFromDB = function (url, sceneLoaded, progressCallBack, errorCallback) {
+        Database.prototype.loadFileFromDB = function (url, sceneLoaded, progressCallBack, errorCallback, useArrayBuffer) {
             var that = this;
             var completeUrl = BABYLON.Database.ReturnFullUrlLocation(url);
 
-            var saveAndLoadScene = function (event) {
+            var saveAndLoadFile = function (event) {
                 // the scene is not yet in the DB, let's try to save it
-                that._saveSceneIntoDBAsync(completeUrl, sceneLoaded, progressCallBack);
+                that._saveFileIntoDBAsync(completeUrl, sceneLoaded, progressCallBack);
             };
 
             this._checkVersionFromDB(completeUrl, function (version) {
                 if (version !== -1) {
                     if (!that.mustUpdateRessources) {
-                        that._loadSceneFromDBAsync(completeUrl, sceneLoaded, saveAndLoadScene);
+                        that._loadFileFromDBAsync(completeUrl, sceneLoaded, saveAndLoadFile, useArrayBuffer);
                     } else {
-                        that._saveSceneIntoDBAsync(completeUrl, sceneLoaded, progressCallBack);
+                        that._saveFileIntoDBAsync(completeUrl, sceneLoaded, progressCallBack, useArrayBuffer);
                     }
                 } else {
                     errorCallback();
@@ -388,14 +388,21 @@ var BABYLON;
             });
         };
 
-        Database.prototype._loadSceneFromDBAsync = function (url, callback, notInDBCallback) {
+        Database.prototype._loadFileFromDBAsync = function (url, callback, notInDBCallback, useArrayBuffer) {
             if (this.isSupported) {
-                var scene;
-                var transaction = this.db.transaction(["scenes"]);
+                var targetStore;
+                if (url.indexOf(".babylon") !== -1) {
+                    targetStore = "scenes";
+                } else {
+                    targetStore = "textures";
+                }
+
+                var file;
+                var transaction = this.db.transaction([targetStore]);
 
                 transaction.oncomplete = function (event) {
-                    if (scene) {
-                        callback(scene.data);
+                    if (file) {
+                        callback(file.data);
                     } else {
                         notInDBCallback();
                     }
@@ -405,13 +412,13 @@ var BABYLON;
                     notInDBCallback();
                 };
 
-                var getRequest = transaction.objectStore("scenes").get(url);
+                var getRequest = transaction.objectStore(targetStore).get(url);
 
                 getRequest.onsuccess = function (event) {
-                    scene = (event.target).result;
+                    file = (event.target).result;
                 };
                 getRequest.onerror = function (event) {
-                    BABYLON.Tools.Error("Error loading scene " + url + " from DB.");
+                    BABYLON.Tools.Error("Error loading file " + url + " from DB.");
                     notInDBCallback();
                 };
             } else {
@@ -420,24 +427,36 @@ var BABYLON;
             }
         };
 
-        Database.prototype._saveSceneIntoDBAsync = function (url, callback, progressCallback) {
+        Database.prototype._saveFileIntoDBAsync = function (url, callback, progressCallback, useArrayBuffer) {
             if (this.isSupported) {
+                var targetStore;
+                if (url.indexOf(".babylon") !== -1) {
+                    targetStore = "scenes";
+                } else {
+                    targetStore = "textures";
+                }
+
                 // Create XHR
-                var xhr = new XMLHttpRequest(), sceneText;
+                var xhr = new XMLHttpRequest(), fileData;
                 var that = this;
 
                 xhr.open("GET", url, true);
 
+                if (useArrayBuffer) {
+                    xhr.responseType = "arraybuffer";
+                }
+
                 xhr.onprogress = progressCallback;
 
                 xhr.addEventListener("load", function () {
-                    if (xhr.status === 200 || BABYLON.Tools.ValidateXHRData(xhr, 1)) {
+                    if (xhr.status === 200 || BABYLON.Tools.ValidateXHRData(xhr, !useArrayBuffer ? 1 : 6)) {
                         // Blob as response (XHR2)
-                        sceneText = xhr.responseText;
+                        //fileData = xhr.responseText;
+                        fileData = !useArrayBuffer ? xhr.responseText : xhr.response;
 
                         if (!that.hasReachedQuota) {
                             // Open a transaction to the database
-                            var transaction = that.db.transaction(["scenes"], "readwrite");
+                            var transaction = that.db.transaction([targetStore], "readwrite");
 
                             // the transaction could abort because of a QuotaExceededError error
                             transaction.onabort = function (event) {
@@ -447,28 +466,33 @@ var BABYLON;
                                     }
                                 } catch (ex) {
                                 }
-                                callback(sceneText);
+                                callback(fileData);
                             };
 
                             transaction.oncomplete = function (event) {
-                                callback(sceneText);
+                                callback(fileData);
                             };
 
-                            var newScene = { sceneUrl: url, data: sceneText, version: that.manifestVersionFound };
+                            var newFile;
+                            if (targetStore === "scenes") {
+                                newFile = { sceneUrl: url, data: fileData, version: that.manifestVersionFound };
+                            } else {
+                                newFile = { textureUrl: url, data: fileData };
+                            }
 
                             try  {
                                 // Put the scene into the database
-                                var addRequest = transaction.objectStore("scenes").put(newScene);
+                                var addRequest = transaction.objectStore(targetStore).put(newFile);
                                 addRequest.onsuccess = function (event) {
                                 };
                                 addRequest.onerror = function (event) {
-                                    BABYLON.Tools.Error("Error in DB add scene request in BABYLON.Database.");
+                                    BABYLON.Tools.Error("Error in DB add file request in BABYLON.Database.");
                                 };
                             } catch (ex) {
-                                callback(sceneText);
+                                callback(fileData);
                             }
                         } else {
-                            callback(sceneText);
+                            callback(fileData);
                         }
                     } else {
                         callback();

+ 53 - 26
Babylon/Tools/babylon.database.ts

@@ -420,22 +420,22 @@ module BABYLON {
             }
         }
 
-        private loadSceneFromDB(url, sceneLoaded, progressCallBack, errorCallback) {
+        private loadFileFromDB(url: string, sceneLoaded, progressCallBack, errorCallback, useArrayBuffer?: boolean) {
             var that = this;
             var completeUrl = BABYLON.Database.ReturnFullUrlLocation(url);
 
-            var saveAndLoadScene = function (event) {
+            var saveAndLoadFile = function (event) {
                 // the scene is not yet in the DB, let's try to save it
-                that._saveSceneIntoDBAsync(completeUrl, sceneLoaded, progressCallBack);
+                that._saveFileIntoDBAsync(completeUrl, sceneLoaded, progressCallBack);
             };
 
             this._checkVersionFromDB(completeUrl, function (version) {
                 if (version !== -1) {
                     if (!that.mustUpdateRessources) {
-                        that._loadSceneFromDBAsync(completeUrl, sceneLoaded, saveAndLoadScene);
+                        that._loadFileFromDBAsync(completeUrl, sceneLoaded, saveAndLoadFile, useArrayBuffer);
                     }
                     else {
-                        that._saveSceneIntoDBAsync(completeUrl, sceneLoaded, progressCallBack);
+                        that._saveFileIntoDBAsync(completeUrl, sceneLoaded, progressCallBack, useArrayBuffer);
                     }
                 }
                 else {
@@ -444,16 +444,24 @@ module BABYLON {
             });
         }
 
-        private _loadSceneFromDBAsync(url, callback, notInDBCallback) {
+        private _loadFileFromDBAsync(url, callback, notInDBCallback, useArrayBuffer?: boolean) {
             if (this.isSupported) {
-                var scene;
-                var transaction = this.db.transaction(["scenes"]);
+                var targetStore: string;
+                if (url.indexOf(".babylon") !== -1) {
+                    targetStore = "scenes";
+                }
+                else {
+                    targetStore = "textures";
+                }
+
+                var file;
+                var transaction = this.db.transaction([targetStore]);
 
                 transaction.oncomplete = function (event) {
-                    if (scene) {
-                        callback(scene.data);
+                    if (file) {
+                        callback(file.data);
                     }
-                    // scene was not found in DB
+                    // file was not found in DB
                     else {
                         notInDBCallback();
                     }
@@ -463,13 +471,13 @@ module BABYLON {
                     notInDBCallback();
                 };
 
-                var getRequest = transaction.objectStore("scenes").get(url);
+                var getRequest = transaction.objectStore(targetStore).get(url);
 
                 getRequest.onsuccess = function (event) {
-                    scene = (<any>(event.target)).result;
+                    file = (<any>(event.target)).result;
                 };
                 getRequest.onerror = function (event) {
-                    BABYLON.Tools.Error("Error loading scene " + url + " from DB.");
+                    BABYLON.Tools.Error("Error loading file " + url + " from DB.");
                     notInDBCallback();
                 };
             }
@@ -479,24 +487,37 @@ module BABYLON {
             }
         }
 
-        private _saveSceneIntoDBAsync(url: string, callback, progressCallback) {
+        private _saveFileIntoDBAsync(url: string, callback, progressCallback, useArrayBuffer?: boolean) {
             if (this.isSupported) {
+                var targetStore: string;
+                if (url.indexOf(".babylon") !== -1) {
+                    targetStore = "scenes";
+                }
+                else {
+                    targetStore = "textures";
+                }
+
                 // Create XHR
-                var xhr = new XMLHttpRequest(), sceneText;
+                var xhr = new XMLHttpRequest(), fileData;
                 var that = this;
 
                 xhr.open("GET", url, true);
 
+                if (useArrayBuffer) {
+                    xhr.responseType = "arraybuffer";
+                }
+
                 xhr.onprogress = progressCallback;
 
                 xhr.addEventListener("load", function () {
-                    if (xhr.status === 200 || BABYLON.Tools.ValidateXHRData(xhr, 1)) {
+                    if (xhr.status === 200 || BABYLON.Tools.ValidateXHRData(xhr, !useArrayBuffer ? 1 : 6)) {
                         // Blob as response (XHR2)
-                        sceneText = xhr.responseText;
+                        //fileData = xhr.responseText;
+                        fileData = !useArrayBuffer ? xhr.responseText : xhr.response
 
                         if (!that.hasReachedQuota) {
                             // Open a transaction to the database
-                            var transaction = that.db.transaction(["scenes"], "readwrite");
+                            var transaction = that.db.transaction([targetStore], "readwrite");
 
                             // the transaction could abort because of a QuotaExceededError error
                             transaction.onabort = function (event) {
@@ -506,30 +527,36 @@ module BABYLON {
                                     }
                                 }
                                 catch (ex) { }
-                                callback(sceneText);
+                                callback(fileData);
                             };
 
                             transaction.oncomplete = function (event) {
-                                callback(sceneText);
+                                callback(fileData);
                             };
 
-                            var newScene = { sceneUrl: url, data: sceneText, version: that.manifestVersionFound};
+                            var newFile;
+                            if (targetStore === "scenes") {
+                                newFile = { sceneUrl: url, data: fileData, version: that.manifestVersionFound };
+                            }
+                            else {
+                                newFile = { textureUrl: url, data: fileData };
+                            }
 
                             try {
                                 // Put the scene into the database
-                                var addRequest = transaction.objectStore("scenes").put(newScene);
+                                var addRequest = transaction.objectStore(targetStore).put(newFile);
                                 addRequest.onsuccess = function (event) {
                                 };
                                 addRequest.onerror = function (event) {
-                                    BABYLON.Tools.Error("Error in DB add scene request in BABYLON.Database.");
+                                    BABYLON.Tools.Error("Error in DB add file request in BABYLON.Database.");
                                 };
                             }
                             catch (ex) {
-                                callback(sceneText);
+                                callback(fileData);
                             }
                         }
                         else {
-                            callback(sceneText);
+                            callback(fileData);
                         }
                     }
                     else {

+ 4 - 0
Babylon/Tools/babylon.gamepads.js

@@ -87,6 +87,10 @@ var BABYLON;
             document.body.appendChild(Gamepads.gamepadDOMInfo);
         };
 
+        Gamepads.prototype.dispose = function () {
+            document.body.removeChild(Gamepads.gamepadDOMInfo);
+        };
+
         Gamepads.prototype._onGamepadConnected = function (evt) {
             var newGamepad = this._addNewGamepad(evt.gamepad);
             if (this._callbackGamepadConnected)

+ 4 - 0
Babylon/Tools/babylon.gamepads.ts

@@ -90,6 +90,10 @@
             document.body.appendChild(Gamepads.gamepadDOMInfo);
         }
 
+        public dispose() {
+            document.body.removeChild(Gamepads.gamepadDOMInfo);
+        }
+
         private _onGamepadConnected(evt) {
             var newGamepad = this._addNewGamepad(evt.gamepad);
             if (this._callbackGamepadConnected) this._callbackGamepadConnected(newGamepad);

+ 3 - 3
Babylon/Tools/babylon.tools.js

@@ -236,15 +236,15 @@
             };
 
             var loadFromIndexedDB = function () {
-                database.loadSceneFromDB(url, callback, progressCallBack, noIndexedDB);
+                database.loadFileFromDB(url, callback, progressCallBack, noIndexedDB, useArrayBuffer);
             };
 
             if (url.indexOf("file:") !== -1) {
                 var fileName = url.substring(5);
                 BABYLON.Tools.ReadFile(BABYLON.FilesInput.FilesToLoad[fileName], callback, progressCallBack, true);
             } else {
-                // Caching only scenes files
-                if (database && url.indexOf(".babylon") !== -1 && (database.enableSceneOffline)) {
+                // Caching all files
+                if (database && database.enableSceneOffline) {
                     database.openAsync(loadFromIndexedDB, noIndexedDB);
                 } else {
                     noIndexedDB();

+ 5 - 3
Babylon/Tools/babylon.tools.ts

@@ -253,7 +253,8 @@
             };
 
             var loadFromIndexedDB = () => {
-                database.loadSceneFromDB(url, callback, progressCallBack, noIndexedDB);
+                
+                database.loadFileFromDB(url, callback, progressCallBack, noIndexedDB, useArrayBuffer);
             };
 
             if (url.indexOf("file:") !== -1) {
@@ -261,9 +262,10 @@
                 BABYLON.Tools.ReadFile(BABYLON.FilesInput.FilesToLoad[fileName], callback, progressCallBack, true);
             }
             else {
-                // Caching only scenes files
-                if (database && url.indexOf(".babylon") !== -1 && (database.enableSceneOffline)) {
+                // Caching all files
+                if (database && database.enableSceneOffline) {
                     database.openAsync(loadFromIndexedDB, noIndexedDB);
+                    
                 }
                 else {
                     noIndexedDB();

文件差異過大導致無法顯示
+ 2 - 2
babylon.1.13-beta-debug.js


文件差異過大導致無法顯示
+ 7 - 7
babylon.1.13-beta.js