Explorar o código

Add support for the detail map to the bump functions

Popov72 %!s(int64=5) %!d(string=hai) anos
pai
achega
6d83be495d

+ 31 - 4
src/Shaders/ShadersInclude/bumpFragment.fx

@@ -1,16 +1,20 @@
 vec2 uvOffset = vec2(0.0, 0.0);
 
-#if defined(BUMP) || defined(PARALLAX)
+#if defined(BUMP) || defined(PARALLAX) || defined(DETAIL)
 	#ifdef NORMALXYSCALE
 		float normalScale = 1.0;
-	#else		
+	#elif defined(BUMP)
 		float normalScale = vBumpInfos.y;
+	#else
+		float normalScale = vDetailInfos.z;
 	#endif
 
 	#if defined(TANGENT) && defined(NORMAL)
 		mat3 TBN = vTBN;
-	#else
+	#elif defined(BUMP)
 		mat3 TBN = cotangent_frame(normalW * normalScale, vPositionW, vBumpUV);
+    #else
+		mat3 TBN = cotangent_frame(normalW * normalScale, vPositionW, vDetailUV, vec2(1., 1.));
 	#endif
 #elif defined(ANISOTROPIC)
 	#if defined(TANGENT) && defined(NORMAL)
@@ -30,11 +34,34 @@
 	#endif
 #endif
 
+#ifdef DETAIL
+	vec4 detailColor = texture2D(detailSampler, vDetailUV + uvOffset);
+    vec2 detailNormalRG = detailColor.wy * 2.0 - 1.0;
+    float detailNormalNormalB = sqrt(1. - saturate(dot(detailNormalRG, detailNormalRG)));
+    vec3 detailNormal = vec3(detailNormalRG, detailNormalNormalB);
+#endif
+
 #ifdef BUMP
 	#ifdef OBJECTSPACE_NORMALMAP
 		normalW = normalize(texture2D(bumpSampler, vBumpUV).xyz  * 2.0 - 1.0);
 		normalW = normalize(mat3(normalMatrix) * normalW);	
-	#else
+	#elif !defined(DETAIL)
 		normalW = perturbNormal(TBN, vBumpUV + uvOffset);
+    #else
+        vec3 bumpNormal = texture2D(bumpSampler, vBumpUV + uvOffset).xyz * 2.0 - 1.0;
+        #if DETAIL_NORMALBLENDMETHOD == 0 // linear
+            vec3 blendedNormal = normalize(bumpNormal + detailNormal * vec3(vDetailInfos.z));
+        #elif DETAIL_NORMALBLENDMETHOD == 1 // whiteout
+            detailNormal.xy *= vDetailInfos.z;
+            vec3 blendedNormal = normalize(vec3(bumpNormal.xy + detailNormal.xy, bumpNormal.z * detailNormal.z));
+        #elif DETAIL_NORMALBLENDMETHOD == 2 // RNM
+            detailNormal.xy *= vDetailInfos.z;
+            bumpNormal += vec3(0.0, 0.0, 1.0);
+            detailNormal *= vec3(-1.0, -1.0, 1.0);
+            vec3 blendedNormal = bumpNormal * dot(bumpNormal, detailNormal) / bumpNormal.z - detailNormal;
+        #endif
+        normalW = perturbNormalBase(TBN, blendedNormal, vBumpInfos.y);
 	#endif
+#elif defined(DETAIL)    
+		normalW = perturbNormalBase(TBN, detailNormal, 1.0 / vDetailInfos.z);
 #endif

+ 11 - 0
src/Shaders/ShadersInclude/bumpFragmentFunctions.fx

@@ -14,6 +14,17 @@
 	}
 #endif
 
+#if defined(DETAIL)
+	#if DETAILDIRECTUV == 1
+		#define vDetailUV vMainUV1
+	#elif DETAILDIRECTUV == 2
+		#define vDetailUV vMainUV2
+	#else
+		varying vec2 vDetailUV;
+	#endif
+	uniform sampler2D detailSampler;
+#endif
+
 #if defined(BUMP)
 	vec3 perturbNormal(mat3 cotangentFrame, vec3 color)
 	{

+ 9 - 6
src/Shaders/ShadersInclude/bumpFragmentMainFunctions.fx

@@ -1,4 +1,4 @@
-#if defined(BUMP) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC)
+#if defined(BUMP) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC) || defined(DETAIL)
 	#if defined(TANGENT) && defined(NORMAL) 
 		varying mat3 vTBN;
 	#endif
@@ -7,15 +7,18 @@
 		uniform mat4 normalMatrix;
 	#endif
 
-	vec3 perturbNormal(mat3 cotangentFrame, vec3 textureSample, float scale)
+	vec3 perturbNormalBase(mat3 cotangentFrame, vec3 normal, float scale)
 	{
-		textureSample = textureSample * 2.0 - 1.0;
-
 		#ifdef NORMALXYSCALE
-			textureSample = normalize(textureSample * vec3(scale, scale, 1.0));
+			normal = normalize(normal * vec3(scale, scale, 1.0));
 		#endif
 
-		return normalize(cotangentFrame * textureSample);
+		return normalize(cotangentFrame * normal);
+	}
+
+	vec3 perturbNormal(mat3 cotangentFrame, vec3 textureSample, float scale)
+	{
+		return perturbNormalBase(cotangentFrame, textureSample * 2.0 - 1.0, scale);
 	}
 
 	// Thanks to http://www.thetenthplanet.de/archives/1180