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