-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* noise: add simplex 2d implementation * noise: add simplex 1d/3d implementation * noise: add simplex 4d implementation * noise: fmt and clean * noise: rename `Perlin` struct to `Generator` and add tests for simplex noise * noise: add comments * noise: examples * noise: add fractal noise example * noise: fmt * Update README.md * Update README.md --------- Co-authored-by: Ulises Jeremias <[email protected]>
- Loading branch information
1 parent
c76b097
commit ebaaf2d
Showing
11 changed files
with
597 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
# Example - noise_fractal_2d 📘 | ||
|
||
This example demonstrates the usage of the V Scientific Library for generating pink noise. | ||
|
||
## Instructions | ||
|
||
1. Ensure you have the V compiler installed. You can download it from [here](https://vlang.io). | ||
2. Ensure you have the VSL installed. You can do it following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)! | ||
3. Navigate to this directory. | ||
4. Run the example using the following command: | ||
|
||
```sh | ||
v run main.v | ||
``` | ||
|
||
Enjoy exploring the capabilities of the V Scientific Library! 😊 |
This file contains 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,54 @@ | ||
module main | ||
|
||
import rand | ||
import vsl.noise | ||
import vsl.plot | ||
|
||
fn main() { | ||
// Creation of the noise generator. | ||
// Other noise functions and dimensions count are available. | ||
rand.seed([u32(3155200429), u32(3208395956)]) | ||
mut generator := noise.Generator.new() | ||
generator.randomize() | ||
|
||
mut x := []f64{} | ||
mut y := []f64{} | ||
mut z := []f64{} | ||
|
||
// 5 layers of simplex noise | ||
octaves := 5 | ||
persistence := 0.5 | ||
|
||
for xx in 0 .. 200 { | ||
for yy in 0 .. 200 { | ||
mut total := 0.0 | ||
mut frequency := 1.0 | ||
mut amplitude := 1.0 | ||
mut max_value := 0.0 | ||
|
||
for _ in 0 .. octaves { | ||
total += generator.simplex_2d(0.03 * xx * frequency, 0.03 * yy * frequency) * amplitude | ||
max_value += amplitude | ||
amplitude *= persistence | ||
frequency *= 2.0 | ||
} | ||
|
||
// Normalize to [-1, 1] | ||
total /= max_value | ||
x << xx | ||
y << yy | ||
z << total | ||
} | ||
} | ||
|
||
mut plt := plot.Plot.new() | ||
plt.heatmap( | ||
x: x | ||
y: y | ||
z: z | ||
) | ||
plt.layout( | ||
title: 'Pink `fractal` noise 2d example' | ||
) | ||
plt.show()! | ||
} |
This file contains 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,16 @@ | ||
# Example - noise_simple_2d 📘 | ||
|
||
This example demonstrates the usage of the V Scientific Library for generating simplex noise. | ||
|
||
## Instructions | ||
|
||
1. Ensure you have the V compiler installed. You can download it from [here](https://vlang.io). | ||
2. Ensure you have the VSL installed. You can do it following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)! | ||
3. Navigate to this directory. | ||
4. Run the example using the following command: | ||
|
||
```sh | ||
v run main.v | ||
``` | ||
|
||
Enjoy exploring the capabilities of the V Scientific Library! 😊 |
This file contains 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,38 @@ | ||
module main | ||
|
||
import rand | ||
import vsl.noise | ||
import vsl.plot | ||
|
||
fn main() { | ||
// Creation of the noise generator. | ||
// Other noise functions and dimensions count are available. | ||
rand.seed([u32(3155200429), u32(3208395956)]) | ||
mut generator := noise.Generator.new() | ||
generator.randomize() | ||
|
||
mut x := []f64{} | ||
mut y := []f64{} | ||
mut z := []f64{} | ||
|
||
for xx in 0 .. 40 { | ||
for yy in 0 .. 40 { | ||
// We need to scale the coordinates to control the frequency of the noise. | ||
val := generator.simplex_2d(0.03 * xx, 0.03 * yy) | ||
x << xx | ||
y << yy | ||
z << val | ||
} | ||
} | ||
|
||
mut plt := plot.Plot.new() | ||
plt.heatmap( | ||
x: x | ||
y: y | ||
z: z | ||
) | ||
plt.layout( | ||
title: 'Simplex noise 2d example' | ||
) | ||
plt.show()! | ||
} |
This file contains 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,20 @@ | ||
module noise | ||
|
||
import rand | ||
|
||
// Generator is a struct holding the permutation table used in perlin and simplex noise | ||
pub struct Generator { | ||
mut: | ||
perm []int = rand.shuffle_clone(permutations) or { panic(err) } | ||
} | ||
|
||
// new is a function that return a new Generator struct | ||
pub fn Generator.new() Generator { | ||
return Generator{} | ||
} | ||
|
||
// randomize is a function that shuffle the permutation set inside the Generator struct | ||
// will not shuffle if rand.seed is not changed | ||
pub fn (mut generator Generator) randomize() { | ||
generator.perm = rand.shuffle_clone(permutations) or { panic(err) } | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.