1 | struct Shaded_OUT
|
---|
2 | {
|
---|
3 | float4 vPos : POSITION;
|
---|
4 | float4 wNormal : TEXCOORD0;
|
---|
5 | float4 wPos : TEXCOORD1;
|
---|
6 | };
|
---|
7 |
|
---|
8 | /////////
|
---|
9 | /// Photon Map shaders
|
---|
10 | /////////
|
---|
11 | float 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
|
---|
21 | float3 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)
|
---|
50 | float4 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 |
|
---|
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
|
---|
82 | struct 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 |
|
---|
94 | CauCube_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 |
|
---|
135 | float4 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 |
|
---|
148 | struct CauCube_Triangles_VS_OUT
|
---|
149 | {
|
---|
150 | float4 hPosition : POSITION;
|
---|
151 | float4 color : COLOR0;
|
---|
152 | };
|
---|
153 |
|
---|
154 | CauCube_Triangles_VS_OUT CauCube_Triangles_VS(
|
---|
155 | float4 position : POSITION,
|
---|
156 | float4 texCoord : TEXCOORD0,
|
---|
157 | float4 color : COLOR0,
|
---|
158 | uniform float WorldSize,
|
---|
159 | uniform float notUseIntensity,
|
---|
160 | uniform float4x4 WorldView,
|
---|
161 | uniform float4x4 Proj,
|
---|
162 | uniform float PhotonMapResolution,
|
---|
163 | uniform sampler2D PhotonHitMap : register(s0))
|
---|
164 | {
|
---|
165 | CauCube_Triangles_VS_OUT OUT;
|
---|
166 |
|
---|
167 | float4 cPosition;
|
---|
168 | float pixel = 1.0 / PhotonMapResolution;
|
---|
169 | float2 uv = float2(position.x, 1.0 - position.y);
|
---|
170 | uv += float2(pixel / 2.0,pixel / 2.0);
|
---|
171 | float4 pos = tex2Dlod(PhotonHitMap, float4(uv,0,0)).rgba;
|
---|
172 |
|
---|
173 | if(pos.a == 0)//no photon hit
|
---|
174 | {
|
---|
175 | OUT.color = float4(0,0,0,0);
|
---|
176 | OUT.hPosition = float4(1000000, 1000000, 1000000, 1);
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | float4 wPosition = float4(normalize(pos.xyz),1);
|
---|
181 | cPosition = mul(WorldView, wPosition);
|
---|
182 | OUT.hPosition = mul(Proj, cPosition);
|
---|
183 |
|
---|
184 | float intensity = 0.25;
|
---|
185 |
|
---|
186 | //read four neighbours
|
---|
187 | float sumdist = 0;
|
---|
188 | float dist;
|
---|
189 | float valids = 0;
|
---|
190 | float4 pos1 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, pixel),0,0));
|
---|
191 | if(pos1.a != 0)
|
---|
192 | {
|
---|
193 | dist = length(pos1.xyz - pos.xyz);
|
---|
194 | sumdist += dist;
|
---|
195 | valids++;
|
---|
196 | }
|
---|
197 |
|
---|
198 | float4 pos2 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, pixel),0,0));
|
---|
199 | if(pos2.a != 0)
|
---|
200 | {
|
---|
201 | dist = length(pos2.xyz - pos.xyz);
|
---|
202 | sumdist += dist;
|
---|
203 | valids++;
|
---|
204 | }
|
---|
205 |
|
---|
206 | float4 pos3 = tex2Dlod(PhotonHitMap, float4(uv + float2(pixel, -pixel),0,0));
|
---|
207 | if(pos3.a != 0)
|
---|
208 | {
|
---|
209 | dist = length(pos3.xyz - pos.xyz);
|
---|
210 | sumdist += dist;
|
---|
211 | valids++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | float4 pos4 = tex2Dlod(PhotonHitMap, float4(uv + float2(-pixel, -pixel),0,0));
|
---|
215 | if(pos4.a != 0)
|
---|
216 | {
|
---|
217 | dist = length(pos4.xyz - pos.xyz);
|
---|
218 | sumdist += dist;
|
---|
219 | valids++;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | float avrdist = sumdist / valids;
|
---|
224 | if(valids == 0) avrdist = WorldSize / 1000.0;
|
---|
225 |
|
---|
226 | intensity = WorldSize / (avrdist * avrdist * 3.14);
|
---|
227 | //intensity = 0.8;
|
---|
228 |
|
---|
229 | OUT.color = intensity;
|
---|
230 | if(notUseIntensity)
|
---|
231 | OUT.color = float4(1,1,1,1);
|
---|
232 | }
|
---|
233 |
|
---|
234 | return OUT;
|
---|
235 | }
|
---|
236 |
|
---|
237 | float4 CauCube_Triangles_PS(CauCube_Triangles_VS_OUT IN,
|
---|
238 | uniform float4 CausticColor ):COLOR
|
---|
239 | {
|
---|
240 | return IN.color * CausticColor;
|
---|
241 | }
|
---|
242 |
|
---|
243 | ///////
|
---|
244 | /// Caustic gather shaders
|
---|
245 | //////
|
---|
246 |
|
---|
247 | float4 GatherCaustic_Cube_PS(Shaded_OUT IN,
|
---|
248 | uniform samplerCUBE CauCubeMap : register(s0),
|
---|
249 | uniform samplerCUBE DistanceCubeMap : register(s1),
|
---|
250 | uniform float3 cubeMapCameraPosition,
|
---|
251 | uniform float attenuation):COLOR
|
---|
252 | {
|
---|
253 | float4 Color = float4(1,1,1,1);
|
---|
254 |
|
---|
255 | float3 dir = IN.wPos - cubeMapCameraPosition;
|
---|
256 | float4 caustic = texCUBE(CauCubeMap, float3(dir.xy, -dir.z) );
|
---|
257 | float mydist = length(dir);
|
---|
258 | float dist = readDistanceCubeMap(DistanceCubeMap, dir);
|
---|
259 |
|
---|
260 | float EPSILON = 1.0;
|
---|
261 |
|
---|
262 | if(mydist > dist + EPSILON) caustic = 0;
|
---|
263 |
|
---|
264 | caustic.rgb *= max(attenuation - mydist, 0.0) / attenuation;
|
---|
265 |
|
---|
266 | Color = caustic;
|
---|
267 | //Color += attenuation * 0.000000000001;
|
---|
268 | Color.a = 1;
|
---|
269 |
|
---|
270 | return Color;
|
---|
271 | }
|
---|
272 |
|
---|