summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/renderergl2/glsl/lightall_fp.glsl67
-rw-r--r--src/renderergl2/glsl/lightall_vp.glsl36
2 files changed, 58 insertions, 45 deletions
diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl
index 53fd1b43..51e4634f 100644
--- a/src/renderergl2/glsl/lightall_fp.glsl
+++ b/src/renderergl2/glsl/lightall_fp.glsl
@@ -46,10 +46,15 @@ varying vec4 var_TexCoords;
varying vec4 var_Color;
-#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
-varying vec4 var_Normal;
-varying vec4 var_Tangent;
-varying vec4 var_Bitangent;
+#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
+ #if defined(USE_VERT_TANGENT_SPACE)
+varying vec4 var_Normal;
+varying vec4 var_Tangent;
+varying vec4 var_Bitangent;
+ #else
+varying vec3 var_Normal;
+varying vec3 var_ViewDir;
+ #endif
#endif
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
@@ -293,6 +298,25 @@ float CalcLightAttenuation(vec3 dir, float sqrRadius)
return attenuation;
}
+// from http://www.thetenthplanet.de/archives/1180
+mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
+{
+ // get edge vectors of the pixel triangle
+ vec3 dp1 = dFdx( p );
+ vec3 dp2 = dFdy( p );
+ vec2 duv1 = dFdx( uv );
+ vec2 duv2 = dFdy( uv );
+
+ // solve the linear system
+ vec3 dp2perp = cross( dp2, N );
+ vec3 dp1perp = cross( N, dp1 );
+ vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
+ vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
+
+ // construct a scale-invariant frame
+ float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
+ return mat3( T * invmax, B * invmax, N );
+}
void main()
{
@@ -300,21 +324,26 @@ void main()
float NL, NH, NE, EH;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
+ #if defined(USE_VERT_TANGENT_SPACE)
mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, var_Normal.xyz);
+ #else
+ mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
+ #endif
#endif
#if defined(USE_DELUXEMAP)
L = (2.0 * texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(1.0));
- #if defined(USE_TANGENT_SPACE_LIGHT)
- L = L * tangentToWorld;
- #endif
L = L * u_EnableTextures.y + var_LightDir.xyz;
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
L = var_LightDir.xyz;
#endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
+ #if defined(USE_VERT_TANGENT_SPACE)
E = normalize(vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w));
+ #else
+ E = normalize(var_ViewDir);
+ #endif
#endif
#if defined(USE_LIGHTMAP)
@@ -333,11 +362,7 @@ void main()
vec2 texCoords = var_TexCoords.xy;
#if defined(USE_PARALLAXMAP)
- #if defined(USE_TANGENT_SPACE_LIGHT)
- vec3 offsetDir = E;
- #else
- vec3 offsetDir = E * tangentToWorld;
- #endif
+ vec3 offsetDir = normalize(E * tangentToWorld);
offsetDir.xy *= -0.05 / offsetDir.z;
@@ -359,11 +384,7 @@ void main()
#endif
N.xy *= u_EnableTextures.x;
N.z = sqrt(1.0 - clamp(dot(N.xy, N.xy), 0.0, 1.0));
- #if !defined(USE_TANGENT_SPACE_LIGHT)
N = normalize(tangentToWorld * N);
- #endif
- #elif defined(USE_TANGENT_SPACE_LIGHT)
- N = vec3(0.0, 0.0, 1.0);
#else
N = normalize(var_Normal.xyz);
#endif
@@ -375,11 +396,7 @@ void main()
float shadowValue = texture2D(u_ShadowMap, shadowTex).r;
// surfaces not facing the light are always shadowed
- #if defined(USE_TANGENT_SPACE_LIGHT)
- shadowValue *= step(0.0, var_PrimaryLightDir.z);
- #else
shadowValue *= step(0.0, dot(var_Normal.xyz, var_PrimaryLightDir.xyz));
- #endif
#if defined(SHADOWMAP_MODULATE)
//vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor);
@@ -395,12 +412,7 @@ void main()
#if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)
vec3 ambientColor = lightColor;
-
- #if defined(USE_TANGENT_SPACE_LIGHT)
- float surfNL = L.z;
- #else
float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0);
- #endif
// Scale the incoming light to compensate for the baked-in light angle
// attenuation.
@@ -418,7 +430,7 @@ void main()
#if defined(USE_SPECULARMAP)
vec4 specular = texture2D(u_SpecularMap, texCoords);
- specular = mix(vec4(1.0), specular, u_EnableTextures.z);
+ specular = (specular - vec4(1.0)) * u_EnableTextures.z + vec4(1.0);
#if defined(USE_GAMMA2_TEXTURES)
specular.rgb *= specular.rgb;
#endif
@@ -473,9 +485,6 @@ void main()
reflectance = EnvironmentBRDF(gloss, NE, specular.rgb);
vec3 R = reflect(E, N);
- #if defined(USE_TANGENT_SPACE_LIGHT)
- R = tangentToWorld * R;
- #endif
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
diff --git a/src/renderergl2/glsl/lightall_vp.glsl b/src/renderergl2/glsl/lightall_vp.glsl
index 284b3973..aa0fe25b 100644
--- a/src/renderergl2/glsl/lightall_vp.glsl
+++ b/src/renderergl2/glsl/lightall_vp.glsl
@@ -6,14 +6,18 @@ attribute vec4 attr_Color;
attribute vec3 attr_Position;
attribute vec3 attr_Normal;
+#if defined(USE_VERT_TANGENT_SPACE)
attribute vec3 attr_Tangent;
attribute vec3 attr_Bitangent;
+#endif
#if defined(USE_VERTEX_ANIMATION)
attribute vec3 attr_Position2;
attribute vec3 attr_Normal2;
+ #if defined(USE_VERT_TANGENT_SPACE)
attribute vec3 attr_Tangent2;
attribute vec3 attr_Bitangent2;
+ #endif
#endif
#if defined(USE_LIGHT) && !defined(USE_LIGHT_VECTOR)
@@ -71,9 +75,14 @@ varying vec4 var_TexCoords;
varying vec4 var_Color;
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
+ #if defined(USE_VERT_TANGENT_SPACE)
varying vec4 var_Normal;
varying vec4 var_Tangent;
varying vec4 var_Bitangent;
+ #else
+varying vec3 var_Normal;
+varying vec3 var_ViewDir;
+ #endif
#endif
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
@@ -156,13 +165,17 @@ void main()
#if defined(USE_VERTEX_ANIMATION)
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp));
+ #if defined(USE_VERT_TANGENT_SPACE)
vec3 tangent = normalize(mix(attr_Tangent, attr_Tangent2, u_VertexLerp));
vec3 bitangent = normalize(mix(attr_Bitangent, attr_Bitangent2, u_VertexLerp));
+ #endif
#else
vec3 position = attr_Position;
vec3 normal = attr_Normal;
+ #if defined(USE_VERT_TANGENT_SPACE)
vec3 tangent = attr_Tangent;
vec3 bitangent = attr_Bitangent;
+ #endif
#endif
#if defined(USE_TCGEN)
@@ -182,8 +195,10 @@ void main()
#if defined(USE_MODELMATRIX)
position = (u_ModelMatrix * vec4(position, 1.0)).xyz;
normal = (u_ModelMatrix * vec4(normal, 0.0)).xyz;
+ #if defined(USE_VERT_TANGENT_SPACE)
tangent = (u_ModelMatrix * vec4(tangent, 0.0)).xyz;
bitangent = (u_ModelMatrix * vec4(bitangent, 0.0)).xyz;
+ #endif
#endif
#if defined(USE_LIGHT_VECTOR)
@@ -232,26 +247,15 @@ void main()
vec3 viewDir = u_ViewOrigin - position;
#endif
-#if defined(USE_TANGENT_SPACE_LIGHT)
- mat3 tangentToWorld = mat3(tangent, bitangent, normal);
-
- #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
- var_PrimaryLightDir.xyz = var_PrimaryLightDir.xyz * tangentToWorld;
- #endif
-
- #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
- var_LightDir.xyz = var_LightDir.xyz * tangentToWorld;
- #endif
-
- #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
- viewDir = viewDir * tangentToWorld;
- #endif
-#endif
-
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
+ #if defined(USE_VERT_TANGENT_SPACE)
// store view direction in tangent space to save on varyings
var_Normal = vec4(normal, viewDir.x);
var_Tangent = vec4(tangent, viewDir.y);
var_Bitangent = vec4(bitangent, viewDir.z);
+ #else
+ var_Normal = normal;
+ var_ViewDir = viewDir;
+ #endif
#endif
}