Selaa lähdekoodia

New event for materials: ```onCompiled``` and ```onError```

David Catuhe 11 vuotta sitten
vanhempi
commit
353421da50

+ 4 - 1
Babylon/Cameras/babylon.camera.js

@@ -289,6 +289,7 @@ var BABYLON = BABYLON || {};
 
         this._computedViewMatrix.invert();
 
+        this._currentRenderId = this._scene.getRenderId();
         return this._computedViewMatrix;
     };
     
@@ -297,8 +298,10 @@ var BABYLON = BABYLON || {};
             return this._computedViewMatrix;
         }
 
-        this._currentRenderId = this._scene.getRenderId();
         this._computedViewMatrix = this._getViewMatrix();
+        if (!this.parent || !this.parent.getWorldMatrix) {
+            this._currentRenderId = this._scene.getRenderId();
+        }
         return this._computedViewMatrix;
     };
 

+ 21 - 7
Babylon/Materials/babylon.effect.js

@@ -4,7 +4,7 @@ var BABYLON = BABYLON || {};
 
 (function () {
 
-    BABYLON.Effect = function (baseName, attributesNames, uniformsNames, samplers, engine, defines, optionalDefines) {
+    BABYLON.Effect = function (baseName, attributesNames, uniformsNames, samplers, engine, defines, optionalDefines, onCompiled, onError) {
         this._engine = engine;
         this.name = baseName;
         this.defines = defines;
@@ -14,6 +14,9 @@ var BABYLON = BABYLON || {};
         this._compilationError = "";
         this._attributesNames = attributesNames;
 
+        this.onError = onError;
+        this.onCompiled = onCompiled;
+
         var vertexSource;
         var fragmentSource;
 
@@ -30,12 +33,16 @@ var BABYLON = BABYLON || {};
             that._loadFragmentShader(fragmentSource, function (fragmentCode) {
                 that._prepareEffect(vertexCode, fragmentCode, attributesNames, defines, optionalDefines);
             });
-        });   
+        });
 
         // Cache
         this._valueCache = [];
     };
 
+    // Events
+    BABYLON.Effect.prototype.onCompiled = null;
+    BABYLON.Effect.prototype.onError = null;
+
     // Properties
     BABYLON.Effect.prototype.isReady = function () {
         return this._isReady;
@@ -87,7 +94,7 @@ var BABYLON = BABYLON || {};
             callback(BABYLON.Effect.ShadersStore[vertex + "VertexShader"]);
             return;
         }
-        
+
         var vertexShaderUrl;
 
         if (vertex[0] === ".") {
@@ -113,7 +120,7 @@ var BABYLON = BABYLON || {};
             callback(BABYLON.Effect.ShadersStore[fragment + "PixelShader"]);
             return;
         }
-        
+
         var fragmentShaderUrl;
 
         if (fragment[0] === ".") {
@@ -146,6 +153,9 @@ var BABYLON = BABYLON || {};
             engine.bindSamplers(this);
 
             this._isReady = true;
+            if (this.onCompiled) {
+                this.onCompiled(this);
+            }
         } catch (e) {
             if (!useFallback && optionalDefines) {
                 for (var index = 0; index < optionalDefines.length; index++) {
@@ -157,6 +167,10 @@ var BABYLON = BABYLON || {};
                 console.error("Defines: " + defines);
                 console.error("Optional defines: " + optionalDefines);
                 this._compilationError = e.message;
+
+                if (this.onError) {
+                    this.onError(this, this._compilationError);
+                }
             }
         }
     };
@@ -215,7 +229,7 @@ var BABYLON = BABYLON || {};
         this._valueCache[uniformName][2] = z;
         this._valueCache[uniformName][3] = w;
     };
-    
+
     BABYLON.Effect.prototype.setArray = function (uniformName, array) {
         this._engine.setArray(this.getUniform(uniformName), array);
 
@@ -259,7 +273,7 @@ var BABYLON = BABYLON || {};
 
         return this;
     };
-    
+
     BABYLON.Effect.prototype.setVector2 = function (uniformName, vector2) {
         if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector2.x && this._valueCache[uniformName][1] == vector2.y)
             return this;
@@ -279,7 +293,7 @@ var BABYLON = BABYLON || {};
 
         return this;
     };
-    
+
     BABYLON.Effect.prototype.setVector3 = function (uniformName, vector3) {
         if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
             return this;

+ 16 - 4
Babylon/Materials/babylon.material.js

@@ -20,8 +20,11 @@ var BABYLON = BABYLON || {};
     BABYLON.Material.prototype._effect = null;
     BABYLON.Material.prototype._wasPreviouslyReady = false;
 
+    // Events
+    BABYLON.Effect.prototype.onCompiled = null;
+    BABYLON.Effect.prototype.onError = null;
     BABYLON.Material.prototype.onDispose = null;
-    
+
     // Properties
     BABYLON.Material.prototype.isReady = function (mesh) {
         return true;
@@ -40,6 +43,9 @@ var BABYLON = BABYLON || {};
     };
 
     // Methods   
+    BABYLON.Material.prototype.trackCreation = function (onCompiled, onError) {
+    };
+
     BABYLON.Material.prototype._preBind = function () {
         var engine = this._scene.getEngine();
         
@@ -53,18 +59,24 @@ var BABYLON = BABYLON || {};
     BABYLON.Material.prototype.unbind = function () {
     };
     
-    BABYLON.Material.prototype.baseDispose = function () {
+    BABYLON.Material.prototype.baseDispose = function (forceDisposeEffect) {
         // Remove from scene
         var index = this._scene.materials.indexOf(this);
         this._scene.materials.splice(index, 1);
 
+        // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
+        if (forceDisposeEffect && this._effect) {
+            this._scene.getEngine()._releaseEffect(this._effect);
+            this._effect = null;
+        }
+
         // Callback
         if (this.onDispose) {
             this.onDispose();
         }
     };
 
-    BABYLON.Material.prototype.dispose = function () {
-        this.baseDispose();
+    BABYLON.Material.prototype.dispose = function (forceDisposeEffect) {
+        this.baseDispose(forceDisposeEffect);
     };
 })();

+ 4 - 4
Babylon/Materials/babylon.shaderMaterial.js

@@ -116,8 +116,8 @@ var BABYLON = BABYLON || {};
         this._effect = engine.createEffect(this._shaderPath,
             this._options.attributes,
             this._options.uniforms,
-            this._options.samplers,
-            "");
+            this._options.samplers,            
+            "", null, this.onCompiled, this.onError);
 
         if (!this._effect.isReady()) {
             return false;
@@ -190,13 +190,13 @@ var BABYLON = BABYLON || {};
         }
     };
 
-    BABYLON.ShaderMaterial.prototype.dispose = function () {
+    BABYLON.ShaderMaterial.prototype.dispose = function (forceDisposeEffect) {
         for (var name in this._textures) {
             this._textures[name].dispose();
         }
 
         this._textures = [];
         
-        this.baseDispose();
+        this.baseDispose(forceDisposeEffect);
     };
 })();

+ 3 - 3
Babylon/Materials/babylon.standardMaterial.js

@@ -250,7 +250,7 @@ var BABYLON = BABYLON || {};
                 ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler",
                  "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
                 ],
-                join, optionalDefines);
+                join, optionalDefines, this.onCompiled, this.onError);
         }
         if (!this._effect.isReady()) {
             return false;
@@ -452,7 +452,7 @@ var BABYLON = BABYLON || {};
         return results;
     };
 
-    BABYLON.StandardMaterial.prototype.dispose = function () {
+    BABYLON.StandardMaterial.prototype.dispose = function (forceDisposeEffect) {
         if (this.diffuseTexture) {
             this.diffuseTexture.dispose();
         }
@@ -481,7 +481,7 @@ var BABYLON = BABYLON || {};
             this.bumpTexture.dispose();
         }
 
-        this.baseDispose();
+        this.baseDispose(forceDisposeEffect);
     };
 
     BABYLON.StandardMaterial.prototype.clone = function (name) {

+ 12 - 2
Babylon/babylon.engine.js

@@ -390,7 +390,16 @@ var BABYLON = BABYLON || {};
     };
 
     // Shaders
-    BABYLON.Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, optionalDefines) {
+    BABYLON.Engine.prototype._releaseEffect = function (effect) {
+        if (this._compiledEffects[effect._key]) {
+            delete this._compiledEffects[effect._key];
+            if (effect._program) {
+                this._gl.deleteProgram(effect._program);
+            }
+        }
+    };
+
+    BABYLON.Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, optionalDefines, onCompiled, onError) {
         var vertex = baseName.vertexElement || baseName.vertex || baseName;
         var fragment = baseName.fragmentElement || baseName.fragment || baseName;
         
@@ -399,7 +408,8 @@ var BABYLON = BABYLON || {};
             return this._compiledEffects[name];
         }
 
-        var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, optionalDefines);
+        var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, optionalDefines, onCompiled, onError);
+        effect._key = name;
         this._compiledEffects[name] = effect;
 
         return effect;

+ 1 - 1
Babylon/babylon.node.js

@@ -52,7 +52,7 @@ var BABYLON = BABYLON || {};
     };
 
     BABYLON.Node.prototype.isSynchronizedWithParent = function() {
-        return this.parent ? this.parent._currentRenderId === this._currentRenderId : true;
+        return this.parent ? this.parent._currentRenderId <= this._currentRenderId : true;
     };
 
     BABYLON.Node.prototype.isSynchronized = function (updateCache) {

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 2 - 2
babylon.1.11.0-beta.js