浏览代码

reorganized calls

Benjamin Guignabert 8 年之前
父节点
当前提交
adcfeffac2
共有 5 个文件被更改,包括 2128 次插入2146 次删除
  1. 1018 1019
      dist/preview release/babylon.d.ts
  2. 1018 1019
      dist/preview release/babylon.module.d.ts
  3. 1 1
      index.html
  4. 82 87
      src/Materials/babylon.standardMaterial.ts
  5. 9 20
      src/Materials/babylon.uniformBuffer.ts

文件差异内容过多而无法显示
+ 1018 - 1019
dist/preview release/babylon.d.ts


文件差异内容过多而无法显示
+ 1018 - 1019
dist/preview release/babylon.module.d.ts


+ 1 - 1
index.html

@@ -58,7 +58,7 @@
 					var spheres = []
 					for (var i = 0; i < squareSize; i++) {
 						for (var j = 0; j < squareSize; j++) {
-							for (var k = 0; k < 4; k++) {
+							for (var k = 0; k < 5; k++) {
 								var sphere = BABYLON.Mesh.CreateSphere("sphere", 16, 0.5, scene);
 
 								// Move the sphere upward 1/2 its height

+ 82 - 87
src/Materials/babylon.standardMaterial.ts

@@ -799,112 +799,105 @@ module BABYLON {
 
             // Bones
             MaterialHelper.BindBonesParameters(mesh, effect);
-            
-            if (!this.isFrozen || !this._uniformBuffer.isSync) {
-                if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
-                    // Fresnel
-                    if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
-                        this._uniformBuffer.updateColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
-                        this._uniformBuffer.updateColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
-                    }
+            if (this._mustRebind(scene, effect)) {
+                effect.setMatrix("viewProjection", scene.getTransformMatrix());
 
-                    if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
-                        this._uniformBuffer.updateColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
-                    }
+                if (!this.isFrozen || !this._uniformBuffer.isSync) {
+                    if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
+                        // Fresnel
+                        if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
+                            this._uniformBuffer.updateColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
+                            this._uniformBuffer.updateColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
+                        }
 
-                    if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
-                        this._uniformBuffer.updateColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
-                        this._uniformBuffer.updateColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
-                    }
+                        if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
+                            this._uniformBuffer.updateColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
+                        }
 
-                    if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
-                        this._uniformBuffer.updateColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
-                        this._uniformBuffer.updateColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
-                    }
+                        if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
+                            this._uniformBuffer.updateColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
+                            this._uniformBuffer.updateColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
+                        }
 
-                    if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
-                        this._uniformBuffer.updateColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
-                        this._uniformBuffer.updateColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
-                    }
-                }
+                        if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
+                            this._uniformBuffer.updateColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
+                            this._uniformBuffer.updateColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
+                        }
 
-                // Textures     
-                if (scene.texturesEnabled) {
-                    if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
-                        this._uniformBuffer.updateMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
+                        if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
+                            this._uniformBuffer.updateColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
+                            this._uniformBuffer.updateColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
+                        }
                     }
 
-                    if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
-                        this._uniformBuffer.updateMatrix("ambientMatrix", this._ambientTexture.getTextureMatrix());
-                    }
+                    // Textures     
+                    if (scene.texturesEnabled) {
+                        if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
+                            this._uniformBuffer.updateMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
+                        }
 
-                    if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
-                        this._uniformBuffer.updateMatrix("opacityMatrix", this._opacityTexture.getTextureMatrix());
-                    }
+                        if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
+                            this._uniformBuffer.updateMatrix("ambientMatrix", this._ambientTexture.getTextureMatrix());
+                        }
 
-                    if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vReflectionInfos", this._reflectionTexture.level, this.roughness);
-                        this._uniformBuffer.updateMatrix("reflectionMatrix", this._reflectionTexture.getReflectionTextureMatrix());
-                    }
+                        if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
+                            this._uniformBuffer.updateMatrix("opacityMatrix", this._opacityTexture.getTextureMatrix());
+                        }
 
-                    if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
-                        this._uniformBuffer.updateMatrix("emissiveMatrix", this._emissiveTexture.getTextureMatrix());
-                    }
+                        if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vReflectionInfos", this._reflectionTexture.level, this.roughness);
+                            this._uniformBuffer.updateMatrix("reflectionMatrix", this._reflectionTexture.getReflectionTextureMatrix());
+                        }
 
-                    if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
-                        this._uniformBuffer.updateMatrix("lightmapMatrix", this._lightmapTexture.getTextureMatrix());
-                    }
+                        if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
+                            this._uniformBuffer.updateMatrix("emissiveMatrix", this._emissiveTexture.getTextureMatrix());
+                        }
 
-                    if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
-                        this._uniformBuffer.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
-                        this._uniformBuffer.updateMatrix("specularMatrix", this._specularTexture.getTextureMatrix());
-                    }
+                        if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
+                            this._uniformBuffer.updateMatrix("lightmapMatrix", this._lightmapTexture.getTextureMatrix());
+                        }
 
-                    if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
-                        this._uniformBuffer.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, 1.0 / this._bumpTexture.level, this.parallaxScaleBias);
-                        this._uniformBuffer.updateMatrix("bumpMatrix", this._bumpTexture.getTextureMatrix());
-                    }
+                        if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
+                            this._uniformBuffer.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
+                            this._uniformBuffer.updateMatrix("specularMatrix", this._specularTexture.getTextureMatrix());
+                        }
 
-                    if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
-                        var depth = 1.0;
-                        if (!this._refractionTexture.isCube) {
-                            this._uniformBuffer.updateMatrix("refractionMatrix", this._refractionTexture.getReflectionTextureMatrix());
+                        if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
+                            this._uniformBuffer.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, 1.0 / this._bumpTexture.level, this.parallaxScaleBias);
+                            this._uniformBuffer.updateMatrix("bumpMatrix", this._bumpTexture.getTextureMatrix());
+                        }
 
-                            if ((<any>this._refractionTexture).depth) {
-                                depth = (<any>this._refractionTexture).depth;
+                        if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
+                            var depth = 1.0;
+                            if (!this._refractionTexture.isCube) {
+                                this._uniformBuffer.updateMatrix("refractionMatrix", this._refractionTexture.getReflectionTextureMatrix());
+
+                                if ((<any>this._refractionTexture).depth) {
+                                    depth = (<any>this._refractionTexture).depth;
+                                }
                             }
-                        }
-                        this._uniformBuffer.updateFloat4("vRefractionInfos", this._refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
-                    }                    
-                }
+                            this._uniformBuffer.updateFloat4("vRefractionInfos", this._refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
+                        }                    
+                    }
 
-                // Point size
-                if (this.pointsCloud) {
-                    this._uniformBuffer.updateFloat("pointSize", this.pointSize);
-                }
+                    // Point size
+                    if (this.pointsCloud) {
+                        this._uniformBuffer.updateFloat("pointSize", this.pointSize);
+                    }
 
-                if (defines.SPECULARTERM) {
-                    this._uniformBuffer.updateColor4("vSpecularColor", this.specularColor, this.specularPower);
+                    if (defines.SPECULARTERM) {
+                        this._uniformBuffer.updateColor4("vSpecularColor", this.specularColor, this.specularPower);
+                    }
+                    this._uniformBuffer.updateColor3("vEmissiveColor", this.emissiveColor);
                 }
-                this._uniformBuffer.updateColor3("vEmissiveColor", this.emissiveColor);
 
-                // Diffuse
-                this._uniformBuffer.updateColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
-
-                this._uniformBuffer.update();
-            }
-            
-            
-            if (this._mustRebind(scene, effect)) {
                 effect.bindUniformBuffer(this._uniformBuffer.getBuffer(), "Material");
-
-                effect.setMatrix("viewProjection", scene.getTransformMatrix());
-
+                
                 // Textures     
                 if (scene.texturesEnabled) {
                     if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
@@ -965,11 +958,12 @@ module BABYLON {
 
                 effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
                 effect.setColor3("vAmbientColor", this._globalAmbientColor);
-
             }
 
-
             if (this._mustRebind(scene, effect) || !this.isFrozen) {
+                // Diffuse
+                this._uniformBuffer.updateColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
+
                 // Lights
                 if (scene.lightsEnabled && !this._disableLighting) {
                     MaterialHelper.BindLights(scene, mesh, effect, defines, this._maxSimultaneousLights);
@@ -993,6 +987,7 @@ module BABYLON {
 
             }
 
+            this._uniformBuffer.update();
             this._afterBind(mesh, this._activeEffect);
         }
 

+ 9 - 20
src/Materials/babylon.uniformBuffer.ts

@@ -5,9 +5,8 @@ module BABYLON {
         private _data: number[];
         private _dynamic: boolean;
         private _uniformName: string;
-        private _uniformNames: string[];
-        private _uniformLocations: number[];
-        private _uniformSizes: number[];
+        private _uniformLocations: { [key:string]:number; };
+        private _uniformSizes: { [key:string]:number; };
         private _uniformLocationPointer: number;
         private _needSync: boolean;
 
@@ -18,9 +17,8 @@ module BABYLON {
 
             this._data = data || [];
 
-            this._uniformNames = [];
-            this._uniformLocations = [];
-            this._uniformSizes = [];
+            this._uniformLocations = {};
+            this._uniformSizes = {};
             this._uniformLocationPointer = 0;
             this._needSync = false;
         }
@@ -68,7 +66,7 @@ module BABYLON {
         }
 
         public addUniform(name: string, size: number | number[]) {
-            if (this._uniformNames.indexOf(name) !== -1) {
+            if (this._uniformLocations[name] !== undefined) {
                 // Already existing uniform
                 return;
             }
@@ -90,9 +88,8 @@ module BABYLON {
 
 
             this._fillAlignment(<number>size);
-            this._uniformNames.push(name);
-            this._uniformSizes.push(<number>size);
-            this._uniformLocations.push(this._uniformLocationPointer);
+            this._uniformSizes[name] = <number>size;
+            this._uniformLocations[name] = this._uniformLocationPointer;
             this._uniformLocationPointer += <number>size;
 
 
@@ -168,9 +165,9 @@ module BABYLON {
         }
 
         public updateUniform(uniformName: string, data: number[] | Float32Array) {
-            var index = this._uniformNames.indexOf(uniformName);
 
-            if (index === -1) {
+            var location = this._uniformLocations[uniformName];
+            if (location === undefined) {
                 return;
             }
 
@@ -178,14 +175,6 @@ module BABYLON {
                 this.create();
             }
 
-            var location = this._uniformLocations[index];
-            var size = data.length;
-
-            if (size != this._uniformSizes[index]) {
-                Tools.Error("Wrong uniform size.");
-                return;
-            }
-
             var changed = false;
             for (var i = 0; i < data.length; i++) {
                 if (this._data[location + i] !== data[i]) {