Example

3D Raymarching

15

McPepic 2023-03-24 18:41 (Edited)

Renders a 3D object to the screen by “marching” along a separate ray for each pixel on the screen. Includes:

Pixel drawing
Signed distance function
Rays
Calculation of normals
Lighting

Light source will move every time the screen finishes drawing. Customize parameters at the top of the source. This can easily be expanded to use more shapes (with optimization) and more types of objects.

Update: Applied “cutout” to object. Added rounded box, sphere, and torus. Added dithering to reduce color-banding.
Update: Lighting was flipped. Now faces the right way. :)

3D Raymarching.nx | Open in app
2023-03-25 18:05
3D Raymarching.nx | Open in app
2023-03-24 18:41

Timo 2023-03-24 18:53

😮


GAMELEGEND 2023-03-24 19:25

I love Lowres NX!


GAMELEGEND 2023-03-24 19:43

ADD CC,1,1 TO 40

Could you always do this or is it new?


McPepic 2023-03-24 20:00

That's always been there. It just loops the value back around if it gets out of range.


was8bit 2023-03-25 03:54

Soooo coool


TheSailor 2023-03-25 04:33

I have zero expertise in any of the amazing things you guys are capable of 😅


SP4CEBAR 2023-03-25 14:51

Wow!


McPepic 2023-03-25 15:10

@TheSailor
I don’t 100% understand how everything works either. A lot of code was adapted from various sources I found online and put together. From there you just fill in the variables and troubleshoot anything that goes wrong. I’m going to go back in and try to optimize it, as it needs to check every object for every step of the ray.


412lop 2023-03-25 23:02

Oh my 3d ray marching in nx!


CreatorJo 2023-04-22 05:17

Holy cow! How is this even possible!?


McPepic 2023-04-22 12:51

@CreatorJo
For each pixel, calculate the direction of a ray pointing towards the current pixel from the camera (I have this stored in a lookup table). You then look at a single point along the ray starting at the camera. You use a signed distance function (you can find some online) to find the distance to the closest object. You then move the current point along the ray for that distance. You repeat the process until the distance traveled reaches some threshold or the point gets within a small distance from the surface of an object.


CreatorJo 2023-04-28 05:04

@McPepic
I think I mis-spoke. What I meant was "How is this even possible in LowRes NX!?"
I actually do know how raymarching works already. In fact, couple years back, I tried to make an implementation of it myself, but it was not that good, honestly.
What really baffled me was that LowRes NX had the capability to run these algorithms at that impressive of a speed. ^-^


was8bit 2023-04-28 12:06

credit to Timo on a wonderful and super fun programming experience :D


McPepic 2023-04-28 15:15

The fast floating-point operations certainly help. The only thing slowing it down is just that it has to run the ray-marching for every pixel on the screen! I even pre-calculate the rays for each pixel and store it in a lookup table for speed.


Mrlegoboy 2023-12-13 04:18

Ok, heres what you should do now:
Reduce the size of the render to about half or less
Save each frame in a buffer in ram
Replay the animation by loading the ram into the character memory


Log in to reply.