1 | // transformations
|
---|
2 | float4x4 WorldViewProj;
|
---|
3 | float4x4 ViewProj;
|
---|
4 | float4x4 ViewProjI;
|
---|
5 | float4x4 World;
|
---|
6 | float4x4 WorldIT;
|
---|
7 | float3 EyePos;
|
---|
8 | float3 TilePos;
|
---|
9 | float RefractionIndex;
|
---|
10 | float colOrder;
|
---|
11 |
|
---|
12 | #define MIN_RAY_DEPTH 0.01
|
---|
13 | #define MAX_RAY_DEPTH 200.0
|
---|
14 | #define RAY_TABLE_SIZE 512.0
|
---|
15 |
|
---|
16 | float4 ConePeak;
|
---|
17 | float4 ConeDirAndCosAngle;
|
---|
18 |
|
---|
19 | texture rayDirTable;
|
---|
20 | sampler rayDirTableSampler = sampler_state
|
---|
21 | {
|
---|
22 | Texture = <rayDirTable>;
|
---|
23 | MinFilter = Point;
|
---|
24 | MagFilter = Point;
|
---|
25 | MipFilter = None;
|
---|
26 | };
|
---|
27 |
|
---|
28 | texture rayOriginTable;
|
---|
29 | sampler rayOriginTableSampler = sampler_state
|
---|
30 | {
|
---|
31 | Texture = <rayOriginTable>;
|
---|
32 | MinFilter = Point;
|
---|
33 | MagFilter = Point;
|
---|
34 | MipFilter = None;
|
---|
35 | };
|
---|
36 |
|
---|
37 | texture coneDirTable;
|
---|
38 | sampler coneDirTableSampler = sampler_state
|
---|
39 | {
|
---|
40 | Texture = <coneDirTable>;
|
---|
41 | MinFilter = Point;
|
---|
42 | MagFilter = Point;
|
---|
43 | MipFilter = None;
|
---|
44 | };
|
---|
45 |
|
---|
46 | texture conePeakTable;
|
---|
47 | sampler conePeakTableSampler = sampler_state
|
---|
48 | {
|
---|
49 | Texture = <conePeakTable>;
|
---|
50 | MinFilter = Point;
|
---|
51 | MagFilter = Point;
|
---|
52 | MipFilter = None;
|
---|
53 | };
|
---|
54 |
|
---|
55 | textureCUBE environmentTexture;
|
---|
56 | sampler environmentSampler = sampler_state
|
---|
57 | {
|
---|
58 | Texture = <environmentTexture>;
|
---|
59 | MinFilter = Linear;
|
---|
60 | MagFilter = Linear;
|
---|
61 | MipFilter = None;
|
---|
62 | };
|
---|
63 |
|
---|
64 | void PrimaryRaysVS (
|
---|
65 | in float4 Pos : POSITION,
|
---|
66 | in float3 Norm : NORMAL,
|
---|
67 | out float4 hPos : POSITION,
|
---|
68 | out float3 wNorm : TEXCOORD0,
|
---|
69 | out float3 wPos : TEXCOORD1
|
---|
70 | )
|
---|
71 | {
|
---|
72 | hPos = mul(Pos, WorldViewProj);
|
---|
73 | wNorm = mul(Norm, WorldIT);
|
---|
74 | wPos = mul(Pos, World).xyz;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void PrimaryRaysPS (
|
---|
78 | in float3 wNorm : TEXCOORD0,
|
---|
79 | in float3 wPos : TEXCOORD1,
|
---|
80 | out float4 Origin : COLOR0,
|
---|
81 | out float4 Dir : COLOR1)
|
---|
82 | {
|
---|
83 | Origin = float4(wPos, 1);
|
---|
84 | wNorm = normalize(wNorm);
|
---|
85 | float3 ViewDir = wPos - EyePos;
|
---|
86 | ViewDir = normalize(ViewDir);
|
---|
87 |
|
---|
88 | float3 refractedDir = refract(ViewDir, wNorm, RefractionIndex);
|
---|
89 | if(dot(refractedDir, refractedDir) < 0.5) //total internal reflection
|
---|
90 | refractedDir = reflect(ViewDir, wNorm);
|
---|
91 |
|
---|
92 | Dir = float4(refractedDir, colOrder);
|
---|
93 | Dir.xyz = normalize(Dir.xyz);
|
---|
94 | }
|
---|
95 |
|
---|
96 | struct vsInputCopyBack
|
---|
97 | {
|
---|
98 | float4 Pos : POSITION;
|
---|
99 | float2 tex : TEXCOORD0;
|
---|
100 | };
|
---|
101 |
|
---|
102 | struct vsOutputCopyBack
|
---|
103 | {
|
---|
104 | float4 Pos : POSITION;
|
---|
105 | float2 tex : TEXCOORD0;
|
---|
106 | };
|
---|
107 |
|
---|
108 | vsOutputCopyBack
|
---|
109 | vsCopyBack(vsInputCopyBack input)
|
---|
110 | {
|
---|
111 | vsOutputCopyBack output = (vsOutputCopyBack)0;
|
---|
112 | output.Pos = input.Pos;
|
---|
113 | output.tex = input.tex;
|
---|
114 | return output;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void
|
---|
118 | psCopyBack(vsOutputCopyBack input, out float4 c0 : COLOR0, out float4 c1 : COLOR1)
|
---|
119 | {
|
---|
120 | c0 = tex2D(rayOriginTableSampler, input.tex);
|
---|
121 | c1 = tex2D(rayDirTableSampler, input.tex);
|
---|
122 | }
|
---|
123 |
|
---|
124 | struct vsInputShowTex
|
---|
125 | {
|
---|
126 | float4 Pos : POSITION;
|
---|
127 | float2 tex : TEXCOORD0;
|
---|
128 | };
|
---|
129 |
|
---|
130 | struct vsOutputShowTex
|
---|
131 | {
|
---|
132 | float4 Pos : POSITION;
|
---|
133 | float2 tex : TEXCOORD0;
|
---|
134 | };
|
---|
135 |
|
---|
136 | vsOutputShowTex
|
---|
137 | vsShowTex(vsInputShowTex input)
|
---|
138 | {
|
---|
139 | vsOutputShowTex output = (vsOutputShowTex)0;
|
---|
140 | output.Pos = input.Pos;
|
---|
141 | output.tex = input.tex;
|
---|
142 |
|
---|
143 | return output;
|
---|
144 | }
|
---|
145 |
|
---|
146 | float4
|
---|
147 | psShowTex(vsOutputShowTex input) : COLOR0
|
---|
148 | {
|
---|
149 | // return abs(float4(tex2D(rayDirTableSampler, input.tex).xyz, 1));
|
---|
150 | return float4(tex2D(rayOriginTableSampler, input.tex).xyz, 1);
|
---|
151 | // return abs(float4(tex2D(conePeakTableSampler, input.tex).xyz, 1));
|
---|
152 | // return float4(1,1,0,1);
|
---|
153 | // return float4(input.tex.x,input.tex.y,0,1);
|
---|
154 | // return float4(1.0 - acos(tex2D(coneDirTableSampler, input.tex).aaa) / 3.14, 1);
|
---|
155 | // + float4(0, 0.7, 0, 0);
|
---|
156 | // + float4(tex2D(rayDirTableSampler, input.tex).xyz, 1);
|
---|
157 | float3 relfDir = tex2D(rayDirTableSampler, input.tex);
|
---|
158 | return float4((relfDir.x +1) / 2,
|
---|
159 | (relfDir.y + 1) / 2,
|
---|
160 | (relfDir.z + 1) / 2, 1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | void RayCastVS(
|
---|
164 | in float4 Sphere : POSITION,
|
---|
165 | in float3 PlanePos : NORMAL,
|
---|
166 | in float3 Invmx0 : TEXCOORD0,
|
---|
167 | in float3 Invmx1 : TEXCOORD1,
|
---|
168 | in float3 Invmx2 : TEXCOORD2,
|
---|
169 | in float3 Normals0 : TEXCOORD3,
|
---|
170 | in float3 Normals1 : TEXCOORD4,
|
---|
171 | in float3 Normals2 : TEXCOORD5,
|
---|
172 |
|
---|
173 | out float4 hPos : POSITION,
|
---|
174 | out float3 oPlanePos : TEXCOORD6,
|
---|
175 | out float3 oInvmx0 : TEXCOORD0,
|
---|
176 | out float3 oInvmx1 : TEXCOORD1,
|
---|
177 | out float3 oInvmx2 : TEXCOORD2,
|
---|
178 | out float3 oNormals0 : TEXCOORD3,
|
---|
179 | out float3 oNormals1 : TEXCOORD4,
|
---|
180 | out float3 oNormals2 : TEXCOORD5
|
---|
181 | )
|
---|
182 | {
|
---|
183 | oPlanePos = PlanePos;
|
---|
184 | oInvmx0 = Invmx0;
|
---|
185 | oInvmx1 = Invmx1;
|
---|
186 | oInvmx2 = Invmx2;
|
---|
187 | oNormals0 = Normals0;
|
---|
188 | oNormals1 = Normals1;
|
---|
189 | oNormals2 = Normals2;
|
---|
190 |
|
---|
191 | hPos = float4(TilePos, 1);
|
---|
192 |
|
---|
193 | ConeDirAndCosAngle.xyz = mul(WorldIT, float4(ConeDirAndCosAngle.xyz, 0));
|
---|
194 | ConePeak.xyz = mul(WorldIT, float4(ConePeak.xyz, 1));
|
---|
195 |
|
---|
196 | float3 sfc = Sphere.xyz - ConePeak.xyz;
|
---|
197 | float lsfc = length(sfc);
|
---|
198 |
|
---|
199 | if(Sphere.w < lsfc) // cone peak not in sphere
|
---|
200 | {
|
---|
201 | // angle difference between cone main direction and direction to sphere centre
|
---|
202 | float angSpMidConeMid = acos(dot(ConeDirAndCosAngle.xyz, sfc) / lsfc);
|
---|
203 | // the half of the opening angle at which the sphere is seen
|
---|
204 | float angSpRad = asin(Sphere.w / lsfc);
|
---|
205 | // cone opening angle
|
---|
206 | float angCone = acos(ConeDirAndCosAngle.w);
|
---|
207 |
|
---|
208 | // if sphere direction not within (cone direction + sphere angle), discard tile for this triangle
|
---|
209 | if(angCone + angSpRad < angSpMidConeMid)
|
---|
210 | {
|
---|
211 | hPos = float4(10000000.0, 10000000.0, 10000000.0, 1.0);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | void
|
---|
217 | RayCastPS(
|
---|
218 | // in float3 PlanePos : TEXCOORD6,
|
---|
219 | in float3 Invmx0 : TEXCOORD0,
|
---|
220 | in float3 Invmx1 : TEXCOORD1,
|
---|
221 | in float3 Invmx2 : TEXCOORD2,
|
---|
222 | in float3 Normals0 : TEXCOORD3,
|
---|
223 | in float3 Normals1 : TEXCOORD4,
|
---|
224 | in float3 Normals2 : TEXCOORD5,
|
---|
225 |
|
---|
226 | in float2 vPos : VPOS,
|
---|
227 | out float4 oOrigin : COLOR0,
|
---|
228 | out float4 oDir : COLOR1,
|
---|
229 | out float1 oDepth : DEPTH)
|
---|
230 | {
|
---|
231 |
|
---|
232 | float3 PlanePos = Invmx0 + Invmx1 + Invmx2;
|
---|
233 | PlanePos /= dot(PlanePos, PlanePos);
|
---|
234 |
|
---|
235 | float2 pixpos = vPos.xy;
|
---|
236 | pixpos /= RAY_TABLE_SIZE; // to texture coordinates
|
---|
237 | float3 rayOrigin = tex2D(rayOriginTableSampler, pixpos.xy);
|
---|
238 | rayOrigin = mul(WorldIT, float4(rayOrigin, 1));
|
---|
239 | float3 rayDir = tex2D(rayDirTableSampler, pixpos.xy);
|
---|
240 | rayDir = mul(WorldIT, float4(rayDir, 0));
|
---|
241 |
|
---|
242 | float hitDepth = (dot(PlanePos, PlanePos) - dot(rayOrigin, PlanePos)) / dot(rayDir, PlanePos);
|
---|
243 | if(hitDepth < MIN_RAY_DEPTH || hitDepth > MAX_RAY_DEPTH)
|
---|
244 | {
|
---|
245 | oDepth = 1000000.0;
|
---|
246 | oOrigin = oDir = 0;
|
---|
247 | }
|
---|
248 | else
|
---|
249 | {
|
---|
250 | float3 hitPoint = rayOrigin + (rayDir * hitDepth);
|
---|
251 |
|
---|
252 | float3 worldDist = mul(float4(hitPoint - rayOrigin, 0), World).xyz;
|
---|
253 |
|
---|
254 | oDepth = length(worldDist) / MAX_RAY_DEPTH;
|
---|
255 |
|
---|
256 | float baryA = dot(Invmx0, hitPoint);
|
---|
257 | float baryB = dot(Invmx1, hitPoint);
|
---|
258 | float baryC = dot(Invmx2, hitPoint);
|
---|
259 | if(baryA > -0.001 && baryB > -0.001 && baryC > -0.001)
|
---|
260 | {
|
---|
261 | oOrigin = mul(float4(hitPoint, 1), World);
|
---|
262 |
|
---|
263 | float3 normalAtHitPoint = Normals0 * baryA;
|
---|
264 | normalAtHitPoint += Normals1 * baryB;
|
---|
265 | normalAtHitPoint += Normals2 * baryC;
|
---|
266 | normalAtHitPoint = normalize(normalAtHitPoint);
|
---|
267 | if(dot(normalAtHitPoint, rayDir) > 0)
|
---|
268 | {
|
---|
269 | normalAtHitPoint = -normalAtHitPoint;
|
---|
270 | RefractionIndex = 1.0 / RefractionIndex; // exiting ray
|
---|
271 | }
|
---|
272 | float3 refractedDir = refract(rayDir, normalAtHitPoint, RefractionIndex);
|
---|
273 | if(dot(refractedDir, refractedDir) < 0.5) // total internal reflection
|
---|
274 | refractedDir = reflect(rayDir, normalAtHitPoint);
|
---|
275 | oDir = float4( mul(float4(refractedDir,0), World).xyz, 1);
|
---|
276 | }
|
---|
277 | else
|
---|
278 | {
|
---|
279 | oOrigin = float4(10.0, 0, 0, 1);
|
---|
280 | oDir = float4(10.0, 0, 0, 1);
|
---|
281 | oDepth = 1000000.0;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | struct vsInputBackGround
|
---|
288 | {
|
---|
289 | float4 Pos : POSITION; //Pos
|
---|
290 | float2 tex : TEXCOORD0;
|
---|
291 | };
|
---|
292 |
|
---|
293 | struct vsOutputBackGround
|
---|
294 | {
|
---|
295 | float4 Pos : POSITION; //Pos
|
---|
296 | float2 tex : TEXCOORD0;
|
---|
297 | float3 wPos : TEXCOORD1;
|
---|
298 | };
|
---|
299 |
|
---|
300 | vsOutputBackGround vsBackground(vsInputBackGround input){
|
---|
301 | vsOutputBackGround output = (vsOutputBackGround)0;
|
---|
302 | output.Pos=input.Pos;
|
---|
303 | float4 worldpos = mul(input.Pos, ViewProjI);
|
---|
304 | worldpos /= worldpos.w;
|
---|
305 | output.wPos = worldpos.xyz;
|
---|
306 | output.tex = input.tex;
|
---|
307 | return output;
|
---|
308 | }
|
---|
309 |
|
---|
310 | void psBackground(vsOutputBackGround input, out float4 o0 : COLOR0)
|
---|
311 | {
|
---|
312 | float4 dird = tex2D(rayDirTableSampler, input.tex);
|
---|
313 | float3 Dir = dird.xyz;
|
---|
314 | float3 col = 1 ;
|
---|
315 | /*float3(pow(0.7, frac(dird.w / 10.0) * 10.0) ,
|
---|
316 | pow(0.7, frac(dird.w / 100.0) * 10.0) ,
|
---|
317 | pow(0.7, frac(dird.w / 1000.0) * 10.0));*/
|
---|
318 | if(dot(Dir, Dir) < 0.5)
|
---|
319 | {
|
---|
320 | Dir = normalize(input.wPos.xyz - EyePos.xyz);
|
---|
321 | col = float3(4, 4, 4);
|
---|
322 | }
|
---|
323 | o0 = float4(texCUBE(environmentSampler, Dir).xyz * col * 1.1, 0.0);
|
---|
324 | // o0 = float4(dird.w / 5.0, dird.w / 2.0, dird.w / 10.0, 1.0);
|
---|
325 |
|
---|
326 | // o0 = float4(input.tex.x, input.tex.y, 1.0, 1.0);
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | void ConeVS(
|
---|
331 | in float4 Pos : POSITION,
|
---|
332 | in float2 Tex : TEXCOORD0,
|
---|
333 | out float4 oPos : POSITION,
|
---|
334 | out float2 oTex : TEXCOORD0
|
---|
335 | )
|
---|
336 | {
|
---|
337 | oPos = Pos;
|
---|
338 | oTex = Tex;
|
---|
339 | }
|
---|
340 |
|
---|
341 | void
|
---|
342 | ConePS(
|
---|
343 | in float2 Tex : TEXCOORD0,
|
---|
344 | out float4 oConePeak : COLOR0,
|
---|
345 | out float4 oConeDir : COLOR1)
|
---|
346 | {
|
---|
347 | float3 peak = 0;
|
---|
348 | float3 dir = 0;
|
---|
349 | float cosAngle = 1.0;
|
---|
350 | float sinAngle = 0.0;
|
---|
351 | for(int i=0; i < 16; i++)
|
---|
352 | for(int j=0; j < 16; j++)
|
---|
353 | {
|
---|
354 | float3 currentRayDir = tex2D(rayDirTableSampler, Tex + float2((float)j/RAY_TABLE_SIZE, (float)i/RAY_TABLE_SIZE));
|
---|
355 | if(dot(currentRayDir, currentRayDir) > 0.5) // a valid ray
|
---|
356 | {
|
---|
357 | float3 currentRayOrigin = tex2D(rayOriginTableSampler, Tex + float2((float)j/RAY_TABLE_SIZE, (float)i/RAY_TABLE_SIZE));
|
---|
358 | if(dot(dir,dir) < 0.5) // no valid ray found before this one
|
---|
359 | {
|
---|
360 | dir = currentRayDir;
|
---|
361 | peak = currentRayOrigin;
|
---|
362 | }
|
---|
363 | else
|
---|
364 | {
|
---|
365 | float ncos = dot(currentRayDir,dir);
|
---|
366 | if(ncos < cosAngle)
|
---|
367 | {
|
---|
368 | float3 perpdir = normalize(currentRayDir - ncos * dir);
|
---|
369 | float3 farConeEdge = cosAngle * dir - sinAngle * perpdir;
|
---|
370 | dir = farConeEdge + currentRayDir;
|
---|
371 | dir = normalize(dir);
|
---|
372 | cosAngle = dot(dir, currentRayDir);
|
---|
373 | sinAngle = sqrt(1.0 - cosAngle * cosAngle);
|
---|
374 | }
|
---|
375 | float3 pd = currentRayOrigin - peak;
|
---|
376 | float3 ptop = normalize(pd);
|
---|
377 | float cospp = dot(dir, ptop);
|
---|
378 | if(cospp < cosAngle)
|
---|
379 | {
|
---|
380 | float3 perpdir = ptop - cospp * dir;
|
---|
381 | perpdir = normalize(perpdir);
|
---|
382 | float3 farConeEdge = (cosAngle * dir - sinAngle * perpdir);
|
---|
383 | float3 nearConeEdge = (cosAngle * dir + sinAngle * perpdir);
|
---|
384 | float3 g = currentRayOrigin - peak - nearConeEdge * dot(nearConeEdge, pd);
|
---|
385 | peak += farConeEdge * dot(g,g) / dot(farConeEdge, g);
|
---|
386 | }
|
---|
387 | }
|
---|
388 | }
|
---|
389 | }
|
---|
390 | oConePeak = float4(peak, 1.0);
|
---|
391 | oConeDir = float4(dir, cosAngle);
|
---|
392 | if(dot(dir, dir) < 0.5) // no valid rays in tile
|
---|
393 | {
|
---|
394 | //cone far away not intersecting anything
|
---|
395 | oConePeak = float4(1000000.0, 0 , 0, 0.0);
|
---|
396 | oConeDir = float4(1, 0, 0, 1.0);
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | technique RenderPrimaryRayArray{
|
---|
402 | pass P0
|
---|
403 | {
|
---|
404 | VertexShader = compile vs_3_0 PrimaryRaysVS();
|
---|
405 | PixelShader = compile ps_3_0 PrimaryRaysPS();
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | technique RayCast{
|
---|
410 | pass P0
|
---|
411 | {
|
---|
412 | VertexShader = compile vs_3_0 RayCastVS();
|
---|
413 | PixelShader = compile ps_3_0 RayCastPS();
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | technique ShowTex{
|
---|
418 | pass P0
|
---|
419 | {
|
---|
420 | VertexShader = compile vs_2_0 vsShowTex();
|
---|
421 | PixelShader = compile ps_2_0 psShowTex();
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | technique Background{
|
---|
426 | pass P0
|
---|
427 | {
|
---|
428 | VertexShader = compile vs_2_0 vsBackground();
|
---|
429 | PixelShader = compile ps_2_0 psBackground();
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | technique CopyBack{
|
---|
434 | pass P0
|
---|
435 | {
|
---|
436 | VertexShader = compile vs_2_0 vsCopyBack();
|
---|
437 | PixelShader = compile ps_2_0 psCopyBack();
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | technique ComputeCones{
|
---|
442 | pass P0
|
---|
443 | {
|
---|
444 | VertexShader = compile vs_3_0 ConeVS();
|
---|
445 | PixelShader = compile ps_3_0 ConePS();
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|