1#version 120 2uniform mat4 fg_ViewMatrix; 3uniform sampler2D depth_tex; 4uniform sampler2D normal_tex; 5uniform sampler2D color_tex; 6uniform sampler2D spec_emis_tex; 7uniform sampler2DShadow shadow_tex; 8uniform vec4 fg_SunDiffuseColor; 9uniform vec4 fg_SunSpecularColor; 10uniform vec3 fg_SunDirection; 11uniform vec3 fg_Planes; 12uniform int fg_ShadowNumber; 13uniform vec4 fg_ShadowDistances; 14 15uniform mat4 fg_ShadowMatrix_0; 16uniform mat4 fg_ShadowMatrix_1; 17uniform mat4 fg_ShadowMatrix_2; 18uniform mat4 fg_ShadowMatrix_3; 19 20uniform int filtering; 21varying vec3 ray; 22 23vec3 position( vec3 viewDir, vec2 coords, sampler2D depth_tex ); 24vec3 normal_decode(vec2 enc); 25 26vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint ) 27{ 28 vec4 coords; 29 vec2 shift = vec2( 0.0 ); 30 int index = 4; 31 float factor = 0.5; 32 if (ecPosition.z > -fg_ShadowDistances.x) { 33 index = 1; 34 if (fg_ShadowNumber == 1) 35 factor = 1.0; 36 tint = vec4(0.0,1.0,0.0,1.0); 37 coords = fg_ShadowMatrix_0 * ecPosition; 38 } else if (ecPosition.z > -fg_ShadowDistances.y && fg_ShadowNumber > 1) { 39 index = 2; 40 shift = vec2( 0.0, 0.5 ); 41 tint = vec4(0.0,0.0,1.0,1.0); 42 coords = fg_ShadowMatrix_1 * ecPosition; 43 } else if (ecPosition.z > -fg_ShadowDistances.z && fg_ShadowNumber > 2) { 44 index = 3; 45 shift = vec2( 0.5, 0.0 ); 46 tint = vec4(1.0,1.0,0.0,1.0); 47 coords = fg_ShadowMatrix_2 * ecPosition; 48 } else if (ecPosition.z > -fg_ShadowDistances.w && fg_ShadowNumber > 3) { 49 shift = vec2( 0.5, 0.5 ); 50 tint = vec4(1.0,0.0,0.0,1.0); 51 coords = fg_ShadowMatrix_3 * ecPosition; 52 } else { 53 return vec4(1.1,1.1,0.0,1.0); // outside, clamp to border 54 } 55 coords.st *= factor; 56 coords.st += shift; 57 return coords; 58} 59void main() { 60 vec2 coords = gl_TexCoord[0].xy; 61 vec4 spec_emis = texture2D( spec_emis_tex, coords ); 62 if ( spec_emis.a < 0.1 ) 63 discard; 64 vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg); 65 vec3 viewDir = normalize(ray); 66 vec3 pos = position( viewDir, coords, depth_tex ); 67 68 vec4 tint; 69 float shadow = 0.0; 70 if (filtering == 1) { 71 shadow = shadow2DProj( shadow_tex, DynamicShadow( vec4( pos, 1.0 ), tint ) ).r; 72 } else if (filtering == 2) { 73 shadow += 0.333 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos, 1.0), tint ) ).r; 74 shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.003 * pos.z, -0.002 * pos.z, 0), 1.0), tint ) ).r; 75 shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3( 0.003 * pos.z, 0.002 * pos.z, 0), 1.0), tint ) ).r; 76 shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.003 * pos.z, 0.002 * pos.z, 0), 1.0), tint ) ).r; 77 shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3( 0.003 * pos.z, -0.002 * pos.z, 0), 1.0), tint ) ).r; 78 } else { 79 float kernel[9] = float[9]( 36/256.0, 24/256.0, 6/256.0, 80 24/256.0, 16/256.0, 4/256.0, 81 6/256.0, 4/256.0, 1/256.0 ); 82 for( int x = -2; x <= 2; ++x ) 83 for( int y = -2; y <= 2; ++y ) 84 shadow += kernel[int(abs(float(x))*3 + abs(float(y)))] * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.0025 * x * pos.z, -0.0025 * y * pos.z, 0), 1.0), tint ) ).r; 85 } 86 vec3 lightDir = (fg_ViewMatrix * vec4( fg_SunDirection, 0.0 )).xyz; 87 lightDir = normalize( lightDir ); 88 vec4 color_material = texture2D( color_tex, coords ); 89 vec3 color = color_material.rgb; 90 vec3 Idiff = clamp( dot( lightDir, normal ), 0.0, 1.0 ) * color * fg_SunDiffuseColor.rgb; 91 vec3 halfDir = normalize( lightDir - viewDir ); 92 vec3 Ispec = vec3(0.0); 93 vec3 Iemis = spec_emis.z * color; 94 95 float cosAngIncidence = clamp(dot(normal, lightDir), 0.0, 1.0); 96 float blinnTerm = clamp( dot( halfDir, normal ), 0.0, 1.0 ); 97 98 if (cosAngIncidence > 0.0) 99 Ispec = pow( blinnTerm, spec_emis.y * 128.0 ) * spec_emis.x * fg_SunSpecularColor.rgb; 100 101 float matID = color_material.a * 255.0; 102 if (matID >= 254.0) // 254: Water, 255: Ubershader 103 Idiff += Ispec * spec_emis.x; 104 105 gl_FragColor = vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0); 106// gl_FragColor = mix(tint, vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0), 0.92); 107} 108