/
C110_Noise.kt
581 lines (508 loc) · 17.8 KB
/
C110_Noise.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
@file:Suppress("UNUSED_EXPRESSION")
@file:Title("Noise")
@file:ParentTitle("ORX")
@file:Order("110")
@file:URL("ORX/noise")
package docs.`80_ORX`
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.dokgen.annotations.*
import org.openrndr.draw.LineCap
import org.openrndr.extra.noise.*
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.math.Vector4
import kotlin.math.abs
fun main() {
@Text """
# orx-noise
A collection of noise generator functions. Source and extra
documentation can be found in the
[orx-noise sourcetree](https://github.com/openrndr/orx/tree/master/orx-noise).
## Prerequisites
Assuming you are working on an
[`openrndr-template`](https://github.com/openrndr/openrndr-template) based
project, all you have to do is enable `orx-noise` in the `orxFeatures`
set in `build.gradle.kts` and reimport the gradle project.
## Uniformly distributed random values
The library provides extension methods for `Double`, `Vector2`, `Vector3`,
`Vector4` to create random vectors easily. To create
scalars and vectors with uniformly distributed noise you use the
`uniform` extension function.
"""
@Code.Block
run {
val d1 = Double.uniform(0.0, 640.0)
val v2 = Vector2.uniform(0.0, 640.0)
val v3 = Vector3.uniform(0.0, 640.0)
val v4 = Vector4.uniform(0.0, 640.0)
}
@Text
"""
To create multiple samples of noise one uses the `uniforms` function.
"""
@Code.Block
run {
val v2 = Vector2.uniforms(100, Vector2(0.0, 0.0), Vector2(640.0, 640.0))
val v3 = Vector3.uniforms(
100,
Vector3(0.0, 0.0, 0.0),
Vector3(640.0, 640.0, 640.0)
)
}
@Text
"""
The `Random` class can also be used to generate Double numbers and vector,
but also booleans and integers.
"""
@Code.Block
run {
// Boolean
val b = Random.bool(probability = 0.2)
// Int
val i1 = Random.int(0, 640)
val i2 = Random.int0(640)
// Double
val d2 = Random.double(0.0, 640.0)
val d3 = Random.double0(640.0)
// Vectors
val v2 = Random.vector2(0.0, 640.0)
val v3 = Random.vector3(0.0, 640.0)
val v4 = Random.vector4(0.0, 640.0)
}
@Text """
## Perlin, Value and Simplex noise
`Random.perlin()` and `Random.value()`
accept 2D and 3D arguments.
`Random.simplex()` up to 4D.
They all return a `Double`.
Some examples:
"""
@Code.Block
run {
// Test vectors to use
val v2 = Vector2(0.1, 0.2)
val v4 = Vector4(0.1, 0.2, 0.3, 0.4)
// Now generate random values
val d1 = Random.perlin(0.1, 0.2)
val d2 = Random.perlin(v2)
val d3 = Random.value(0.1, 0.2, 0.3)
val d4 = Random.simplex(v4)
}
@Text """
## Uniform ring noise
"""
@Code.Block
run {
val v2 = Vector2.uniformRing(0.0, 300.0)
val v3 = Vector3.uniformRing(0.0, 300.0)
val v4 = Vector4.uniformRing(0.0, 300.0)
}
@Media.Image "../media/orx-noise-001.jpg"
@Application
@ProduceScreenshot("media/orx-noise-001.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
extend {
drawer.fill = ColorRGBa.PINK
drawer.stroke = null
drawer.translate(width / 2.0, height / 2.00)
for (i in 0 until 1000) {
drawer.circle(Vector2.uniformRing(150.0, 250.0), 10.0)
}
}
}
}
@Text """
## Perlin noise
"""
@Media.Image "../media/orx-noise-002.jpg"
@Application
@ProduceScreenshot("media/orx-noise-002.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
extend {
drawer.fill = ColorRGBa.PINK
drawer.stroke = null
val scale = 0.005
for (y in 16 until height step 32) {
for (x in 16 until width step 32) {
val radius = perlinLinear(
100,
x * scale,
y * scale
) * 16.0 + 16.0
drawer.circle(x * 1.0, y * 1.0, radius)
}
}
}
}
}
@Text """
## Value noise
"""
@Media.Image "../media/orx-noise-003.jpg"
@Application
@ProduceScreenshot("media/orx-noise-003.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
extend {
drawer.fill = ColorRGBa.PINK
drawer.stroke = null
val scale = 0.0150
for (y in 16 until height step 32) {
for (x in 16 until width step 32) {
val radius =
valueLinear(100, x * scale, y * scale) * 16.0 + 16.0
drawer.circle(x * 1.0, y * 1.0, radius)
}
}
}
}
}
@Text """
## Simplex noise
"""
@Media.Image "../media/orx-noise-004.jpg"
@Application
@ProduceScreenshot("media/orx-noise-004.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
extend {
drawer.fill = ColorRGBa.PINK
drawer.stroke = null
val scale = 0.004
for (y in 16 until height step 32) {
for (x in 16 until width step 32) {
val radius =
simplex(100, x * scale, y * scale) * 16.0 + 16.0
drawer.circle(x * 1.0, y * 1.0, radius)
}
}
}
}
}
@Text """
## Fractal/FBM noise
"""
@Media.Video "../media/orx-noise-005-fbm.mp4"
@Application
@ProduceVideo("media/orx-noise-005-fbm.mp4", 9.0)
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
extend {
drawer.fill = ColorRGBa.PINK
drawer.stroke = null
val s = 0.0080
val t = seconds
for (y in 4 until height step 8) {
for (x in 4 until width step 8) {
val radius = when {
t < 3.0 -> abs(
fbm(100, x * s, y * s, t, ::perlinLinear)
) * 16.0
t < 6.0 -> billow(
100, x * s, y * s, t, ::perlinLinear
) * 2.0
else -> rigid(
100, x * s, y * s, t, ::perlinLinear
) * 16.0
}
drawer.circle(x * 1.0, y * 1.0, radius)
}
}
}
}
}
@Text """
## Noise gradients
Noise functions have evaluable gradients, a direction to where the
value of the function increases the fastest. The `gradient1D`,
`gradient2D`, `gradient3D` and `gradient4D` functions can be used
to estimate gradients for noise functions.
"""
@Media.Video "../media/orx-noise-300.mp4"
@Application
@ProduceVideo("media/orx-noise-300.mp4", 9.0)
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
extend {
drawer.fill = null
drawer.stroke = ColorRGBa.PINK
drawer.lineCap = LineCap.ROUND
drawer.strokeWeight = 3.0
val t = seconds
for (y in 4 until height step 8) {
for (x in 4 until width step 8) {
val g = gradient3D(
::perlinQuintic, 100,
x * 0.005, y * 0.005, t, 0.0005
).xy
drawer.lineSegment(
Vector2(x * 1.0, y * 1.0) - g * 2.0,
Vector2(x * 1.0, y * 1.0) + g * 2.0
)
}
}
}
}
}
@Text
"""
Gradients can also be calculated for the fbm, rigid and billow versions
of the noise functions. However,
we first have to create a function that can be used by the gradient
estimator. For this `fbmFunc3D`, `billowFunc3D`, and
`rigidFunc3D` can be used (which works through
[partial application](https://en.wikipedia.org/wiki/Partial_application)).
"""
@Media.Video "../media/orx-noise-301.mp4"
@Application
@ProduceVideo("media/orx-noise-301.mp4", 9.0)
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
val noise = fbmFunc3D(::simplex, octaves = 3)
extend {
drawer.fill = null
drawer.stroke = ColorRGBa.PINK
drawer.lineCap = LineCap.ROUND
drawer.strokeWeight = 1.5
val t = seconds
for (y in 4 until height step 8) {
for (x in 4 until width step 8) {
val g = gradient3D(
noise, 100, x * 0.002, y * 0.002, t, 0.002
).xy
drawer.lineSegment(
Vector2(x * 1.0, y * 1.0) - g * 1.0,
Vector2(x * 1.0, y * 1.0) + g * 1.0
)
}
}
}
}
}
// The following are commented out until
// https://github.com/openrndr/orx/blob/master/orx-noise/src/commonMain/kotlin/filters/NoiseFilters.kt
// is available again.
/*
@Text """
## Noise filters
The library contains a number of Filters with which noise image
can be generated efficiently on the GPU.
### Hash noise
A white-noise-like noise generator.
Parameter | Default value | Description
---------------------|-------------------------------|-------------------------------------------
`seed` | `0.0` | Noise seed
`gain` | `Vector4(1.0, 1.0, 1.0, 0.0)` | Noise gain per channel
`bias` | `Vector4(0.0, 0.0, 0.0, 1.0)` | Value to add to the generated noise
`monochrome` | `true` | Outputs monochrome noise if true
`premultipliedAlpha` | `true` | Outputs premultiplied alpha if true
"""
@Media.Image "../media/orx-noise-filter-001.jpg"
@Application
@ProduceScreenshot("media/orx-noise-filter-001.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
val cb = colorBuffer(width, height)
val hn = HashNoise()
extend {
hn.seed = seconds
hn.apply(emptyArray(), cb)
drawer.image(cb)
}
}
}
@Text """
### 3D Simplex noise filter
The `SimplexNoise3D` filter is based on Ken Perlin's improvement
over Perlin noise, but with fewer directional artifacts and, in
higher dimensions, a lower computational overhead.
Parameter | Default value | Description
---------------------|-------------------------------|-------------------------------------------
`seed` | `Vector3(0.0, 0.0, 0.0)` | Noise seed / offset
`scale` | `Vector3(1.0, 1.0, 1.0)` | The noise scale at the first octave
`octaves` | `4` | The number of octaves
`gain` | `Vector4(0.5, 0.5, 0.5, 0.5)` | Noise gain per channel per octave
`decay` | `Vector4(0.5, 0.5, 0.5, 0.5)` | Noise decay per channel per octave
`bias` | `Vector4(0.5, 0.5, 0.5, 0.5)` | Value to add to the generated noise
`lacunarity` | `Vector4(2.0, 2.0, 2.0, 2.0)` | Multiplication of noise scale per octave
`premultipliedAlpha` | `true` | Outputs premultiplied alpha if true
"""
@Media.Video "../media/orx-noise-filter-008.mp4"
@Application
@ProduceVideo("media/orx-noise-filter-008.mp4", 9.0)
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
val cb = colorBuffer(width, height)
val sn = SimplexNoise3D()
extend {
sn.seed = Vector3(0.0, 0.0, seconds * 0.1)
sn.scale = Vector3.ONE * 2.0
sn.octaves = 8
sn.premultipliedAlpha = false
sn.apply(emptyArray(), cb)
drawer.image(cb)
}
}
}
@Text """
### Cell noise
A cell, Worley or Voronoi noise generator
Parameter | Default value | Description
---------------------|-------------------------------|-------------------------------------------
`seed` | `Vector2(0.0, 0.0)` | Noise seed / offset
`scale` | `Vector2(1.0, 1.0)` | The noise scale at the first octave
`octaves` | `4` | The number of octaves
`gain` | `Vector4(1.0, 1.0, 1.0, 0.0)` | Noise gain per channel per octave
`decay` | `Vector4(0.5, 0.5, 0.5, 0.5)` | Noise decay per channel per octave
`bias` | `Vector4(0.0, 0.0, 0.0, 1.0)` | Value to add to the generated noise
`lacunarity` | `Vector4(2.0, 2.0, 2.0, 2.0)` | Multiplication of noise scale per octave
`premultipliedAlpha` | `true` | Outputs premultiplied alpha if true
"""
@Media.Image "../media/orx-noise-filter-002.jpg"
@Application
@ProduceScreenshot("media/orx-noise-filter-002.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
val cb = colorBuffer(width, height)
val cn = CellNoise()
extend {
cn.octaves = 4
cn.apply(emptyArray(), cb)
drawer.image(cb)
}
}
}
@Text """
### Speckle noise
A speckle noise generator.
Parameter | Default value | Description
---------------------|-------------------------------|-------------------------------------------
`color` | `ColorRGBa.WHITE` | Speckle color
`density` | `1.0` | Speckle density
`seed` | `0.0` | Noise seed
`noise` | `0.0` | Speckle noisiness
`premultipliedAlpha` | `true` | Outputs premultiplied alpha if true
"""
@Media.Image "../media/orx-noise-filter-003.jpg"
@Application
@ProduceScreenshot("media/orx-noise-filter-003.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
val cb = colorBuffer(width, height)
val sn = SpeckleNoise()
extend {
sn.seed = seconds
sn.apply(emptyArray(), cb)
drawer.image(cb)
}
}
}
@Text """
### Value noise
The `ValueNoise` filter generates a simple fractal noise. Value
noise is a computationally cheap form of creating
'smooth noise' by interpolating random values on a lattice.
Parameter | Default value | Description
---------------------|-------------------------------|-------------------------------------------
`seed` | `Vector2(0.0, 0.0)` | Noise seed / offset
`scale` | `Vector2(1.0, 1.0)` | The noise scale at the first octave
`octaves` | `4` | The number of octaves
`gain` | `Vector4(1.0, 1.0, 1.0, 0.0)` | Noise gain per channel per octave
`decay` | `Vector4(0.5, 0.5, 0.5, 0.5)` | Noise decay per channel per octave
`bias` | `Vector4(0.0, 0.0, 0.0, 1.0)` | Value to add to the generated noise
`lacunarity` | `Vector4(2.0, 2.0, 2.0, 2.0)` | Multiplication of noise scale per octave
`premultipliedAlpha` | `true` | Outputs premultiplied alpha if true
"""
@Media.Image "../media/orx-noise-filter-004.jpg"
@Application
@ProduceScreenshot("media/orx-noise-filter-004.jpg")
@Code
application {
@Exclude
configure {
width = 770
height = 578
}
program {
val cb = colorBuffer(width, height)
val vn = ValueNoise()
extend {
vn.scale = Vector2.ONE * 4.0
vn.gain = Vector4.ONE * 0.5
vn.octaves = 8
vn.apply(emptyArray(), cb)
drawer.image(cb)
}
}
}
*/
}