Skip to content

Commit 0c23c39

Browse files
committed
voronoi and disk banners, and FBM
1 parent 3829b08 commit 0c23c39

File tree

6 files changed

+96
-22
lines changed

6 files changed

+96
-22
lines changed

poisson.png

0 Bytes
Loading

tests/LinuxMain.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import NoiseTests
1010

1111
banners(width: 700, ratio: 5)
1212

13-
/*
13+
1414
var pixbuf:[UInt8]
1515
let png_properties:PNGProperties = PNGProperties(width: viewer_size, height: viewer_size, bit_depth: 8, color: .grayscale, interlaced: false)!
1616

@@ -112,4 +112,3 @@ t0 = clock()
112112
pixbuf = SS3D.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
113113
print(clock() - t0)
114114
try png_encode(path: "super_simplex3D.png", raw_data: pixbuf, properties: png_properties)
115-
*/

tests/banner_FBM.png

208 KB
Loading

tests/banner_disk2d.png

41.1 KB
Loading

tests/banner_voronoi2d.png

17.5 KB
Loading

tests/noise/tests.swift

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func write_rgb_png(path:String, width:Int, height:Int, pixbuf:[UInt8])
2121
}
2222

2323
func color_noise_png(r_noise:Noise, g_noise:Noise, b_noise:Noise,
24-
width:Int, height:Int, value_offset:Double, invert:Bool = false, path:String)
24+
width:Int, height:Int, value_offset:(r:Double, g:Double, b:Double), invert:Bool = false, path:String)
2525
{
2626
var pixbuf:[UInt8] = []
2727
pixbuf.reserveCapacity(3 * width * height)
28-
for (r, (g, b)) in zip(r_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset),
29-
zip(g_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset),
30-
b_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset)))
28+
for (r, (g, b)) in zip(r_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset.r),
29+
zip(g_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset.g),
30+
b_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset.b)))
3131
{
3232
if invert
3333
{
@@ -53,7 +53,7 @@ func banner_simplex2d(width:Int, height:Int, seed:Int)
5353
b_noise: SimplexNoise2D(amplitude: 0.5*255, frequency: 0.00375, seed: seed + 2),
5454
width: width,
5555
height: height,
56-
value_offset: 0.65*255,
56+
value_offset: (0.65*255, 0.65*255, 0.65*255),
5757
path: "tests/banner_simplex2d.png")
5858
}
5959

@@ -64,7 +64,7 @@ func banner_supersimplex2d(width:Int, height:Int, seed:Int)
6464
b_noise: SuperSimplexNoise2D(amplitude: 0.5*255, frequency: 0.0025, seed: seed + 2),
6565
width: width,
6666
height: height,
67-
value_offset: 0.65*255,
67+
value_offset: (0.65*255, 0.65*255, 0.65*255),
6868
path: "tests/banner_supersimplex2d.png")
6969
}
7070

@@ -75,7 +75,7 @@ func banner_supersimplex3d(width:Int, height:Int, seed:Int)
7575
b_noise: SuperSimplexNoise3D(amplitude: 0.5*255, frequency: 0.0025, seed: seed + 2),
7676
width: width,
7777
height: height,
78-
value_offset: 0.65*255,
78+
value_offset: (0.65*255, 0.65*255, 0.65*255),
7979
path: "tests/banner_supersimplex3d.png")
8080
}
8181

@@ -86,7 +86,7 @@ func banner_cell2d(width:Int, height:Int, seed:Int)
8686
b_noise: CellNoise2D(amplitude: 3*255, frequency: 0.0075, seed: seed + 2),
8787
width: width,
8888
height: height,
89-
value_offset: 0,
89+
value_offset: (0, 0, 0),
9090
invert: true,
9191
path: "tests/banner_cell2d.png")
9292
}
@@ -98,11 +98,23 @@ func banner_cell3d(width:Int, height:Int, seed:Int)
9898
b_noise: CellNoise3D(amplitude: 3*255, frequency: 0.0075, seed: seed + 2),
9999
width: width,
100100
height: height,
101-
value_offset: 0,
101+
value_offset: (0, 0, 0),
102102
invert: true,
103103
path: "tests/banner_cell3d.png")
104104
}
105105

106+
func banner_FBM(width:Int, height:Int, seed:Int)
107+
{
108+
color_noise_png(r_noise: FBM<CellNoise3D> (amplitude: 2.7*255, frequency: 0.01, octaves: 7, seed: seed + 2),
109+
g_noise: FBM<SuperSimplexNoise3D>(amplitude: 255/3, frequency: 0.005, octaves: 7, seed: seed + 1),
110+
b_noise: FBM<SimplexNoise2D> (amplitude: 255/3, frequency: 0.03, octaves: 7, seed: seed),
111+
width: width,
112+
height: height,
113+
value_offset: (0, 150, 150),
114+
invert: false,
115+
path: "tests/banner_FBM.png")
116+
}
117+
106118
func circle_at(cx:Double, cy:Double, r:Double, width:Int, height:Int, _ f:(Int, Int, Double) -> ())
107119
{
108120
// get bounding box
@@ -130,29 +142,92 @@ func banner_disk2d(width:Int, height:Int, seed:Int)
130142
var pixbuf:[UInt8] = [UInt8](repeating: 255, count: 3 * width * height)
131143

132144
let points = poisson.generate(radius: 20, width: width, height: height, k: 80)
133-
for point:(x:Double, y:Double) in points
145+
146+
@inline(__always)
147+
func _dots(_ points:[(x:Double, y:Double)], color: (r:UInt8, g:UInt8, b:UInt8))
134148
{
135-
circle_at(cx: point.x, cy: point.y, r: 5, width: width, height: height,
149+
for point:(x:Double, y:Double) in points
136150
{
137-
(x:Int, y:Int, v:Double) in
138-
139-
let base_addr:Int = 3 * (y * width + x)
140-
pixbuf[base_addr + 1] = pixbuf[base_addr + 1] &- UInt8(255 * v)
141-
})
151+
circle_at(cx: point.x, cy: point.y, r: 10, width: width, height: height,
152+
{
153+
(x:Int, y:Int, v:Double) in
154+
155+
let base_addr:Int = 3 * (y * width + x)
156+
pixbuf[base_addr ] = UInt8(clamping: Int(pixbuf[base_addr ]) - Int(Double(color.r) * v))
157+
pixbuf[base_addr + 1] = UInt8(clamping: Int(pixbuf[base_addr + 1]) - Int(Double(color.g) * v))
158+
pixbuf[base_addr + 2] = UInt8(clamping: Int(pixbuf[base_addr + 2]) - Int(Double(color.b) * v))
159+
})
160+
}
142161
}
143162

163+
_dots(poisson.generate(radius: 35, width: width, height: height, k: 80, seed: (10, 10)), color: (0, 210, 70))
164+
_dots(poisson.generate(radius: 25, width: width, height: height, k: 80, seed: (45, 15)), color: (0, 10, 235))
165+
_dots(poisson.generate(radius: 30, width: width, height: height, k: 80, seed: (15, 45)), color: (225, 20, 0))
166+
144167
write_rgb_png(path: "tests/banner_disk2d.png", width: width, height: height, pixbuf: pixbuf)
145168
}
146169

170+
func banner_voronoi2d(width:Int, height:Int, seed:Int)
171+
{
172+
let voronoi = CellNoise2D(amplitude: 255, frequency: 0.025, seed: seed)
173+
var pixbuf:[UInt8] = [UInt8](repeating: 0, count: 3 * width * height)
174+
175+
let r:PermutationTable = PermutationTable(seed: seed),
176+
g:PermutationTable = PermutationTable(seed: seed + 1),
177+
b:PermutationTable = PermutationTable(seed: seed + 2)
178+
179+
var base_addr:Int = 0
180+
for y in 0 ..< height
181+
{
182+
for x in 0 ..< width
183+
{
184+
@inline(__always)
185+
func _supersample(_ x:Double, _ y:Double) -> (r:UInt8, g:UInt8, b:UInt8)
186+
{
187+
let (point, _):((Int, Int), Double) = voronoi.closest_point(Double(x), Double(y))
188+
let r:UInt8 = r.hash(point.0, point.1),
189+
g:UInt8 = g.hash(point.0, point.1),
190+
b:UInt8 = b.hash(point.0, point.1),
191+
peak:UInt8 = max(r, max(g, b)),
192+
saturate:Double = Double(UInt8.max) / Double(peak)
193+
return (UInt8(Double(r) * saturate), UInt8(Double(g) * saturate), UInt8(Double(b) * saturate))
194+
}
195+
196+
var r:Int = 0,
197+
g:Int = 0,
198+
b:Int = 0
199+
let supersamples:[(Double, Double)] = [(0, 0), (0, 0.4), (0.4, 0), (-0.4, 0), (0, -0.4),
200+
(-0.25, -0.25), (0.25, 0.25), (-0.25, 0.25), (0.25, -0.25)]
201+
for (dx, dy) in supersamples
202+
{
203+
let contribution:(r:UInt8, g:UInt8, b:UInt8) = _supersample(Double(x) + dx, Double(y) + dy)
204+
r += Int(contribution.r)
205+
g += Int(contribution.g)
206+
b += Int(contribution.b)
207+
}
208+
pixbuf[base_addr ] = UInt8(r / supersamples.count)
209+
pixbuf[base_addr + 1] = UInt8(g / supersamples.count)
210+
pixbuf[base_addr + 2] = UInt8(b / supersamples.count)
211+
212+
base_addr += 3
213+
}
214+
}
215+
216+
write_rgb_png(path: "tests/banner_voronoi2d.png", width: width, height: height, pixbuf: pixbuf)
217+
}
218+
147219
public
148220
func banners(width:Int, ratio:Double)
149221
{
150222
let height:Int = Int(Double(width) / ratio)
151-
banner_simplex2d(width: width, height: height, seed: 5)
223+
banner_simplex2d (width: width, height: height, seed: 5)
152224
banner_supersimplex2d(width: width, height: height, seed: 8)
153225
banner_supersimplex3d(width: width, height: height, seed: 0)
154-
banner_cell2d(width: width, height: height, seed: 0)
155-
banner_cell3d(width: width, height: height, seed: 0)
226+
banner_cell2d (width: width, height: height, seed: 0)
227+
banner_cell3d (width: width, height: height, seed: 0)
228+
banner_voronoi2d (width: width, height: height, seed: 3)
229+
230+
banner_FBM (width: width, height: height, seed: 0)
156231

157-
banner_disk2d(width: width, height: height, seed: 0)
232+
banner_disk2d (width: width, height: height, seed: 0)
158233
}

0 commit comments

Comments
 (0)