Skip to content

Commit ea38cfc

Browse files
committed
allow field offsets, stub for worley noise
1 parent 5625169 commit ea38cfc

File tree

8 files changed

+58
-13
lines changed

8 files changed

+58
-13
lines changed

Package.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ let package = Package(
1515
targets: [
1616
.target(
1717
name: "Noise",
18-
dependencies: ["MaxPNG"],
19-
path: "Sources/noise"),
18+
dependencies: ["MaxPNG"],
19+
path: "sources/noise"),
2020
.testTarget(
2121
name: "NoiseTests",
22-
dependencies: ["Noise"]),
22+
dependencies: ["Noise"],
23+
path: "tests/noise"),
2324
]
2425
)

sources/noise/cell.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public
2+
struct Cell2D:HashedNoise
3+
{
4+
let perm1024:[Int],
5+
hashes:[Int]
6+
7+
static
8+
var n_hashes:Int = 1024
9+
10+
public
11+
init(amplitude:Double, frequency:Double, seed:Int = 0)
12+
{
13+
(self.perm1024, self.hashes) = SuperSimplex2D.table(seed: seed)
14+
}
15+
16+
public
17+
func evaluate(_ x:Double, _ y:Double) -> Double
18+
{
19+
return 0
20+
}
21+
22+
public
23+
func evaluate(_ x:Double, _ y:Double, _:Double) -> Double
24+
{
25+
return self.evaluate(x, y)
26+
}
27+
28+
public
29+
func evaluate(_ x:Double, _ y:Double, _:Double, _:Double) -> Double
30+
{
31+
return self.evaluate(x, y)
32+
}
33+
}

Sources/noise/noise.swift renamed to sources/noise/noise.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,35 @@ public
2020
extension Noise
2121
{
2222
public
23-
func noise2d(width:Int, height:Int) -> [Double]
23+
func sample_area(width:Int, height:Int) -> [(x:Double, y:Double, z:Double)]
2424
{
25-
var samples:[Double] = []
25+
var samples:[(x:Double, y:Double, z:Double)] = []
2626
samples.reserveCapacity(width * height)
27-
for y in 0 ..< height
27+
for i in 0 ..< height
2828
{
29-
for x in 0 ..< width
29+
for j in 0 ..< width
3030
{
31-
samples.append(self.evaluate(Double(x), Double(y)))
31+
let x:Double = Double(i) + 0.5,
32+
y:Double = Double(j) + 0.5
33+
samples.append((x: x, y: y, z: self.evaluate(x, y)))
3234
}
3335
}
3436
return samples
3537
}
3638

3739
public
38-
func noise2d_u8(width:Int, height:Int) -> [UInt8]
40+
func sample_area_saturated_to_u8(width:Int, height:Int, offset:Double = 0.5) -> [UInt8]
3941
{
40-
return self.noise2d(width: width, height: height).map{ UInt8(max(0, min(255, $0 + 127.5))) }
42+
var samples:[UInt8] = []
43+
samples.reserveCapacity(width * height)
44+
for y in 0 ..< height
45+
{
46+
for x in 0 ..< width
47+
{
48+
samples.append(UInt8(max(0, min(255, self.evaluate(Double(x), Double(y)) + offset))))
49+
}
50+
}
51+
return samples
4152
}
4253
}
4354

File renamed without changes.
File renamed without changes.
File renamed without changes.

Tests/LinuxMain.swift renamed to tests/LinuxMain.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ try png_encode(path: "poisson.png", raw_data: pixbuf, properties: png_properties
3939

4040
let S:fBm<Simplex2D> = fBm<Simplex2D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
4141
t0 = clock()
42-
pixbuf = S.noise2d_u8(width: viewer_size, height: viewer_size)
42+
pixbuf = S.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
4343
print(clock() - t0)
4444
try png_encode(path: "simplex.png", raw_data: pixbuf, properties: png_properties)
4545

4646

4747
let SS:fBm<SuperSimplex2D> = fBm<SuperSimplex2D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
4848
t0 = clock()
49-
pixbuf = SS.noise2d_u8(width: viewer_size, height: viewer_size)
49+
pixbuf = SS.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
5050
print(clock() - t0)
5151
try png_encode(path: "super_simplex.png", raw_data: pixbuf, properties: png_properties)
5252

5353
let SS3D:fBm<SuperSimplex3D> = fBm<SuperSimplex3D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
5454
t0 = clock()
55-
pixbuf = SS3D.noise2d_u8(width: viewer_size, height: viewer_size)
55+
pixbuf = SS3D.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
5656
print(clock() - t0)
5757
try png_encode(path: "super_simplex3D.png", raw_data: pixbuf, properties: png_properties)
File renamed without changes.

0 commit comments

Comments
 (0)