https://github.com/statusfailed/wgpu-voxel-terrain
A WebGPU demo for rendering voxels
https://github.com/statusfailed/wgpu-voxel-terrain
Last synced: 9 months ago
JSON representation
A WebGPU demo for rendering voxels
- Host: GitHub
- URL: https://github.com/statusfailed/wgpu-voxel-terrain
- Owner: statusfailed
- Created: 2023-11-01T12:51:43.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-01T12:52:18.000Z (over 2 years ago)
- Last Synced: 2025-01-31T08:18:51.509Z (about 1 year ago)
- Language: Rust
- Size: 2.82 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# WGPU Voxel Terrain
Voxel terrain generated by a compute shader & then rendered with ambient
occlusion on the GPU.
Run with `cargo run` and use the WASD keys to zoom and rotate the camera:

This is just a project to learn WGPU; it's not very useful, and the code could
do with a tidy!
# Overview
Here's roughly what this program does:
- Every frame, a [compute shader](./src/compute.wgsl) generates terrain as a giant array of voxels
- Voxels are culled by checking if they have all opaque neighbours
- Voxel data is used as the vertex buffer in the [vertex shader](./src/shader.wgsl)
- Render pass uses `DrawIndirect` to render voxels
- `vertex_count: 36` sets 36 vertices per voxel, corresponding to the
`6*2*3` vertices needed to make a cube from triangles
- The number of visible voxels changes dynamically after culling, so it gets
calculated and passed to the DrawIndirect call on each frame as
`instance_count`.
- Fragment shader computes locations of each vertex by adding the triangle
vertex offsets (stored as a constant array `TRI_VERTICES`) to the base voxel
coords
- Final shading and ambient occlusion is calculated in `vs_main` of the vertex
shader.
# Acknowledgements
Very helpful sources for learning:
- the [learn WGPU tutorial](https://sotrh.github.io/learn-wgpu/)
- gfx-rs [wgpu](https://github.com/gfx-rs/wgpu) (see examples)