fractal-noise-js icon indicating copy to clipboard operation
fractal-noise-js copied to clipboard

Support getting noise value at arbitrary points to support warping

Open lukehesluke opened this issue 8 months ago • 0 comments

Hi, I'd like to implement warping (e.g. https://iquilezles.org/articles/warp/) using this library. An example warping function is provided in that article. It looks like:

float pattern( in vec2 p )
{
    vec2 q = vec2( fbm( p + vec2(0.0,0.0) ),
                   fbm( p + vec2(5.2,1.3) ) );

    return fbm( p + 4.0*q );
}

where fbm(..) is a fractal noise function that takes, in this case, a 2D vector and returns the noise value at that point. This example therefore deals with the same kind of noise as makeRectangle(..) in fractal-noise-js.

This technique requires being able to get noise values at arbitrary points and is therefore not presently possible with the grid-like approach used in this library.

One way to be able to support this and maintain the grid-first approach that makes this library very approachable would be to provide a function like:

getRectangleNoiseValue(noise2: Noise2Fn, options: Options, x: number, y: number): number {

Which gets the 2D noise value at x and y, which can here be non-integer, non-positive, etc, but still describe the same surface as makeRectangle(..). makeRectangle(..) could then be adjusted to use the above function.

Extra Benefits

  • There are many other techniques that similarly make use of being able to sample arbitrary points. For example, calculating normals.
  • For situations in which the grid is too large to be stored in memory and it is better to iterate through and reduce.

Example

An example of how this could look is in this draft PR: https://github.com/joshforisha/fractal-noise-js/pull/6. Let me know if you agree with the approach or would prefer changes (or disagree with its inclusion in this repo) and then it can potentially be finalised.

lukehesluke avatar Jun 06 '24 22:06 lukehesluke