Show HN: 3D Raycasting Engine in JavaScript
ahuth.github.io
ahuth.github.io
I went deep down the rabbit hole with raycaster engines over the last 2 years. While this particular rendering technique has lots of limitations (like a fixed camera - unable to look up and down) it also has some interesting properties. Such as the ability to create windows, mirrors, and portals very easily by re-directing the way the rays fly.
I actually put together an entire game around this to test the limits of what I could achieve with raycasting. If anyone is curious you can see a video of it on my blog: https://blog.sb1.io/gateescape/
What I think would be a great project is moving the casting and drawing part over to a compute shader. My engine is multi threaded and chunks the screen so each thread can do some section of the work. Unfortunately I want the engine to be cross platform, but Apple has made that rather difficult with the requirement of Metal going forward. If anyone has seen this style of raycasting implemented on a GPU I would be very interested!
The simplicity of them makes them fun to experiment with: I used one in a Ludum Dare game (https://www.mrspeaker.net/dev/ld27/) and made the field-of-view and movement speed a "mechanic".
https://permadi.com/1996/05/ray-casting-tutorial-16/#LOOKING...
Thanks again!
Spheres + Displacement
https://www.dwitter.net/d/15947
https://www.dwitter.net/d/15951
Spheres Orbit + Fake Lighting
https://www.dwitter.net/d/16192
"Smoke" (3D perlin-esc noise)
https://www.dwitter.net/d/16179
https://www.dwitter.net/d/16169
Snowman + Snow
https://www.dwitter.net/d/16955
Not very practical for much of an engine of course, and also much slower than casting, but it makes it dead easy to understand by removing matrix transforms et al.Though my "negative nancy" comment would be that this is the wrong tool for the job (or at least, it could use a little renaming?) because after jumping from Scene to App to useCastRays hook... I finally found all the actual logic in "utils": which I'm always suspicious of ;)
Anyhoo - really cool, I'd like to see how the 2D sprites are integrated!
One thing that surprised me about this is how fast the browser renders 320 absolutely positioned divs that are constantly updating.
But then I thought about it some more, and realized that many websites these days probably have way more DOM and DOM manipulation than that going on!
We've seen a lot of raycasters, so one made with React and hooks isn't a bad thing.
Interesting code of using useRef with your state, and then useEffect to update it every render. A simple/better Redux?
Ray-Casting is fun because it's a relatively simply algorithm to "rediscover", and can be coded in just a few hundred lines of code... I've debated making it a guided question to ask during interviews, though I've resisted thus far because I'm afraid it might be a bit too much for a junior candidate, and might be too easy for a senior candidate.
That's probably where your guidance comes in, though.
A couple of years ago, I wrote a Javascript implementation myself: https://github.com/avik-das/js-raycast
And then, when asm.js (precursor to WebAssembly) became a thing, I rewrote it in C and compiled it to asm.js: https://github.com/avik-das/emcc-raycast
I'd already written a ray tracer (so a fully 3D renderer) prior to that in college, so it was fun to learn about a more limited algorithm that was better suited to older hardware.
Shameless: http://www.dormando.me/post/fpga-raycaster/ - I did one of these last year with an arduino using an FPGA as a weird GPU :)
edit: it suddenly started working after I switched to a different tab, and then back to the raycast's tab.
Do you have shortcuts bound to those keys? Maybe I need to preventDefault() on keypresses, or something.
return findWall(map, position.add(deltaX, deltaY), deltaX, deltaY);
no reason not to loop here.
You can have perspective in flatland.
Made for some nice games in the early 90s, though!