Skip to content

Commit

Permalink
Added documentation, initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
nightcycle committed Feb 4, 2024
1 parent f39269a commit 514f783
Show file tree
Hide file tree
Showing 11 changed files with 610 additions and 4 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Usage

```lua
local noise = Noise.new(12345)
local value = noise:Perlin(1, 2) -- a value from 0 to 1 (ish)
```

## Random

<img src="./media/random.png" alt="random">

## Perlin

<img src="./media/perlin.png" alt="perlin">

## Worley / Cellular

<img src="./media/worley.png" alt="worley">

## Voronoi

<img src="./media/voronoi.png" alt="voronoi">

# Benchmark

Each map was sampled 32 times, including math.noise, to determine how long each of them took. Some maps have built in optimizations for when only x and y are provided.

<img src="./media/bench.png" alt="bench">

As you can see, math.noise is 2x faster than anything else. The perlin implementation is literally a translation of the official luau source code, so it's unsurprisingly running the fastest. The only reason you'd used this over math.noise is if you want deterministic seed control. The random noise map is just a bunch of Random datatypes in a trenchcoat, so it's also fairly fast.

In general, the more complex maps require around 50-60 microseconds to run per call - not a dealbreaker, but you'll want to be smart when you use them. They do cache their outputs though, so calling a repeat set of conditions can be faster than math.noise.

Binary file added media/bench.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/perlin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/random.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/voronoi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/worley.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added noise.rbxl
Binary file not shown.
2 changes: 1 addition & 1 deletion scripts/workspace/rojo-build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
rojo build dev.project.json -o dev.rbxl
rojo build dev.project.json -o noise.rbxl
92 changes: 92 additions & 0 deletions src/Noise.bench.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
--!strict
-- Services
-- Packages
-- Modules
local Noise = require(game:GetService("ReplicatedStorage"):WaitForChild("Packages"):WaitForChild("package"))
-- Types
-- Constants
local SAMPLE = 32
-- Variables
local noise = Noise.new()
-- References
-- Private Functions
-- Class
return {
Functions = {
["math.noise"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do
math.noise(x,y)
end
end
end,
-- ["math.noise-3D"] = function()
-- for x=1, SAMPLE do
-- for y=1, SAMPLE do
-- math.noise(x,y,0)
-- end
-- end
-- end,
["perlin-2D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do

noise:Perlin(x,y)
end
end
end,
["perlin-3D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do
noise:Perlin(x,y,0)
end
end
end,
["cellular-2D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do

noise:Cellular(x,y)
end
end
end,
["cellular-3D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do
noise:Cellular(x,y,0)
end
end
end,
["voronoi-2D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do

noise:Voronoi(x,y)
end
end
end,
["voronoi-3D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do
noise:Voronoi(x,y,0)
end
end
end,
["random-2D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do

noise:Random(x,y)
end
end
end,
["random-3D"] = function()
for x=1, SAMPLE do
for y=1, SAMPLE do
noise:Random(x,y,0)
end
end
end,
},

}
105 changes: 105 additions & 0 deletions src/Test.story.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
--!strict
-- Services
-- Packages
local Maid = require(game:GetService("ReplicatedStorage"):WaitForChild("Packages"):WaitForChild("Maid"))
-- Modules
local Noise = require(script.Parent)

-- Types
type Maid = Maid.Maid
type Noise = Noise.Noise

-- Constants
local SIZE = Vector2.one * 128 --(64,64)
local FREQ = SIZE.X / 8
local SCALE = 256/SIZE.X
local SKIP_RENDER = false
-- Variables
-- References

function drawNoise(maid: Maid, label: string, solver: (noise: Noise, x: number, y: number) -> number): ImageLabel

local imageLabel = maid:GiveTask(Instance.new("ImageLabel"))
imageLabel.Size = UDim2.fromOffset(SIZE.X * SCALE, SIZE.Y * SCALE)
imageLabel.LayoutOrder = -time()

local editableImage = maid:GiveTask(Instance.new("EditableImage"))
editableImage.Size = SIZE

local isAlive = true
maid:GiveTask(function()
isAlive = false
end)

local noise = Noise.new(tick())

local duration = 0
local count = 0
for y=0, editableImage.Size.Y-1 do
if isAlive == false then break end
for x=0, editableImage.Size.X-1 do
local start = tick()
local value = solver(noise, x,y)
duration += tick() - start
count += 1

if not SKIP_RENDER then
editableImage:DrawRectangle(
Vector2.new(x,y),
Vector2.one,
Color3.fromHSV(0, 0, value),
0
)
end
end
end

editableImage.Parent = imageLabel

return imageLabel
end

local function mathNoise(x: number, y: number, freq: number): number
return 0.5 + 0.5 * math.noise(x/freq,y/freq,0)
end
-- Class
return function(frame: Frame)
local maid = Maid.new()
task.spawn(function()

local listLayout = Instance.new("UIListLayout")
listLayout.Parent = frame
listLayout.Wraps = true
listLayout.Padding = UDim.new(0, 4)

local padding = Instance.new("UIPadding")
padding.PaddingTop = listLayout.Padding
padding.PaddingBottom = listLayout.Padding
padding.PaddingLeft = listLayout.Padding
padding.PaddingRight = listLayout.Padding
padding.Parent = frame

drawNoise(maid, "math.noise", function(noise: Noise, x: number, y: number): number
return mathNoise(x,y,FREQ)
end).Parent = frame

drawNoise(maid, "random-2D", function(noise: Noise, x: number, y: number): number
return math.clamp(noise:Random(x/FREQ,y/FREQ,0), 0, 1)
end).Parent = frame

drawNoise(maid, "perlin-2D", function(noise: Noise, x: number, y: number): number
return math.clamp(noise:Perlin(x/FREQ,y/FREQ), 0, 1)
end).Parent = frame

drawNoise(maid, "cellular-2D", function(noise: Noise, x: number, y: number): number
return math.clamp(noise:Cellular(x/FREQ,y/FREQ), 0, 1)
end).Parent = frame

drawNoise(maid, "voronoi-2D", function(noise: Noise, x: number, y: number): number
return math.clamp(noise:Voronoi(x/FREQ,y/FREQ), 0, 1)
end).Parent = frame
end)
return function()
maid:Destroy()
end
end
Loading

0 comments on commit 514f783

Please sign in to comment.