source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_HPS_Smoke_L_Depth.hlsl @ 1838

Revision 1838, 6.8 KB checked in by szirmay, 18 years ago (diff)
Line 
1#define method1
2
3struct VS_OUT
4{
5        float4 hPosition        : POSITION;
6        float4 cPosition        : TEXCOORD1;
7        float2 texCoord         : TEXCOORD0;
8        float r                         : TEXCOORD2;   
9        float4 center           : TEXCOORD3;
10        float4 Color            : TEXCOORD4;   
11        float2 screenCoord      : TEXCOORD5;
12};
13
14VS_OUT HPS_Smoke_L_Depth_VS (float4 position : POSITION,       
15                float4 texCoord : TEXCOORD0,
16                float4 Color: COLOR0,
17                uniform float baseRadius,
18                uniform float4x4 worldView,
19                uniform float4x4 Proj,
20                uniform float4x4 worldViewProj)
21{
22        VS_OUT OUT;
23        //texCoord.z = normalize(texCoord.z);
24        //texCoord.w = normalize(texCoord.w);
25    float2 offset = texCoord.zw * baseRadius;
26        OUT.r = abs(offset.x);
27        float4 cPosition;   
28        cPosition = mul(worldView, position);
29        OUT.center = cPosition;
30        cPosition.xy += offset;
31        OUT.cPosition = cPosition;
32        OUT.cPosition.z *= -1;
33        //OUT.cPosition.z = OUT.cPosition.z - OUT.r;   
34       
35    OUT.hPosition = mul( Proj, cPosition );
36    float4 sPosition = OUT.hPosition / OUT.hPosition.w;
37    OUT.screenCoord = (sPosition.xy + float2(1, 1)) / 2.0;
38    OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
39     
40    OUT.texCoord = texCoord.xy; 
41    OUT.Color = Color; 
42        return OUT;
43}
44
45
46float4 HPS_Smoke_L_Depth_PS(VS_OUT IN,
47                                                uniform float4x4 Proj,
48                                                uniform sampler2D Texture  : register(s0),
49                                                uniform sampler2D depthTexture : register(s1),
50                                                uniform float farplane,
51                                                uniform float width = 800,
52                                                uniform float height = 600
53                                                        ) : COLOR
54{
55        float4 Color = 0;
56        float4 impostor = tex2D(Texture, IN.texCoord);
57        float f = 1.0 - impostor.g;
58        float b = impostor.r;
59        float alpha = 0;
60       
61        if(b == 0)
62                discard;
63       
64        float2 halfpixel = float2(1.0 / width, 1.0 / height);
65    IN.screenCoord += halfpixel;
66   
67        float sceneDepth = tex2D(depthTexture, IN.screenCoord).r;
68        if(sceneDepth == 0)
69                //discard;
70                sceneDepth = farplane;
71       
72        float size = 2.0 * IN.r;
73        float frontDepth = IN.cPosition.z + size * (f - 0.5);
74        float backDepth = IN.cPosition.z +  size * (b - 0.5);
75       
76        alpha = (sceneDepth - frontDepth) / (backDepth - frontDepth);
77        alpha = saturate(alpha) * impostor.a;
78       
79        //Color = float4(Color.g, 0, 0, 1);
80        Color = float4(1, 1, 1, alpha) * IN.Color;
81        //Color = float4(b - 0.5, 0, 0, 1);
82        //Color = sceneDepth / farplane;
83        //Color = 1;
84        return Color;
85       
86}
87
88struct VS_OUT_ILLUM
89{
90        float4 hPosition        : POSITION;
91        float4 cPosition        : TEXCOORD1;
92        float2 texCoord         : TEXCOORD0;
93        float r                         : TEXCOORD2;   
94        float4 center           : TEXCOORD3;
95        float4 Color            : TEXCOORD4;   
96        float2 screenCoord      : TEXCOORD5;
97        float4 lightCoord       : TEXCOORD6;
98};
99
100VS_OUT_ILLUM HPS_Smoke_L_Depth_Illum_VS (float4 position : POSITION,   
101                float4 texCoord : TEXCOORD0,
102                float4 Color: COLOR0,
103                uniform float width,
104                uniform float height,
105                uniform float baseRadius,
106                uniform float4x4 worldView,
107                uniform float4x4 worldViewInv,
108                uniform float4x4 lightViewProj,
109                uniform float4x4 Proj,
110                uniform float4x4 worldViewProj)
111{
112        VS_OUT_ILLUM OUT;
113        //texCoord.z = normalize(texCoord.z);
114        //texCoord.w = normalize(texCoord.w);
115    float2 offset = texCoord.zw * baseRadius;
116        OUT.r = abs(offset.x);
117        float4 cPosition;   
118        cPosition = mul(worldView, position);
119        OUT.center = cPosition;
120        cPosition.xy += offset;
121        OUT.cPosition = cPosition;
122        OUT.cPosition.z *= -1;
123        //OUT.cPosition.z = OUT.cPosition.z - OUT.r;   
124       
125    OUT.hPosition = mul( Proj, cPosition );
126    float4 sPosition = OUT.hPosition / OUT.hPosition.w;
127    OUT.screenCoord = (sPosition.xy + float2(1.0, 1.0)) / float2(2.0, 2.0);
128    float2 halfpixel = float2(0.5 / width, 0.5 / height);
129    OUT.screenCoord += halfpixel;   
130    OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
131         
132    OUT.texCoord = texCoord.xy; 
133    OUT.Color = Color;
134    float4 wPosition = mul(worldViewInv, cPosition);
135    OUT.lightCoord = mul(lightViewProj, wPosition);
136        return OUT;
137}
138
139float ext(float z, sampler2D illumVolume, float2 lightCoord)
140{
141        float4 extintion = tex2D(illumVolume, lightCoord);
142       
143        float intensities[5];
144        intensities[0] = 1.0;
145        intensities[1] = extintion.r;
146        intensities[2] = extintion.g;
147        intensities[3] = extintion.b;
148        intensities[4] = extintion.a;
149       
150        z = 4.0 * z;
151        int start = floor(z);
152        return lerp( intensities[start], intensities[start + 1], z - ((float) start)); 
153}
154
155float4 HPS_Smoke_L_Depth_Illum_PS(VS_OUT_ILLUM IN,
156                                                uniform float4x4 Proj,
157                                                uniform sampler2D Texture  : register(s0),
158                                                uniform sampler2D depthTexture : register(s1),
159                                                uniform sampler2D illumVolume : register(s2),
160                                                uniform float farplane                                         
161                                                        ) : COLOR
162{
163        float4 Color = 0;
164        float4 impostor = tex2D(Texture, IN.texCoord);
165        float f = 1.0 - impostor.g;
166        float b = impostor.r;
167        float alpha = 0;
168       
169        if(b == 0)
170                discard;       
171
172        float sceneDepth = tex2D(depthTexture, IN.screenCoord).r;
173        if(sceneDepth == 0)
174        {
175                alpha = impostor.a;
176        }
177        else
178        {       
179                float size = 2.0 * IN.r;
180                float frontDepth = IN.cPosition.z + size * (f - 0.5);
181                float backDepth = IN.cPosition.z +  size * (b - 0.5);
182               
183                alpha = (sceneDepth - frontDepth) / (backDepth - frontDepth);
184                alpha = saturate(alpha) * impostor.a;
185        }
186       
187        float2 lightCoord;
188        lightCoord = (IN.lightCoord.xy + float2(1.0, 1.0)) / 2.0;
189        lightCoord.y = 1.0 - lightCoord.y;
190        float z = IN.lightCoord.z / IN.lightCoord.w;
191       
192#ifdef method1
193       
194                float4 extintion = tex2D(illumVolume, lightCoord);
195       
196                float intensities[5];
197                intensities[0] = 1.0;
198                intensities[1] = extintion.r;
199                intensities[2] = extintion.g;
200                intensities[3] = extintion.b;
201                intensities[4] = extintion.a;
202                       
203                float3 start;
204                float3 end;
205                float3 temp = 1.0;
206                float t;
207               
208                float4 planes = float4(0.33, 0.5, 0.66, 1);
209                if(z < planes.x)
210                {
211                        start = intensities[0];
212                        end = intensities[1];
213                        t = z / planes.x;
214                        temp = lerp(start, end, t);                             
215                }
216                if(z > planes.x && z < planes.y)
217                {
218                        start = intensities[1];
219                        end = intensities[2];
220                        t = (z - planes.x) / (planes.y - planes.x);
221                        temp = lerp(start, end, t);                                     
222                }
223                if(z > planes.y && z < planes.z)
224                {
225                        start = intensities[2];
226                        end = intensities[3];
227                        t = (z - planes.y) / (planes.z - planes.y);
228                        temp = lerp(start, end, t);                             
229                }
230                if(z > planes.z)
231                {
232                        start = intensities[3];
233                        end = intensities[4];
234                        t = (z - planes.z) / (planes.a - planes.z);
235                        temp = lerp(start, end, t);                                     
236                }
237                IN.Color.rgb *= temp;
238
239#else
240       
241        float4 extintion = tex3D(illumVolume, float3(lightCoord, z));
242        IN.Color.rgb *= extintion;
243        /*
244        float intensities[5];
245        intensities[0] = 1.0;
246        intensities[1] = extintion.r;
247        intensities[2] = extintion.g;
248        intensities[3] = extintion.b;
249        intensities[4] = extintion.a;
250       
251        z = 4.0 * z;
252        int start = floor(z);
253        float ext = lerp( intensities[start], intensities[start + 1], z - ((float) start));     
254        IN.Color.rgb *= ext;*/
255
256#endif
257       
258        Color = float4(1, 1, 1, alpha) * IN.Color;
259      return Color;
260       
261}
262
263
Note: See TracBrowser for help on using the repository browser.