TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: 3D Raycasting Engine in JavaScript

113 pointsby ahuthabout 5 years ago

16 comments

gh123manabout 5 years ago
Very cool!<p>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.<p>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: <a href="https:&#x2F;&#x2F;blog.sb1.io&#x2F;gateescape&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.sb1.io&#x2F;gateescape&#x2F;</a><p>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!
评论 #22741483 未加载
评论 #22743418 未加载
评论 #22745114 未加载
评论 #22741545 未加载
tomxorabout 5 years ago
Very neat. Shameless plug: Things can get extremely minimal if you make the view frustum orthogonal and also use <i>raymarching</i> instead, then the geometry (SDF) is very concise to define... a lot of the math disappears and the code gets super short... like 140 chars short:<p><pre><code> Spheres + Displacement https:&#x2F;&#x2F;www.dwitter.net&#x2F;d&#x2F;15947 https:&#x2F;&#x2F;www.dwitter.net&#x2F;d&#x2F;15951 Spheres Orbit + Fake Lighting https:&#x2F;&#x2F;www.dwitter.net&#x2F;d&#x2F;16192 &quot;Smoke&quot; (3D perlin-esc noise) https:&#x2F;&#x2F;www.dwitter.net&#x2F;d&#x2F;16179 https:&#x2F;&#x2F;www.dwitter.net&#x2F;d&#x2F;16169 Snowman + Snow https:&#x2F;&#x2F;www.dwitter.net&#x2F;d&#x2F;16955 </code></pre> 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.
mrspeakerabout 5 years ago
Ha ha - very interesting use of React... I love that the columns are components!<p>Though my &quot;negative nancy&quot; 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 &quot;utils&quot;: which I&#x27;m always suspicious of ;)<p>Anyhoo - really cool, I&#x27;d like to see how the 2D sprites are integrated!
评论 #22741531 未加载
0-_-0about 5 years ago
Interesting trivia: The game Wolfenstein 3D originally used raycasting to determine visibility. Later, id software signed a contract with Nintendo for a SNES port, which they outsourced to a contractor while they worked on Doom. During this time Carmack realized that the same method won&#x27;t be fast enough for Doom and started studying BSP [1] trees. Meanwhile, the contractor was unable to do the port because of the very paltform-specific code (heavy optimisations for the 386 and 486). So id software took time off from Doom and went back to do the port themselves, eventually succeeeding by using BSP for visibility and a lower resolution.<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Binary_space_partitioning" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Binary_space_partitioning</a>
tombertabout 5 years ago
A couple years ago I got bored on the train, and coded a simple ray-caster in Pico-8.<p>Ray-Casting is fun because it&#x27;s a relatively simply algorithm to &quot;rediscover&quot;, and can be coded in just a few hundred lines of code... I&#x27;ve debated making it a guided question to ask during interviews, though I&#x27;ve resisted thus far because I&#x27;m afraid it might be a bit too much for a junior candidate, and might be too easy for a senior candidate.
评论 #22742010 未加载
akdasabout 5 years ago
Great demo.<p>A couple of years ago, I wrote a Javascript implementation myself: <a href="https:&#x2F;&#x2F;github.com&#x2F;avik-das&#x2F;js-raycast" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;avik-das&#x2F;js-raycast</a><p>And then, when asm.js (precursor to WebAssembly) became a thing, I rewrote it in C and compiled it to asm.js: <a href="https:&#x2F;&#x2F;github.com&#x2F;avik-das&#x2F;emcc-raycast" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;avik-das&#x2F;emcc-raycast</a><p>I&#x27;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.
评论 #22743449 未加载
atum47about 5 years ago
Nice. Not so long ago I was trying to make one without the grid layout, using lines instead. Never got the math quite right thou. Here&#x27;s the link if you wanna take a look:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;myRaycast" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;myRaycast</a>
dormandoabout 5 years ago
These are fun!<p>Shameless: <a href="http:&#x2F;&#x2F;www.dormando.me&#x2F;post&#x2F;fpga-raycaster&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.dormando.me&#x2F;post&#x2F;fpga-raycaster&#x2F;</a> - I did one of these last year with an arduino using an FPGA as a weird GPU :)
malkiaabout 5 years ago
Also this -&gt; <a href="https:&#x2F;&#x2F;keithclark.co.uk&#x2F;labs&#x2F;css-fps&#x2F;" rel="nofollow">https:&#x2F;&#x2F;keithclark.co.uk&#x2F;labs&#x2F;css-fps&#x2F;</a> - 3D using CSS :)
btzllabout 5 years ago
Very cool but the WASD keys do not work on Firefox. Arrow keys work fine though!<p>edit: it suddenly started working after I switched to a different tab, and then back to the raycast&#x27;s tab.
评论 #22742418 未加载
matt_eeeeabout 5 years ago
Nice! A trick from Wolfenstein 3D to improve the look is to give all the north-south walls a lighter colour, and all the east-west walls a darker colour.
lukevpabout 5 years ago
Can you add virtual keys so I can play with it on my phone? I like raycasters, written a few back in the day :)
评论 #22745831 未加载
high_byteabout 5 years ago
you&#x27;re killing your stack with redundant recursion, here:<p>return findWall(map, position.add(deltaX, deltaY), deltaX, deltaY);<p>no reason not to loop here.
wdbabout 5 years ago
Doesn&#x27;t work with the arrow keys :(
alyssaxuuabout 5 years ago
Oh wow, this is quite impressive.
tantalorabout 5 years ago
Looks more like 2d?
评论 #22740882 未加载