Skip to content

Commit

Permalink
✨ Add argument to control output scaling in noise matrix functions (#267
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aksiome authored Oct 24, 2024
1 parent 422865c commit 055ded1
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# Documentation of the feature: https://bookshelf.docs.gunivers.net/en/latest/modules/random.html#noise-generators
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:ctx _ set value {octaves:4,persistence:.5,lacunarity:2.0,size:16}
data modify storage bs:ctx _ set value {octaves:4,persistence:.5,lacunarity:2.0,size:16,scale:1}
$scoreboard players set #w bs.ctx $(width)
$scoreboard players set #h bs.ctx $(height)
$data modify storage bs:ctx _ merge value $(with)
execute store result storage bs:ctx _.scale double .000001 run data get storage bs:ctx _.scale 1000

execute store result score $random.fractal_noise_2d.octaves bs.in run data get storage bs:ctx _.octaves
execute store result score $random.fractal_noise_2d.persistence bs.in run data get storage bs:ctx _.persistence 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:out random.fractal_noise_mat_2d[-1] append value 0f
execute store result storage bs:out random.fractal_noise_mat_2d[-1][-1] float .001 run function #bs.random:fractal_noise_2d
$execute store result storage bs:out random.fractal_noise_mat_2d[-1][-1] float $(scale) run function #bs.random:fractal_noise_2d

execute store result score $random.fractal_noise_2d.x bs.in run scoreboard players add #x bs.ctx 1
scoreboard players operation $random.fractal_noise_2d.x bs.in *= #k bs.ctx
execute if score #x bs.ctx < #w bs.ctx run function bs.random:noise/fractal_noise_mat_2d/xloop
execute if score #x bs.ctx < #w bs.ctx run function bs.random:noise/fractal_noise_mat_2d/xloop with storage bs:ctx _
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

execute store result score $random.fractal_noise_2d.x bs.in run scoreboard players set #x bs.ctx 0
data modify storage bs:out random.fractal_noise_mat_2d append value []
function bs.random:noise/fractal_noise_mat_2d/xloop
function bs.random:noise/fractal_noise_mat_2d/xloop with storage bs:ctx _

execute store result score $random.fractal_noise_2d.y bs.in run scoreboard players add #y bs.ctx 1
scoreboard players operation $random.fractal_noise_2d.y bs.in *= #k bs.ctx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# Documentation of the feature: https://bookshelf.docs.gunivers.net/en/latest/modules/random.html#noise-generators
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:ctx _ set value {size:16}
data modify storage bs:ctx _ set value {size:16,scale:1}
$scoreboard players set #w bs.ctx $(width)
$scoreboard players set #h bs.ctx $(height)
$data modify storage bs:ctx _ merge value $(with)
execute store result storage bs:ctx _.scale double .000001 run data get storage bs:ctx _.scale 1000

execute if data storage bs:ctx _.seed store result score $random.simplex_noise_2d.seed bs.in run data get storage bs:ctx _.seed
execute unless data storage bs:ctx _.seed store result score $random.simplex_noise_2d.seed bs.in run random value 1.. bs.random:simplex_noise_mat_2d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:out random.simplex_noise_mat_2d[-1] append value 0f
execute store result storage bs:out random.simplex_noise_mat_2d[-1][-1] float .001 run function #bs.random:simplex_noise_2d
$execute store result storage bs:out random.simplex_noise_mat_2d[-1][-1] float $(scale) run function #bs.random:simplex_noise_2d

execute store result score $random.simplex_noise_2d.x bs.in run scoreboard players add #x bs.ctx 1
scoreboard players operation $random.simplex_noise_2d.x bs.in *= #k bs.ctx
execute if score #x bs.ctx < #w bs.ctx run function bs.random:noise/simplex_noise_mat_2d/xloop
execute if score #x bs.ctx < #w bs.ctx run function bs.random:noise/simplex_noise_mat_2d/xloop with storage bs:ctx _
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

execute store result score $random.simplex_noise_2d.x bs.in run scoreboard players set #x bs.ctx 0
data modify storage bs:out random.simplex_noise_mat_2d append value []
function bs.random:noise/simplex_noise_mat_2d/xloop
function bs.random:noise/simplex_noise_mat_2d/xloop with storage bs:ctx _

execute store result score $random.simplex_noise_2d.y bs.in run scoreboard players add #y bs.ctx 1
scoreboard players operation $random.simplex_noise_2d.y bs.in *= #k bs.ctx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@

scoreboard players remove #i bs.ctx 1
data modify storage bs:out random.white_noise_mat_1d append value 0f
execute store result storage bs:out random.white_noise_mat_1d[-1] float .001 run random value 1..1000
execute if score #i bs.ctx matches 1.. run function bs.random:noise/white_noise_mat_1d/loop
$execute store result storage bs:out random.white_noise_mat_1d[-1] float $(scale) run random value 1..1000
execute if score #i bs.ctx matches 1.. run function bs.random:noise/white_noise_mat_1d/loop with storage bs:ctx _
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
# Documentation of the feature: https://bookshelf.docs.gunivers.net/en/latest/modules/random.html#noise-generators
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:ctx _ set value {scale:1}
$scoreboard players set #i bs.ctx $(length)
$data modify storage bs:ctx _ merge value $(with)
execute store result storage bs:ctx _.scale double .000001 run data get storage bs:ctx _.scale 1000

data modify storage bs:out random.white_noise_mat_1d set value []
execute if score #i bs.ctx matches 1.. run function bs.random:noise/white_noise_mat_1d/loop
execute if score #i bs.ctx matches 1.. run function bs.random:noise/white_noise_mat_1d/loop with storage bs:ctx _
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
# Documentation of the feature: https://bookshelf.docs.gunivers.net/en/latest/modules/random.html#noise-generators
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:ctx _ set value {scale:1}
$scoreboard players set #w bs.ctx $(width)
$scoreboard players set #h bs.ctx $(height)
$data modify storage bs:ctx _ merge value $(with)
execute store result storage bs:ctx _.scale double .000001 run data get storage bs:ctx _.scale 1000

scoreboard players set #y bs.ctx 0
data modify storage bs:out random.white_noise_mat_2d set value []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
# ------------------------------------------------------------------------------------------------------------

data modify storage bs:out random.white_noise_mat_2d[-1] append value 0f
execute store result storage bs:out random.white_noise_mat_2d[-1][-1] float .001 run random value 1..1000
$execute store result storage bs:out random.white_noise_mat_2d[-1][-1] float $(scale) run random value 1..1000
scoreboard players add #x bs.ctx 1
execute if score #x bs.ctx < #w bs.ctx run function bs.random:noise/white_noise_mat_2d/xloop
execute if score #x bs.ctx < #w bs.ctx run function bs.random:noise/white_noise_mat_2d/xloop with storage bs:ctx _
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

scoreboard players set #x bs.ctx 0
data modify storage bs:out random.white_noise_mat_2d append value []
function bs.random:noise/white_noise_mat_2d/xloop
function bs.random:noise/white_noise_mat_2d/xloop with storage bs:ctx _

scoreboard players add #y bs.ctx 1
execute if score #y bs.ctx < #h bs.ctx run function bs.random:noise/white_noise_mat_2d/yloop
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# White noise 1d generates the right number of random values
# @batch bs.random

function #bs.random:white_noise_mat_1d {length:0}
function #bs.random:white_noise_mat_1d {length:0,with:{}}
execute store result score #n bs.ctx if data storage bs:out random.white_noise_mat_1d[]
assert score #n bs.ctx matches 0

function #bs.random:white_noise_mat_1d {length:5}
function #bs.random:white_noise_mat_1d {length:5,with:{}}
execute store result score #n bs.ctx if data storage bs:out random.white_noise_mat_1d[]
assert score #n bs.ctx matches 5

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# White noise 2d generates the right number of random values
# @batch bs.random

function #bs.random:white_noise_mat_2d {width:0,height:8}
function #bs.random:white_noise_mat_2d {width:0,height:8,with:{}}
execute store result score #n bs.ctx if data storage bs:out random.white_noise_mat_2d[]
assert score #n bs.ctx matches 0

function #bs.random:white_noise_mat_2d {width:8,height:0}
function #bs.random:white_noise_mat_2d {width:8,height:0,with:{}}
execute store result score #n bs.ctx if data storage bs:out random.white_noise_mat_2d[]
assert score #n bs.ctx matches 0

function #bs.random:white_noise_mat_2d {width:3,height:2}
function #bs.random:white_noise_mat_2d {width:3,height:2,with:{}}
execute store result score #n bs.ctx if data storage bs:out random.white_noise_mat_2d[]
assert score #n bs.ctx matches 2
execute store result score #n bs.ctx if data storage bs:out random.white_noise_mat_2d[0][]
Expand Down
22 changes: 14 additions & 8 deletions docs/modules/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function #bs.random:fractal_noise_2d
:::::{tab-set}
::::{tab-item} White noise 1D

```{function} #bs.random:white_noise_mat_1d {length:<length>}
```{function} #bs.random:white_noise_mat_1d {length:<length>,with:{}}
Generates a 1-dimensional array of white noise values.
Expand All @@ -335,17 +335,19 @@ Generates a 1-dimensional array of white noise values.
:::{treeview}
- {nbt}`compound` Arguments
- {nbt}`int` **length**: Length of the array to generate.
- {nbt}`compound` **with**:
- {nbt}`number` **scale**: Scalar for the function's output (default: 1).
:::
:Outputs:
**Storage `bs:out random.white_noise_mat_1d`**: {nbt}`list` Array of values between 0 and 1.
**Storage `bs:out random.white_noise_mat_1d`**: {nbt}`list` Array of values between 0 and {scale}.
```

*Generate 4 random values:*

```mcfunction
# Generate random noise
function #bs.random:white_noise_mat_1d {length:4}
function #bs.random:white_noise_mat_1d {length:4,with:{}}
# Display the result
tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"white_noise_mat_1d","storage":"bs:out", "color": "gold"}]
Expand All @@ -354,7 +356,7 @@ tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"white_noise_mat_1d
::::
::::{tab-item} White noise 2D

```{function} #bs.random:white_noise_mat_2d {width:<width>,height:<height>}
```{function} #bs.random:white_noise_mat_2d {width:<width>,height:<height>,with:{}}
Generates a 2-dimensional array of white noise values.
Expand All @@ -364,17 +366,19 @@ Generates a 2-dimensional array of white noise values.
- {nbt}`compound` Arguments
- {nbt}`int` **width**: Width of the array to generate.
- {nbt}`int` **height**: Height of the array to generate.
- {nbt}`compound` **with**:
- {nbt}`number` **scale**: Scalar for the function's output (default: 1).
:::
:Outputs:
**Storage `bs:out random.white_noise_mat_2d`**: {nbt}`list` 2D array of values between 0 and 1.
**Storage `bs:out random.white_noise_mat_2d`**: {nbt}`list` 2D array of values between 0 and {scale}.
```

*Generate a 4x4 random noise pattern:*

```mcfunction
# Generate random noise
function #bs.random:white_noise_mat_2d {width:4,height:4}
function #bs.random:white_noise_mat_2d {width:4,height:4,with:{}}
# Display the result
tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"white_noise_mat_2d","storage":"bs:out", "color": "gold"}]
Expand All @@ -395,10 +399,11 @@ Generates a 2D simplex noise texture of size `width` by `height`. Simplex noise
- {nbt}`compound` **with**:
- {nbt}`int` **size**: Size of the noise "cell" (default: 16). Lower size means more detail.
- {nbt}`int` **seed**: Seed for the noise generation, allowing for reproducibility (default: random).
- {nbt}`number` **scale**: Scalar for the function's output (default: 1).
:::
:Outputs:
**Storage `bs:out random.simplex_noise_mat_2d`**: 2D array of values between -1 and 1.
**Storage `bs:out random.simplex_noise_mat_2d`**: 2D array of values between {-scale} and {scale}.
```

*Generate a 16×16 simplex noise pattern:*
Expand Down Expand Up @@ -431,10 +436,11 @@ Generates a 2D fractal noise texture of size `width` by `height`. Fractal noise
- {nbt}`int` **octaves**: Number of noise layers (default: 4). More octaves = more detail.
- {nbt}`double` **persistence**: Contribution of each octave (default: 0.5). Higher means more detail.
- {nbt}`double` **lacunarity**: Increase in frequency for each octave (default: 2.0). Higher means more rapid frequency increase.
- {nbt}`number` **scale**: Scalar for the function's output (default: 1).
:::
:Outputs:
**Storage `bs:out random.fractal_noise_mat_2d`**: 2D array of values between -1 and 1.
**Storage `bs:out random.fractal_noise_mat_2d`**: 2D array of values between {-scale} and {scale}.
```

*Generate a 16×16 fractal noise pattern:*
Expand Down

0 comments on commit 055ded1

Please sign in to comment.