News for hackers. Stuff that matters.
Re:SDF Fractal Noise by Athas on Tuesday July 13, 2021 @10:19AM (#27821299)
> So, when adding a regular fBM, sine wave or any other displacement function to a "host" SDF, we don't get a valid SDF anymore (we violate the principle that the gradient of an SDF must have length 1.0).

Can someone elaborate or point to an elaboration of this point? I guess there must be a good reason why the gradient cannot just be normalised, but I don't know what it is.

Re:SDF Fractal Noise by EliasWatson on Tuesday July 13, 2021 @02:29PM (#27824414)
I'm not the greatest at explaining things, but I'll give it a shot.

Just so everyone is on the same page, an SDF is a function that simply returns the distance to the closest surface for any input point. If the distance is positive, the point is outside of the geometry. If the distance is negative, the point is inside the geometry. The gradient of the SDF is calculated by sampling the SDF across multiple points, so the SDF itself doesn't return a gradient.

An SDF is no longer a valid SDF if the distance isn't the true distance to the closest surface. This is usually caused by transforming the SDF non-uniformly, such as adding a sine wave. Another way to think about this is that a valid SDF is in euclidean space while an invalid SDF is in non-euclidean space. Applying a displacement function will compress and stretch space non-uniformly.

Imagine you have an SDF function that just represents a sphere. Then you transform the vertical position of the sphere based on the absolute value of the horizontal position of the input point. This will turn the sphere into a V shape. However, the SDF is no longer a valid SDF. If you sampled a point next to one of the inner walls of the V shape, it wouldn't return the distance to the wall, it would return the distance to the point on the shape below it.

The gradient for every possible point in a valid SDF will be a unit vector because space is uniform. A non-valid SDF will contain non-unit-vector gradients between areas of non-uniformity.

Hopefully that made sense. If you are still confused, I would suggest writing a simple raymarcher on Shadertoy and playing around with distorting SDFs yourself. A simple raymarcher is only a couple lines of code (probably less than 20 lines of GLSL).

Re:SDF Fractal Noise by xyzzy_plugh on Tuesday July 13, 2021 @10:40AM (#27821534)
Because you're displacing the existing gradient. The existing SDF is by definition valid but now being transformed. It's been a while since I dabbled in this but I'm not sure how you'd normalize and maintain the transform. If you naively normalize then you lose all the smooth continuity of the function, which is no bueno.
Re:SDF Fractal Noise by Athas on Tuesday July 13, 2021 @10:46AM (#27821620)
Could you naively compose the distance functions and then use automatic differentiation to compute the gradient? As long as the distance functions are differentiable, so should their composition be.
Re:SDF Fractal Noise by xyzzy_plugh on Tuesday July 13, 2021 @10:51AM (#27821702)
Possibly but I suspect that is more complex than it seems at first glance.
Re:SDF Fractal Noise by Athas on Tuesday July 13, 2021 @10:53AM (#27821729)
Certainly, it reduces to reverse-mode AD on fairly arbitrary (although usual scalar) code. You definitely need a compiler or preprocessor to do that for you.
Re:SDF Fractal Noise by pxf on Tuesday July 13, 2021 @12:31PM (#27823009)
After deforming the space with a sine function there is no problem in calculating the gradient. The problem is calculating the new correct distance to the nearest surface after a space transformation. You can see this problem in 2D, here I made an example https://www.shadertoy.com/view/NtsSRf
Re:SDF Fractal Noise by user-the-name on Tuesday July 13, 2021 @11:50AM (#27822455)
The gradient is derived from the SDF. This derived value needs to be of unit length. Normalising the value after deriving it does nothing.

We don't use this gradient for anything much. What we use is the knowledge that the gradient is of unit length.