source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/GTPParticles/GTP_HPS.hlsl @ 2024

Revision 2024, 10.9 KB checked in by szirmay, 17 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                                                        ) : COLOR
169{
170        float4 Color = 0;
171        float4 impostor = tex2D(Texture, IN.texCoord);
172        float f = 1.0 - impostor.g;
173        float b = impostor.r;
174        float alpha = 0;
175       
176        if(b == 0)
177                discard;       
178////altering opacity according to scene depth
179        float sceneDepth = tex2D(depthTexture, IN.screenCoord).r;
180        if(sceneDepth == 0)
181                sceneDepth = farplane;
182           
183        float size = 2.0 * IN.r;
184        float frontDepth = IN.cPosition.z + size * (f - 0.5);
185        float backDepth = IN.cPosition.z +  size * (b - 0.5);
186       
187        float far = min(sceneDepth, backDepth);
188        float near = max(frontDepth, nearplane);
189       
190        alpha = (far - near) / (backDepth - frontDepth);
191        alpha = saturate(alpha) * impostor.a;
192       
193///final color 
194        Color = float4(1, 1, 1, alpha) * IN.Color;
195    return Color;
196       
197}
198
199//////////////
200///This shader is used when rendering the illumination volume  of a HPS. Each color channel represents a layer of the volume.
201///The vertex shader decides if a color channel (ie. a layer) should be written.
202/////////////
203struct ILLUMVOLUME_VS_OUT
204{
205        float4 hPosition        : POSITION;
206        float4 texCoord         : TEXCOORD0;
207        float4 color            : COLOR0;
208};
209
210ILLUMVOLUME_VS_OUT HPS_IllumVolume_VS(float4 position : POSITION,       
211                                                                                float4 texCoord : TEXCOORD0,
212                                                                                float4 color    : COLOR0,
213                                                                                uniform float baseRadius,
214                                                                                uniform float4x4 worldView,
215                                                                                uniform float4x4 Proj)
216{
217        ILLUMVOLUME_VS_OUT OUT;
218///transform vertices to camera space and create a sprite
219   texCoord.y = 1.0 - texCoord.y;
220   float2 offset = texCoord.zw * baseRadius;
221   float4 cPosition = mul(worldView, position);
222   cPosition.xy += offset;
223///project   
224   OUT.hPosition = mul( Proj, cPosition );
225   OUT.texCoord = texCoord;
226   OUT.color = color.a;
227////identify slices and set channels to zero if needed   
228   float z = OUT.hPosition.z / OUT.hPosition.w;
229       
230   float4 planes = float4(0.33, 0.5, 0.66, 1);
231   if(z > planes.x)
232        {
233                OUT.color.r = 0;
234        }
235        if(z > planes.y)
236        {
237                OUT.color.g = 0;
238        }
239        if(z > planes.z)
240        {
241                OUT.color.b = 0;
242        }
243           
244   return OUT;
245}
246
247float4 HPS_IllumVolume_PS(ILLUMVOLUME_VS_OUT IN ,
248                uniform sampler2D colorTexture : register(s0),
249                uniform float density):COLOR //0.2
250{
251        return tex2D( colorTexture, IN.texCoord.xy).a * density * IN.color;
252}
253
254//////////////
255///This shader renders a HPS with depth calculation and with shading (light extintion).
256///The pixel shader identifies the two layers of the illumination volume between wich the shaded point is,
257/// and interpolates the stored extintion values.
258/////////////
259struct VS_OUT_DEPTH_ILLUM
260{
261        float4 hPosition        : POSITION;
262        float4 cPosition        : TEXCOORD1;
263        float2 texCoord         : TEXCOORD0;
264        float r                         : TEXCOORD2;   
265        float4 center           : TEXCOORD3;
266        float4 Color            : TEXCOORD4;   
267        float2 screenCoord      : TEXCOORD5;
268        float4 lightCoord       : TEXCOORD6;
269};
270
271VS_OUT_DEPTH_ILLUM HPS_Large_Depth_Illum_VS (float4 position : POSITION,       
272                                                                                                float4 texCoord : TEXCOORD0,
273                                                                                                float4 Color: COLOR0,
274                                                                                                uniform float width,
275                                                                                                uniform float height,
276                                                                                                uniform float baseRadius,
277                                                                                                uniform float4x4 worldView,
278                                                                                                uniform float4x4 worldViewInv,
279                                                                                                uniform float4x4 lightViewProj,
280                                                                                                uniform float4x4 Proj)
281{
282///transform vertices to camera space and create a sprite
283        VS_OUT_DEPTH_ILLUM OUT;
284        //texCoord.z = normalize(texCoord.z);
285        //texCoord.w = normalize(texCoord.w);
286    float2 offset = texCoord.zw * baseRadius;
287        OUT.r = abs(offset.x);
288        float4 cPosition;   
289        cPosition = mul(worldView, position);
290        OUT.center = cPosition;
291        cPosition.xy += offset;
292        OUT.cPosition = cPosition;
293        OUT.cPosition.z *= -1;
294        //OUT.cPosition.z = OUT.cPosition.z - OUT.r;   
295
296///     project
297    OUT.hPosition = mul( Proj, cPosition );
298/// calculate screen coordinates
299    float4 sPosition = OUT.hPosition / OUT.hPosition.w;
300    OUT.screenCoord = (sPosition.xy + float2(1.0, 1.0)) / float2(2.0, 2.0);
301    float2 halfpixel = float2(0.5 / width, 0.5 / height);
302    OUT.screenCoord += halfpixel;   
303    OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
304         
305    OUT.texCoord = texCoord.xy; 
306    OUT.Color = Color;
307///calculate ligth space coordinates
308    float4 wPosition = mul(worldViewInv, cPosition);
309    OUT.lightCoord = mul(lightViewProj, wPosition);
310        return OUT;
311}
312
313
314float4 HPS_Large_Depth_Illum_PS(VS_OUT_DEPTH_ILLUM IN,
315                                                uniform sampler2D Texture  : register(s0),
316                                                uniform sampler2D depthTexture : register(s1),
317                                                uniform sampler2D illumVolume : register(s2)                   
318                                                        ) : COLOR
319{
320        float4 Color = 0;
321        float4 impostor = tex2D(Texture, IN.texCoord);
322        float f = 1.0 - impostor.g;
323        float b = impostor.r;
324        float alpha = 0;
325       
326        if(b == 0)
327                discard;       
328////altering opacity according to scene depth
329        float sceneDepth = tex2D(depthTexture, IN.screenCoord).r;
330        if(sceneDepth == 0)
331        {
332                alpha = impostor.a;
333        }
334        else
335        {       
336                float size = 2.0 * IN.r;
337                float frontDepth = IN.cPosition.z + size * (f - 0.5);
338                float backDepth = IN.cPosition.z +  size * (b - 0.5);
339               
340                alpha = (sceneDepth - frontDepth) / (backDepth - frontDepth);
341                alpha = saturate(alpha) * impostor.a;
342        }
343///identify light volume slices and interpolation       
344        float2 lightCoord;
345        lightCoord = (IN.lightCoord.xy + float2(1.0, 1.0)) / 2.0;
346        lightCoord.y = 1.0 - lightCoord.y;
347        float z = IN.lightCoord.z / IN.lightCoord.w;
348               
349        float4 extintion = tex2D(illumVolume, lightCoord);
350       
351                float intensities[5];
352                intensities[0] = 1.0;
353                intensities[1] = extintion.r;
354                intensities[2] = extintion.g;
355                intensities[3] = extintion.b;
356                intensities[4] = extintion.a;
357                       
358                float3 start;
359                float3 end;
360                float3 temp = 1.0;
361                float t;
362               
363                float4 planes = float4(0.33, 0.5, 0.66, 1);
364                if(z < planes.x)
365                {
366                        start = intensities[0];
367                        end = intensities[1];
368                        t = z / planes.x;
369                        temp = lerp(start, end, t);                             
370                }
371                if(z > planes.x && z < planes.y)
372                {
373                        start = intensities[1];
374                        end = intensities[2];
375                        t = (z - planes.x) / (planes.y - planes.x);
376                        temp = lerp(start, end, t);                                     
377                }
378                if(z > planes.y && z < planes.z)
379                {
380                        start = intensities[2];
381                        end = intensities[3];
382                        t = (z - planes.y) / (planes.z - planes.y);
383                        temp = lerp(start, end, t);                             
384                }
385                if(z > planes.z)
386                {
387                        start = intensities[3];
388                        end = intensities[4];
389                        t = (z - planes.z) / (planes.a - planes.z);
390                        temp = lerp(start, end, t);                                     
391                }
392                IN.Color.rgb *= temp;
393///final color 
394 
395        Color = float4(1, 1, 1, alpha) * IN.Color;
396    return Color;       
397}
398
399
Note: See TracBrowser for help on using the repository browser.