@@ -80,23 +80,23 @@ uniform vec4 u_VertColor;
8080uniform vec4 u_Disintegration;
8181uniform int u_ColorGen;
8282
83+ #if defined(PER_PIXEL_LIGHTING) && defined(USE_NORMALMAP) && defined(USE_PARALLAXMAP)
84+ uniform sampler2D u_NormalMap;
85+ #endif
86+
8387out vec4 var_TexCoords;
8488out vec4 var_Color;
8589
8690#if defined(PER_PIXEL_LIGHTING)
8791out vec4 var_Normal;
8892out vec4 var_Tangent;
8993out vec4 var_ViewDir;
90- out vec4 var_TangentViewDir ;
94+ out vec4 var_LightDir ;
9195#else
9296out vec3 var_Position;
9397out vec3 var_Normal;
9498#endif
9599
96- #if defined(PER_PIXEL_LIGHTING)
97- out vec4 var_LightDir;
98- #endif
99-
100100vec4 CalcColor(vec3 position)
101101{
102102 vec4 color = vec4 (1.0 );
@@ -327,15 +327,30 @@ void main()
327327
328328#if defined(PER_PIXEL_LIGHTING)
329329 vec3 viewDir = u_ViewOrigin.xyz - position;
330-
331- // store view direction in tangent space to save on outs
332- var_Normal = vec4 (normal, 0.0 );
333330 var_Tangent = vec4 (tangent, (attr_Tangent.w * 2.0 - 1.0 ));
334- var_ViewDir = vec4 (viewDir.xyz, 0.0 );
335331
336- vec3 bitangent = cross (normal, tangent) * var_Tangent.w;
337- mat3 TBN = mat3 (tangent, bitangent, normal);
338- var_TangentViewDir = vec4 (var_ViewDir.xyz * TBN, 0.0 );
332+ #if defined(USE_NORMALMAP) && defined(USE_PARALLAXMAP)
333+ vec3 bitangent = cross (normal, tangent) * var_Tangent.w;
334+ mat3 TBN = mat3 (tangent, bitangent, normal);
335+ vec3 tangentViewDir = viewDir * TBN;
336+
337+ // normal map aspect correction for parallax mapping
338+ vec2 normalSize = vec2 (textureSize(u_NormalMap, 0 ));
339+ float normalMapAspect = normalSize.y / normalSize.x;
340+
341+ tangentViewDir *= vec3 (
342+ max (1.0 , normalMapAspect),
343+ max (1.0 , 1.0 / normalMapAspect),
344+ 1.0
345+ );
346+ #else
347+ vec3 tangentViewDir = vec3 (0.0 );
348+ #endif
349+
350+ // store tangent view direction in other outs to save space
351+ var_LightDir.w = tangentViewDir.x;
352+ var_Normal = vec4 (normal, tangentViewDir.y);
353+ var_ViewDir = vec4 (viewDir, tangentViewDir.z);
339354#else
340355 var_Normal = normal;
341356 var_Position = position;
@@ -456,7 +471,6 @@ in vec4 var_Color;
456471in vec4 var_Normal;
457472in vec4 var_Tangent;
458473in vec4 var_ViewDir;
459- in vec4 var_TangentViewDir;
460474in vec4 var_LightDir;
461475#else
462476in vec3 var_Position;
@@ -615,7 +629,7 @@ float RayIntersectDisplaceMap(in vec2 inDp, in vec2 ds, in sampler2D normalMap,
615629 const int linearSearchSteps = 16 ;
616630 const int binarySearchSteps = 8 ;
617631
618- vec2 dp = inDp - parallaxBias * ds;
632+ vec2 dp = fract ( inDp - parallaxBias * ds) ;
619633
620634 // current size of search window
621635 float size = 1.0 / float (linearSearchSteps);
@@ -629,8 +643,16 @@ float RayIntersectDisplaceMap(in vec2 inDp, in vec2 ds, in sampler2D normalMap,
629643 vec2 dx = dFdx (inDp);
630644 vec2 dy = dFdy (inDp);
631645
646+ // try sampling at least one border pixel
647+ vec2 tMin = (vec2 (0.0 ) - dp) / ds;
648+ vec2 tMax = (vec2 (1.0 ) - dp) / ds;
649+ vec2 t = max (tMin, tMax);
650+ float tExit = min (t.x, t.y);
651+ float stepFraction = fract (tExit / size) * size;
652+ depth -= size- stepFraction;
653+
632654 // search front to back for first point inside object
633- for (int i = 0 ; i < linearSearchSteps - 1 ; ++ i)
655+ for (int i = 0 ; i < linearSearchSteps; ++ i)
634656 {
635657 depth += size;
636658
@@ -676,9 +698,7 @@ float RayIntersectDisplaceMap(in vec2 inDp, in vec2 ds, in sampler2D normalMap,
676698vec2 GetParallaxOffset(in vec2 texCoords, in vec3 tangentDir)
677699{
678700#if defined(USE_PARALLAXMAP)
679- ivec2 normalSize = textureSize(u_NormalMap, 0 );
680- vec3 nonSquareScale = mix (vec3 (normalSize.y / normalSize.x, 1.0 , 1.0 ), vec3 (1.0 , normalSize.x / normalSize.y, 1.0 ), float (normalSize.y <= normalSize.x));
681- vec3 offsetDir = normalize (tangentDir * nonSquareScale);
701+ vec3 offsetDir = normalize (tangentDir);
682702 offsetDir.xy *= - u_NormalScale.a / offsetDir.z;
683703
684704 return offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap, u_ParallaxBias);
@@ -1052,7 +1072,9 @@ void main()
10521072 vec2 texCoords = var_TexCoords.xy;
10531073 vec2 lmCoords = var_TexCoords.zw;
10541074#if defined(PER_PIXEL_LIGHTING)
1055- vec2 tex_offset = GetParallaxOffset(texCoords, var_TangentViewDir.xyz);
1075+ // Unpack tangent view direction
1076+ vec3 tangentViewDir = vec3 (var_LightDir.w, var_Normal.w, var_ViewDir.w);
1077+ vec2 tex_offset = GetParallaxOffset(texCoords, tangentViewDir);
10561078 texCoords += tex_offset;
10571079#endif
10581080
@@ -1127,10 +1149,6 @@ void main()
11271149 vec3 normalBias = vertexNormal * (1.0 - NPL);
11281150 float shadowValue = sunShadow(u_ViewOrigin, viewDir, normalBias, u_ShadowMap) * NPL;
11291151
1130- // surfaces not facing the light are always shadowed
1131-
1132- // shadowValue = mix(0.0, shadowValue, dot(N, primaryLightDir) > 0.0);
1133-
11341152 #if defined(SHADOWMAP_MODULATE)
11351153 vec3 ambientScale = mix (vec3 (1.0 ), u_PrimaryLightAmbient, u_EnableTextures.z);
11361154 lightColor = mix (ambientScale * lightColor, lightColor, shadowValue);
0 commit comments