I used DirectX12 to create a Ray Tracing pipeline that could read OBJ files and generate a BLAS for each unique mesh and update a TLAS every frame to pass into the DirectXR Raytracing Pipeline to generate an image. I wrote a Ray Generation, Closest Hit, and Miss shader to be used in HLSL for generating an image. It reuses many of the features from my other DirectX11 projects, like an Entity system and a controllable camera.
The Generation shader creates 30 rays per pixel, and each one can bounce 15 times before returning black. They are randomly scattered on the pixel and launched to avoid any aliasing and provide Raytraced diffuse lighting by averaging out random bounces.
The miss shader just returns a fixed sky color, which is a gradient lerped from a light blue color to a white color depending on the Y value of the rays direction; where straight up is white and straight down is light blue.
The Closest Hit shader uses the three vertices of the triangle hit to interpolate their position and normal using barycentric coordinates. This vertex is converted to world space and used in diffuse, reflection, and refraction. The Material is passed in as 6 floats: color, roughness, whether it is transparent, and its index of refraction. Diffusion uses a random ray on the surface of a unit sphere moved one unit in the normal direction as a direction to move towards. Reflection simply reflects along the normal. For roughness values between 0 and 1, I calculate a diffuse ray and a reflected ray and use the roughness to slerp the normal from reflection to diffuse. When refraction is true, it uses the index of refraction to refract the ray’s normal, using the reciprocal when the ray hits a back face.