Skip to content

Commit 02710dc

Browse files
authored
Merge branch 'tayloraswift:master' into main
2 parents 5b887d2 + cb7975a commit 02710dc

31 files changed

+330
-471
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: build
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [ master ]
66
pull_request:
7-
branches: [ main ]
7+
branches: [ master ]
88

99
jobs:
1010
build-macos:
@@ -51,3 +51,4 @@ jobs:
5151
run: |
5252
swift --version
5353
swift build
54+
swift build -c release

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.build
2+
/.build.ssgc
23
/.vscode
34
/Packages
45
.swiftpm

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
<div align="center">
2-
3-
***`noise`***<br>`2.0.0`
4-
5-
[![ci build status](https://github.com/kelvin13/swift-noise/actions/workflows/build.yml/badge.svg)](https://github.com/kelvin13/swift-noise/actions/workflows/build.yml)
62

7-
[![swift package index versions](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fkelvin13%2Fswift-noise%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/kelvin13/swift-noise)
8-
[![swift package index platforms](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fkelvin13%2Fswift-noise%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/kelvin13/swift-noise)
3+
***`noise`***<br>`2.0`
4+
5+
[![ci build status](https://github.com/tayloraswift/swift-noise/actions/workflows/build.yml/badge.svg)](https://github.com/tayloraswift/swift-noise/actions/workflows/build.yml)
6+
[![swift package index versions](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Ftayloraswift%2Fswift-noise%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/tayloraswift/swift-noise)
7+
[![swift package index platforms](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Ftayloraswift%2Fswift-noise%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/tayloraswift/swift-noise)
98

109
</div>
1110

12-
![](doc/1.0.0/png/banner_FBM.png)
11+
![](Sources/Noise/docs.docc/png/banner_FBM.png)
1312

14-
**`swift-noise`** is a free, pure Swift procedural noise generation library. The library product has no dependencies and does not import Foundation or any system frameworks.
13+
**`swift-noise`** is a free, pure Swift procedural noise generation library. The library product has no dependencies and does not import Foundation or any system frameworks.
1514

16-
All popular types of procedural noise are supported, including three [gradient noises](https://en.wikipedia.org/wiki/Perlin_noise) (often called Perlin or simplex noises), and two [cellular noises](https://en.wikipedia.org/wiki/Worley_noise) (sometimes called Worley or Voronoi noises).
15+
All popular types of procedural noise are supported, including three [gradient noises](https://en.wikipedia.org/wiki/Perlin_noise) (often called Perlin or simplex noises), and two [cellular noises](https://en.wikipedia.org/wiki/Worley_noise) (sometimes called Worley or Voronoi noises).
1716

1817
`swift-noise` includes a [fractal brownian motion](https://thebookofshaders.com/13/) (FBM) noise composition framework, and a [disk point sampler](https://en.wikipedia.org/wiki/Supersampling#Poisson_disc) (often called a Poisson sampler), for generating visually even point distributions in the plane. `swift-noise` also includes pseudo-random number generation and hashing tools.
1918

20-
`swift-noise`’s entire public API is [documented](doc/1.0.0).
19+
`swift-noise`’s entire public API is [documented](https://swiftinit.org/hist/swift-noise:master/noise).
2120

2221
## Building
2322

24-
Build *Noise* with the Swift Package Manager. *Noise* itself has no dependencies, but the tests use [`swift-png`](https://github.com/kelvin13/swift-png) to view the generated noise.
23+
Build *Noise* with the Swift Package Manager. *Noise* itself has no dependencies, but the tests use [`swift-png`](https://github.com/tayloraswift/swift-png) to view the generated noise.

Sources/Noise/cell.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ extension _CellNoise2D
166166
}
167167
}
168168

169+
/// A type of two-dimensional cellular noise (sometimes called
170+
/// [Worley noise](https://en.wikipedia.org/wiki/Worley_noise), or Voronoi noise), suitable for
171+
/// texturing two-dimensional planes.
172+
///
173+
/// ![preview](png/banner_cell2d.png)
174+
///
175+
/// Unlike many other cell noise implementations, *Noise*’s implementation samples all relevant
176+
/// generating-points, preventing artifacts or discontinuities from ever appearing in the noise.
177+
/// Accordingly, *Noise*’s implementation is heavily optimized to prevent the additional edge
178+
/// cases from impacting the performance of the cell noise.
179+
///
180+
/// Cell noise has a three-dimensional version, ``CellNoise3D``.
169181
public
170182
struct CellNoise2D:_CellNoise2D, HashedNoise
171183
{
@@ -180,6 +192,14 @@ struct CellNoise2D:_CellNoise2D, HashedNoise
180192
self.permutation_table = permutation_table
181193
}
182194

195+
/// Creates an instance with the given `amplitude`, `frequency`, and random `seed` values.
196+
/// Creating an instance generates a new pseudo-random permutation table for that instance,
197+
/// and a new instance does not need to be regenerated to sample the same procedural noise
198+
/// field.
199+
///
200+
/// The given amplitude is adjusted internally to produce output *exactly* within the range
201+
/// of `0 ... amplitude`. However, in practice the cell noise rarely reaches the maximum
202+
/// threshold, as it is often useful to inflate the amplitude to get the desired appearance.
183203
public
184204
init(amplitude:Double, frequency:Double, seed:Int = 0)
185205
{
@@ -188,25 +208,34 @@ struct CellNoise2D:_CellNoise2D, HashedNoise
188208
self.permutation_table = PermutationTable(seed: seed)
189209
}
190210

211+
/// Returns the index numbers of the closest feature point to the given coordinate, and the
212+
/// squared distance from the given coordinate to the feature point. These index numbers can
213+
/// be fed to a color hashing function to produce a
214+
/// [Voronoi diagram](https://en.wikipedia.org/wiki/Voronoi_diagram).
191215
public
192216
func closest_point(_ x:Double, _ y:Double) -> (point:(Int, Int), r2:Double)
193217
{
194218
return self._closest_point(x, y)
195219
}
196220

221+
/// Evaluates the cell noise field at the given coordinates.
197222
public
198223
func evaluate(_ x:Double, _ y:Double) -> Double
199224
{
200225
let (_, r2):((Int, Int), Double) = self.closest_point(x, y)
201226
return self.amplitude * r2
202227
}
203228

229+
/// Evaluates the cell noise field at the given coordinates. The third coordinate is
230+
/// ignored.
204231
public
205232
func evaluate(_ x:Double, _ y:Double, _:Double) -> Double
206233
{
207234
return self.evaluate(x, y)
208235
}
209236

237+
/// Evaluates the cell noise field at the given coordinates. The third and fourth
238+
/// coordinates are ignored.
210239
public
211240
func evaluate(_ x:Double, _ y:Double, _:Double, _:Double) -> Double
212241
{
@@ -518,6 +547,26 @@ extension _CellNoise3D
518547
}
519548
}
520549

550+
/// A type of three-dimensional cellular noise (sometimes called
551+
/// [Worley noise](https://en.wikipedia.org/wiki/Worley_noise), or Voronoi noise), suitable for
552+
/// texturing arbitrary three-dimensional objects.
553+
///
554+
/// ![preview](png/banner_cell3d.png)
555+
///
556+
/// Unlike many other cell noise implementations, *Noise*’s implementation samples all relevant
557+
/// generating-points, preventing artifacts or discontinuities from ever appearing in the noise.
558+
/// Accordingly, *Noise*’s implementation is heavily optimized to prevent the additional edge
559+
/// cases from impacting the performance of the cell noise.
560+
///
561+
/// Three dimensional cell noise is approximately three to four times slower than its
562+
/// [two-dimensional version](doc:CellNoise2D), but has a vastly superior visual appearance,
563+
/// even when sampled in two dimensions.
564+
///
565+
/// `CellNoise3D` is analogous to
566+
/// [Blender Voronoi noise](https://docs.blender.org/manual/en/dev/render/cycles/nodes/types/textures/voronoi.html),
567+
/// with the *Distance Squared* metric. The *Scale* of Blender Voronoi noise is identical to the
568+
/// ``frequency`` of `CellNoise3D`; its range is approximately `0 ... 10/3` in `CellNoise3D`
569+
/// units.
521570
public
522571
struct CellNoise3D:_CellNoise3D, HashedNoise
523572
{
@@ -532,6 +581,14 @@ struct CellNoise3D:_CellNoise3D, HashedNoise
532581
self.permutation_table = permutation_table
533582
}
534583

584+
/// Creates an instance with the given `amplitude`, `frequency`, and random `seed` values.
585+
/// Creating an instance generates a new pseudo-random permutation table for that instance,
586+
/// and a new instance does not need to be regenerated to sample the same procedural noise
587+
/// field.
588+
///
589+
/// The given amplitude is adjusted internally to produce output *exactly* within the range
590+
/// of `0 ... amplitude`. However, in practice the cell noise rarely reaches the maximum
591+
/// threshold, as it is often useful to inflate the amplitude to get the desired appearance.
535592
public
536593
init(amplitude:Double, frequency:Double, seed:Int = 0)
537594
{
@@ -540,25 +597,34 @@ struct CellNoise3D:_CellNoise3D, HashedNoise
540597
self.permutation_table = PermutationTable(seed: seed)
541598
}
542599

600+
/// Returns the index numbers of the closest feature point to the given coordinate, and the
601+
/// squared distance from the given coordinate to the feature point. These index numbers can
602+
/// be fed to a color hashing function to produce a
603+
/// [Voronoi diagram](https://en.wikipedia.org/wiki/Voronoi_diagram).
543604
public
544605
func closest_point(_ x:Double, _ y:Double, _ z:Double) -> (point:(Int, Int, Int), r2:Double)
545606
{
546607
return self._closest_point(x, y, z)
547608
}
548609

610+
/// Evaluates the cell noise field at the given `x, y` coordinates, supplying `0` for the
611+
/// missing `z` coordinate.
549612
public
550613
func evaluate(_ x:Double, _ y:Double) -> Double
551614
{
552615
return self.evaluate(x, y, 0)
553616
}
554617

618+
/// Evaluates the cell noise field at the given coordinates.
555619
public
556620
func evaluate(_ x:Double, _ y:Double, _ z:Double) -> Double
557621
{
558622
let (_, r2):((Int, Int, Int), Double) = self.closest_point(x, y, z)
559623
return self.amplitude * r2
560624
}
561625

626+
/// Evaluates the cell noise field at the given coordinates. The fourth coordinate is
627+
/// ignored.
562628
public
563629
func evaluate(_ x:Double, _ y:Double, _ z:Double, _:Double) -> Double
564630
{

Sources/Noise/compounds.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/// A generic [fractal brownian motion](https://thebookofshaders.com/13/) noise generator,
2+
/// capable of overlaying multiple instances of procedural ``Noise`` at increasing frequencies.
3+
///
4+
/// ![preview](png/banner_FBM.png)
15
public
26
struct FBM<Source>:Noise where Source:Noise
37
{
@@ -34,6 +38,12 @@ struct FBM<Source>:Noise where Source:Noise
3438
self.generators = []
3539
}
3640

41+
/// Creates an instance with the given number of `octaves` of noise. The given `amplitude`
42+
/// is the amplitude of the first octave of noise, and is multiplied by `persistence` for
43+
/// each successive octave. The given `frequency` is the frequency of the first octave of
44+
/// noise, and is multiplied by the `lacunarity` for each successive octave. The `seed`
45+
/// value is passed through to the first octave of noise, and is incremented for each
46+
/// successive octave.
3747
@available(*, unavailable, message: "use init(_:octaves:persistence:lacunarity:) instead")
3848
public
3949
init(amplitude:Double, frequency:Double, octaves:Int, persistence:Double = 0.75, lacunarity:Double = 2, seed:Int = 0)

Sources/Noise/disk.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/// A point sampler capable of producing uniform and roughly-evenly spaced pseudo-random point
2+
/// distributions in the plane. Disk sampling is sometimes referred to as
3+
/// [Poisson sampling](https://en.wikipedia.org/wiki/Supersampling#Poisson_disc).
4+
///
5+
/// ![preview](png/banner_disk2d.png)
6+
///
7+
/// Disk samples are not a noise field — its generation is inherently sequential, as opposed to
8+
/// most procedural noise fields which are embarrassingly parallel. Thus, disk samples have no
9+
/// concept of *evaluation*; the entire sample set must be generated as a whole.
10+
///
11+
/// Disk samples have an internal state, which is advanced every time the point generator runs.
12+
/// In many ways, disk samples have more in common with pseudo-random number generators than
13+
/// they do with procedural noise fields.
114
public
215
struct DiskSampler2D
316
{
@@ -17,6 +30,9 @@ struct DiskSampler2D
1730
private static
1831
let candidate_table_bitmask:Int = 0b1111111111 // 1023
1932

33+
/// Creates an instance with the given fixed random `seed`. This process calculates a random
34+
/// table used internally in the sample generation step. The same instance can be reused to
35+
/// generate multiple, different point distributions.
2036
public
2137
init(seed:Int = 0)
2238
{
@@ -46,6 +62,13 @@ struct DiskSampler2D
4662
self.candidate_ring = candidate_ring
4763
}
4864

65+
/// Generates a set of sample points that are spaced no less than `radius` apart over a
66+
/// region sized `width` by `height` . Up to `k` candidate points will be used to generate
67+
/// each sample point; higher values of `k` yield more compact point distributions, but take
68+
/// longer to run. The `seed` point specifies the first point that is added to the
69+
/// distribution, and influences where subsequent sample points are added. This `seed` is
70+
/// orthogonal to the `seed` supplied in the initializer. If `seed` is left `nil`, the seed
71+
/// point is placed at the center of the region.
4972
public mutating
5073
func generate(radius:Double, width:Int, height:Int, k:Int = 32, seed:(Double, Double)? = nil) -> [(Double, Double)]
5174
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ``CellNoise2D``
2+
3+
## Topics
4+
5+
### Evaluating noise
6+
7+
- ``closest_point(_:_:)``
8+
- ``evaluate(_:_:)``
9+
- ``evaluate(_:_:_:)``
10+
- ``evaluate(_:_:_:_:)``
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ``CellNoise3D``
2+
3+
## Topics
4+
5+
### Evaluating noise
6+
7+
- ``closest_point(_:_:_:)``
8+
- ``evaluate(_:_:)``
9+
- ``evaluate(_:_:_:)``
10+
- ``evaluate(_:_:_:_:)``

Sources/Noise/docs.docc/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ``/Noise``
2+
3+
Generate and combine commonly used procedural noise patterns and distributions.
4+
5+
## Topics
6+
7+
### Protocols
8+
9+
- ``Noise``
10+
11+
### Noise generators
12+
13+
- ``SimplexNoise2D``
14+
- ``GradientNoise2D``
15+
- ``GradientNoise3D``
16+
- ``CellNoise2D``
17+
- ``CellNoise3D``
18+
19+
### Noise composition
20+
21+
- ``FBM``
22+
23+
### Pattern generators
24+
25+
- ``DiskSampler2D``
26+
27+
### Psuedo-random number generators
28+
29+
- ``PermutationTable``
30+
- ``RandomXorshift``

0 commit comments

Comments
 (0)