Coder Social home page Coder Social 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.

raycaster-rust's People

Contributors

hydrixos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mlaass elo-siema

raycaster-rust's Issues

Can't build for web

Hi!
I forked your project and modified it to use hyperbolic space, and would like to release it for web.
Problem is, when I build it for web using build-web.sh using latest stable Rust and latest Emscripten SDK, I'm getting blank window in the browser and the console says this:

Calling stub instead of sigaction() 3 raycaster.js:1:200486
emscripten_set_main_loop_timing: Cannot set timing mode for main loop since a main loop does not exist! Call emscripten_set_main_loop first to set one up. raycaster.js:1:100579
    _emscripten_set_main_loop_timing https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    _eglSwapInterval https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:0
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:576087
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:556576
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:223620
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:207062
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:98679
    invoke_vii https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:63431
    invoke_vii https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:55167
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:60324
    invoke_ii https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:124368
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:58224
    createExportWrapper https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    callMain https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    doRun https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    run https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    runCaller https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    removeRunDependency https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    receiveInstance https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    receiveInstantiatedSource https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    (asynchroniczny: promise callback)
    instantiateAsync https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    (asynchroniczny: promise callback)
    instantiateAsync https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    createWasm https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
exception thrown: RuntimeError: unreachable executed,@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[438]:0x180cb
invoke_vii@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:221535
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[346]:0xf8f1
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[321]:0xe343
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[344]:0xf3b8
browserIterationFunc@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:186476
runIter@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:105494
Browser_mainLoop_runner@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:103972
FrameRequestCallback*requestAnimationFrame@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:114682
Browser_mainLoop_scheduler_rAF@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:101118
setMainLoop@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:104348
_emscripten_set_main_loop@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:186491
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[320]:0xd77f
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[336]:0xeba4
invoke_ii@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:222359
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[561]:0x1e5d0
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:wasm-function[322]:0xe370
createExportWrapper/<@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:22853
callMain@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:257917
doRun@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:258509
run@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:258673
runCaller@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:257350
removeRunDependency@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:21746
receiveInstance@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:23923
receiveInstantiatedSource@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:24220
promise callback*instantiateAsync/<@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:24806
promise callback*instantiateAsync@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:24709
createWasm@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:25266
@https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1:218429
raycaster.js:1:105616
Uncaught Error: undefined raycaster.wasm:98507:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:98507
    invoke_vii https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:63729
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:58179
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:62392
    browserIterationFunc https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    runIter https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    Browser_mainLoop_runner https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    (asynchroniczny: FrameRequestCallback)
    requestAnimationFrame https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    Browser_mainLoop_scheduler_rAF https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    setMainLoop https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    _emscripten_set_main_loop https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:55167
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:60324
    invoke_ii https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:124368
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.wasm:58224
    createExportWrapper https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    callMain https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    doRun https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    run https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    runCaller https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    removeRunDependency https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    receiveInstance https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    receiveInstantiatedSource https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    (asynchroniczny: promise callback)
    instantiateAsync https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    (asynchroniczny: promise callback)
    instantiateAsync https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    createWasm https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1
    <anonimowa> https://elo-siema.github.io/hyperbolic-raycaster-rust/html/raycaster.js:1

This happens both when hosted and using emrun index.html, and with both your original project and my fork.

Would you mind trying to build and run it with your current toolchain, or perhaps do you know what was the latest Rust and Emscripten versions that were able to build it?

Thanks!

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.