HackerPoet / PySpace

GLSL Fractal Ray Marcher in Python
MIT License
1.15k stars 147 forks source link

How did you come up with the NP functions? #14

Closed FilipLitwora closed 4 years ago

FilipLitwora commented 4 years ago

Hi, I would like to know how did you come up with NP functions for each shape. Have you found some article on this topic? Please let me know.

HackerPoet commented 4 years ago

Great question!

For primitive geometry like cubes and spheres, the nearest point methods are pretty easy and you can find the formulas online, so I won't go over those.

But for the folds, things get a little more complicated. Each fold is a transformation that inputs a 3D point P and outputs a 3D point P'. To get the NP function, we fold the space normally, get the nearest point, and then unfold. To unfold, all we have to do is find the inverse equation from P' to P. However this is usually impossible since the functions are non-invertible, you don't know which side of the plane you came from. However, we DO know which path we took during the original folding, so by using that point P as extra information during the unfolding, you can determine which way to unfold the new point Q.