展示 HN:一个能在任何 GPU 上运行的 Rust 光线追踪器——甚至在浏览器中。
Show HN: a Rust ray tracer that runs on any GPU – even in the browser

原始链接: https://github.com/tchauffi/rust-rasterizer

这个 Rust 项目展示了三种不同的光线追踪实现:基于 CPU 的渲染器、离线 GPU 渲染器和实时交互式 GPU 光线追踪器。 **CPU 光线追踪器** 执行软件渲染,将场景输出到 PPM 图像文件。**GPU 光线追踪器** 利用计算着色器实现显著更快的渲染速度,达到与 CPU 版本相当的质量,同样输出到 PPM。 最后,**实时 GPU 光线追踪器** 提供实时、交互式体验,具有相机控制。用户可以在具有光照/阴影的完整光线追踪和法线可视化调试模式之间切换。 所有版本都支持网格 (.obj) 和球体图元,GPU 版本需要兼容的 GPU(Vulkan、Metal 或 DirectX 12)。该项目使用最新的稳定 Rust 版本构建,并为每种实现提供清晰的构建/运行说明。 还有一个实时、基于 Web 的演示。

一位名为tchauffi的开发者创建了一个基于Rust的光线追踪器,它既可以在本地运行,也可以通过WebAssembly和wgpu图形库直接在浏览器中运行。该项目受到Sebastian Lague的作品启发,能够渲染具有网格渲染(由BVH加速)和通过直接和间接照明实现逼真光照的3D场景。 该光线追踪器作为一个免费的网页演示托管在GitHub Pages上([https://tchauffi.github.io/rust-rasterizer/](https://tchauffi.github.io/rust-rasterizer/)),源代码在GitHub上([https://github.com/tchauffi/rust-rasterizer](https://github.com/tchauffi/rust-rasterizer))。该开发者正在寻求具有wgpu、光线追踪或Rust图形编程经验的人的反馈,并计划未来探索使用Rust进行机器学习项目。
相关文章

原文

A rasterizer implementation in Rust

Example

Try it online: Live WebGPU Raytracer

This project includes three different raytracing implementations:

  1. CPU Raytracer - Software-based raytracing running on the CPU
  2. GPU Raytracer - Hardware-accelerated raytracing using GPU compute shaders (offline rendering)
  3. Live GPU Raytracer - Real-time interactive GPU raytracer with camera controls

CPU Raytracer (Software Rendering)

The CPU version renders scenes using traditional CPU-based raytracing and outputs to a PPM image file.

# Build and run (outputs to stdout, redirect to file)
cargo run --release > output.ppm

# Or build first, then run
cargo build --release
./target/release/rust-rasterizer > output.ppm

Features:

  • Full path tracing with multiple bounces
  • Direct and indirect lighting
  • Mesh support (.obj files)
  • Sphere primitives

GPU Raytracer (Offline Rendering)

The GPU version uses compute shaders to accelerate rendering, outputting to a PPM file.

# Build and run
cargo run --bin gpu_raytracer --release > output.ppm

# Or build separately
cargo build --bin gpu_raytracer --release
./target/release/gpu_raytracer > output.ppm

Features:

  • GPU-accelerated compute shader rendering
  • Same scene quality as CPU version
  • Significantly faster rendering times
  • Hardware-accelerated ray-triangle intersection

Live GPU Raytracer (Interactive Real-time)

The live version provides a real-time interactive window where you can navigate the scene.

# Run the live raytracer
cargo run --bin live_raytracer --release

Controls:

  • Mouse: Click and drag to rotate the camera
  • SPACE: Toggle between raytracing and normals visualization modes
  • Window Title: Displays current mode and FPS

Features:

  • Real-time GPU raytracing
  • Interactive camera controls
  • Two rendering modes:
    • Raytracing: Full path tracing with lighting and shadows
    • Normals: Fast visualization showing surface normals (useful for debugging)
  • Live FPS counter in window title
  • Rust (latest stable version)
  • For GPU versions: A GPU with compute shader support (Vulkan, Metal, or DirectX 12)
联系我们 contact @ memedata.com