diff --git a/noise/perlin2d.v b/noise/perlin2d.v index a65a553cf..4e5d2661e 100644 --- a/noise/perlin2d.v +++ b/noise/perlin2d.v @@ -2,11 +2,17 @@ module noise import rand +// Perlin is a struct that hold the permutation set for perlin noise pub struct Perlin { mut: perm []int = rand.shuffle_clone(permutations) or { panic(err) } } +// new is a function that return a new Perlin struct +pub fn Perlin.new() Perlin { + return Perlin{} +} + // randomize is a function that shuffle the permutation set inside the Perlin struct // will not shuffle if rand.seed is not changed pub fn (mut perlin Perlin) randomize() { @@ -38,10 +44,13 @@ pub fn (perlin Perlin) perlin2d(x f64, y f64) f64 { return (lerp(x1, x2, v) + 1) / 2 } +// fade is a function that return a fade value for a given value +@[inline] fn fade(t f64) f64 { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0) } +// grad2d is a function that return a gradient value for a given hash and 2d position fn grad2d(hash int, x f64, y f64) f64 { match hash & 0xF { 0x0 { return x + y } @@ -64,6 +73,8 @@ fn grad2d(hash int, x f64, y f64) f64 { } } +// lerp is a function that return a linear interpolation value for a given 2 values and a factor +@[inline] fn lerp(a f64, b f64, x f64) f64 { return a + x * (b - a) } diff --git a/noise/perlin2d_test.v b/noise/perlin2d_test.v index 410bd4327..6b4c81d83 100644 --- a/noise/perlin2d_test.v +++ b/noise/perlin2d_test.v @@ -5,7 +5,8 @@ import vsl.float.float64 fn test_perlin2d() { rand.seed([u32(3155200429), u32(3208395956)]) - mut gen := Perlin{} + + mut gen := Perlin.new() gen.randomize() result := gen.perlin2d(0.125, 0.125) diff --git a/noise/perlin3d.v b/noise/perlin3d.v index a0ebdcd06..c32a6131e 100644 --- a/noise/perlin3d.v +++ b/noise/perlin3d.v @@ -38,6 +38,7 @@ pub fn (perlin Perlin) perlin3d(x f64, y f64, z f64) f64 { return (lerp(y1, y2, w) + 1) / 2 } +// grad3d is a function that returns a single value of gradient noise for a given 3d position fn grad3d(hash int, x f64, y f64, z f64) f64 { match hash & 0xF { 0x0 { return x + y } diff --git a/noise/perlin3d_test.v b/noise/perlin3d_test.v index ceb3061e5..8edde204c 100644 --- a/noise/perlin3d_test.v +++ b/noise/perlin3d_test.v @@ -5,7 +5,8 @@ import vsl.float.float64 fn test_perlin3d() { rand.seed([u32(3155200429), u32(3208395956)]) - mut gen := Perlin{} + + mut gen := Perlin.new() gen.randomize() result := gen.perlin3d(0.125, 0.125, 0.125)