Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cellular Distance gives out-of-range results #141

Open
ForeverZer0 opened this issue Jun 15, 2024 · 1 comment
Open

Cellular Distance gives out-of-range results #141

ForeverZer0 opened this issue Jun 15, 2024 · 1 comment

Comments

@ForeverZer0
Copy link
Contributor

I am uncertain if this is known issue, but the cellular distance algorithm will output values outside the range of of -1.0 and 1.0 under normal conditions, specifically with the "hybrid" distance function with my tests.

Minimal example using the C port:

#include <stdio.h>
#define FNL_IMPL
#include "FastNoiseLite.h"

#define SIZE 768

int main(int argc, const char **argv) {
    fnl_state state = fnlCreateState();
    state.seed = 1337;
    state.noise_type = FNL_NOISE_CELLULAR;
    state.frequency = 0.02f;
    state.cellular_distance_func = FNL_CELLULAR_DISTANCE_HYBRID;
    state.cellular_return_type = FNL_CELLULAR_RETURN_TYPE_DISTANCE2;
    state.cellular_jitter_mod = 1.0;

    int over = 0;
    int under = 0;
    for (int y = 0; y < SIZE; y++) {
        for (int x = 0; x < SIZE; x++) {
            float f = fnlGetNoise2D(&state, x, y);
            if (f > 1.0f) over++;
            if (f < -1.0f) under++;
        }
    }
    printf("TOTAL: %d, OVER: %d, UNDER: %d\n", SIZE * SIZE, over, under);
    return 0;
}

Output:

TOTAL: 589824, OVER: 77817, UNDER: 0

Other language ports output the same out-of-range values with equivalent settings. The web preview is coded defensively and clamps the values into range before conversion to an integer, but it is visually noticeable with the abundance of white in the output compared to similar configurations.

For the C port, this is the offending part of the algorithm on line 1647:

float newDistance = (FastAbs(vecX) + FastAbs(vecY)) + (vecX * vecX + vecY * vecY);

Assuming this is correct, would it be better to clamp it before returning the result from this function?

@Auburn
Copy link
Owner

Auburn commented Jun 16, 2024

Cellular output is actually never bounded, it's just normally falls within bounds unless using hybrid distance.
I have done some work to add correct bounding to it, but since it is a output changing fix I'd want to bump the minor version at least

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants