summaryrefslogtreecommitdiff
path: root/src/rend2/glsl/pshadow_fp.glsl
diff options
context:
space:
mode:
authorJames Canete <use.less01@gmail.com>2012-12-04 01:56:52 +0000
committerTim Angus <tim@ngus.net>2013-01-12 21:24:53 +0000
commit8cb69adad0915fc5de6dc87db1ae28aba44d350e (patch)
tree7db5db21b83edf73c901c4e6eae884037407adde /src/rend2/glsl/pshadow_fp.glsl
parent815a7ae53be78f1f7108dbf086b2928d3017bbe4 (diff)
#5808 - Include and use .glsl in source (rend2)
Diffstat (limited to 'src/rend2/glsl/pshadow_fp.glsl')
-rw-r--r--src/rend2/glsl/pshadow_fp.glsl98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/rend2/glsl/pshadow_fp.glsl b/src/rend2/glsl/pshadow_fp.glsl
new file mode 100644
index 00000000..b152971a
--- /dev/null
+++ b/src/rend2/glsl/pshadow_fp.glsl
@@ -0,0 +1,98 @@
+uniform sampler2D u_ShadowMap;
+
+uniform vec3 u_LightForward;
+uniform vec3 u_LightUp;
+uniform vec3 u_LightRight;
+uniform vec4 u_LightOrigin;
+uniform float u_LightRadius;
+varying vec3 var_Position;
+varying vec3 var_Normal;
+
+float sampleDistMap(sampler2D texMap, vec2 uv, float scale)
+{
+ vec3 distv = texture2D(texMap, uv).xyz;
+ return dot(distv, vec3(1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0)) * scale;
+}
+
+void main()
+{
+ vec3 lightToPos = var_Position - u_LightOrigin.xyz;
+ vec2 st = vec2(-dot(u_LightRight, lightToPos), dot(u_LightUp, lightToPos));
+
+ float fade = length(st);
+
+#if defined(USE_DISCARD)
+ if (fade >= 1.0)
+ {
+ discard;
+ }
+#endif
+
+ fade = clamp(8.0 - fade * 8.0, 0.0, 1.0);
+
+ st = st * 0.5 + vec2(0.5);
+
+#if defined(USE_SOLID_PSHADOWS)
+ float intensity = max(sign(u_LightRadius - length(lightToPos)), 0.0);
+#else
+ float intensity = clamp((1.0 - dot(lightToPos, lightToPos) / (u_LightRadius * u_LightRadius)) * 2.0, 0.0, 1.0);
+#endif
+
+ float lightDist = length(lightToPos);
+ float dist;
+
+#if defined(USE_DISCARD)
+ if (dot(u_LightForward, lightToPos) <= 0.0)
+ {
+ discard;
+ }
+
+ if (dot(var_Normal, lightToPos) > 0.0)
+ {
+ discard;
+ }
+#else
+ intensity *= max(sign(dot(u_LightForward, lightToPos)), 0.0);
+ intensity *= max(sign(-dot(var_Normal, lightToPos)), 0.0);
+#endif
+
+ intensity *= fade;
+#if defined(USE_PCF)
+ float part;
+
+ dist = sampleDistMap(u_ShadowMap, st + vec2(-1.0/512.0, -1.0/512.0), u_LightRadius);
+ part = max(sign(lightDist - dist), 0.0);
+
+ dist = sampleDistMap(u_ShadowMap, st + vec2( 1.0/512.0, -1.0/512.0), u_LightRadius);
+ part += max(sign(lightDist - dist), 0.0);
+
+ dist = sampleDistMap(u_ShadowMap, st + vec2(-1.0/512.0, 1.0/512.0), u_LightRadius);
+ part += max(sign(lightDist - dist), 0.0);
+
+ dist = sampleDistMap(u_ShadowMap, st + vec2( 1.0/512.0, 1.0/512.0), u_LightRadius);
+ part += max(sign(lightDist - dist), 0.0);
+
+ #if defined(USE_DISCARD)
+ if (part <= 0.0)
+ {
+ discard;
+ }
+ #endif
+
+ intensity *= part * 0.25;
+#else
+ dist = sampleDistMap(u_ShadowMap, st, u_LightRadius);
+
+ #if defined(USE_DISCARD)
+ if (lightDist - dist <= 0.0)
+ {
+ discard;
+ }
+ #endif
+
+ intensity *= max(sign(lightDist - dist), 0.0);
+#endif
+
+ gl_FragColor.rgb = vec3(0);
+ gl_FragColor.a = clamp(intensity, 0.0, 0.75);
+}