[777] | 1 | ///////depth map
|
---|
| 2 | struct VS_OUT
|
---|
| 3 | {
|
---|
| 4 | float4 hPosition : POSITION;
|
---|
[1118] | 5 | float4 Position : TEXCOORD0;
|
---|
[777] | 6 | };
|
---|
| 7 |
|
---|
[1118] | 8 | VS_OUT DepthVS(float4 position : POSITION,
|
---|
[1131] | 9 | uniform float4x4 worldViewProj)
|
---|
[777] | 10 | {
|
---|
| 11 |
|
---|
| 12 | VS_OUT OUT;
|
---|
[1118] | 13 |
|
---|
[777] | 14 | OUT.hPosition = mul(worldViewProj, position);
|
---|
[1131] | 15 | OUT.Position = mul(worldViewProj, position);
|
---|
[1118] | 16 |
|
---|
[777] | 17 | return OUT;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[1118] | 20 | //for directional light
|
---|
[777] | 21 | float4 DepthPS(VS_OUT IN ):COLOR
|
---|
| 22 | {
|
---|
[1629] | 23 | //return 1;
|
---|
[1118] | 24 | float4 pos = (IN.Position / IN.Position.w);
|
---|
[1629] | 25 | //pos = (pos +1.0 ) / 2.0;
|
---|
[1131] | 26 | return float4(pos.z, pos.z * pos.z, 1, 1);
|
---|
[777] | 27 | }
|
---|
| 28 |
|
---|
[1671] | 29 | VS_OUT DepthDistVS(float4 position : POSITION,
|
---|
| 30 | uniform float4x4 worldViewProj,
|
---|
| 31 | uniform float4x4 worldView)
|
---|
| 32 | {
|
---|
| 33 |
|
---|
| 34 | VS_OUT OUT;
|
---|
| 35 |
|
---|
| 36 | OUT.hPosition = mul(worldViewProj, position);
|
---|
| 37 | OUT.Position = mul(worldView, position);
|
---|
| 38 |
|
---|
| 39 | return OUT;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | float4 DepthDistPS(VS_OUT IN,
|
---|
| 43 | uniform float farPlane ):COLOR
|
---|
| 44 | {
|
---|
| 45 | float dist = length(IN.Position.xyz) / farPlane;
|
---|
| 46 | return float4(dist, dist * dist, 1, 1);
|
---|
| 47 | //return farPlane;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[777] | 50 | ///////////////Shadow
|
---|
| 51 |
|
---|
| 52 | struct VS_OUT_SHADOW
|
---|
| 53 | {
|
---|
| 54 | float4 hPosition : POSITION;
|
---|
| 55 | float4 Position : TEXCOORD0;
|
---|
[1118] | 56 | float4 lPosition : TEXCOORD1;
|
---|
[1671] | 57 | float4 lVPosition : TEXCOORD2;
|
---|
[777] | 58 | };
|
---|
| 59 |
|
---|
| 60 | VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
|
---|
[1118] | 61 | uniform float4x4 worldViewProj,
|
---|
[1131] | 62 | uniform float4x4 world,
|
---|
[1118] | 63 | uniform float4x4 lightViewProj)
|
---|
[777] | 64 | {
|
---|
| 65 | VS_OUT_SHADOW OUT;
|
---|
| 66 | OUT.hPosition = mul(worldViewProj, position);
|
---|
[1131] | 67 | float4 wPos = mul(world, position);
|
---|
| 68 | OUT.lPosition = mul(lightViewProj, wPos);
|
---|
[1671] | 69 | OUT.lVPosition = 0;
|
---|
[1118] | 70 | OUT.Position = position;
|
---|
[777] | 71 | return OUT;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1671] | 74 |
|
---|
[777] | 75 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
| 76 | uniform float4x4 lightViewProj,
|
---|
[1118] | 77 | uniform sampler2D depthShadowMap : register(s0)
|
---|
| 78 | ):COLOR
|
---|
| 79 | {
|
---|
[1638] | 80 | float bias = 0.001;
|
---|
[1118] | 81 | float4 light = float4(1,1,1,1);
|
---|
[1691] | 82 | float4 shadow = float4(0.25,0.25,0.25,1);
|
---|
[1638] | 83 | //float4 shadow = float4(0,0,0,1);
|
---|
[777] | 84 |
|
---|
[1638] | 85 | if(IN.lPosition.z > 0.0)
|
---|
[1131] | 86 | {
|
---|
[1638] | 87 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
[1131] | 88 |
|
---|
[1638] | 89 | if(length(pos.xy)>1)
|
---|
| 90 | light = shadow;
|
---|
| 91 | else
|
---|
| 92 | {
|
---|
| 93 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
| 94 | pos.y = 1.0 - pos.y;
|
---|
| 95 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
| 96 |
|
---|
[1671] | 97 |
|
---|
| 98 | float M1 = storedDepth.r;
|
---|
| 99 | float M2 = storedDepth.g;
|
---|
| 100 | float v2 = M2 - M1 * M1;
|
---|
| 101 | float pmax = v2 / (v2 + pow(M1 - pos.z, 2));
|
---|
| 102 | light = saturate(shadow + (1 - shadow) * pmax);
|
---|
| 103 |
|
---|
[1638] | 104 | }
|
---|
| 105 |
|
---|
[1131] | 106 | }
|
---|
[1638] | 107 | else
|
---|
| 108 | light = shadow;
|
---|
| 109 |
|
---|
[1118] | 110 | return light;
|
---|
[777] | 111 | }
|
---|
| 112 |
|
---|
[1629] | 113 |
|
---|
[1671] | 114 |
|
---|
[2054] | 115 |
|
---|
[1671] | 116 | VS_OUT_SHADOW distShadowVS(float4 position : POSITION,
|
---|
| 117 | uniform float4x4 worldViewProj,
|
---|
| 118 | uniform float4x4 world,
|
---|
| 119 | uniform float4x4 lightView,
|
---|
| 120 | uniform float4x4 lightViewProj)
|
---|
| 121 | {
|
---|
| 122 | VS_OUT_SHADOW OUT;
|
---|
| 123 | OUT.hPosition = mul(worldViewProj, position);
|
---|
| 124 | float4 wPos = mul(world, position);
|
---|
| 125 | OUT.lPosition = mul(lightViewProj, wPos);
|
---|
| 126 | OUT.lVPosition = mul(lightView, wPos);
|
---|
| 127 | OUT.Position = position;
|
---|
| 128 | return OUT;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[2095] | 131 |
|
---|
[1671] | 132 | float4 distShadowPS(VS_OUT_SHADOW IN,
|
---|
| 133 | uniform float4x4 lightViewProj,
|
---|
| 134 | uniform float lightFarPlane,
|
---|
| 135 | uniform sampler2D depthShadowMap : register(s0)
|
---|
| 136 | ):COLOR
|
---|
| 137 | {
|
---|
| 138 | float bias = 0.001;
|
---|
| 139 | float epsilon = 0.001;
|
---|
| 140 | float4 light = float4(1,1,1,1);
|
---|
| 141 | float4 shadow = float4(0.85,0.85,0.85,1);
|
---|
| 142 |
|
---|
| 143 | if(IN.lPosition.z > 0.0)
|
---|
| 144 | {
|
---|
| 145 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
| 146 |
|
---|
| 147 | float d = length(pos.xy);
|
---|
| 148 |
|
---|
| 149 | light = saturate((1.0 - d)/0.05);
|
---|
| 150 |
|
---|
| 151 | if(d <= 1.0)
|
---|
| 152 | {
|
---|
| 153 | float dist = length(IN.lVPosition.xyz) / lightFarPlane;
|
---|
| 154 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
| 155 | pos.y = 1.0 - pos.y;
|
---|
| 156 | float4 storedDist = tex2D(depthShadowMap, pos.xy);
|
---|
| 157 | dist -= bias;
|
---|
| 158 | float lit_factor = light * (dist <= storedDist.r);
|
---|
| 159 |
|
---|
| 160 | float M1 = storedDist.r;
|
---|
| 161 | float M2 = storedDist.g;
|
---|
| 162 | float v2 = min(max(M2 - M1 * M1, 0.0) + epsilon, 1.0);
|
---|
| 163 | float m_d = M1 - dist;
|
---|
| 164 | float pmax = v2 / (v2 + m_d * m_d);
|
---|
| 165 |
|
---|
| 166 | // Adjust the light color based on the shadow attenuation
|
---|
| 167 | light = max(lit_factor, pmax);
|
---|
| 168 |
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | }
|
---|
| 172 | else
|
---|
| 173 | light = 0;
|
---|
| 174 |
|
---|
| 175 | return shadow + (1 - shadow) * light;
|
---|
[2095] | 176 | }
|
---|
| 177 | /*
|
---|
[2054] | 178 | float4 distShadowPS(VS_OUT_SHADOW IN,
|
---|
| 179 | uniform float4x4 lightViewProj,
|
---|
| 180 | uniform float lightFarPlane,
|
---|
| 181 | uniform sampler2D depthShadowMap : register(s0)
|
---|
| 182 | ):COLOR
|
---|
| 183 | {
|
---|
| 184 | float bias = 0.001;
|
---|
| 185 | float epsilon = 0.001;
|
---|
| 186 | float4 light = float4(1,1,1,1);
|
---|
[2138] | 187 | float4 shadow = float4(0.65,0.65,0.65,1);
|
---|
[2054] | 188 |
|
---|
| 189 | if(IN.lPosition.z > 0.0)
|
---|
| 190 | {
|
---|
| 191 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
| 192 |
|
---|
| 193 | float d = length(pos.xy);
|
---|
| 194 |
|
---|
| 195 | light = saturate((1.0 - d)/0.05);
|
---|
| 196 |
|
---|
| 197 | if(d <= 1.0)
|
---|
| 198 | {
|
---|
| 199 | float dist = length(IN.lVPosition.xyz) / lightFarPlane;
|
---|
| 200 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
| 201 | pos.y = 1.0 - pos.y;
|
---|
| 202 | float4 storedDist = tex2D(depthShadowMap, pos.xy);
|
---|
| 203 |
|
---|
| 204 | light = storedDist.r + dist*0.000000000001;
|
---|
| 205 |
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | }
|
---|
| 209 | else
|
---|
| 210 | light = 0;
|
---|
| 211 |
|
---|
| 212 | return shadow + (1 - shadow) * light;
|
---|
[1671] | 213 | }
|
---|
[2095] | 214 | */
|
---|
[1671] | 215 | /*
|
---|
[1629] | 216 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
| 217 | uniform float4x4 lightViewProj,
|
---|
| 218 | uniform sampler2D depthShadowMap : register(s0)
|
---|
| 219 | ):COLOR
|
---|
| 220 | {
|
---|
| 221 | float bias = 0.0001;
|
---|
| 222 | float4 light = float4(1,1,1,1);
|
---|
[1638] | 223 | float4 shadow = float4(0.95,0.95,0.95,1);
|
---|
[1629] | 224 | //float4 shadow = float4(0,0,0,1);
|
---|
| 225 |
|
---|
| 226 | if(IN.lPosition.z > 0.0)
|
---|
| 227 | {
|
---|
| 228 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
[1638] | 229 |
|
---|
| 230 | if(length(pos.xy)>1)
|
---|
| 231 | light = shadow;
|
---|
| 232 | else
|
---|
| 233 | {
|
---|
| 234 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
| 235 | pos.y = 1.0 - pos.y;
|
---|
| 236 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
[1629] | 237 |
|
---|
[1638] | 238 | if(storedDepth.r + bias < pos.z)
|
---|
| 239 | {
|
---|
| 240 | light = shadow;
|
---|
| 241 | }
|
---|
[1629] | 242 | }
|
---|
[1638] | 243 |
|
---|
[1629] | 244 | }
|
---|
[1638] | 245 | else
|
---|
| 246 | light = shadow;
|
---|
[1629] | 247 |
|
---|
| 248 |
|
---|
| 249 | return light;
|
---|
| 250 | }
|
---|
[1671] | 251 | */ |
---|