Kaynağa Gözat

updated vertex shader

Benjamin Guignabert 8 yıl önce
ebeveyn
işleme
5da188450e

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1564 - 1562
dist/preview release/babylon.d.ts


Dosya farkı çok büyük olduğundan ihmal edildi
+ 1564 - 1562
dist/preview release/babylon.module.d.ts


+ 3 - 2
index.html

@@ -60,10 +60,11 @@
 					sphere.position.y = 1;
 					sphere.material = new BABYLON.StandardMaterial("test");
 					sphere.material.diffuseColor = new BABYLON.Color3(1, 1, 1);
+					sphere.material.diffuseTexture = new BABYLON.Texture("test.jpeg", scene);
 					var t = 0;
 					scene.registerAfterRender(function() {
-						sphere.material.specularColor = new BABYLON.Color3(0, 0, Math.sin(t/100));
-						sphere.material.diffuseColor = new BABYLON.Color3(Math.abs(Math.sin(t/100)), Math.abs(Math.cos(t/200)), Math.abs(Math.sin(t/50)));
+						// sphere.material.specularColor = new BABYLON.Color3(0, 0, Math.sin(t/100));
+						// sphere.material.diffuseColor = new BABYLON.Color3(Math.abs(Math.sin(t/100)), Math.abs(Math.cos(t/200)), Math.abs(Math.sin(t/50)));
 						t++;
 					})
 

+ 84 - 28
src/Materials/babylon.effect.ts

@@ -197,6 +197,28 @@
             return this._uniforms[this._uniformsNames.indexOf(uniformName)];
         }
 
+        public getUniformWithinDynamicUbo(uniformName: string): number {
+            var index;
+            var location;
+            if ((index = this._uniformBufferDynamicNames.indexOf(uniformName)) !== -1) {
+                location = this._uniformBufferDynamicLocations[index]
+                return location;
+            } else {
+                return -1;
+            }
+        }
+
+        public getUniformWithinStaticUbo(uniformName: string): number {
+            var index;
+            var location;
+            if ((index = this._uniformBufferStaticNames.indexOf(uniformName)) !== -1) {
+                location = this._uniformBufferStaticLocations[index]
+                return location;
+            } else {
+                return -1;
+            }
+        }
+
         public getSamplers(): string[] {
             return this._samplers;
         }
@@ -732,7 +754,14 @@
 
         public setMatrix(uniformName: string, matrix: Matrix): Effect {
             if (this._cacheMatrix(uniformName, matrix)) {
-                this._engine.setMatrix(this.getUniform(uniformName), matrix);
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
+                    this._uniformBufferStaticCache.set(matrix.toArray(), location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    this._uniformBufferDynamicCache.set(matrix.toArray(), location);
+                } else {
+                    this._engine.setMatrix(this.getUniform(uniformName), matrix);
+                }
             }
             return this;
         }
@@ -777,28 +806,39 @@
 
         public setVector2(uniformName: string, vector2: Vector2): Effect {
             if (this._cacheFloat2(uniformName, vector2.x, vector2.y)) {
-                this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
+                    vector2.toArray(this._uniformBufferStaticCache, location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    vector2.toArray(this._uniformBufferDynamicCache, location);
+                } else {
+                    this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
+                }
             }
             return this;
         }
 
         public setFloat2(uniformName: string, x: number, y: number): Effect {
             if (this._cacheFloat2(uniformName, x, y)) {
-                this._engine.setFloat2(this.getUniform(uniformName), x, y);
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
+                    this._uniformBufferStaticCache.set([x, y], location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    this._uniformBufferDynamicCache.set([x, y], location);
+                } else {
+                    this._engine.setFloat2(this.getUniform(uniformName), x, y);
+                }
             }
             return this;
         }
 
         public setVector3(uniformName: string, vector3: Vector3): Effect {
             if (this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z)) {
-                var index;
-                var location;
-                if ((index = this._uniformBufferDynamicNames.indexOf(uniformName)) !== -1) {
-                    location = this._uniformBufferDynamicLocations[index];
-                    vector3.toArray(this._uniformBufferDynamicCache, location);
-                } else if ((index = this._uniformBufferStaticNames.indexOf(uniformName)) !== -1) {
-                    location = this._uniformBufferStaticLocations[index];
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
                     vector3.toArray(this._uniformBufferStaticCache, location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    vector3.toArray(this._uniformBufferDynamicCache, location);
                 } else {
                     this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
                 }
@@ -808,35 +848,54 @@
 
         public setFloat3(uniformName: string, x: number, y: number, z: number): Effect {
             if (this._cacheFloat3(uniformName, x, y, z)) {
-                this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
+                    this._uniformBufferStaticCache.set([x, y, z], location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    this._uniformBufferDynamicCache.set([x, y, z], location);
+                } else {
+                    this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
+                }
             }
             return this;
         }
 
         public setVector4(uniformName: string, vector4: Vector4): Effect {
             if (this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w)) {
-                this._engine.setFloat4(this.getUniform(uniformName), vector4.x, vector4.y, vector4.z, vector4.w);
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
+                    vector4.toArray(this._uniformBufferStaticCache, location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    vector4.toArray(this._uniformBufferDynamicCache, location);
+                } else {
+                    this._engine.setFloat4(this.getUniform(uniformName), vector4.x, vector4.y, vector4.z, vector4.w);
+                }
             }
             return this;
         }
 
         public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): Effect {
             if (this._cacheFloat4(uniformName, x, y, z, w)) {
-                this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
+                    this._uniformBufferStaticCache.set([x, y, z, w], location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    this._uniformBufferDynamicCache.set([x, y, z, w], location);
+                } else {
+                    this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
+                }
             }
             return this;
         }
 
         public setColor3(uniformName: string, color3: Color3): Effect {
+
             if (this._cacheFloat3(uniformName, color3.r, color3.g, color3.b)) {
-                var index;
-                var location;
-                if ((index = this._uniformBufferDynamicNames.indexOf(uniformName)) !== -1) {
-                    location = this._uniformBufferDynamicLocations[index];
-                    color3.toArray(this._uniformBufferDynamicCache, location);
-                } else if ((index = this._uniformBufferStaticNames.indexOf(uniformName)) !== -1) {
-                    location = this._uniformBufferStaticLocations[index];
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
                     color3.toArray(this._uniformBufferStaticCache, location);
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    color3.toArray(this._uniformBufferDynamicCache, location);
                 } else {
                     this._engine.setColor3(this.getUniform(uniformName), color3);
                 }
@@ -846,16 +905,13 @@
 
         public setColor4(uniformName: string, color3: Color3, alpha: number): Effect {
             if (this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha)) {
-                var index;
-                var location;
-                if ((index = this._uniformBufferDynamicNames.indexOf(uniformName)) !== -1) {
-                    location = this._uniformBufferDynamicLocations[index];
-                    color3.toArray(this._uniformBufferDynamicCache, location);
-                    this._uniformBufferDynamicCache[location + 3] = alpha;
-                } else if ((index = this._uniformBufferStaticNames.indexOf(uniformName)) !== -1) {
-                    location = this._uniformBufferStaticLocations[index];
+                var location = this.getUniformWithinStaticUbo(uniformName);
+                if (location !== -1) {
                     color3.toArray(this._uniformBufferStaticCache, location);
                     this._uniformBufferStaticCache[location + 3] = alpha;
+                } else if ((location = this.getUniformWithinDynamicUbo(uniformName)) !== -1) {
+                    color3.toArray(this._uniformBufferDynamicCache, location);
+                    this._uniformBufferDynamicCache[location + 3] = alpha;
                 } else {
                     this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
                 }

+ 14 - 0
src/Materials/babylon.standardMaterial.ts

@@ -729,14 +729,28 @@ module BABYLON {
         }
 
         public buildUniformLayout(): void {
+            // Order is important !
             this._effect.addUniform("vDiffuseColor", 4, false);
+
+            if (this._defines.DIFFUSE) {
+                this._effect.addUniform("vDiffuseInfos", 2, false);
+                this._effect.addUniform("diffuseMatrix", 16, false);
+            }
+
             this._effect.addUniform("vAmbientColor", 3, false);
+
             if (this._defines.SPECULARTERM) {
                 this._effect.addUniform("vSpecularColor", 3, false);
             }
 
+            this._effect.addUniform("vEmissiveColor", 3, false);
+            
             // Dynamic uniforms
             this._effect.addUniform("vEyePosition", 3, true);
+            // this._effect.addUniform("world", 16, true);
+            // this._effect.addUniform("view", 16, true);
+            // this._effect.addUniform("viewProjection", 16, true);
+            // "world", "view", "viewProjection"
         }
 
         public unbind(): void {

+ 12 - 6
src/Shaders/default.fragment.fx

@@ -6,12 +6,21 @@ struct Camera {
 
 struct Material
 {
-  	vec4 vDiffuseColor;
+	vec4 vDiffuseColor;
+
+  	#ifdef DIFFUSE
+  	vec2 vDiffuseInfos;
+  	mat4 diffuseMatrix;
+  	#endif
+
   	vec3 vAmbientColor;
+
   	#ifdef SPECULARTERM
   	vec4 vSpecularColor;
   	#endif
+  	
   	vec3 vEmissiveColor;
+
 };
 
 uniform Dynamic {
@@ -34,8 +43,6 @@ uniform Static {
 // Constants
 #define RECIPROCAL_PI2 0.15915494
 
-uniform vec3 vEmissiveColor;
-
 // Input
 varying vec3 vPositionW;
 
@@ -60,7 +67,6 @@ varying vec4 vColor;
 #ifdef DIFFUSE
 varying vec2 vDiffuseUV;
 uniform sampler2D diffuseSampler;
-uniform vec2 vDiffuseInfos;
 #endif
 
 #ifdef AMBIENT
@@ -213,7 +219,7 @@ void main(void) {
 	alpha *= baseColor.a;
 #endif
 
-	baseColor.rgb *= vDiffuseInfos.y;
+	baseColor.rgb *= uStatic.material.vDiffuseInfos.y;
 #endif
 
 #ifdef VERTEXCOLOR
@@ -360,7 +366,7 @@ void main(void) {
 #endif
 
 	// Emissive
-	vec3 emissiveColor = vEmissiveColor;
+	vec3 emissiveColor = uStatic.material.vEmissiveColor;
 #ifdef EMISSIVE
 	emissiveColor += texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb * vEmissiveInfos.y;
 #endif

+ 38 - 6
src/Shaders/default.vertex.fx

@@ -1,4 +1,38 @@
-// Attributes
+layout(std140, column_major) uniform;
+
+struct Camera {
+	vec3 vEyePosition;
+};
+
+struct Material
+{
+	vec4 vDiffuseColor;
+
+  	#ifdef DIFFUSE
+  	vec2 vDiffuseInfos;
+  	mat4 diffuseMatrix;
+  	#endif
+
+  	vec3 vAmbientColor;
+
+  	#ifdef SPECULARTERM
+  	vec4 vSpecularColor;
+  	#endif
+  	
+  	vec3 vEmissiveColor;
+
+};
+
+uniform Dynamic {
+	Camera camera;
+}  uDynamic;
+
+uniform Static {
+	Material material;
+}  uStatic;
+
+
+// Attributes
 attribute vec3 position;
 #ifdef NORMAL
 attribute vec3 normal;
@@ -26,8 +60,6 @@ uniform mat4 viewProjection;
 
 #ifdef DIFFUSE
 varying vec2 vDiffuseUV;
-uniform mat4 diffuseMatrix;
-uniform vec2 vDiffuseInfos;
 #endif
 
 #ifdef AMBIENT
@@ -125,13 +157,13 @@ void main(void) {
 #endif
 
 #ifdef DIFFUSE
-	if (vDiffuseInfos.x == 0.)
+	if (uStatic.material.vDiffuseInfos.x == 0.)
 	{
-		vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));
+		vDiffuseUV = vec2(uStatic.material.diffuseMatrix * vec4(uv, 1.0, 0.0));
 	}
 	else
 	{
-		vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));
+		vDiffuseUV = vec2(uStatic.material.diffuseMatrix * vec4(uv2, 1.0, 0.0));
 	}
 #endif