@@ -9,6 +9,7 @@ struct UniformBuffer {
99 float4x4 model;
1010 float4x4 view;
1111 float4x4 proj;
12+ float3 cameraPos;
1213};
1314[[vk::binding(0 ,0 )]]
1415ConstantBuffer < UniformBuffer> ubo;
@@ -128,18 +129,58 @@ bool in_shadow(float3 P)
128129 return hit;
129130}
130131
132+ void apply_reflection(float3 P, float3 N, inout float4 baseColor) {
133+ // Build the reflections ray
134+ float3 V = normalize(ubo .cameraPos - P);
135+ float3 R = reflect(- V, N);
136+
137+ RayDesc reflectionRayDesc;
138+ reflectionRayDesc .Origin = P;
139+ reflectionRayDesc .Direction = R;
140+ reflectionRayDesc .TMin = EPSILON;
141+ reflectionRayDesc .TMax = 1 e 4 ;
142+
143+ // Initialize a ray-query for reflections
144+ RayQuery< RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES |
145+ RAY_FLAG_FORCE_OPAQUE> rq;
146+ let rayFlags = RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES |
147+ RAY_FLAG_FORCE_OPAQUE;
148+
149+ rq .TraceRayInline (accelerationStructure, rayFlags, 0x FF , reflectionRayDesc);
150+
151+ rq .Proceed ();
152+
153+ bool hit = (rq .CommittedStatus () == COMMITTED_TRIANGLE_HIT);
154+
155+ if (hit)
156+ {
157+ uint instanceID = rq .CommittedInstanceID ();
158+ uint primIndex = rq .CommittedPrimitiveIndex ();
159+
160+ float2 uv = intersection_uv(instanceID, primIndex, rq .CommittedTriangleBarycentrics ());
161+
162+ uint materialID = instanceLUTBuffer [instanceID].materialID ;
163+ float4 intersectionColor = textures [materialID].Sample (textureSampler, uv);
164+
165+ baseColor .rgb = lerp(baseColor .rgb , intersectionColor .rgb , 0 . 7 );
166+ }
167+ }
168+
131169[shader(" fragment" )]
132170float4 fragMain(VSOutput vertIn) : SV_TARGET {
133171 float4 baseColor = textures [pc .materialIndex ].Sample (textureSampler, vertIn .fragTexCoord );
134172
135173 // Alpha test
136174 if (baseColor .a < 0 . 5 ) discard ;
137175
176+ float3 P = vertIn .worldPos ;
177+ float3 N = vertIn .fragNormal ;
178+
138179 if (pc .reflective > 0 ) {
139- baseColor . rgb = float3( 1 . 0 , 0 . 0 , 0 . 0 );
180+ apply_reflection(P, N, baseColor );
140181 }
141182
142- bool inShadow = in_shadow(vertIn . worldPos );
183+ bool inShadow = in_shadow(P );
143184
144185 // Darken if in shadow
145186 if (inShadow) {
0 commit comments