From 5678c1cd76e8db6d839afa309a1e1deee1976849 Mon Sep 17 00:00:00 2001 From: Frederic Bouvier Date: Wed, 4 Apr 2012 20:42:05 +0200 Subject: [PATCH] Add a sunlight technique to disable shadows, and avoid shader syntax problems with old NV cards --- Effects/sunlight.eff | 71 +++++++++++++++++++++++++++++++++- Shaders/sunlight-noshadow.frag | 41 ++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 Shaders/sunlight-noshadow.frag diff --git a/Effects/sunlight.eff b/Effects/sunlight.eff index d32d6af79..b685049ed 100644 --- a/Effects/sunlight.eff +++ b/Effects/sunlight.eff @@ -1,7 +1,10 @@ Effects/sunlight - + + + /sim/rendering/shadows/enabled + false @@ -76,5 +79,71 @@ --> + + + false + + false + + + one + one + + + 1 + RenderBin + + + 0 + depth-buffer + + + 1 + normal-buffer + + + 2 + diffuse-buffer + + + 3 + spec-emis-buffer + + + Shaders/sunlight.vert + Shaders/sunlight-noshadow.frag + + + depth_tex + sampler-2d + 0 + + + normal_tex + sampler-2d + 1 + + + color_tex + sampler-2d + 2 + + + spec_emis_tex + sampler-2d + 3 + + + + diff --git a/Shaders/sunlight-noshadow.frag b/Shaders/sunlight-noshadow.frag new file mode 100644 index 000000000..79f5ea664 --- /dev/null +++ b/Shaders/sunlight-noshadow.frag @@ -0,0 +1,41 @@ +uniform mat4 fg_ViewMatrix; +uniform sampler2D depth_tex; +uniform sampler2D normal_tex; +uniform sampler2D color_tex; +uniform sampler2D spec_emis_tex; +uniform vec4 fg_SunDiffuseColor; +uniform vec4 fg_SunSpecularColor; +uniform vec3 fg_SunDirection; +uniform vec3 fg_Planes; +varying vec3 ray; +void main() { + vec2 coords = gl_TexCoord[0].xy; + vec4 spec_emis = texture2D( spec_emis_tex, coords ); + if ( spec_emis.a < 0.1 ) + discard; + vec3 normal; + normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0); + normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) ); + float len = length(normal); + normal /= len; + vec3 viewDir = normalize(ray); + float depth = texture2D( depth_tex, coords ).r; + vec3 pos; + pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z); + pos.xy = viewDir.xy / viewDir.z * pos.z; + + vec4 tint; + vec3 lightDir = (fg_ViewMatrix * vec4( fg_SunDirection, 0.0 )).xyz; + lightDir = normalize( lightDir ); + vec3 color = texture2D( color_tex, coords ).rgb; + vec3 Idiff = clamp( dot( lightDir, normal ), 0.0, 1.0 ) * color * fg_SunDiffuseColor.rgb; + vec3 halfDir = lightDir - viewDir; + len = length( halfDir ); + vec3 Ispec = vec3(0.0); + vec3 Iemis = spec_emis.z * color; + if (len > 0.0001) { + halfDir /= len; + Ispec = pow( clamp( dot( halfDir, normal ), 0.0, 1.0 ), spec_emis.y * 255.0 ) * spec_emis.x * fg_SunSpecularColor.rgb; + } + gl_FragColor = vec4(Idiff + Ispec + Iemis, 1.0); +}