/
C220_Distance_fields.kt
200 lines (173 loc) · 5.39 KB
/
C220_Distance_fields.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
@file:Suppress("UNUSED_EXPRESSION")
@file:Title("Distance fields")
@file:ParentTitle("ORX")
@file:Order("220")
@file:URL("ORX/distanceFields")
package docs.`80_ORX`
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.dokgen.annotations.*
import org.openrndr.draw.ColorType
import org.openrndr.draw.loadImage
import org.openrndr.extra.compositor.*
import org.openrndr.extra.jumpfill.DistanceField
import org.openrndr.extra.jumpfill.fx.InnerGlow
import org.openrndr.extra.jumpfill.fx.OuterGlow
import kotlin.math.cos
fun main() {
@Text
"""
# Distance fields using orx-jumpflood
## 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-jumpflood` in the `orxFeatures`
set in `build.gradle.kts` and reimport the gradle project.
## Distance field visualization
"""
@Media.Video "../media/distance-fields-001.mp4"
@Application
@ProduceVideo("media/distance-fields-001.mp4", 6.28318)
@Code
application {
program {
@Exclude
configure {
width = 640
height = 480
}
val image = loadImage("data/images/cheeta.jpg")
val c = compose {
layer {
colorType = ColorType.FLOAT32
draw {
drawer.image(image)
}
post(DistanceField()) {
threshold = cos(seconds) * 0.5 + 0.5
distanceScale = 0.008
}
}
}
extend {
c.draw(drawer)
}
}
}
@Text """
## Outer glow
orx-jumpflood comes with a filter that creates Photoshop-style outer glow effect.
"""
@Media.Video "../media/distance-fields-101.mp4"
@Application
@ProduceVideo("media/distance-fields-101.mp4", 6.28318)
@Code
application {
program {
@Exclude
configure {
width = 640
height = 480
}
val c = compose {
layer {
draw {
drawer.fill = ColorRGBa.PINK
drawer.circle(width / 2.0, height / 2.0, 200.0)
}
post(OuterGlow()) {
this.width = (cos(seconds) * 0.5 + 0.5) * 100.0
}
}
}
extend {
c.draw(drawer)
}
}
}
@Text """
## Inner glow
Similar to the outer glow effect, but the glow is placed in the inside
of the shape.
"""
@Media.Video "../media/distance-fields-102.mp4"
@Application
@ProduceVideo("media/distance-fields-102.mp4", 6.28318)
@Code
application {
program {
@Exclude
configure {
width = 640
height = 480
}
val c = compose {
layer {
draw {
drawer.fill = ColorRGBa.PINK
drawer.circle(width / 2.0, height / 2.0, 200.0)
}
post(InnerGlow()) {
this.width = (cos(seconds) * 0.5 + 0.5) * 100.0
}
}
}
extend {
c.draw(drawer)
}
}
}
@Text """
## Sampling distance
"""
@Media.Video "../media/distance-fields-002.mp4"
@Application
@ProduceVideo("media/distance-fields-002.mp4", 6.28318)
@Code
application {
program {
@Exclude
configure {
width = 640
height = 480
}
val image = loadImage("data/images/cheeta.jpg")
val c = compose {
// -- make sure accumulation is done in float32
colorType = ColorType.FLOAT32
layer {
colorType = ColorType.FLOAT32
draw {
drawer.image(image)
}
post(DistanceField()) {
threshold = cos(seconds) * 0.5 + 0.5
distanceScale = 1.0
}
}
layer {
draw {
// -- use the accumulation buffer to get the distance field
accumulation?.let {
val s = it.shadow
s.download()
drawer.fill = ColorRGBa.PINK
drawer.stroke = null
for (y in 0 until height step 10) {
for (x in 0 until width step 10) {
val distance = s[x, y].r
drawer.circle(x * 1.0, y * 1.0, distance * 0.05)
}
}
// -- clear the accumulated contents
it.fill(ColorRGBa.TRANSPARENT)
}
}
}
}
extend {
c.draw(drawer)
}
}
}
}