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

Revision 2471, 7.0 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 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  }     
127 
128  OUT.r.x = radius;
129  OUT.r.y = radius;
130  OUT.texCoord = texCoord;
131  OUT.color = float4(pos);
132  OUT.dist = length(pos.xyz);
133   
134  return OUT;
135}
136
137float4 CauCube_PointSprite_PS(CauCube_PointSprite_VS_OUT IN,
138                                                                uniform float4 CausticColor,
139                                                                uniform sampler2D intensityTex : register(s1)
140                                                                 ):COLOR
141{
142  IN.color = CausticColor;
143  float intens = tex2D(intensityTex, IN.texCoord).r;
144  //IN.color.a *= intens;
145  IN.color *= IN.color.a * intens;
146  IN.color.a = IN.dist;
147  return IN.color; 
148}
149
150//transformed caustic caster
151
152struct CauCube_Triangles_VS_OUT
153{
154        float4 hPosition        : POSITION;
155        float4 color            : COLOR0;
156        float dist              : TEXCOORD0;   
157};
158
159CauCube_Triangles_VS_OUT CauCube_Triangles_VS(
160                                        float4 position : POSITION,     
161                                                float4 texCoord : TEXCOORD0,
162                                                float4 color    : COLOR0,
163                                                uniform float WorldSize,
164                                                uniform float notUseIntensity,
165                                                uniform float4x4 WorldView,
166                                                uniform float4x4 Proj,
167                                                uniform float PhotonMapResolution,
168                                                uniform sampler2D PhotonHitMap : register(s0))
169{
170  CauCube_Triangles_VS_OUT OUT;
171     
172  float4 cPosition;
173  float pixel = 1.0 / PhotonMapResolution;   
174  float2 uv = float2(position.x, 1.0 - position.y);
175  uv += float2(pixel / 2.0,pixel / 2.0);
176  float4 pos = tex2Dlod(PhotonHitMap, float4(uv,0,0)).rgba;
177
178   if(pos.a == 0)//no photon hit
179   {
180                OUT.color = float4(0,0,0,0);           
181                OUT.hPosition = float4(1000000, 1000000, 1000000, 1);
182   }
183   else
184   {
185                float4 wPosition = float4(normalize(pos.xyz),1);   
186                cPosition = mul(WorldView, wPosition);
187                OUT.hPosition = mul(Proj, cPosition);
188               
189                float intensity = 0.25;
190               
191                //read four neighbours
192                float sumdist = 0;
193                float dist;
194                float valids = 0;
195                float4 pos1 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, pixel),0,0));
196                if(pos1.a != 0)
197                {
198                        dist = length(pos1.xyz - pos.xyz);
199                        sumdist += dist;
200                        valids++;
201                }                       
202               
203                float4 pos2 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, pixel),0,0));
204                if(pos2.a != 0)
205                {
206                        dist = length(pos2.xyz - pos.xyz);
207                        sumdist += dist;
208                        valids++;
209                }
210               
211                float4 pos3 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, -pixel),0,0));
212                if(pos3.a != 0)
213                {
214                        dist = length(pos3.xyz - pos.xyz);
215                        sumdist += dist;
216                        valids++;
217                }
218               
219                float4 pos4 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, -pixel),0,0));
220                if(pos4.a != 0)
221                {
222                        dist = length(pos4.xyz - pos.xyz);
223                        sumdist += dist;
224                        valids++;
225                }
226               
227               
228                float avrdist = sumdist / valids;
229                if(valids == 0) avrdist = WorldSize / 1000.0;           
230               
231                intensity = WorldSize / (avrdist * avrdist * 3.14);
232                //intensity = 0.8;
233               
234                OUT.color = intensity;
235                if(notUseIntensity)
236                        OUT.color = float4(1,1,1,1);           
237        } 
238 
239   OUT.dist = length(pos.xyz);
240   return OUT;
241}
242
243float4 CauCube_Triangles_PS(CauCube_Triangles_VS_OUT IN,
244                uniform float4 CausticColor ):COLOR
245{
246
247  return float4(IN.color.rgb * CausticColor.rgb * IN.color.a, IN.dist); 
248}
249
250///////
251/// Caustic gather shaders
252//////
253
254float4 GatherCaustic_Cube_PS(Shaded_OUT IN,
255                                uniform samplerCUBE CauCubeMap : register(s0),
256                                uniform samplerCUBE DistanceCubeMap : register(s1),
257                                uniform float3 cubeMapCameraPosition,
258                                uniform float attenuation):COLOR
259{
260        float4 Color = float4(1,1,1,1);
261       
262        float3 dir = IN.wPos - cubeMapCameraPosition;
263        float4 caustic = texCUBE(CauCubeMap, float3(dir.xy, -dir.z) );
264        float  mydist = length(dir);
265        float  dist = readDistanceCubeMap(DistanceCubeMap, dir);
266       
267        float EPSILON = 1;
268
269        //if(mydist > dist + EPSILON) caustic = 0;
270       
271        caustic.rgb *= max(attenuation - mydist, 0.0) / attenuation;
272                       
273        Color = caustic;
274        Color.a = 1;
275
276        return Color;
277}
278
Note: See TracBrowser for help on using the repository browser.