source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/GTPCaustic/GTPCaustic.hlsl @ 2095

Revision 2095, 6.8 KB checked in by szirmay, 17 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 reflIndex,
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, reflIndex);             
64       
65        dir = R;                       
66        dir = Hit(cubeMapPos, R, DistanceMap);
67               
68        Color = float4(dir, 1);
69       
70        if(dot(V, wNormal)>0)
71        {
72                Color = float4(1,0,0,0);               
73        }               
74        return Color;
75}
76
77//////
78/// Caustic CubeMap shaders
79//////
80
81///point sprite caustic spots
82struct CauCube_PointSprite_VS_OUT
83{
84        float4 hPosition        : POSITION;
85        float4 texCoord         : TEXCOORD0;
86        float4 color            : COLOR0;
87        float2 r                        : TEXCOORD1;
88        float2 center           : TEXCOORD2;
89        float4 position         : TEXCOORD3;
90        float pSize                     : PSIZE;
91        float dist                      : TEXCOORD4;
92};
93
94CauCube_PointSprite_VS_OUT CauCube_PointSprite_VS(
95                                                                float4 position : POSITION,     
96                                                                float4 texCoord : TEXCOORD0,
97                                                                float4 color    : COLOR0,
98                                                                uniform float4x4 WorldView,
99                                                                uniform float4x4 Proj,
100                                                                uniform float CauSpriteSize,
101                                                        uniform sampler2D PhotonHitMap : register(s0))
102{
103  CauCube_PointSprite_VS_OUT OUT;
104     
105  float radius = CauSpriteSize;
106  OUT.pSize = radius;
107  float4 cPosition; 
108
109  float4 pos = tex2Dlod(PhotonHitMap, float4(position.x, 1.0 - position.y,0,0)).rgba;
110       
111  if(pos.a == 0)//no photon hit
112  {
113        OUT.center = 1000.0f;
114        OUT.position = OUT.hPosition = -1000.0f; //transform out of view
115  }
116  else
117  {
118                float4 wPosition = float4(normalize(pos.xyz),1);
119   
120                cPosition = mul(WorldView, wPosition);
121                OUT.center = cPosition.xy;
122                OUT.position = cPosition;       
123                OUT.hPosition = mul(Proj, cPosition);           
124  }     
125 
126  OUT.r.x = radius;
127  OUT.r.y = radius;
128  OUT.texCoord = texCoord;
129  OUT.color = float4(pos);
130  OUT.dist = length(pos.xyz);
131   
132  return OUT;
133}
134
135float4 CauCube_PointSprite_PS(CauCube_PointSprite_VS_OUT IN,
136                                                                uniform float4 CausticColor,
137                                                                uniform sampler2D intensityTex : register(s1)
138                                                                 ):COLOR
139{
140  IN.color = CausticColor;
141  float intens = tex2D(intensityTex, IN.texCoord).r;
142  IN.color.a *= intens;
143  return IN.color; 
144}
145
146//transformed caustic caster
147
148struct CauCube_Triangles_VS_OUT
149{
150        float4 hPosition        : POSITION;
151        float4 color            : COLOR0;       
152};
153
154CauCube_Triangles_VS_OUT CauCube_Triangles_VS(
155                                        float4 position : POSITION,     
156                                                float4 texCoord : TEXCOORD0,
157                                                float4 color    : COLOR0,
158                                                uniform float4x4 WorldView,
159                                                uniform float4x4 Proj,
160                                                uniform float PhotonMapResolution,
161                                                uniform sampler2D PhotonHitMap : register(s0))
162{
163  CauCube_Triangles_VS_OUT OUT;
164     
165  float4 cPosition;
166  float pixel = 1.0 / PhotonMapResolution;   
167  float2 uv = float2(position.x, 1.0 - position.y);
168  float4 pos = tex2Dlod(PhotonHitMap, float4(uv,0,0)).rgba;
169
170   if(pos.a == 0)//no photon hit
171   {
172                OUT.color = float4(0,0,0,0);           
173                OUT.hPosition = float4(0, 0, -1000000, 1);
174   }
175   else
176   {
177                float4 wPosition = float4(normalize(pos.xyz),1);   
178                cPosition = mul(WorldView, wPosition);
179                OUT.hPosition = mul(Proj, cPosition);
180               
181                float intensity = 0.25;
182               
183                //read four neighbours
184                float sumdist = 0;
185                float dist;
186                float valids = 0;
187                float4 pos1 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, pixel),0,0));
188                if(pos1.a != 0)
189                {
190                        dist = length(pos1.xyz - pos.xyz);
191                        sumdist += dist;
192                        valids++;
193                }                       
194               
195                float4 pos2 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, pixel),0,0));
196                if(pos2.a != 0)
197                {
198                        dist = length(pos2.xyz - pos.xyz);
199                        sumdist += dist;
200                        valids++;
201                }
202               
203                float4 pos3 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, -pixel),0,0));
204                if(pos3.a != 0)
205                {
206                        dist = length(pos3.xyz - pos.xyz);
207                        sumdist += dist;
208                        valids++;
209                }
210               
211                float4 pos4 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, -pixel),0,0));
212                if(pos4.a != 0)
213                {
214                        dist = length(pos4.xyz - pos.xyz);
215                        sumdist += dist;
216                        valids++;
217                }
218                //if(valids == 0) sumdist = 100000;             
219                float avrdist = sumdist / valids;
220                //float maxdist = 10;
221                //intensity = max(maxdist - avrdist, 0.0) / maxdist;
222                intensity = 5 / (avrdist * avrdist * 3.14);
223                //intensity = valids / 4.0;//avrdist;
224               
225                //OUT.color = float4(1,1,1,intensity);
226                OUT.color = intensity;
227        } 
228   
229   return OUT;
230}
231
232float4 CauCube_Triangles_PS(CauCube_Triangles_VS_OUT IN,
233                uniform float4 CausticColor ):COLOR
234{
235  return IN.color * CausticColor; 
236}
237
238///////
239/// Caustic gather shaders
240//////
241
242float4 GatherCaustic_Cube_PS(Shaded_OUT IN,
243                                uniform samplerCUBE CauCubeMap : register(s0),
244                                uniform samplerCUBE DistanceCubeMap : register(s1),
245                                uniform float3 cubeMapCameraPosition,
246                                uniform float attenuation):COLOR
247{
248        float4 Color = float4(1,1,1,1);
249       
250        float3 dir = IN.wPos - cubeMapCameraPosition;
251        float4 caustic = texCUBE(CauCubeMap, float3(dir.xy, -dir.z) );
252        float  mydist = length(dir);
253        float  dist = readDistanceCubeMap(DistanceCubeMap, dir);
254       
255        float EPSILON = 1.0;
256
257        if(mydist > dist + EPSILON) caustic = 0;
258       
259        caustic.rgb *= max(attenuation - mydist, 0.0) / attenuation;
260                       
261        Color = caustic;
262        //Color += attenuation * 0.000000000001;
263        Color.a = 1;
264
265        return Color;
266}
267
Note: See TracBrowser for help on using the repository browser.