Code Monkey home page Code Monkey logo

raycaster-rust's Introduction

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!

Supported Plattforms

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:

source ~/.profile

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

cargo run

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:

./build-web.sh

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.

Feedback

I’m still a Rust novice: I’m really interested in any feedback regarding my Rust code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.