Skip to content

BlueLupa/RT-Game-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RT Game Engine

A from-scratch game engine written in Rust using WGPU, featuring real-time Ray Tracing and Physically Based Rendering (PBR)

Rust Logo

Tech

  • Language: Rust
  • Graphics: wgpu
  • Shaders: WGSL

Ray-Traced Shadows

Shadows are generated via a shadow-mask approach:

  • A per-pixel shadow ray is evaluated to determine if the visible point is occluded relative to the light.
  • The shadow mask is computed at half resolution for performance.
  • The half-res mask is bilaterally blurred and upscaled back to full resolution to preserve edges.

Soft Shadows

  • Soft shadows are currently approximated by filtering rather than true multi-ray sampling.

Acceleration:

  • The world is partitioned into spatial chunks.
  • Each frame, the engine determines which chunks are needed/visible and streams only their child geometry into GPU buffers.
  • This keeps VRAM usage under control in large scenes and avoids irrelevant geometry.
  • BVH intersection tests are then used to efficiently rule out unneeded triangles for each ray.

Currently, BVH rebuilds are required for object rotation because the AABB bounds change. This can be expensive for dense geometry, making translation-only updates favourable.

Example:

The first image shows an object casting a shadow, while the second is its respective shadow mask.

Shadows Shadow Mask


Here is another example showing the full shadow mask, which uses the RGB channels to store reflection data (for Ray Tracing) alongside the shadows:

Example scene Full Shadow mask


Ray Tracing for Reflections

Ray Tracing can be used for Reflections

  • Texture references are pooled and de-duplicated, then sent to the GPU.
  • Utilises the pre-existing Ray Tracing pass for shadows; is calculated per pixel.
  • A ray is reflected from the visible point, and hits another triangle.
  • The intersected geometry is sampled, then rendered using PBR.
  • Reflected colors are stored in the RGB channels of the Shadow Mask, while shadow visibility is packed into the Alpha channel.

Benefits

  • Accurate reflections
  • Dynamic - updated per frame
  • World Space; reflections can show occluded objects
  • No visual artifacts

Limitations

  • High compute cost (averages at ~30 fps in moderately simple scenes)

RT reflections RT reflections RT reflections


Reflection Probes

Reflections for Low and Medium use a reflection probe.

  • A reflection probe centered on the camera.
  • Every frame, the scene is rendered from each of the cube's six faces.
  • The hidden render is then used for reflections in the shader.

Benefits

  • Low compute cost
  • Can be easily filtered for PBR
  • Compute doesn't scale with scene complexity

Limitations

  • Approximated reflections
  • Prone to parallax errors due to the lack of depth
  • Not World Space; can only reflect onto objects visible from the camera's position

Probe example 1


Sometimes the reflections from the probe can start to break, as seen here:

Parallax Error


Render Pipeline (Overview)

  1. Scene updates: Instance and collision updates
  2. Acceleration updates: Chunks have their child objects updated and BVHs are refit when needed.
  3. Ray Tracing Pass: Shadow mask (and RT reflections if enabled) are computed on the GPU
  4. Upscale/Blur Pass: Bilateral blur is used to upscale the shadow mask (if needed) to native resolution
  5. Reflection Probe capture: The scene is rendered into a cubemap from the camera's origin (for Low and Medium quality settings)
  6. Raster Pass: Objects are drawn and shaded using PBR lighting and the upscaled shadow mask
  7. Post-Processing: Tone mapping + Colour Grading

Physically Based Rendering (PBR)

The renderer uses a standard metallic/roughness workflow:

  • Cook-Torrance BRDF
    • Normal Distribution Function: GGX
    • Fresnel: Schlick approximation
    • Geometry term: Smith masking-shadowing (Schlick-GGX)
  • Image-based lighting (IBL):
    • Diffuse irradiance map
    • Prefiltered environment map using mip levels for roughness-based reflection blur.

PBR Screenshots:

These are screenshots of the same model (and skybox) shown with different PBR values


Mirror Finish - Metallic: 1.0, Roughness: 0.0

Mirror Finish


Matte Metal - Metallic: 1.0, Roughness: 0.1

Matte Metal


Plastic - Metallic: 0.0, Roughness: 0.0

Plastic


Different normal map

Normal Map


Graphics

The quality of different features is decided based on its individual graphics setting (0: Disabled, 1: Low, 2: Medium, 3: High)

Ray Tracing

Adjusts shadow mask resolution

  • Disabled: Uses direction based shading for objects; lacks shadows.
  • Low: 1/4 resolution
  • Medium: 1/2 resolution
  • High: Native resolution (recommended for RT reflections)

Reflections

Adjusts reflection type and quality

  • Disabled: Simple skybox reflections
  • Low: Reflection Probes with a 256x256px resolution per face
  • Medium: Reflection Probes with a 512x512px resolution per face
  • High: Ray Traced Reflections

Benchmarks

In standard scenes without extreme polygon counts, the engine maintains a stable 60 FPS. To push the limits, multiple stress tests were performed using duplicate instances of a high-poly sphere (~1.015M triangles per instance).

High-poly_sphere

  • Test Device: Apple MacBook Pro (M2 Pro)
  • Resolution: 1440p
  • Graphics
    • Ray Tracing: Medium (1/2 resolution)
    • Reflections: Skybox Reflections
Instances Approx. Triangles In Frame FPS
1 ~1.0M ~30
4 ~4.0M ~27
9 ~9.1M ~25
16 ~16.2M ~20

Performance degrades sub-linearly relative to triangle count, indicating effective chunk culling and BVH traversal.

Limitations

  • No Global Illumination
  • No true area-light soft shadows
  • Real transparency and Refraction not supported
  • No Volumetrics (e.g. fog)
  • Models are rendered in full detail; no LOD
  • Compute-based ray tracing only (no hardware RT such as MetalRT/DXR)
  • The current Ray Tracing is far from optimised

Attribution

This project started from the Learn WGPU tutorial (MIT licensed) as a starting point for wgpu setup and basic rendering. Credit to the Learn WGPU authors.

The codebase has since been substantially reworked and expanded; the ray traced shadows and reflections, PBR/IBL implementation, BVH acceleration, and engine architecture are original work.


The Rust logo is a trademark of the Rust Foundation. This project is an independent work and not an official Rust Foundation product.

Third-Party Assets

Some assets are from Poly Haven and are licensed under CC0.

License

This project is licensed under the MIT License

About

A from-scratch game engine written in Rust using WGPU, featuring real-time Ray Tracing and Physically Based Rendering (PBR)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors