|
| 1 | +/* Copyright (c) 2025, Sascha Willems |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +struct VSInput |
| 8 | +{ |
| 9 | + float3 Pos; |
| 10 | + float2 UV; |
| 11 | + float3 Color; |
| 12 | + float3 Normal; |
| 13 | +} |
| 14 | + |
| 15 | +struct VSOutput |
| 16 | +{ |
| 17 | + float4 Pos : SV_POSITION; |
| 18 | + float3 Normal; |
| 19 | + float3 Color; |
| 20 | + float3 ViewVec; |
| 21 | + float3 LightVec; |
| 22 | + float3 WorldPos; |
| 23 | +} |
| 24 | + |
| 25 | +struct UBO |
| 26 | +{ |
| 27 | + float4x4 projection; |
| 28 | + float4x4 view; |
| 29 | + float4x4 model; |
| 30 | + float3 lightPos; |
| 31 | +}; |
| 32 | +ConstantBuffer<UBO> ubo; |
| 33 | + |
| 34 | +RaytracingAccelerationStructure accelStruct; |
| 35 | + |
| 36 | +#define ambient 0.1 |
| 37 | + |
| 38 | +[shader("vertex")] |
| 39 | +VSOutput vertexMain(VSInput input) |
| 40 | +{ |
| 41 | + VSOutput output; |
| 42 | + output.Color = input.Color; |
| 43 | + output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0)))); |
| 44 | + float4 pos = mul(ubo.model, float4(input.Pos, 1.0)); |
| 45 | + output.WorldPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz; |
| 46 | + output.Normal = mul((float4x3)ubo.model, input.Normal).xyz; |
| 47 | + output.LightVec = normalize(ubo.lightPos - input.Pos); |
| 48 | + output.ViewVec = -pos.xyz; |
| 49 | + return output; |
| 50 | +} |
| 51 | + |
| 52 | +[shader("fragment")] |
| 53 | +float4 fragmentMain(VSOutput input) |
| 54 | +{ |
| 55 | + float3 N = normalize(input.Normal); |
| 56 | + float3 L = normalize(input.LightVec); |
| 57 | + float3 V = normalize(input.ViewVec); |
| 58 | + float3 R = normalize(-reflect(L, N)); |
| 59 | + float3 diffuse = max(dot(N, L), ambient) * input.Color; |
| 60 | + |
| 61 | + float4 color = float4(diffuse, 1.0); |
| 62 | + |
| 63 | + RayDesc rayDesc; |
| 64 | + rayDesc.Origin = input.WorldPos; |
| 65 | + rayDesc.Direction = L; |
| 66 | + rayDesc.TMin = 0.01; |
| 67 | + rayDesc.TMax = 1000.0; |
| 68 | + |
| 69 | + RayQuery<RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH> rayQuery; |
| 70 | + rayQuery.TraceRayInline(accelStruct, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xFF, rayDesc); |
| 71 | + |
| 72 | + // Traverse the acceleration structure and store information about the first intersection (if any) |
| 73 | + rayQuery.Proceed(); |
| 74 | + |
| 75 | + // If the intersection has hit a triangle, the fragment is shadowed |
| 76 | + if (rayQuery.CommittedStatus() == COMMITTED_TRIANGLE_HIT) { |
| 77 | + color *= 0.1; |
| 78 | + } |
| 79 | + |
| 80 | + return color; |
| 81 | +} |
0 commit comments