-
-
Notifications
You must be signed in to change notification settings - Fork 45
refactor(noise): fix naming issues / file organisation #221
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
Open
PottierLoic
wants to merge
6
commits into
vlang:main
Choose a base branch
from
PottierLoic:clean_noise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6251098
rename functions
PottierLoic d5cc755
reduce duplication
PottierLoic 861fe0b
add test_utils file
PottierLoic 5b9eb97
clean(noise): reorder simplex and perlin files content for consistent…
PottierLoic 142a0eb
refactor: ensure consistency in `perlin_grad_3d`
PottierLoic 9379763
clean: place return before match in grad functions
PottierLoic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| module noise | ||
|
|
||
| // perlin_2d is a function that return a single value of perlin gen for a given 2d position | ||
| pub fn (gen Generator) perlin_2d(x f64, y f64) f64 { | ||
| xi := int(x) & 0xFF | ||
| yi := int(y) & 0xFF | ||
|
|
||
| xf := x - int(x) | ||
| yf := y - int(y) | ||
|
|
||
| u := fade(xf) | ||
| v := fade(yf) | ||
|
|
||
| pxi := gen.perm[xi] | ||
| pxi1 := gen.perm[xi + 1] | ||
|
|
||
| aa := gen.perm[pxi + yi] | ||
| ab := gen.perm[pxi + yi + 1] | ||
| ba := gen.perm[pxi1 + yi] | ||
| bb := gen.perm[pxi1 + yi + 1] | ||
|
|
||
| x1 := lerp(perlin_grad_2d(aa, xf, yf), perlin_grad_2d(ba, xf - 1, yf), u) | ||
| x2 := lerp(perlin_grad_2d(ab, xf, yf - 1), perlin_grad_2d(bb, xf - 1, yf - 1), u) | ||
|
|
||
| return (lerp(x1, x2, v) + 1) / 2 | ||
| } | ||
|
|
||
| // perlin_3d is a function that return a single value of perlin gen for a given 3d position | ||
| pub fn (gen Generator) perlin_3d(x f64, y f64, z f64) f64 { | ||
| xi := int(x) & 0xFF | ||
| yi := int(y) & 0xFF | ||
| zi := int(z) & 0xFF | ||
| xf := x - int(x) | ||
| yf := y - int(y) | ||
| zf := z - int(z) | ||
| u := fade(xf) | ||
| v := fade(yf) | ||
| w := fade(zf) | ||
|
|
||
| pxi := gen.perm[xi] | ||
| pxi_yi := gen.perm[pxi + yi] | ||
| pxi_yi1 := gen.perm[pxi + yi + 1] | ||
| pxi1 := gen.perm[xi + 1] | ||
| pxi1_yi := gen.perm[pxi1 + yi] | ||
| pxi1_yi1 := gen.perm[pxi1 + yi + 1] | ||
|
|
||
| aaa := gen.perm[pxi_yi + zi] | ||
| aba := gen.perm[pxi_yi1 + zi] | ||
| aab := gen.perm[pxi_yi + zi + 1] | ||
| abb := gen.perm[pxi_yi1 + zi + 1] | ||
| baa := gen.perm[pxi1_yi + zi] | ||
| bba := gen.perm[pxi1_yi1 + zi] | ||
| bab := gen.perm[pxi1_yi + zi + 1] | ||
| bbb := gen.perm[pxi1_yi1 + zi + 1] | ||
|
|
||
| mut x1 := lerp(perlin_grad_3d(aaa, xf, yf, zf), perlin_grad_3d(baa, xf - 1, yf, zf), | ||
| u) | ||
| mut x2 := lerp(perlin_grad_3d(aba, xf, yf - 1, zf), perlin_grad_3d(bba, xf - 1, yf - 1, | ||
| zf), u) | ||
| y1 := lerp(x1, x2, v) | ||
| x1 = lerp(perlin_grad_3d(aab, xf, yf, zf - 1), perlin_grad_3d(bab, xf - 1, yf, zf - 1), | ||
| u) | ||
| x2 = lerp(perlin_grad_3d(abb, xf, yf - 1, zf - 1), perlin_grad_3d(bbb, xf - 1, yf - 1, | ||
| zf - 1), u) | ||
| y2 := lerp(x1, x2, v) | ||
|
|
||
| return (lerp(y1, y2, w) + 1) / 2 | ||
| } | ||
|
|
||
| // perlin_grad_3d is a function that returns a single value of gradient gen for a given 3d position | ||
| fn perlin_grad_3d(hash int, x f64, y f64, z f64) f64 { | ||
| match hash & 0xF { | ||
| 0x0 { return x + y } | ||
| 0x1 { return -x + y } | ||
| 0x2 { return x - y } | ||
| 0x3 { return -x - y } | ||
| 0x4 { return x + z } | ||
| 0x5 { return -x + z } | ||
| 0x6 { return x - z } | ||
| 0x7 { return -x - z } | ||
| 0x8 { return y + z } | ||
| 0x9 { return -y + z } | ||
| 0xA { return y - z } | ||
| 0xB { return -y - z } | ||
| 0xC { return y + x } | ||
| 0xD { return -y + z } | ||
| 0xE { return y - x } | ||
| 0xF { return -y - z } | ||
| else { return 0 } | ||
| } | ||
| } | ||
|
|
||
| // lerp is a function that return a linear interpolation value for 2 given values and a factor | ||
| @[inline] | ||
| fn lerp(a f64, b f64, x f64) f64 { | ||
| return a + x * (b - a) | ||
| } | ||
|
|
||
| // 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) | ||
| } | ||
|
|
||
| // perlin_grad_2d is a function that return a gradient value for a given hash and 2d position | ||
| fn perlin_grad_2d(hash int, x f64, y f64) f64 { | ||
| match hash & 0xF { | ||
| 0x0 { return x + y } | ||
| 0x1 { return -x + y } | ||
| 0x2 { return x - y } | ||
| 0x3 { return -x - y } | ||
| 0x4 { return x } | ||
| 0x5 { return -x } | ||
| 0x6 { return x } | ||
| 0x7 { return -x } | ||
| 0x8 { return y } | ||
| 0x9 { return -y } | ||
| 0xA { return y } | ||
| 0xB { return -y } | ||
| 0xC { return y + x } | ||
| 0xD { return -y } | ||
| 0xE { return y - x } | ||
| 0xF { return -y } | ||
| else { return 0 } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| module noise | ||
|
|
||
| import rand | ||
| import vsl.float.float64 | ||
|
|
||
| fn test_perlin_2d() { | ||
| rand.seed([u32(3155200429), u32(3208395956)]) | ||
| mut gen := Generator.new() | ||
| gen.randomize() | ||
| result := gen.perlin_2d(0.125, 0.125) | ||
| expected := 0.4948387311305851 | ||
| assert float64.tolerance(result, expected, 1.0e-6) | ||
| } | ||
|
|
||
| fn test_perlin_3d() { | ||
| rand.seed([u32(3155200429), u32(3208395956)]) | ||
| mut gen := Generator.new() | ||
| gen.randomize() | ||
| result := gen.perlin_3d(0.125, 0.125, 0.125) | ||
| expected := 0.3713334855776509 | ||
| assert float64.tolerance(result, expected, 1.0e-6) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.