A Ray Caster in Rust (+ Webassembly)
This is a small sample project I used for learning Rust and experimenting with Webassembly. It is a simple Ray Caster that renders a simple 3D maze completely in Software. There is also a Swift version of the Ray Caster. It also contains a detailed introduction into Ray Casting with code examples.
Try the browser version to play with it instantly or compile on your desktop!
Browser: iOS 11.3+ (iOS 11.2.x is broken…) Android 8.0 At least: Safari 11, Chrome, Firefox
Native: macOS, Linux, Windows
How to Build
To build the desktop or the browser version you need to install the Rust compiler first:
curl https://sh.rustup.rs -sSf | sh
During the installation, you may be asked for installation options. Just press Enter to select the default option. After the installation succeeded make sure that all environment variables are set up:
You may also need to install libSDL. On macOS you can do this with brew:
brew install sdl2
On Ubuntu / Debian:
apt install libsdl2-dev
The Desktop Version
You can build an run the desktop version by typing
The Browser Version
To build the browser version, you need to install the Emscripten SDK. Create a new folder on your file system and open it in your terminal. Then run the following commands to install the SDK:
git clone https://github.com/juj/emsdk.git cd emsdk ./emsdk update ./emsdk install latest ./emsdk activate latest source ./emsdk_env.sh embuilder.py build sdl2
We also need to setup Webassembly support for Rust:
rustup target add wasm32-unknown-emscripten
Finally, switch back to the ray caster source folder to build the browser version:
The compilation result is then stored to the folder
html. Since Webassembly can’t be directly embedded to a HTML page you need a web server for running the binary. Just copy the entire
html folder to your web server and then open the
index.html page in your browser.
Changing the Map
You can change the map by editing the text file in „assets/map.txt“. Each character represents a field in the map. Use the space character for empty fields. You can use „R“, „G“, „B“, „Y“, „O“ to create colored walls. The map is embedded during compilation, so every change of the map requires a recompilation.
I’m still a Rust novice: I’m really interested in any feedback regarding my Rust code.