source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreGames/MoriaBattle/Media/GTPParticles/GTP_HPS.hlsl @ 3255

Revision 3255, 11.0 KB checked in by szirmay, 15 years ago (diff)
Line 
1struct HPS_VS_OUT
2{
3        float4 hPosition        : POSITION;
4        float4 cPosition        : TEXCOORD1;
5        float2 texCoord         : TEXCOORD0;
6        float r                         : TEXCOORD2;   
7        float4 center           : TEXCOORD3;
8        float4 Color            : TEXCOORD4;
9};
10
11//////////////
12///This shader used when rendering an image of the small particle system of a HPS.
13///It stores max distance from the camera in the red channel, one minus min distance in the green channel
14/// and the opacity in the alpha channel.
15/////////////
16HPS_VS_OUT GTP_HPS_Small_VS (float4 position : POSITION,       
17                                                                        float4 texCoord : TEXCOORD0,
18                                                                        float4 Color: COLOR0,
19                                                                        uniform float4x4 worldView,
20                                                                        uniform float4x4 Proj)
21{
22        HPS_VS_OUT OUT;
23 ///transform vertices to camera space and create a sprite
24        float2 offset = texCoord.zw;
25        float4 cPosition;   
26        cPosition = mul(worldView, position);
27        OUT.center = cPosition;
28        cPosition.xy += offset;                         
29        OUT.cPosition = cPosition;
30        OUT.r = abs(texCoord.z);               
31 ///projection
32    OUT.hPosition = mul( Proj, cPosition );
33     
34    OUT.texCoord = texCoord.xy;
35    OUT.Color = Color;   
36        return OUT;
37}
38
39
40float4 GTP_HPS_Small_PS(HPS_VS_OUT IN,
41                                                uniform float4x4 Proj,
42                                                uniform sampler2D Texture       ) : COLOR
43{
44        float4 Color = 0;
45        float f = 0;
46        float b = 1;
47        float alpha = 0;
48
49///calculate back and front distances           
50        float d = length( IN.cPosition.xy- IN.center.xy);
51       
52        if( d < IN.r )
53        {       
54         float w = sqrt( IN.r * IN.r - d * d );
55         float4 fPosition = IN.cPosition;
56         float4 bPosition = IN.cPosition;
57         fPosition.z -= w;
58         bPosition.z += w;
59         fPosition = mul(Proj, fPosition);
60         bPosition = mul(Proj, bPosition);
61         f = fPosition.z / fPosition.w;
62         b = bPosition.z / bPosition.w;
63         
64        // alpha = pow(w / IN.r, 4);           
65        }
66        else
67         discard;
68       
69        Color.r = b;
70        Color.g = 1 - f;
71       
72        alpha = 1;
73        Color.a = alpha * IN.Color.a;
74        Color.a *= tex2D(Texture, IN.texCoord).r * 0.7;
75       
76       
77        return Color;
78}
79
80//////////////
81///This shader renders a HPS, the image of the smaller system is passed to the fragment shader
82/////////////
83HPS_VS_OUT HPS_Large_VS (float4 position : POSITION,   
84                                                        float4 texCoord : TEXCOORD0,
85                                                        float4 Color: COLOR0,
86                                                        uniform float baseRadius,
87                                                        uniform float4x4 worldView,
88                                                        uniform float4x4 Proj)
89{
90///transform vertices to camera space and create a sprite
91        HPS_VS_OUT OUT;
92    float2 offset = texCoord.zw * baseRadius;
93        float4 cPosition;   
94        cPosition = mul(worldView, position);
95        OUT.center = cPosition;
96        cPosition.xy += offset;                         
97        OUT.cPosition = cPosition;
98        OUT.r = abs(offset.x);         
99 ///project
100    OUT.hPosition = mul( Proj, cPosition );
101     
102    OUT.texCoord = texCoord.xy; 
103    OUT.Color = Color; 
104        return OUT;
105}
106
107
108float4 HPS_Large_PS(HPS_VS_OUT IN,
109                                        uniform sampler2D Texture       ) : COLOR
110{
111        float4 Color = 0;
112        Color = tex2D(Texture, IN.texCoord);
113        Color = float4(1, 1, 1, Color.a) * IN.Color;
114        return Color;
115}
116
117//////////////
118///This shader renders a HPS with depth calculation, the scene distance map is passed to the fragment shader
119/////////////
120struct HPS_DEPTH_VS_OUT
121{
122        float4 hPosition        : POSITION;
123        float4 cPosition        : TEXCOORD1;
124        float2 texCoord         : TEXCOORD0;
125        float r                         : TEXCOORD2;   
126        float4 center           : TEXCOORD3;
127        float4 Color            : TEXCOORD4;   
128        float2 screenCoord      : TEXCOORD5;
129};
130
131HPS_DEPTH_VS_OUT HPS_Large_Depth_VS (float4 position : POSITION,       
132                                                                        float4 texCoord : TEXCOORD0,
133                                                                        float4 Color: COLOR0,
134                                                                        uniform float baseRadius,
135                                                                        uniform float4x4 worldView,
136                                                                        uniform float4x4 Proj)
137{
138///transform vertices to camera space and create a sprite
139        HPS_DEPTH_VS_OUT OUT;
140        float2 offset = texCoord.zw * baseRadius;
141        OUT.r = abs(offset.x);
142        float4 cPosition;   
143        cPosition = mul(worldView, position);
144        OUT.center = cPosition;
145        cPosition.xy += offset;
146        OUT.cPosition = cPosition;
147        OUT.cPosition.z += baseRadius;
148        OUT.cPosition.z *= -1;
149///projection
150    OUT.hPosition = mul( Proj, cPosition );
151///calculate screen space coordinates   
152    float4 sPosition = OUT.hPosition / OUT.hPosition.w;
153    OUT.screenCoord = (sPosition.xy + float2(1, 1)) / 2.0;
154    OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
155     
156    OUT.texCoord = texCoord.xy; 
157    OUT.Color = Color; 
158        return OUT;
159}
160
161
162float4 HPS_Large_Depth_PS(HPS_DEPTH_VS_OUT IN,
163                                                uniform sampler2D Texture  : register(s0),
164                                                uniform sampler2D depthTexture : register(s1),
165                                                uniform sampler2D illumVolume : register(s2),
166                                                uniform float farplane,
167                                                uniform float nearplane,
168                                                uniform float4 color                                           
169                                                        ) : COLOR
170{
171        float4 Color = IN.Color * color;
172        float4 impostor = tex2D(Texture, IN.texCoord);
173        float f = 1.0 - impostor.g;
174        float b = impostor.r;
175        float alpha = 0;
176       
177        if(b == 0)
178                discard;       
179////altering opacity according to scene depth
180        float sceneDepth = tex2D(depthTexture, IN.screenCoord).r;
181        if(sceneDepth == 0)
182                sceneDepth = farplane;
183           
184        float size = 2.0 * IN.r;
185        float frontDepth = IN.cPosition.z + size * (f - 0.5);
186        float backDepth = IN.cPosition.z +  size * (b - 0.5);
187       
188        float far = min(sceneDepth, backDepth);
189        float near = max(frontDepth, nearplane);
190       
191        alpha = (far - near) / (backDepth - frontDepth);
192        alpha = saturate(alpha) * impostor.a;
193       
194///final color 
195        Color.a *= alpha;
196
197    return Color;
198       
199}
200
201//////////////
202///This shader is used when rendering the illumination volume  of a HPS. Each color channel represents a layer of the volume.
203///The vertex shader decides if a color channel (ie. a layer) should be written.
204/////////////
205struct ILLUMVOLUME_VS_OUT
206{
207        float4 hPosition        : POSITION;
208        float4 texCoord         : TEXCOORD0;
209        float4 color            : COLOR0;
210};
211
212ILLUMVOLUME_VS_OUT HPS_IllumVolume_VS(float4 position : POSITION,       
213                                                                                float4 texCoord : TEXCOORD0,
214                                                                                float4 color    : COLOR0,
215                                                                                uniform float baseRadius,
216                                                                                uniform float4x4 worldView,
217                                                                                uniform float4x4 Proj)
218{
219        ILLUMVOLUME_VS_OUT OUT;
220///transform vertices to camera space and create a sprite
221   texCoord.y = 1.0 - texCoord.y;
222   float2 offset = texCoord.zw * baseRadius;
223   float4 cPosition = mul(worldView, position);
224   cPosition.xy += offset;
225///project   
226   OUT.hPosition = mul( Proj, cPosition );
227   OUT.texCoord = texCoord;
228   OUT.color = color.a;
229////identify slices and set channels to zero if needed   
230   float z = OUT.hPosition.z / OUT.hPosition.w;
231       
232   float4 planes = float4(0.33, 0.5, 0.66, 1);
233   if(z > planes.x)
234        {
235                OUT.color.r = 0;
236        }
237        if(z > planes.y)
238        {
239                OUT.color.g = 0;
240        }
241        if(z > planes.z)
242        {
243                OUT.color.b = 0;
244        }
245           
246   return OUT;
247}
248
249float4 HPS_IllumVolume_PS(ILLUMVOLUME_VS_OUT IN ,
250                uniform sampler2D colorTexture : register(s0),
251                uniform float density):COLOR //0.2
252{
253        return tex2D( colorTexture, IN.texCoord.xy).a * density * IN.color;
254}
255
256//////////////
257///This shader renders a HPS with depth calculation and with shading (light extintion).
258///The pixel shader identifies the two layers of the illumination volume between wich the shaded point is,
259/// and interpolates the stored extintion values.
260/////////////
261struct VS_OUT_DEPTH_ILLUM
262{
263        float4 hPosition        : POSITION;
264        float4 cPosition        : TEXCOORD1;
265        float2 texCoord         : TEXCOORD0;
266        float r                         : TEXCOORD2;   
267        float4 center           : TEXCOORD3;
268        float4 Color            : TEXCOORD4;   
269        float2 screenCoord      : TEXCOORD5;
270        float4 lightCoord       : TEXCOORD6;
271};
272
273VS_OUT_DEPTH_ILLUM HPS_Large_Depth_Illum_VS (float4 position : POSITION,       
274                                                                                                float4 texCoord : TEXCOORD0,
275                                                                                                float4 Color: COLOR0,
276                                                                                                uniform float width,
277                                                                                                uniform float height,
278                                                                                                uniform float baseRadius,
279                                                                                                uniform float4x4 worldView,
280                                                                                                uniform float4x4 worldViewInv,
281                                                                                                uniform float4x4 lightViewProj,
282                                                                                                uniform float4x4 Proj)
283{
284///transform vertices to camera space and create a sprite
285        VS_OUT_DEPTH_ILLUM OUT;
286        //texCoord.z = normalize(texCoord.z);
287        //texCoord.w = normalize(texCoord.w);
288    float2 offset = texCoord.zw * baseRadius;
289        OUT.r = abs(offset.x);
290        float4 cPosition;   
291        cPosition = mul(worldView, position);
292        OUT.center = cPosition;
293        cPosition.xy += offset;
294        OUT.cPosition = cPosition;
295        OUT.cPosition.z *= -1;
296        //OUT.cPosition.z = OUT.cPosition.z - OUT.r;   
297
298///     project
299    OUT.hPosition = mul( Proj, cPosition );
300/// calculate screen coordinates
301    float4 sPosition = OUT.hPosition / OUT.hPosition.w;
302    OUT.screenCoord = (sPosition.xy + float2(1.0, 1.0)) / float2(2.0, 2.0);
303    float2 halfpixel = float2(0.5 / width, 0.5 / height);
304    OUT.screenCoord += halfpixel;   
305    OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
306         
307    OUT.texCoord = texCoord.xy; 
308    OUT.Color = Color;
309///calculate ligth space coordinates
310    float4 wPosition = mul(worldViewInv, cPosition);
311    OUT.lightCoord = mul(lightViewProj, wPosition);
312        return OUT;
313}
314
315
316float4 HPS_Large_Depth_Illum_PS(VS_OUT_DEPTH_ILLUM IN,
317                                                uniform float nearplane,
318                                                uniform sampler2D Texture  : register(s0),
319                                                uniform sampler2D depthTexture : register(s1),
320                                                uniform sampler2D illumVolume : register(s2)                   
321                                                        ) : COLOR
322{
323        float4 Color = 0;
324        float4 impostor = tex2D(Texture, IN.texCoord);
325        float f = 1.0 - impostor.g;
326        float b = impostor.r;
327        float alpha = 0;
328       
329        if(b == 0)
330                discard;       
331////altering opacity according to scene depth
332        float sceneDepth = tex2D(depthTexture, IN.screenCoord).r;
333        if(sceneDepth == 0)
334        {
335                alpha = impostor.a;
336        }
337        else
338        {       
339                float size = 2.0 * IN.r;
340                float frontDepth = IN.cPosition.z + size * (f - 0.5);
341                float backDepth = IN.cPosition.z +  size * (b - 0.5);
342                float B = min(sceneDepth, backDepth);
343                float F = max(frontDepth, 0.01);
344                float ds = B - F;
345                alpha = ds / (backDepth - frontDepth);
346                alpha =saturate(alpha) * impostor.a;
347        }
348///identify light volume slices and interpolation       
349        float2 lightCoord;
350        lightCoord = (IN.lightCoord.xy + float2(1.0, 1.0)) / 2.0;
351        lightCoord.y = 1.0 - lightCoord.y;
352        float z = IN.lightCoord.z / IN.lightCoord.w;
353               
354        float4 extintion = tex2D(illumVolume, lightCoord);
355       
356                float intensities[5];
357                intensities[0] = 1.0;
358                intensities[1] = extintion.r;
359                intensities[2] = extintion.g;
360                intensities[3] = extintion.b;
361                intensities[4] = extintion.a;
362                       
363                float3 start;
364                float3 end;
365                float3 temp = 1.0;
366                float t;
367               
368                float4 planes = float4(0.33, 0.5, 0.66, 1);
369                if(z < planes.x)
370                {
371                        start = intensities[0];
372                        end = intensities[1];
373                        t = z / planes.x;
374                        temp = lerp(start, end, t);                             
375                }
376                if(z > planes.x && z < planes.y)
377                {
378                        start = intensities[1];
379                        end = intensities[2];
380                        t = (z - planes.x) / (planes.y - planes.x);
381                        temp = lerp(start, end, t);                                     
382                }
383                if(z > planes.y && z < planes.z)
384                {
385                        start = intensities[2];
386                        end = intensities[3];
387                        t = (z - planes.y) / (planes.z - planes.y);
388                        temp = lerp(start, end, t);                             
389                }
390                if(z > planes.z)
391                {
392                        start = intensities[3];
393                        end = intensities[4];
394                        t = (z - planes.z) / (planes.a - planes.z);
395                        temp = lerp(start, end, t);                                     
396                }
397                IN.Color.rgb *= temp;
398///final color 
399 
400        Color = float4(1, 1, 1, alpha) * IN.Color;
401    return Color;       
402}
Note: See TracBrowser for help on using the repository browser.