[3255] | 1 | #define BiasSlope 2.0
|
---|
| 2 | #define LightSize 2.0
|
---|
| 3 | #define Bias 0.05
|
---|
| 4 | #define KernelSize 4.0
|
---|
| 5 | #define LightFov 3.14 * 0.5
|
---|
| 6 | #define ShadowIntensity 3.0
|
---|
| 7 | #define SHADOWMAP_SIZE 1024.0f
|
---|
| 8 |
|
---|
| 9 | #define shadowColor float4(0.2,0.2,0.2,1)
|
---|
| 10 |
|
---|
| 11 | uniform sampler2D ShadowMap: register(s0);
|
---|
| 12 | struct LightCPos_OUT
|
---|
| 13 | {
|
---|
| 14 | float4 VPos : POSITION;
|
---|
| 15 | float4 LightVPos : TEXCOORD0;
|
---|
| 16 | float4 LightCPos : TEXCOORD1;
|
---|
| 17 | };
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | float4 ShadowAccum_PS( LightCPos_OUT IN) : COLOR
|
---|
| 21 | {
|
---|
| 22 | IN.LightCPos.z *= -1;
|
---|
| 23 | //return float4(IN.LightCPos.xyz,1);
|
---|
| 24 |
|
---|
| 25 | float R = max( LightSize, 0.1h );
|
---|
| 26 | //BiasSlope = max( BiasSlope, 0.1h );
|
---|
| 27 | float n = R;
|
---|
| 28 | float mapsize = 2 * n * tan( LightFov / 2.0f ); // size of shadow map in world space
|
---|
| 29 | float Deltal = mapsize / SHADOWMAP_SIZE; // size of a lexel edge in world space
|
---|
| 30 |
|
---|
| 31 | float3 r = IN.LightCPos.xyz; // shaded point in light's camera space
|
---|
| 32 | float RR = R * (r.z - n) / r.z; // directional set in which the light is seen from the shaded point
|
---|
| 33 | float step = RR / KernelSize;
|
---|
| 34 | float RR2 = RR * RR;
|
---|
| 35 | float2 q = r.xy * n / r.z; // projection of r onto the shadow plane
|
---|
| 36 | float2 quv = q / mapsize;
|
---|
| 37 | quv = float2( ( 0.5 + quv.x ), ( 0.5 - quv.y ) ); // r in texture coordinates
|
---|
| 38 |
|
---|
| 39 | //return (r.z - tex2D( ShadowMap, quv).r);
|
---|
| 40 | float shadow = 0;
|
---|
| 41 | for ( int i = 0; i < KernelSize; i++ )
|
---|
| 42 | {
|
---|
| 43 | for ( int j = 0; j < KernelSize; j++ )
|
---|
| 44 | {
|
---|
| 45 | float2 uvoffset = float2( i - KernelSize / 2.0f, j - KernelSize / 2.0f ) * step / SHADOWMAP_SIZE;
|
---|
| 46 | float2 l = q + float2( uvoffset.x, -uvoffset.y ) * mapsize;
|
---|
| 47 | float cz = tex2D( ShadowMap, quv + uvoffset ).x;
|
---|
| 48 | float size = (r.z - n)/(r.z - cz) * cz / n;
|
---|
| 49 |
|
---|
| 50 | float2 lc = (n - cz)/(r.z - cz) * r.xy + size * l.xy;
|
---|
| 51 | if( cz < r.z - Bias && dot(lc - q, lc - q) < RR2 + BiasSlope )
|
---|
| 52 | shadow += size * size;
|
---|
| 53 | /*
|
---|
| 54 | if( cz < r.z - Bias)
|
---|
| 55 | shadow=1;*/
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | //return step;
|
---|
| 60 | shadow *= Deltal * Deltal * step * step / RR2 * ShadowIntensity * ShadowIntensity;
|
---|
| 61 |
|
---|
| 62 | return shadowColor + (1 - shadowColor) * (1 - shadow);
|
---|
| 63 | } |
---|