14
14
#define RAYTRACING_HLSL
15
15
#define HLSL
16
16
#include "RaytracingHlslCompat.h"
17
+ #include "Star.hlsli"
17
18
18
19
using namespace dx;
19
20
RaytracingAccelerationStructure Scene : register (t0, space0);
@@ -42,8 +43,7 @@ SamplerState BushSampler : register(s1);
42
43
43
44
44
45
typedef BuiltInTriangleIntersectionAttributes MyAttributes;
45
-
46
-
46
+
47
47
48
48
// Struct defines the payload used during ray tracing
49
49
struct [raypayload] RayPayload
@@ -53,14 +53,6 @@ struct [raypayload] RayPayload
53
53
uint reflectHint : write (caller) : read (closesthit, caller);
54
54
};
55
55
56
- struct [raypayload] ShadowRayPayload
57
- {
58
- bool hit : write (closesthit, miss) : read (caller);
59
- };
60
-
61
-
62
-
63
-
64
56
65
57
// Retrieve hit world position.
66
58
float3 HitWorldPosition ()
@@ -216,55 +208,12 @@ float4 TraceRadianceRay(in Ray ray, in int currentRayRecursionDepth)
216
208
217
209
return rayPayload.color;
218
210
}
219
-
220
- // Trace a shadow ray and return true if it hits any geometry.
221
- bool TraceShadowRayAndReportIfHit (in Ray ray, in int currentRayRecursionDepth)
222
- {
223
- if (currentRayRecursionDepth >= 1 )
224
- {
225
- return false ;
226
- }
227
-
228
- // Set the ray's extents.
229
- RayDesc rayDesc;
230
- rayDesc.Origin = ray.Origin;
231
- rayDesc.Direction = ray.Direction;
232
- // Set TMin to a zero value to avoid aliasing artifcats along contact areas.
233
- // Note: make sure to enable back-face culling so as to avoid surface face fighting.
234
- rayDesc.TMin = 0 ;
235
- rayDesc.TMax = 10000 ;
236
-
237
- // Initialize shadow ray payload.
238
- // Set the initial value to true since closest and any hit shaders are skipped.
239
- // Shadow miss shader, if called, will set it to false.
240
- ShadowRayPayload shadowPayload = { true };
241
- TraceRay (Scene,
242
- RAY_FLAG_CULL_BACK_FACING_TRIANGLES
243
- | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH
244
- | RAY_FLAG_FORCE_OPAQUE // ~skip any hit shaders
245
- | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, // ~skip closest hit shaders,
246
- ~0 , // InstanceInclusionMask
247
- 1 , // RayContributionToHitGroupIndex for shadow rays
248
- 2 , // MultiplierForGeometryContributionToHitGroupIndex
249
- 1 , // MissShaderIndex for shadow rays
250
- rayDesc, shadowPayload);
251
-
252
- return shadowPayload.hit;
253
- }
254
211
255
212
256
213
[shader ("closesthit" )]
257
214
void FloorClosestHitShader (inout RayPayload payload, in MyAttributes attr)
258
215
{
259
216
float3 hitPosition = HitWorldPosition ();
260
-
261
- // Shadow component - trace for all floor pixels, not just reflective ones
262
- // Ray shadowRay = { hitPosition + float3(0, 0.001f, 0), normalize(g_sceneCB.lightPosition.xyz - hitPosition) };
263
- // bool shadowRayHit = TraceShadowRayAndReportIfHit(shadowRay, payload.recursionDepth);
264
-
265
- // Calculate shadow factor
266
- // float shadowFactor = shadowRayHit ? 0.3f : 1.0f; // 0.3 for ambient when in shadow
267
-
268
217
uint baseIndex = PrimitiveIndex () * 3 ;
269
218
uint offset = baseIndex * 4 ;
270
219
uint3 indices = IndicesCube.Load3 (offset);
@@ -280,8 +229,8 @@ void FloorClosestHitShader(inout RayPayload payload, in MyAttributes attr)
280
229
float3 vertexNormals[3 ] =
281
230
{
282
231
VerticesCube[indices.x].normal,
283
- VerticesCube[indices.y].normal,
284
- VerticesCube[indices.z].normal
232
+ VerticesCube[indices.y].normal,
233
+ VerticesCube[indices.z].normal
285
234
};
286
235
287
236
triangleNormal = HitAttribute (vertexNormals, attr);
@@ -298,52 +247,7 @@ void FloorClosestHitShader(inout RayPayload payload, in MyAttributes attr)
298
247
float4 reflectionColor = TraceRadianceRay (reflectionRay, payload.recursionDepth);
299
248
float3 fresnel = FresnelReflectanceSchlick (WorldRayDirection (), triangleNormal, baseColor);
300
249
float3 refColor = lerp (baseColor, reflectionColor.rgb, fresnel) * 1.7f ;
301
-
302
- // Star Nest by Pablo Roman Andrioli
303
- // Volumetric fractal clouds effect
304
- const int FractalSampleSteps = 270 ;
305
- const int FractalIterations = 50 ;
306
- const float SampleSpacing = 0.05 ;
307
- const float FractalOffset = 0.53 ;
308
-
309
- float fractalDensity = 0 ;
310
- float fadeFactor = 1.0 ;
311
-
312
- for (int s = 0 ; s < FractalSampleSteps; ++s)
313
- {
314
- float3 p = hitPosition + triangleNormal * (s * SampleSpacing);
315
- p = abs (fmod (p, 2.0 ) - 1.0 );
316
-
317
- float pa = 0 ;
318
- float a = 0 ;
319
- for (int i = 0 ; i < FractalIterations; ++i)
320
- {
321
- float invLen2 = 1.0 / dot (p, p);
322
- p = abs (p) * invLen2 - FractalOffset;
323
- float lenP = length (p);
324
- a += abs (lenP - pa);
325
- pa = lenP;
326
- }
327
-
328
- fractalDensity += a * fadeFactor;
329
- fadeFactor *= 0.9 ;
330
- }
331
-
332
- // Normalize & smooth fractal density
333
- fractalDensity = saturate (fractalDensity / (FractalSampleSteps * FractalIterations * 0.12 ));
334
- float d = smoothstep (0.1 , 0.9 , fractalDensity);
335
-
336
- // Cloud color ramp & light glow
337
- float3 blue = float3 (0.4 , 0.6 , 1.0 ); // Soft blue
338
- float3 pink = float3 (1.0 , 0.6 , 0.8 ); // Soft pink
339
- float3 cloud = lerp (blue, pink, d);
340
-
341
- float3 L = normalize (g_sceneCB.lightPosition.xyz - hitPosition);
342
- float lightD = saturate (dot (triangleNormal, L));
343
- cloud *= lerp (0.8 + 0.2 * lightD, 4.0 , d); // lightD ranges [0.8,1.0]
344
-
345
- // Composite reflection with clouds
346
- finalColor = lerp (refColor, cloud, d * 1.3 );
250
+ finalColor = refColor;
347
251
}
348
252
else
349
253
{
@@ -504,52 +408,19 @@ void MyMissShader(inout RayPayload payload)
504
408
float3 rayOrigin = WorldRayOrigin ();
505
409
float3 rayDir = normalize (WorldRayDirection ());
506
410
float3 skyPosition = rayDir * 10.0f ;
507
-
508
- // Star Nest by Pablo Roman Andrioli
509
- // Volumetric fractal clouds effect
510
- const int FractalSampleSteps = 200 ;
511
- const int FractalIterations = 50 ;
512
- const float SampleSpacing = 0.05 ;
513
- const float FractalOffset = 0.53 ;
514
-
515
- float fractalDensity = 0 ;
516
- float fadeFactor = 1.0 ;
517
-
518
- for (int s = 0 ; s < FractalSampleSteps; ++s)
519
- {
520
- float3 p = skyPosition + rayDir * (s * SampleSpacing);
521
- p = abs (fmod (p, 2.0 ) - 1.0 );
522
-
523
- float pa = 0 ;
524
- float a = 0 ;
525
- for (int i = 0 ; i < FractalIterations; ++i)
526
- {
527
- float invLen2 = 1.0 / dot (p, p);
528
- p = abs (p) * invLen2 - FractalOffset;
529
- float lenP = length (p);
530
- a += abs (lenP - pa);
531
- pa = lenP;
532
- }
533
-
534
- fractalDensity += a * fadeFactor;
535
- fadeFactor *= 0.9 ;
536
- }
537
-
538
- // Normalize & smooth fractal density
539
- fractalDensity = saturate (fractalDensity / (FractalSampleSteps * FractalIterations * 0.12 ));
540
- float d = smoothstep (0.1 , 0.9 , fractalDensity);
411
+ float fractalDensity = CalculateStarNest (skyPosition, rayDir);
541
412
542
- // Cloud color ramp & light glow
413
+ // Create soft purple light glow
543
414
float3 blue = float3 (0.4 , 0.6 , 1.0 ); // Soft blue
544
415
float3 pink = float3 (1.0 , 0.6 , 0.8 ); // Soft pink
545
- float3 cloud = lerp (blue, pink, d );
416
+ float3 cloud = lerp (blue, pink, fractalDensity );
546
417
547
418
float3 L = normalize (g_sceneCB.lightPosition.xyz - skyPosition);
548
- float lightD = saturate (dot (rayDir, L)); // Use ray direction as "normal" for sky
549
- cloud *= lerp (0.8 + 0.2 * lightD, 4.0 , d );
419
+ float lightD = saturate (dot (rayDir, L));
420
+ cloud *= lerp (0.8 + 0.2 * lightD, 4.0 , fractalDensity );
550
421
551
422
// Composite background with clouds
552
- float3 finalColor = lerp (background.rgb, cloud, d * 1.3 );
423
+ float3 finalColor = lerp (background.rgb, cloud, fractalDensity * 1.3 );
553
424
payload.color = float4 (finalColor, 1.0f );
554
425
}
555
426
0 commit comments