source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_DepthShadow.hlsl @ 2181

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