source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreGames/SpaceStation/Media/GTPCaustic/GTPCaustic.hlsl @ 3255

Revision 3255, 8.0 KB checked in by szirmay, 15 years ago (diff)
Line 
1struct Shaded_OUT
2{
3 float4 vPos : POSITION;
4 float4 wNormal : TEXCOORD0;
5 float4 wPos    : TEXCOORD1;
6};
7
8/////////
9/// Photon Map shaders
10/////////
11float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
12{
13        float dist = texCUBE(dcm, float4(coord.xy, - coord.z, 0)).r;
14        if(dist == 0) dist = 1000; ///sky
15        return dist;
16}
17
18#define SECANT_ITERATIONCOUNT 2
19
20///Localization with secant iteration
21float3 Hit( float3 x, float3 R, samplerCUBE mp )
22{
23        float rl = readDistanceCubeMap( mp, R);                         // |r|
24       
25        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
26        float dun = 0, pun = ppp, dov = 0, pov = 0;
27        float dl = rl * ( 1 - ppp );                                                    // eq. 2
28        float3 l = x + R * dl;                                                                  // ray equation
29
30        // iteration
31        for( int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
32        {
33                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
34                if ( llp < 0.999f )                                                                     // undershooting
35                {
36                        dun = dl; pun = llp;                                                    // last undershooting
37                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
38                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
39                } else if ( llp > 1.001f )                                                      // overshooting
40                {
41                        dov = dl; pov = llp;                                                    // last overshooting
42                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
43                }
44                l = x + R * dl;                                                                         // ray equation
45        }
46        return l;                                                                                               // computed hit point
47}
48
49//Hit the environment sampled by DistanceMap (single refraction)
50float4 PhotonMapHitEnvPS(  Shaded_OUT IN,
51                           uniform float3 lastCenter,
52                                   uniform float3 cameraPos,
53                                   uniform float refIndex,
54                                   uniform samplerCUBE DistanceMap : register(s0)):COLOR0
55{
56       
57        float4 Color = float4(1,1,1,1);
58        float3 wNormal = normalize(IN.wNormal);
59        float3 dir;     
60        float3 cubeMapPos = IN.wPos.xyz - lastCenter;
61        float3 V = normalize(IN.wPos.xyz - cameraPos);
62               
63        float3 R = refract(V, wNormal, refIndex);               
64        if(dot(R,R) == 0)
65          R = reflect(V, wNormal);             
66
67        dir = R;                       
68        dir = Hit(cubeMapPos, R, DistanceMap);
69               
70        Color = float4(dir, 1);
71       
72        if(dot(V, wNormal)>0)
73        {
74                Color = float4(1,0,0,0);               
75        }               
76        return Color;
77}
78
79//////
80/// Caustic CubeMap shaders
81//////
82
83///point sprite caustic spots
84struct CauCube_PointSprite_VS_OUT
85{
86        float4 hPosition        : POSITION;
87        float4 texCoord         : TEXCOORD0;
88        float4 color            : COLOR0;
89        float2 r                        : TEXCOORD1;
90        float2 center           : TEXCOORD2;
91        float4 position         : TEXCOORD3;
92        float pSize                     : PSIZE;
93        float dist                      : TEXCOORD4;
94};
95
96CauCube_PointSprite_VS_OUT CauCube_PointSprite_VS(
97                                                                float4 position : POSITION,     
98                                                                float4 texCoord : TEXCOORD0,
99                                                                float4 color    : COLOR0,
100                                                                uniform float4x4 WorldView,
101                                                                uniform float4x4 Proj,
102                                                                uniform float CauSpriteSize,
103                                                        uniform sampler2D PhotonHitMap : register(s0))
104{
105  CauCube_PointSprite_VS_OUT OUT;
106     
107  float radius = CauSpriteSize;
108  OUT.pSize = radius;
109  float4 cPosition; 
110
111  float4 pos = tex2Dlod(PhotonHitMap, float4(position.x, 1.0 - position.y,0,0)).rgba;
112       
113  if(pos.a == 0)//no photon hit
114  {
115        OUT.center = 1000.0f;
116        OUT.position = OUT.hPosition = -1000.0f; //transform out of view
117  }
118  else
119  {
120                float4 wPosition = float4(normalize(pos.xyz),1);
121   
122                cPosition = mul(WorldView, wPosition);
123                OUT.center = cPosition.xy;
124                OUT.position = cPosition;       
125                OUT.hPosition = mul(Proj, cPosition);
126                OUT.center = float2(OUT.hPosition.x, OUT.hPosition.y) / OUT.hPosition.w;
127               
128  }     
129 
130  OUT.r.x = radius;
131  OUT.r.y = radius;
132  OUT.texCoord = texCoord;
133  OUT.color = float4(pos);
134  OUT.dist = length(pos.xyz);
135   
136  return OUT;
137}
138
139float4 CauCube_PointSprite_PS(CauCube_PointSprite_VS_OUT IN,
140                                                                uniform float4 CausticColor,
141                                                                uniform sampler2D intensityTex : register(s1)
142                                                                 ):COLOR
143{
144  IN.color = CausticColor;
145  float intens = tex2D(intensityTex, IN.texCoord).r;
146  //IN.color.a *= intens;
147  IN.color *= IN.color.a * intens;
148  IN.color.a = IN.dist;
149  return IN.color; 
150}
151
152
153float4 CauCube_Point_PS(CauCube_PointSprite_VS_OUT IN,
154                                                uniform float targetResolution,
155                                                float2 pixelPos : VPOS,
156                                                                uniform float4 CausticColor,
157                                                                uniform sampler2D intensityTex : register(s1)
158                                                                 ):COLOR
159{
160 IN.color = CausticColor;
161 
162 float2 screen = pixelPos / targetResolution;
163 screen *= float2(2, 2);
164 screen -= float2(1, 1);
165 screen.y *= -1;
166 float2 texCoord = (screen - IN.center) / (IN.r / targetResolution);//(-1 , 1)
167 
168 float intens = 1.0 - saturate(length(texCoord));
169 //return float4(intens,0,1,1);
170 //texCoord = (texCoord + 1.0) / 2.0; // (0, 1) 
171 //float intens = tex2D(intensityTex, texCoord).r;
172 
173 
174  //IN.color.a *= intens;
175  IN.color *= IN.color.a * intens;
176  IN.color.a = IN.dist;
177  return IN.color; 
178}
179
180//transformed caustic caster
181
182struct CauCube_Triangles_VS_OUT
183{
184        float4 hPosition        : POSITION;
185        float4 color            : COLOR0;
186        float dist              : TEXCOORD0;   
187};
188
189CauCube_Triangles_VS_OUT CauCube_Triangles_VS(
190                                        float4 position : POSITION,     
191                                                float4 texCoord : TEXCOORD0,
192                                                float4 color    : COLOR0,
193                                                uniform float WorldSize,
194                                                uniform float notUseIntensity,
195                                                uniform float4x4 WorldView,
196                                                uniform float4x4 Proj,
197                                                uniform float PhotonMapResolution,
198                                                uniform sampler2D PhotonHitMap : register(s0))
199{
200  CauCube_Triangles_VS_OUT OUT;
201     
202  float4 cPosition;
203  float pixel = 1.0 / PhotonMapResolution;   
204  float2 uv = float2(position.x, 1.0 - position.y);
205  uv += float2(pixel / 2.0,pixel / 2.0);
206  float4 pos = tex2Dlod(PhotonHitMap, float4(uv,0,0)).rgba;
207
208   if(pos.a == 0)//no photon hit
209   {
210                OUT.color = float4(0,1,0,1);           
211                OUT.hPosition = float4(1000000, 1000000, 1000000, 1);
212   }
213   else
214   {
215                float4 wPosition = float4(normalize(pos.xyz),1);   
216                cPosition = mul(WorldView, wPosition);
217                OUT.hPosition = mul(Proj, cPosition);
218               
219                float intensity = 0.25;
220               
221                //read four neighbours
222                float sumdist = 0;
223                float dist;
224                float valids = 0;
225                float4 pos1 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, pixel),0,0));
226                if(pos1.a != 0)
227                {
228                        dist = length(pos1.xyz - pos.xyz);
229                        sumdist += dist;
230                        valids++;
231                }                       
232               
233                float4 pos2 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, pixel),0,0));
234                if(pos2.a != 0)
235                {
236                        dist = length(pos2.xyz - pos.xyz);
237                        sumdist += dist;
238                        valids++;
239                }
240               
241                float4 pos3 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, -pixel),0,0));
242                if(pos3.a != 0)
243                {
244                        dist = length(pos3.xyz - pos.xyz);
245                        sumdist += dist;
246                        valids++;
247                }
248               
249                float4 pos4 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, -pixel),0,0));
250                if(pos4.a != 0)
251                {
252                        dist = length(pos4.xyz - pos.xyz);
253                        sumdist += dist;
254                        valids++;
255                }
256               
257               
258                float avrdist = sumdist / valids;
259                if(valids * avrdist == 0) avrdist = WorldSize / 1000.0;         
260               
261                intensity = WorldSize / (avrdist * avrdist * 3.14);
262                intensity = min(50, max(0,intensity));
263                //intensity = 0.8;
264       
265        //      if(dot(intensity, intensity) == 0)
266        //              OUT.color = float4(1,0,0,1);   
267        //      else
268                OUT.color = intensity;
269       
270        //      if(notUseIntensity)
271        //              OUT.color = float4(1,1,1,1);           
272        } 
273 
274   OUT.dist = length(pos.xyz);
275   //OUT.color = float4(0,0,0,0);
276   return OUT;
277}
278
279float4 CauCube_Triangles_PS(CauCube_Triangles_VS_OUT IN,
280                uniform float4 CausticColor ):COLOR
281{
282
283  return float4(IN.color.rgb * CausticColor.rgb * IN.color.a, IN.dist); 
284}
285
286///////
287/// Caustic gather shaders
288//////
289
290float4 GatherCaustic_Cube_PS(Shaded_OUT IN,
291                                uniform samplerCUBE CauCubeMap : register(s0),
292                                uniform samplerCUBE DistanceCubeMap : register(s1),
293                                uniform float3 cubeMapCameraPosition,
294                                uniform float attenuation):COLOR
295{
296        float4 Color = float4(1,1,1,1);
297       
298        float3 dir = IN.wPos - cubeMapCameraPosition;
299        float4 caustic = texCUBE(CauCubeMap, float3(dir.xy, -dir.z) );
300        float  mydist = length(dir);
301        float  dist = readDistanceCubeMap(DistanceCubeMap, dir);
302       
303        float EPSILON = 1;
304
305        //if(mydist > dist + EPSILON) caustic = 0;
306       
307        caustic.rgb *= max(attenuation - mydist, 0.0) / attenuation;
308                       
309        Color = caustic;
310        Color.a = 1;
311
312        return Color;
313}
314
Note: See TracBrowser for help on using the repository browser.