瀏覽代碼

end of UVMerge optimization

David Catuhe 8 年之前
父節點
當前提交
b9959c078a

文件差異過大導致無法顯示
+ 3678 - 3670
dist/preview release/babylon.d.ts


文件差異過大導致無法顯示
+ 3678 - 3670
dist/preview release/babylon.module.d.ts


+ 23 - 0
src/Materials/babylon.materialHelper.ts

@@ -1,6 +1,29 @@
 module BABYLON {
     export class MaterialHelper {
 
+        public static PrepareDefinesForMergedUV(texture: BaseTexture, defines: MaterialDefines, key: string): void {
+            defines._needUVs = true;
+            defines[key] = true;
+            if (texture.getTextureMatrix().isIdentity(true)) {
+                defines[key + "DIRECTUV"] = texture.coordinatesIndex + 1;
+                if (texture.coordinatesIndex === 0) {
+                    defines["MAINUV1"] = true;
+                } else {
+                    defines["MAINUV2"] = true;
+                }
+            } else {
+                defines[key + "DIRECTUV"] = 0;
+            }
+        }
+
+        public static BindTextureMatrix(texture: BaseTexture, uniformBuffer: UniformBuffer, key: string): void {
+            var matrix = texture.getTextureMatrix();
+
+            if (!matrix.isIdentity(true)) {
+                uniformBuffer.updateMatrix(key + "Matrix", matrix);
+            }
+        }
+
         public static PrepareDefinesForMisc(mesh: AbstractMesh, scene: Scene, useLogarithmicDepth: boolean, pointsCloud, fogEnabled: boolean, defines: MaterialDefines): void {
             if (defines._areMiscDirty) {
                 defines["LOGARITHMICDEPTH"] = useLogarithmicDepth;

+ 20 - 35
src/Materials/babylon.standardMaterial.ts

@@ -5,12 +5,17 @@ module BABYLON {
         public DIFFUSE = false;
         public DIFFUSEDIRECTUV = 0;
         public AMBIENT = false;
+        public AMBIENTDIRECTUV = 0;
         public OPACITY = false;
+        public OPACITYDIRECTUV = 0;
         public OPACITYRGB = false;
         public REFLECTION = false;
         public EMISSIVE = false;
+        public EMISSIVEDIRECTUV = 0;
         public SPECULAR = false;
+        public SPECULARDIRECTUV = 0;
         public BUMP = false;
+        public BUMPDIRECTUV = 0;
         public PARALLAX = false;
         public PARALLAXOCCLUSION = false;
         public SPECULAROVERALPHA = false;
@@ -40,6 +45,7 @@ module BABYLON {
         public LINKEMISSIVEWITHDIFFUSE = false;
         public REFLECTIONFRESNELFROMSPECULAR = false;
         public LIGHTMAP = false;
+        public LIGHTMAPDIRECTUV = 0;
         public USELIGHTMAPASSHADOWMAP = false;
         public REFLECTIONMAP_3D = false;
         public REFLECTIONMAP_SPHERICAL = false;
@@ -521,18 +527,7 @@ module BABYLON {
                         if (!this._diffuseTexture.isReadyOrNotBlocking()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.DIFFUSE = true;
-                            if (this._diffuseTexture.getTextureMatrix().isIdentity(true)) {
-                                defines.DIFFUSEDIRECTUV = this._diffuseTexture.coordinatesIndex + 1;
-                                if (this._diffuseTexture.coordinatesIndex === 0) {
-                                    defines.MAINUV1 = true;
-                                } else {
-                                    defines.MAINUV2 = true;
-                                }
-                            } else {
-                                defines.DIFFUSEDIRECTUV = 0;
-                            }
+                            MaterialHelper.PrepareDefinesForMergedUV(this._diffuseTexture, defines, "DIFFUSE");
                         }
                     } else {
                         defines.DIFFUSE = false;
@@ -542,8 +537,7 @@ module BABYLON {
                         if (!this._ambientTexture.isReadyOrNotBlocking()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.AMBIENT = true;
+                            MaterialHelper.PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
                         }
                     } else {
                         defines.AMBIENT = false;
@@ -553,8 +547,7 @@ module BABYLON {
                         if (!this._opacityTexture.isReadyOrNotBlocking()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.OPACITY = true;
+                            MaterialHelper.PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
                             defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
                         }
                     } else {
@@ -612,8 +605,7 @@ module BABYLON {
                         if (!this._emissiveTexture.isReadyOrNotBlocking()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.EMISSIVE = true;
+                            MaterialHelper.PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
                         }
                     } else {
                         defines.EMISSIVE = false;
@@ -623,8 +615,7 @@ module BABYLON {
                         if (!this._lightmapTexture.isReadyOrNotBlocking()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.LIGHTMAP = true;
+                            MaterialHelper.PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
                             defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
                         }
                     } else {
@@ -635,8 +626,7 @@ module BABYLON {
                         if (!this._specularTexture.isReadyOrNotBlocking()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.SPECULAR = true;
+                            MaterialHelper.PrepareDefinesForMergedUV(this._specularTexture, defines, "SPECULAR");
                             defines.GLOSSINESS = this._useGlossinessFromSpecularMapAlpha;
                         }
                     } else {
@@ -648,8 +638,7 @@ module BABYLON {
                         if (!this._bumpTexture.isReady()) {
                             return false;
                         } else {
-                            defines._needUVs = true;
-                            defines.BUMP = true;
+                            MaterialHelper.PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
 
                             defines.INVERTNORMALMAPX = this.invertNormalMapX;
                             defines.INVERTNORMALMAPY = this.invertNormalMapY;
@@ -999,21 +988,17 @@ module BABYLON {
                     if (scene.texturesEnabled) {
                         if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
                             this._uniformBuffer.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
-                            var matrix = this._diffuseTexture.getTextureMatrix();
-
-                            if (!matrix.isIdentity(true)) {
-                                this._uniformBuffer.updateMatrix("diffuseMatrix", matrix);
-                            }
+                            MaterialHelper.BindTextureMatrix(this._diffuseTexture, this._uniformBuffer, "diffuse");
                         }
 
                         if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
                             this._uniformBuffer.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
-                            this._uniformBuffer.updateMatrix("ambientMatrix", this._ambientTexture.getTextureMatrix());
+                            MaterialHelper.BindTextureMatrix(this._ambientTexture, this._uniformBuffer, "ambient");
                         }
 
                         if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
                             this._uniformBuffer.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
-                            this._uniformBuffer.updateMatrix("opacityMatrix", this._opacityTexture.getTextureMatrix());
+                            MaterialHelper.BindTextureMatrix(this._opacityTexture, this._uniformBuffer, "opacity");
                         }
 
                         if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
@@ -1023,22 +1008,22 @@ module BABYLON {
 
                         if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
                             this._uniformBuffer.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
-                            this._uniformBuffer.updateMatrix("emissiveMatrix", this._emissiveTexture.getTextureMatrix());
+                            MaterialHelper.BindTextureMatrix(this._emissiveTexture, this._uniformBuffer, "emissive");
                         }
 
                         if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
                             this._uniformBuffer.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
-                            this._uniformBuffer.updateMatrix("lightmapMatrix", this._lightmapTexture.getTextureMatrix());
+                            MaterialHelper.BindTextureMatrix(this._lightmapTexture, this._uniformBuffer, "lightmap");
                         }
 
                         if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
                             this._uniformBuffer.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
-                            this._uniformBuffer.updateMatrix("specularMatrix", this._specularTexture.getTextureMatrix());
+                            MaterialHelper.BindTextureMatrix(this._specularTexture, this._uniformBuffer, "specular");
                         }
 
                         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());
+                            MaterialHelper.BindTextureMatrix(this._bumpTexture, this._uniformBuffer, "bump");
                         }
 
                         if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {

+ 2 - 2
src/Math/babylon.math.ts

@@ -2941,7 +2941,7 @@
         /**
          * Boolean : True is the matrix is the identity matrix
          */
-        public isIdentity(considerAs3x3 = false): boolean {
+        public isIdentity(considerAsTextureMatrix = false): boolean {
             if (this._isIdentityDirty) {
                 this._isIdentityDirty = false;
                 if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[15] !== 1.0) {
@@ -2955,7 +2955,7 @@
                     this._isIdentity = true;
                 }
 
-                if (!considerAs3x3 && this.m[10] !== 1.0) {
+                if (!considerAsTextureMatrix && this.m[10] !== 1.0) {
                     this._isIdentity = false;
                 }
             }

+ 7 - 1
src/Shaders/ShadersInclude/bumpFragmentFunctions.fx

@@ -1,5 +1,11 @@
 #ifdef BUMP
-	varying vec2 vBumpUV;
+	#if BUMPDIRECTUV == 1
+		#define vBumpUV vMainUV1
+	#elif BUMPDIRECTUV == 2
+		#define vBumpUV vMainUV2
+	#else
+		varying vec2 vBumpUV;
+	#endif
 	uniform sampler2D bumpSampler;
 #if defined(TANGENT) && defined(NORMAL) 
 	varying mat3 vTBN;

+ 44 - 16
src/Shaders/default.fragment.fx

@@ -46,34 +46,56 @@ varying vec4 vColor;
 #ifdef DIFFUSE
 	#if DIFFUSEDIRECTUV == 1
 		#define vDiffuseUV vMainUV1
-	#else 
-		#if DIFFUSEDIRECTUV == 2
-			#define vDiffuseUV vMainUV2
-		#else
-			varying vec2 vDiffuseUV;
-		#endif
+	#elif DIFFUSEDIRECTUV == 2
+		#define vDiffuseUV vMainUV2
+	#else
+		varying vec2 vDiffuseUV;
 	#endif
 	uniform sampler2D diffuseSampler;
 #endif
 
 #ifdef AMBIENT
-varying vec2 vAmbientUV;
-uniform sampler2D ambientSampler;
+	#if AMBIENTDIRECTUV == 1
+		#define vAmbientUV vMainUV1
+	#elif AMBIENTDIRECTUV == 2
+		#define vAmbientUV vMainUV2
+	#else
+		varying vec2 vAmbientUV;
+	#endif
+	uniform sampler2D ambientSampler;
 #endif
 
 #ifdef OPACITY	
-varying vec2 vOpacityUV;
-uniform sampler2D opacitySampler;
+	#if OPACITYDIRECTUV == 1
+		#define vOpacityUV vMainUV1
+	#elif OPACITYDIRECTUV == 2
+		#define vOpacityUV vMainUV2
+	#else
+		varying vec2 vOpacityUV;
+	#endif
+	uniform sampler2D opacitySampler;
 #endif
 
 #ifdef EMISSIVE
-varying vec2 vEmissiveUV;
-uniform sampler2D emissiveSampler;
+	#if EMISSIVEDIRECTUV == 1
+		#define vEmissiveUV vMainUV1
+	#elif EMISSIVEDIRECTUV == 2
+		#define vEmissiveUV vMainUV2
+	#else
+		varying vec2 vEmissiveUV;
+	#endif
+	uniform sampler2D emissiveSampler;
 #endif
 
 #ifdef LIGHTMAP
-varying vec2 vLightmapUV;
-uniform sampler2D lightmapSampler;
+	#if LIGHTMAPDIRECTUV == 1
+		#define vLightmapUV vMainUV1
+	#elif LIGHTMAPDIRECTUV == 2
+		#define vLightmapUV vMainUV2
+	#else
+		varying vec2 vLightmapUV;
+	#endif
+	uniform sampler2D lightmapSampler;
 #endif
 
 #ifdef REFRACTION
@@ -87,8 +109,14 @@ uniform sampler2D refraction2DSampler;
 #endif
 
 #if defined(SPECULAR) && defined(SPECULARTERM)
-varying vec2 vSpecularUV;
-uniform sampler2D specularSampler;
+	#if SPECULARDIRECTUV == 1
+		#define vSpecularUV vMainUV1
+	#elif SPECULARDIRECTUV == 2
+		#define vSpecularUV vMainUV2
+	#else
+		varying vec2 vSpecularUV;
+	#endif
+	uniform sampler2D specularSampler;
 #endif
 
 // Fresnel

+ 14 - 14
src/Shaders/default.vertex.fx

@@ -30,31 +30,31 @@ attribute vec4 color;
 	varying vec2 vMainUV2;
 #endif
 
-#ifdef DIFFUSE
+#if defined(DIFFUSE) && DIFFUSEDIRECTUV == 0
 varying vec2 vDiffuseUV;
 #endif
 
-#ifdef AMBIENT
+#if defined(AMBIENT) && AMBIENTDIRECTUV == 0
 varying vec2 vAmbientUV;
 #endif
 
-#ifdef OPACITY
+#if defined(OPACITY) && OPACITYDIRECTUV == 0
 varying vec2 vOpacityUV;
 #endif
 
-#ifdef EMISSIVE
+#if defined(EMISSIVE) && EMISSIVEDIRECTUV == 0
 varying vec2 vEmissiveUV;
 #endif
 
-#ifdef LIGHTMAP
+#if defined(LIGHTMAP) && LIGHTMAPDIRECTUV == 0
 varying vec2 vLightmapUV;
 #endif
 
-#if defined(SPECULAR) && defined(SPECULARTERM)
+#if defined(SPECULAR) && defined(SPECULARTERM) && SPECULARDIRECTUV == 0
 varying vec2 vSpecularUV;
 #endif
 
-#ifdef BUMP
+#if defined(BUMP) && BUMPDIRECTUV == 0
 varying vec2 vBumpUV;
 #endif
 
@@ -132,7 +132,7 @@ void main(void) {
 #endif
 
 #ifdef MAINUV2
-	vMainUV2 = uv;
+	vMainUV2 = uv2;
 #endif
 
 #if defined(DIFFUSE) && DIFFUSEDIRECTUV == 0
@@ -146,7 +146,7 @@ void main(void) {
 	}
 #endif
 
-#ifdef AMBIENT
+#if defined(AMBIENT) && AMBIENTDIRECTUV == 0
 	if (vAmbientInfos.x == 0.)
 	{
 		vAmbientUV = vec2(ambientMatrix * vec4(uv, 1.0, 0.0));
@@ -157,7 +157,7 @@ void main(void) {
 	}
 #endif
 
-#ifdef OPACITY
+#if defined(OPACITY) && OPACITYDIRECTUV == 0
 	if (vOpacityInfos.x == 0.)
 	{
 		vOpacityUV = vec2(opacityMatrix * vec4(uv, 1.0, 0.0));
@@ -168,7 +168,7 @@ void main(void) {
 	}
 #endif
 
-#ifdef EMISSIVE
+#if defined(EMISSIVE) && EMISSIVEDIRECTUV == 0
 	if (vEmissiveInfos.x == 0.)
 	{
 		vEmissiveUV = vec2(emissiveMatrix * vec4(uv, 1.0, 0.0));
@@ -179,7 +179,7 @@ void main(void) {
 	}
 #endif
 
-#ifdef LIGHTMAP
+#if defined(LIGHTMAP) && LIGHTMAPDIRECTUV == 0
 	if (vLightmapInfos.x == 0.)
 	{
 		vLightmapUV = vec2(lightmapMatrix * vec4(uv, 1.0, 0.0));
@@ -190,7 +190,7 @@ void main(void) {
 	}
 #endif
 
-#if defined(SPECULAR) && defined(SPECULARTERM)
+#if defined(SPECULAR) && defined(SPECULARTERM) && SPECULARDIRECTUV == 0
 	if (vSpecularInfos.x == 0.)
 	{
 		vSpecularUV = vec2(specularMatrix * vec4(uv, 1.0, 0.0));
@@ -201,7 +201,7 @@ void main(void) {
 	}
 #endif
 
-#ifdef BUMP
+#if defined(BUMP) && BUMPDIRECTUV == 0
 	if (vBumpInfos.x == 0.)
 	{
 		vBumpUV = vec2(bumpMatrix * vec4(uv, 1.0, 0.0));