Skip to content

Commit

Permalink
optimized 2d perm access + names
Browse files Browse the repository at this point in the history
  • Loading branch information
PottierLoic committed Jan 2, 2024
1 parent 8d682fa commit 604af08
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
17 changes: 9 additions & 8 deletions noise/perlin2d.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import rand

pub struct Perlin {
mut:
perm []int = (rand.shuffle_clone(permutations) or { permutations })
perm []int = rand.shuffle_clone(permutations) or { permutations }
}

// 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() {
perlin.perm = (rand.shuffle_clone(permutations) or { permutations })
perlin.perm = rand.shuffle_clone(permutations) or { permutations }
}

// perlin2d is a function that return a single value of perlin noise for a given 2d position
Expand All @@ -24,12 +24,13 @@ pub fn (perlin Perlin) perlin2d(x f64, y f64) f64 {
u := fade(xf)
v := fade(yf)

a := perlin.perm[xi] + yi
aa := perlin.perm[a]
ab := perlin.perm[a + 1]
b := perlin.perm[xi + 1] + yi
ba := perlin.perm[b]
bb := perlin.perm[b + 1]
pxi := perlin.perm[xi]
pxi1 := perlin.perm[xi + 1]

aa := perlin.perm[pxi + yi]
ab := perlin.perm[pxi + yi + 1]
ba := perlin.perm[pxi1 + yi]
bb := perlin.perm[pxi1 + yi + 1]

x1 := lerp(grad2d(aa, xf, yf), grad2d(ba, xf - 1, yf), u)
x2 := lerp(grad2d(ab, xf, yf - 1), grad2d(bb, xf - 1, yf - 1), u)
Expand Down
20 changes: 10 additions & 10 deletions noise/perlin3d.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ pub fn (perlin Perlin) perlin3d(x f64, y f64, z f64) f64 {

pxi := perlin.perm[xi]
pxi_yi := perlin.perm[pxi + yi]
pxi_yi_b := perlin.perm[pxi + yi + 1]
pxib := perlin.perm[xi + 1]
pxib_yi := perlin.perm[pxib + yi]
pxib_yi_b := perlin.perm[pxib + yi + 1]
pxi_yi1 := perlin.perm[pxi + yi + 1]
pxi1 := perlin.perm[xi + 1]
pxi1_yi := perlin.perm[pxi1 + yi]
pxi1_yi1 := perlin.perm[pxi1 + yi + 1]

aaa := perlin.perm[pxi_yi + zi]
aba := perlin.perm[pxi_yi_b + zi]
aba := perlin.perm[pxi_yi1 + zi]
aab := perlin.perm[pxi_yi + zi + 1]
abb := perlin.perm[pxi_yi_b + zi + 1]
baa := perlin.perm[pxib_yi + zi]
bba := perlin.perm[pxib_yi_b + zi]
bab := perlin.perm[pxib_yi + zi + 1]
bbb := perlin.perm[pxib_yi_b + zi + 1]
abb := perlin.perm[pxi_yi1 + zi + 1]
baa := perlin.perm[pxi1_yi + zi]
bba := perlin.perm[pxi1_yi1 + zi]
bab := perlin.perm[pxi1_yi + zi + 1]
bbb := perlin.perm[pxi1_yi1 + zi + 1]

mut x1 := lerp(grad3d(aaa, xf, yf, zf), grad3d(baa, xf - 1, yf, zf), u)
mut x2 := lerp(grad3d(aba, xf, yf - 1, zf), grad3d(bba, xf - 1, yf - 1, zf), u)
Expand Down

0 comments on commit 604af08

Please sign in to comment.