[1493] | 1 | // Shadow map always comes in texture unit 0,
|
---|
| 2 | // so we have to decal all other textures if any...
|
---|
| 3 |
|
---|
| 4 | sampler2D indirectTexture : TEXUNIT0;
|
---|
| 5 | sampler2D sourceTexture : TEXUNIT1;
|
---|
| 6 | sampler2D visibilityTexture : TEXUNIT2;
|
---|
[1546] | 7 | sampler2D noisyTexture : TEXUNIT3;
|
---|
[1493] | 8 |
|
---|
| 9 |
|
---|
| 10 | struct Vertex
|
---|
| 11 | {
|
---|
| 12 | float4 position : POSITION; // Fragment's position in screen-space
|
---|
| 13 | float2 texCoord : TEXCOORD0; // Fragment's texture coordinates
|
---|
| 14 | float2 subTexCoord : TEXCOORD1; // Fragment's texture coordinates
|
---|
| 15 | float3 normal : TEXCOORD2; // Fragment's normal in eye-space
|
---|
| 16 | float3 halfVector : TEXCOORD3; // Fragment's half angle vector in eye-space
|
---|
| 17 | float3 lightVector : TEXCOORD4; // Fragment's light vector in eye-space
|
---|
| 18 | float4 wposition : TEXCOORD5; // Vertex position in world-space
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | struct Fragment
|
---|
| 22 | {
|
---|
| 23 | float4 color : COLOR0;
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | Fragment main(Vertex p_In,
|
---|
| 27 | uniform float4 p_LightDiffuse, // Light diffuse component
|
---|
| 28 | uniform float p_LightPower, // Light power
|
---|
| 29 | uniform float4 p_Diffuse, // Material diffuse component
|
---|
| 30 | uniform float4 p_LightSpecular, // Light specular component
|
---|
| 31 | uniform float4 p_Specular, // Material specular component + specular exponent
|
---|
| 32 | uniform float4 p_Ambient,
|
---|
| 33 | uniform float epsilonX,
|
---|
| 34 | uniform float epsilonY,
|
---|
| 35 | uniform float sourceTextureSize,
|
---|
| 36 | uniform float sqrtNumSamples,
|
---|
| 37 | uniform float greyRange,
|
---|
| 38 | uniform float absGreyMin
|
---|
| 39 | )
|
---|
| 40 | {
|
---|
| 41 | Fragment l_Out;
|
---|
| 42 |
|
---|
| 43 | l_Out.color = float4(0.0, 0.0, 0.0, 0.0);
|
---|
| 44 | float2 epsilon = float2(epsilonX, epsilonY);
|
---|
| 45 | float4 value = tex2D(indirectTexture, p_In.subTexCoord).xyzw;
|
---|
| 46 | float2 coords = p_In.texCoord - ( value.xy - epsilon );
|
---|
| 47 |
|
---|
| 48 | if (value.w != 0.0)
|
---|
| 49 | {
|
---|
| 50 | float2 unifcoord;
|
---|
| 51 | float2 scale;
|
---|
| 52 | unifcoord = (p_In.texCoord - value.xy) * float2(sourceTextureSize, sourceTextureSize);
|
---|
| 53 | //scale = coords * float2(sourceTextureSize, sourceTextureSize) / float2(sqrtNumSamples, sqrtNumSamples);
|
---|
| 54 | //float2 newcoord = float2(1.0, 1.0) - value.zw + scale;
|
---|
| 55 | //float4 diffuseColor = tex2D(sourceTexture, newcoord).xyzw;
|
---|
| 56 | float4 diffuseColor = tex2D(sourceTexture, unifcoord / float2(sqrtNumSamples, sqrtNumSamples) - epsilon).xyzw;
|
---|
| 57 |
|
---|
| 58 | // Normalized normal.
|
---|
| 59 | float3 l_Normal = normalize(p_In.normal);
|
---|
| 60 |
|
---|
| 61 | // Normalized light vector.
|
---|
| 62 | float3 l_LightVector = normalize(p_In.lightVector);
|
---|
| 63 |
|
---|
| 64 | // Normalized half angle vector.
|
---|
| 65 | float3 l_HalfVector = normalize(p_In.halfVector);
|
---|
| 66 |
|
---|
| 67 | // Ambient component
|
---|
| 68 | //------------------
|
---|
| 69 | float3 ambientColor = float3(p_Ambient.xyz);
|
---|
| 70 | if (diffuseColor.w != 0.0)
|
---|
| 71 | {
|
---|
| 72 | float4 visibility = tex2D(visibilityTexture, p_In.subTexCoord).xyzw;
|
---|
| 73 | //greyRange = greyRange - 0.20;
|
---|
| 74 | visibility.xyzw = (visibility.xyzw * float4(greyRange, greyRange, greyRange, greyRange)) - float4(absGreyMin, absGreyMin, absGreyMin, absGreyMin);
|
---|
| 75 | float e = lerp(visibility.x, visibility.y, unifcoord.x);
|
---|
| 76 | float f = lerp(visibility.w, visibility.z, unifcoord.x);
|
---|
[1546] | 77 | float g = 1.0 - clamp(lerp(e, f, unifcoord.y), 0.0, 1.0);
|
---|
[1493] | 78 | ambientColor += float3(g, g, g);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | l_Out.color.rgb = (ambientColor * diffuseColor.rgb);
|
---|
| 82 |
|
---|
| 83 | // Diffuse component
|
---|
| 84 | // -----------------
|
---|
| 85 |
|
---|
| 86 | // Angle between normal and light vector
|
---|
[1546] | 87 | float l_CosNL = clamp(dot(l_Normal, l_LightVector), 0.0, 1.0);
|
---|
[1493] | 88 |
|
---|
| 89 | // No light can reach back surfaces...
|
---|
[1546] | 90 | if (l_CosNL == 0)
|
---|
| 91 | {
|
---|
| 92 | l_CosNL = clamp(-dot(l_Normal, l_LightVector), 0.0, 1.0);
|
---|
| 93 | }
|
---|
[1493] | 94 |
|
---|
[1546] | 95 | l_Out.color.rgb += (p_Diffuse.rgb * diffuseColor.rgb) * p_LightDiffuse.rgb * l_CosNL + float3(tex2D(noisyTexture,float2(p_In.wposition.x,p_In.wposition.z)/ 10000.0).x * 0.15, tex2D(noisyTexture,float2(p_In.wposition.z,p_In.wposition.x)/10000.0).x * 0.25,0.0);
|
---|
[1493] | 96 |
|
---|
| 97 | // Specular component
|
---|
| 98 | // ------------------
|
---|
| 99 |
|
---|
| 100 | // Apply cosine power distribution around mirror direction
|
---|
[1546] | 101 | float l_CosNH = clamp(dot(l_Normal, l_HalfVector), 0.0, 1.0) + clamp(-dot(l_Normal, l_HalfVector), 0.0, 1.0);
|
---|
[1493] | 102 |
|
---|
| 103 | float l_SpecularPower = pow(l_CosNH, p_Specular.a);
|
---|
| 104 |
|
---|
| 105 | float3 l_Specular = (p_Specular.rgb * diffuseColor.rgb) * p_LightSpecular.rgb * l_SpecularPower;
|
---|
| 106 |
|
---|
| 107 | // Add specular component
|
---|
[1546] | 108 | //l_Out.color.rgb += l_Specular.rgb;
|
---|
[1493] | 109 |
|
---|
| 110 | // Modulate by light incoming power
|
---|
| 111 | l_Out.color.rgb *= p_LightPower;
|
---|
| 112 |
|
---|
| 113 | l_Out.color.a = diffuseColor.a;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | return l_Out;
|
---|
| 117 | } |
---|