This project is a fully custom ray tracer implemented in Unity using compute shaders. It simulates the physics of light to render realistic scenes from scratch and includes a wide range of techniques commonly used in both academic and production-grade renderers.
Beyond producing physically-inspired visuals, the project also serves as an educational tool. It includes visual debugging utilities to help developers understand ray paths, mesh geometry, and the camera frustum in real-time.
- Phong Shading: Classic specular lighting model with ambient, diffuse, and reflective components.
- Lambertian Shading: Used for diffuse materials for a matte, physically plausible look.
- Metallic and Smoothness Parameters: Enables materials to exhibit various real-world surface behaviors.
- Per-pixel Shading: Shading is performed at sub-pixel resolution using supersampling.
Matte pastel-colored spheres rendered with soft shadows and Lambertian shading
- Direct Lighting: Supports both directional and point lights.
- Hard Shadows: Achieved via single shadow rays per light.
- Soft Shadows: Area light simulation using multi-sample shadow rays for penumbra effects.
Scene featuring both glossy metallic and matte spheres, showcasing material diversity and specular highlights
- Möller–Trumbore Triangle Intersection: Efficient intersection test for triangle meshes.
- Multiple Rays per Pixel (Supersampling): Reduces aliasing and noise for smooth results.
- Bounding Volume Hierarchy (BVH): Built on startup to accelerate ray-mesh intersection.
- GPU-accelerated Ray Traversal: All core computation is performed on the GPU using compute shaders.
- Efficient Memory Layout: Optimized for structured GPU access via Unity buffers.
Each frame, the renderer casts several rays per pixel. Rays intersect with geometry via BVH traversal, and at each hit point, lighting is calculated using surface normals, light positions, visibility (shadows), and material properties.
Soft Shadows are computed by randomly sampling within the area light's radius. For each sample, a shadow ray is tested for occlusion. The final visibility is averaged across all shadow rays.
These utilities are available in the project to assist with debugging and educational insights:
- Camera Frustum Visualizer: Renders camera frustum corners using compute shader outputs and
Debug.DrawRay
. - Ray Visualizer: Displays rays being cast into the scene.
- Mesh Triangle Visualizer: Shows individual triangle geometry for objects in world space.
- Clone or download this repository.
- Open the project in Unity.
- Open the main scene.
- Enter Play Mode — rendering will begin automatically.
You can adjust lighting, material, and rendering parameters directly from the Unity Editor.
RayTracingManager.cs
: Main Unity component that builds the scene data, initializes GPU buffers, and dispatches the compute shader.RayTracing.compute
: GPU compute shader that handles ray generation, BVH traversal, shading, and final image output.BVH.cs
: CPU-side BVH construction code (not shown above, but assumed to be present).- Visualization scripts/files:
- Frustum buffer setup and debug drawing
- Ray drawing utility
- Triangle debug view