Razaekel / noise-rs

Procedural noise generation library for Rust.
Apache License 2.0
854 stars 120 forks source link

the method get() of perlin's noise will always be 0 when x, y >=1 #316

Closed lishaoxia1985 closed 7 months ago

lishaoxia1985 commented 1 year ago
let perlin = Perlin::new(1000);
let x_size = 1024;
let y_size = 1024;
for y in 0..y_size {
        for x in 0..x_size {
            let p = [x as f64, y as f64];
            let perlin_value = perlin.get(p);
            dbg!(perlin_value);
       }
}

it will always print "perlin_value = 0.0". That means you should use this:

for y in 0..y_size {
        for x in 0..x_size {
            let p = [x as f64 / x_size as f64, y as f64 / y_size as f64];
            let perlin_value = perlin.get(p);
            dbg!(perlin_value);
       }
}

This is differernt with other noise (e.g. worley). We should add some comment to describe this.

Sp0-0n commented 1 year ago

Thanks for commenting this, I was really confused why my code was just spitting out zeroes.

Kyonaliz commented 10 months ago

what if my use-case has x_size = infinity? (like for procedurally generating terrain)

amaranth commented 10 months ago

You could probably just get away with adding an offset or just divide by 10000 or something. There isn't really anything special about dividing by your world size, you just need to avoid only sampling on whole numbers.

lishaoxia1985 commented 10 months ago

@amaranth hello, do you think my issue is a bug? No I have find another problem: RidgedMulti is only used in the Worley noise whose return type is Value, because you can't set return type when you use Worley noise in RidgedMulti.

PrinceOfBorgo commented 7 months ago

The problem you are reporting is the normal behaviour of Perlin noise. Perlin noise returns 0 when sampled on whole numbers. Your second example works because all the samples are fractional numbers. More info here.

lishaoxia1985 commented 7 months ago

@PrinceOfBorgo Thank you😂 Do you think we should add some comment to describe this?