Reputation: 4593
I try to make 2D Perlin noise in the language Rust as described in: https://en.wikipedia.org/wiki/Perlin_noise
My random gradient function is as follows:
fn random_gradient_vector(rng : &mut rand::Rng) -> (f64, f64)
{
let theta = rng.next_f64() * 2.0 * 3.1415926536;
return (theta.cos(), theta.sin());
}
My interpolation function:
fn interpolate(a : f64, b : f64, x : f64) -> f64
{
let mut w = x*x*x*(x*(x*6.0 - 15.0) + 10.0);
return a * (1.0-w) + b * (w);
}
The result, at best:
What have I done wrong to cause the clearly visible grid lines? I think of three ways to fix:
1) There is a bug in my code I need to find. 2) Try adding different octaves at noise, maybe each one transformed to get rid of the artifacts. 3) Try a different noise algorithm.
Steve
Upvotes: 2
Views: 827
Reputation: 9289
The "blocky artifacts" occur, if the distance vectors are incorrectly calculated. They should point from the corners to the point:
var d0 = vec2.dot(vx0, [u,v]);
var d1 = vec2.dot(vx1, [u-1,v]);
var d2 = vec2.dot(vx2, [u,v-1]);
var d3 = vec2.dot(vx3, [u-1,v-1]);
Correct result: https://jsfiddle.net/f65w5jqa/
Blocky artifacts (the uv and the ones are exchanged): https://jsfiddle.net/p17fh1qp/
Upvotes: 1