浏览代码

made toNormalMatrix change a given matrix and cached that one for pushMaterial

Max Limper 7 年之前
父节点
当前提交
281734f725

+ 3 - 3
src/Materials/PBR/babylon.pbrBaseMaterial.ts

@@ -1271,7 +1271,7 @@
             this._uniformBuffer.addUniform("opacityMatrix", 16);
             this._uniformBuffer.addUniform("emissiveMatrix", 16);
             this._uniformBuffer.addUniform("lightmapMatrix", 16);
-            this._uniformBuffer.addUniform("reflectivityMatrix", 16);            
+            this._uniformBuffer.addUniform("reflectivityMatrix", 16);
             this._uniformBuffer.addUniform("microSurfaceSamplerMatrix", 16);
             this._uniformBuffer.addUniform("bumpMatrix", 16);
             this._uniformBuffer.addUniform("vTangentSpaceParams", 2);
@@ -1334,8 +1334,8 @@
             // Normal Matrix
             if (defines.OBJECTSPACE_NORMALMAP)
             {
-                var normalMatrix = world.toNormalMatrix();
-                this.bindOnlyNormalMatrix(normalMatrix);                
+                world.toNormalMatrix(this._normalMatrix);
+                this.bindOnlyNormalMatrix(this._normalMatrix);                
             }
 
             let mustRebind = this._mustRebind(scene, effect, mesh.visibility);

+ 12 - 0
src/Materials/babylon.pushMaterial.ts

@@ -3,6 +3,8 @@
 
         protected _activeEffect: Effect;
 
+        protected _normalMatrix : Matrix = new Matrix();
+
         constructor(name: string, scene: Scene) {
             super(name, scene);
             this.storeEffectOnSubMeshes = true;
@@ -24,10 +26,20 @@
             return this.isReadyForSubMesh(mesh, mesh.subMeshes[0], useInstances);
         }
 
+         /**
+         * Binds the given world matrix to the active effect
+         * 
+         * @param world the matrix to bind
+         */
         public bindOnlyWorldMatrix(world: Matrix): void {
             this._activeEffect.setMatrix("world", world);
         }
 
+        /**
+         * Binds the given normal matrix to the active effect
+         * 
+         * @param normalMatrix the matrix to bind
+         */
         public bindOnlyNormalMatrix(normalMatrix: Matrix): void {                        
             this._activeEffect.setMatrix("normalMatrix", normalMatrix);
         }

+ 2 - 2
src/Materials/babylon.standardMaterial.ts

@@ -984,8 +984,8 @@ module BABYLON {
             // Normal Matrix
             if (defines.OBJECTSPACE_NORMALMAP)
             {
-                var normalMatrix = world.toNormalMatrix();
-                this.bindOnlyNormalMatrix(normalMatrix);                
+                world.toNormalMatrix(this._normalMatrix);
+                this.bindOnlyNormalMatrix(this._normalMatrix);               
             }
 
             let mustRebind = this._mustRebind(scene, effect, mesh.visibility);

+ 5 - 7
src/Math/babylon.math.ts

@@ -3898,17 +3898,15 @@
         /**
          * Returns a new Matrix which is the normal matrix computed from the current one (using values from identity matrix for fourth row and column).  
          */
-        public toNormalMatrix(): Matrix {
-            var result = Matrix.Identity();
-            this.invertToRef(result)
-            result.transpose();
-            var m = result.m;
+        public toNormalMatrix(ref : Matrix): void {            
+            this.invertToRef(ref)
+            ref.transpose();
+            var m = ref.m;
             Matrix.FromValuesToRef(
                 m[0], m[1], m[2],  0,
                 m[4], m[5], m[6],  0,
                 m[8], m[9], m[10], 0,
-                0,    0,    0,     1, result);
-            return result;
+                0,    0,    0,     1, ref);
         }
 
         /**