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

Revision 1691, 4.6 KB checked in by szirmay, 18 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 Position         : TEXCOORD0;
56        float4 lPosition        : TEXCOORD1;
57        float4 lVPosition       : TEXCOORD2;
58};
59
60VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
61                                                        uniform float4x4 worldViewProj,
62                                                        uniform float4x4 world,
63                                                        uniform float4x4 lightViewProj)
64{
65  VS_OUT_SHADOW OUT;
66  OUT.hPosition = mul(worldViewProj, position);
67  float4 wPos = mul(world, position);
68  OUT.lPosition = mul(lightViewProj, wPos); 
69  OUT.lVPosition = 0; 
70  OUT.Position = position; 
71  return OUT;
72}
73
74
75float4 depthShadowPS(VS_OUT_SHADOW IN,
76                                        uniform float4x4 lightViewProj,
77                                        uniform sampler2D depthShadowMap : register(s0)
78                                        ):COLOR
79{       
80        float bias = 0.001;
81        float4 light = float4(1,1,1,1);
82        float4 shadow = float4(0.25,0.25,0.25,1);
83        //float4 shadow = float4(0,0,0,1);
84       
85        if(IN.lPosition.z > 0.0)
86        {
87                float4 pos = (IN.lPosition / IN.lPosition.w);
88               
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       
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                       
104                }
105               
106        }
107        else
108                light = shadow;
109       
110        return light;   
111}
112
113
114
115VS_OUT_SHADOW distShadowVS(float4 position : POSITION, 
116                                                        uniform float4x4 worldViewProj,
117                                                        uniform float4x4 world,
118                                                        uniform float4x4 lightView,
119                                                        uniform float4x4 lightViewProj)
120{
121  VS_OUT_SHADOW OUT;
122  OUT.hPosition = mul(worldViewProj, position);
123  float4 wPos = mul(world, position);
124  OUT.lPosition = mul(lightViewProj, wPos);
125  OUT.lVPosition = mul(lightView, wPos); 
126  OUT.Position = position; 
127  return OUT;
128}
129
130
131float4 distShadowPS(VS_OUT_SHADOW IN,
132                                        uniform float4x4 lightViewProj,
133                                        uniform float lightFarPlane,
134                                        uniform sampler2D depthShadowMap : register(s0)
135                                        ):COLOR
136{       
137        float bias = 0.001;
138        float epsilon = 0.001;
139        float4 light = float4(1,1,1,1);
140        float4 shadow = float4(0.85,0.85,0.85,1);
141       
142        if(IN.lPosition.z > 0.0)
143        {
144                float4 pos = (IN.lPosition / IN.lPosition.w);
145               
146                float d = length(pos.xy);
147                       
148                light = saturate((1.0 - d)/0.05);
149               
150                if(d <= 1.0)
151                {
152                        float dist = length(IN.lVPosition.xyz) / lightFarPlane;
153                        pos.xy = (pos.xy + 1.0) / 2.0;
154                        pos.y = 1.0 - pos.y;
155                        float4 storedDist = tex2D(depthShadowMap, pos.xy);
156                        dist -= bias;
157                        float lit_factor = light * (dist <= storedDist.r);     
158         
159                        float M1 = storedDist.r;
160                        float M2 = storedDist.g;
161                        float v2 = min(max(M2 - M1 * M1, 0.0) +  epsilon, 1.0);
162                        float m_d = M1 - dist;
163                float pmax = v2 / (v2 + m_d * m_d);
164                                               
165                // Adjust the light color based on the shadow attenuation
166                light = max(lit_factor, pmax);
167       
168                }
169               
170        }
171        else
172                light = 0;     
173       
174        return shadow + (1 - shadow) * light;
175}
176
177
178/*
179float4 depthShadowPS(VS_OUT_SHADOW IN,
180                                        uniform float4x4 lightViewProj,
181                                        uniform sampler2D depthShadowMap : register(s0)
182                                        ):COLOR
183{       
184        float bias = 0.0001;
185        float4 light = float4(1,1,1,1);
186        float4 shadow = float4(0.95,0.95,0.95,1);
187        //float4 shadow = float4(0,0,0,1);
188       
189        if(IN.lPosition.z > 0.0)
190        {
191                float4 pos = (IN.lPosition / IN.lPosition.w);
192               
193                if(length(pos.xy)>1)           
194                        light = shadow;
195                else
196                {
197                        pos.xy = (pos.xy + 1.0) / 2.0;
198                        pos.y = 1.0 - pos.y;
199                        float4 storedDepth = tex2D(depthShadowMap, pos.xy);
200       
201                        if(storedDepth.r + bias < pos.z)
202                        {
203                                light = shadow;
204                        }
205                }
206               
207        }
208        else
209                light = shadow;
210       
211       
212        return light;   
213}
214*/
Note: See TracBrowser for help on using the repository browser.