{"id":"nightcycle/noise","name":"noise","scope":"nightcycle","platform":"roblox","description":"Seeded noise maps for Luau: white, value, perlin, simplex, cellular, edge and voronoi","version":"2.0.0","latest":"2.0.0","versions":["1.0.0","2.0.0"],"license":"Apache-2.0","licenseRating":"safe","licenseCaveats":["Modified files must carry a notice of changes. If the package ships a NOTICE file, its attributions must be preserved."],"licenseVerified":true,"dependencies":{},"integrity":"f551b445120988dec118e67082f1a88b49233a4ef8dd20735e6d3e41874d4cbd","likes":0,"downloads":0,"install":"forest install nightcycle/noise","url":"https://forest.dev/p/roblox/nightcycle/noise","files":"https://api.forest.dev/ai/package/roblox/nightcycle/noise/files","readme":"# Noise\n\nSeeded noise maps for Luau: white, value, Perlin, simplex, cellular, edge and Voronoi, each in 2D and 3D.\n\nPlain Luau with no dependencies. It runs the same in Roblox and in any other Luau runtime, and a seed gives the same\nvalues on every machine.\n\n```toml\n[dependencies]\nNoise = \"nightcycle/noise@2.0.0\"\n```\n\n## Usage\n\n```lua\nlocal Noise = require(ReplicatedStorage.Packages.Noise)\n\nlocal noise = Noise.new(12345) -- leave the seed out for a fresh one\n\nlocal flat = noise:Perlin(x, y) -- 2D\nlocal volume = noise:Perlin(x, y, z) -- 3D\n```\n\nEvery map takes `(x, y)` or `(x, y, z)` and returns a number from 0 to 1. One unit is one lattice cell, so scale your\ncoordinates to choose how coarse the pattern is: `noise:Perlin(x / 16, y / 16)` puts a feature roughly every 16 studs.\n\nPassing `z` selects the 3D map even when it is 0. The 2D maps are their own, faster paths, not a slice of the 3D ones.\n\n| | | |\n| --- | --- | --- |\n| <img src=\"./media/random.png\" width=\"192\" alt=\"random\"> | `noise:Random(x, y, z?)` | White noise. Every distinct coordinate gets an unrelated value in [0, 1). |\n| <img src=\"./media/value.png\" width=\"192\" alt=\"value\"> | `noise:Value(x, y, z?)` | Random values on the lattice corners, blended smoothly, in [0, 1). The cheapest smooth map, and the blobbiest: its highs and lows can only sit on corners. |\n| <img src=\"./media/perlin.png\" width=\"192\" alt=\"perlin\"> | `noise:Perlin(x, y, z?)` | Smooth gradient noise in [0, 1], centred on 0.5. A seeded take on `math.noise`. |\n| <img src=\"./media/simplex.png\" width=\"192\" alt=\"simplex\"> | `noise:Simplex(x, y, z?)` | Gradient noise on a lattice of triangles, in [0, 1], centred on 0.5. It has no favoured directions, where Perlin leans toward the axes and diagonals, and it uses far more of its range. |\n| <img src=\"./media/cellular.png\" width=\"192\" alt=\"cellular\"> | `noise:Cellular(x, y, z?)` | Worley noise: the distance to the nearest feature point. 0 on a point, capped at 1. |\n| <img src=\"./media/edge.png\" width=\"192\" alt=\"edge\"> | `noise:Edge(x, y, z?)` | How much further the second nearest feature point is than the nearest, in [0, 1]. 0 along the border between two cells: cracks, veins, mortar, or a mask for blending across a border. |\n| <img src=\"./media/voronoi.png\" width=\"192\" alt=\"voronoi\"> | `noise:Voronoi(x, y, z?)` | The value in [0, 1) of the nearest feature point's cell, giving flat regions with hard edges. |\n\nCellular, Edge and Voronoi share their feature points, so for one seed the Voronoi borders run along the Cellular\nridges and the Edge zeroes.\n\nCoordinates have to stay within about two billion cells of the origin (±2^31).\n\n### The nearest point\n\n`noise:Nearest(x, y, z?)` returns the feature point nearest a sample: the centre of the Voronoi cell the sample is in.\nUse it to find which region a position belongs to and where that region's centre is.\n\n```lua\nlocal centreX, centreY = noise:Nearest(x, y)\nlocal biome = noise:Voronoi(centreX, centreY) -- the same value as noise:Voronoi(x, y)\n```\n\nIt always returns three numbers, with 0 for z in 2D. Take the two you need before passing them on: handing all three\nto a map selects its 3D form.\n\n### Slopes\n\n`ValueDerivative`, `PerlinDerivative` and `SimplexDerivative` return the same value as the plain map, followed by how\nfast it is changing along x, y and z (0 for z in 2D). That is one sample where measuring the slope by hand takes three\nor four, and it is exact.\n\n```lua\nlocal height, slopeX, slopeZ = noise:PerlinDerivative(x / 64, z / 64)\n\n-- the slope is per cell, and a cell is 64 studs here\nlocal normal = Vector3.new(-slopeX * HEIGHT / 64, 1, -slopeZ * HEIGHT / 64).Unit\n```\n\nSlopes are what terrain normals, slope-based texturing and flow fields are built from.\n\n### Options\n\n`Noise.new` takes options as a second argument. They are fixed for the life of the object; make another object, with\nthe same seed if you like, for another combination.\n\n```lua\nlocal tiles = Noise.new(12345, { Metric = \"Manhattan\", PeriodX = 16, PeriodY = 16 })\n```\n\n**`Metric`** is how Cellular, Edge, Voronoi and Nearest measure the distance to a feature point. The points do not\nmove, so objects that share a seed still line up.\n\n| `\"Euclidean\"` (default) | `\"Manhattan\"` | `\"Chebyshev\"` |\n| --- | --- | --- |\n| <img src=\"./media/voronoi.png\" width=\"192\" alt=\"voronoi, euclidean\"> | <img src=\"./media/voronoi-manhattan.png\" width=\"192\" alt=\"voronoi, manhattan\"> | <img src=\"./media/voronoi-chebyshev.png\" width=\"192\" alt=\"voronoi, chebyshev\"> |\n| Straight-line distance. | The distances along each axis added together, so the points equally far from somewhere form a diamond. | The largest distance along any one axis, so the points equally far from somewhere form a square. |\n\nManhattan and Chebyshev both give borders made of straight runs along the axes and diagonals, which suits city\nblocks, circuit boards and crystals.\n\n**`PeriodX`, `PeriodY`, `PeriodZ`** make every map repeat after that many cells along an axis, for textures that tile,\nworlds that wrap around, and animation that loops. Each is a whole number from 1 to 2^31, and an axis left out never\nrepeats.\n\n<img src=\"./media/perlin-periodic.png\" width=\"192\" alt=\"perlin with a period of half the image\">\n\n- Inside its first period a map matches the same map without a period, except in the cells that reach across the\n  seam. Simplex is the exception: its usual lattice cannot repeat, so with a period it is laid out on a different one\n  and gives different values. In 2D that lattice is also stretched slightly to fit the periods.\n- Random has no lattice, so it wraps the coordinate itself. It repeats exactly where a coordinate and the same\n  coordinate one period on are both exact, which holds for whole numbers and binary fractions like 0.25 but not for a\n  step like 0.1.\n\nAn object made without options pays nothing for them. With them, a period costs the lattice maps a fifth to two\nfifths of their speed, and either option costs the point maps about half; the figures are in the benchmark below.\n\n### In a hot loop\n\nCalling through a local skips the method lookup. Under lune that made Perlin about a fifth faster:\n\n```lua\nlocal perlin = noise.Perlin\nfor x = 1, 1024 do\n\theights[x] = perlin(noise, x / 16, 0)\nend\n```\n\n## Benchmark\n\nMeasured on 2026-11-22 in a Roblox Studio 0.748 server playtest on a Ryzen 9 9950X. Each figure is the median of 51\nruns of 4,096 samples.\n\nSpeed is given two ways: millions of samples a second, and the time to fill a 256 x 256 map (65,536 samples). For\nscale, a frame at 60 frames a second is 16.7 ms.\n\n| Map | 2D million / s | 2D map | 3D million / s | 3D map |\n| --- | ---: | ---: | ---: | ---: |\n| Random | 52.3 | 1.3 ms | 35.7 | 1.8 ms |\n| Value | 54.8 | 1.2 ms | 41.3 | 1.6 ms |\n| Perlin | 44.8 | 1.5 ms | 28.7 | 2.3 ms |\n| Simplex | 37.0 | 1.8 ms | 22.0 | 3.0 ms |\n| Cellular | 17.8 | 3.7 ms | 7.3 | 9.0 ms |\n| Edge | 11.8 | 5.6 ms | 4.7 | 14.0 ms |\n| Voronoi | 17.0 | 3.9 ms | 6.9 | 9.5 ms |\n| Nearest | 16.7 | 3.9 ms | 6.8 | 9.7 ms |\n| ValueDerivative | 45.5 | 1.4 ms | 33.0 | 2.0 ms |\n| PerlinDerivative | 35.4 | 1.9 ms | 22.1 | 3.0 ms |\n| SimplexDerivative | 31.9 | 2.1 ms | 17.9 | 3.7 ms |\n\nThe samples are called through a local, as in the loop above.\n\n### When the built-ins will do\n\nThe same loop, in the same session, over what the engine already provides:\n\n| Built-in | million / s | map |\n| --- | ---: | ---: |\n| `math.noise(x, y)` | 45.4 | 1.4 ms |\n| `math.noise(x, y, z)` | 45.9 | 1.4 ms |\n| `random:NextNumber()`, one generator drawn from repeatedly | 39.7 | 1.7 ms |\n| `Random.new(seed):NextNumber()`, a generator per sample | 19.3 | 3.4 ms |\n\n**`math.noise`** is the engine's Perlin noise. In 2D `noise:Perlin` keeps pace with it; in 3D `math.noise` is about\n1.6 times as fast. Use it when all of these are fine:\n\n- You want Perlin, and nothing else on this page.\n- The pattern can be the same for everyone. It takes no seed, so the only way to vary it is to offset the coordinates.\n- A range of roughly -1 to 1, not 0 to 1.\n- A pattern that repeats every 256 units.\n\n**`Random`** produces a sequence: each number depends on how many were drawn before it. Use it for things that happen\nin an order, such as loot rolls, shuffles and spawn timing, and for its other methods (`NextInteger`, `NextUnitVector`,\n`Shuffle`). Speed is not a reason to pick it: `noise:Random` is faster than `NextNumber` in 2D and close behind it in\n3D.\n\nUse `noise:Random` when a value has to depend on where it is rather than on when it was asked for: it can be sampled\nin any order, from any thread, and gives the same answer. Seeding a `Random` from the coordinates gets the same\neffect at well under half the speed, creating a generator for every sample.\n\n### With options\n\nA period of 16 cells on every axis:\n\n| Map | 2D million / s | 2D map | 3D million / s | 3D map |\n| --- | ---: | ---: | ---: | ---: |\n| Random | 38.4 | 1.7 ms | 27.9 | 2.3 ms |\n| Value | 40.7 | 1.6 ms | 25.8 | 2.5 ms |\n| Perlin | 31.2 | 2.1 ms | 18.1 | 3.6 ms |\n| Simplex | 29.0 | 2.3 ms | 16.4 | 4.0 ms |\n| Cellular | 8.5 | 7.7 ms | 3.9 | 17.0 ms |\n| Edge | 7.1 | 9.3 ms | 3.1 | 21.1 ms |\n| Voronoi | 8.1 | 8.1 ms | 3.8 | 17.3 ms |\n| Nearest | 8.2 | 8.0 ms | 3.8 | 17.2 ms |\n| ValueDerivative | 33.3 | 2.0 ms | 20.1 | 3.3 ms |\n| PerlinDerivative | 24.8 | 2.6 ms | 15.1 | 4.3 ms |\n| SimplexDerivative | 23.6 | 2.8 ms | 13.3 | 4.9 ms |\n\nThe Manhattan metric, with no period:\n\n| Map | 2D million / s | 2D map | 3D million / s | 3D map |\n| --- | ---: | ---: | ---: | ---: |\n| Cellular | 7.8 | 8.4 ms | 3.2 | 20.2 ms |\n| Edge | 6.9 | 9.5 ms | 2.8 | 23.8 ms |\n| Voronoi | 6.9 | 9.5 ms | 1.7 | 37.5 ms |\n| Nearest | 7.0 | 9.3 ms | 1.7 | 38.1 ms |\n\nThe point maps have one hand-tuned search for the plain case and share a general one for everything else, which costs\nthem between a third and three fifths of their speed. Voronoi and Nearest lose the most, three quarters, in 3D with\nManhattan: there the nearest point is more often outside the neighbouring cells and the search has to widen.\n\n### Against 1.0\n\n1.0 run through the same cases as 2.0, in one session earlier the same day on the same machine. The 2.0 column\ndiffers a little from the table above because it comes from that session.\n\n| Map | 2.0 million / s | 1.0 million / s | Speedup |\n| --- | ---: | ---: | ---: |\n| Random 2D | 51.8 | 4.8 | 10.8x |\n| Random 3D | 35.5 | 2.9 | 12.0x |\n| Perlin 2D | 41.7 | 8.4 | 5.0x |\n| Perlin 3D | 26.7 | 8.0 | 3.3x |\n| Cellular 2D | 17.0 | 4.7 | 3.6x |\n| Cellular 3D | 6.6 | 2.6 | 2.5x |\n| Voronoi 2D | 15.9 | 4.8 | 3.3x |\n| Voronoi 3D | 6.4 | 3.2 | 2.0x |\n\n1.0 cached the feature points of every cell it touched and never let them go. Those numbers are with that cache\nwarm, which is its best case; 2.0 keeps no state at all, so its memory use does not grow.\n\n### Running it yourself\n\n| Where | How |\n| --- | --- |\n| Outside Roblox | `lune run scripts/bench` |\n| Studio command bar, while the place is running | `print(require(game.ReplicatedStorage.noise.bench.Studio).Run())` |\n| boatbomber's Benchmarker plugin | open `bench/Noise.bench` |\n\nAll three time the cases in [bench/Cases.luau](bench/Cases.luau). The two that run in Roblox add the `Random` class\nfrom [bench/BuiltIns.luau](bench/BuiltIns.luau), which plain Luau does not have. The Studio runner times everything\ntwice, once with `os.clock` and once with MicroProfiler scopes read back through\n[LibMP](https://github.com/Roblox/libmp), and leaves the `Noise/...` scopes in the MicroProfiler for a closer look. On\nthe machine above lune lands within about ten percent of Studio.\n\n## Debug visuals\n\nEach map has a [UI Labs](https://github.com/PepeElToro41/ui-labs) story that renders it live from a set of controls:\nseed, cells across the image, resolution, a 3D toggle with a Z slider to scrub through the volume, an overlay, the\nmetric and a period. Under the map sit a histogram of its values and a readout of the minimum, maximum, mean and\nsampling speed. A value outside [0, 1] is painted red or blue, so a map that breaks its range is obvious at a glance.\n\nThe overlay draws the lattice in blue, plus whatever the map is built from: feature points as orange dots, gradients\nas green arrows. On a 3D slice a feature point fades as it moves away from the slice.\n\n| Perlin | Cellular | Voronoi |\n| --- | --- | --- |\n| <img src=\"./media/perlin-debug.png\" width=\"256\" alt=\"perlin with its gradients\"> | <img src=\"./media/cellular-debug.png\" width=\"256\" alt=\"cellular with its feature points\"> | <img src=\"./media/voronoi-debug.png\" width=\"256\" alt=\"voronoi with its feature points\"> |\n\nBuild the development place with `lune run scripts/build`, open `noise.rbxl`, and the stories appear in UI Labs under\nthe Noise storybook. `All` shows every map side by side.\n\nThe same renderer runs outside Roblox: `lune run scripts/render` writes the images in [media/](media/).\n\n## Adding a map\n\n1. Write `src/Maps/<Name>.luau`, returning a `Types.Map`: a name and two samplers, each a\n   `function(self, x, y, z?)`. `Sample` is the map as an object without options uses it, reading the seed from\n   `self._Seed32`. `Configured` is the map for an object with options: it also wraps its hashing at `self._WrapX`,\n   `_WrapY` and `_WrapZ`, and a point map measures with `self._Metric` (the search in `src/Search.luau` does both).\n   Add `Derivative` and `ConfiguredDerivative` if the map has a slope. Expose `Point` or `Gradient` if it has feature\n   points or gradients, and the overlay will draw them.\n2. List it in [src/Maps/init.luau](src/Maps/init.luau).\n3. Give the classes their methods for it in [src/init.luau](src/init.luau).\n4. Add `stories/<Name>.story.luau` (two lines; copy an existing one).\n\nThe benchmarks, the renderer, the `All` story and the range, determinism and period tests read the map list, so the\nnew map is covered by each of them without further changes. The build fails if a map has no story.\n\nMaps hash lattice cells with the functions in [src/Hash.luau](src/Hash.luau), but keep their own copy of the mixing\nstep rather than calling it: a function call per cell costs more than the hash does. The tests check the copies\nagainst the reference. For the same reason the plain, configured and derivative forms of a map are written out\nseparately rather than sharing a core.\n\n## Development\n\nTools are managed with [Rokit](https://github.com/rojo-rbx/rokit).\n\n```sh\nrokit install\nlune run scripts/setup   # dev packages, sourcemap, type definitions\nlune run scripts/check   # tests, formatting, strict analysis\nlune run scripts/build   # build/noise.rbxm, the Wally archive, and noise.rbxl\n```\n\n`src/` is the library and the only thing that ships. Stories, benchmarks and tests live beside it and are left out of\nboth the Wally package and the release model; the build checks that.\n\nTo release, set the version in `wally.toml`, commit, and push a matching tag (`v2.0.0`). The release workflow\nre-runs the checks, publishes to Wally and attaches the model and archive to a GitHub release.\n\n## Changes from 1.x\n\n- A seed produces different values than it did in 1.x. Anything that stored a seed to regenerate a map will get a\n  new map.\n- Every map now stays within [0, 1]. Cellular returns the plain distance capped at 1; in 1.x the 2D form was\n  rescaled and could go negative, and the 3D form was uncapped.\n- The 2D maps are no longer the `z = 0` slice of the 3D ones.\n- The library no longer depends on any packages or on Roblox datatypes.\n- New maps: Value, Simplex and Edge. New queries: `Nearest`, and derivatives for Value, Perlin and Simplex. New\n  options: a distance metric for the point maps, and a period for every map.\n","readmeTruncated":false}