-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstipple.go
171 lines (140 loc) · 2.95 KB
/
stipple.go
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
package main
import (
"image"
"os"
"github.com/buchanae/ink/app"
"github.com/buchanae/ink/color"
"github.com/buchanae/ink/dd"
"github.com/buchanae/ink/gfx"
"github.com/buchanae/ink/math"
"github.com/buchanae/ink/midi"
"github.com/buchanae/ink/rand"
"github.com/buchanae/ink/voronoi"
)
func Ink(doc *app.Doc) {
midin := midi.NewMidi()
go midin.Run()
fh, err := os.Open("toshiro_copy.png")
if err != nil {
panic(err)
}
defer fh.Close()
img, _, err := image.Decode(fh)
if err != nil {
panic(err)
}
imgn := doc.NewImage(img)
//imgn.Draw(doc)
noise := rand.BlueNoise{
Spacing: 0.0025,
Limit: 70000,
}.Generate()
var (
alpha float32 = 0.3
lumBase float32 = 130
size float32 = 0.3
)
pos, col := buildPoints(
img, imgn.Rect, noise, alpha, lumBase,
)
build(doc, pos, col, size)
app.Send(doc)
for val := range midin.Listen() {
switch val.Key {
case 22:
alpha = val.Val
pos, col = buildPoints(
img, imgn.Rect, noise, alpha, lumBase,
)
case 21:
size = val.Val
default:
continue
}
build(doc, pos, col, size)
app.Send(doc)
}
/*
vm := VoronoiMesh{
XYs: pos,
Rect: imgn.Rect,
}
gfx.Fill{vm.Mesh(), color.Black}.Draw(doc)
*/
}
func buildPoints(img image.Image, rect dd.Rect, noise []dd.XY, alpha, lumBase float32) ([]dd.XY, []color.RGBA) {
w, h := img.Bounds().Dx(), img.Bounds().Dy()
var pos []dd.XY
var col []color.RGBA
for _, xy := range noise {
if !rect.Contains(xy) {
continue
}
ix := int(xy.X * float32(w))
iy := int((1 - xy.Y) * float32(h))
r, g, b, _ := img.At(ix, iy).RGBA()
rf := float32(r) / 255
gf := float32(g) / 255
bf := float32(b) / 255
lum := 0.2126*rf + 0.7152*gf + 0.0722*bf
lumf := lum / lumBase
if rand.Bool(lumf) {
continue
}
pos = append(pos, xy)
col = append(col, color.RGBA{
R: lumf,
G: lumf,
B: lumf,
A: alpha,
})
}
return pos, col
}
func build(doc *app.Doc, pos []dd.XY, col []color.RGBA, sizeInput float32) {
doc.Ops = nil
gfx.Clear(doc, color.White)
circle := dd.Circle{
Radius: math.Interp(0.0018, 0.008, sizeInput),
Segments: 20,
}
s := gfx.NewShader(circle.Fill())
s.Set("a_color", col)
s.Set("a_pos", pos)
s.Instances = len(pos)
s.Divisors = map[string]int{
"a_color": 1,
"a_pos": 1,
}
s.Draw(doc)
}
type VoronoiMesh struct {
XYs []dd.XY
Rect dd.Rect
}
func (vm VoronoiMesh) Mesh() dd.Mesh {
v := voronoi.New(vm.XYs, vm.Rect)
// voronoi generates triangles that share edges,
// so track which lines have already been drawn
// to avoid double-drawing shared edges.
seen := map[dd.Line]struct{}{}
var meshes []dd.Mesh
for _, t := range v.Triangulate() {
for _, e := range t.Edges() {
if e.A.X < e.B.X || e.A.Y < e.B.Y {
e.A, e.B = e.B, e.A
// cool mistake
//e.A = e.B
}
if _, ok := seen[e]; ok {
continue
}
seen[e] = struct{}{}
stk := e.Stroke(dd.StrokeOpt{
Width: 0.0005,
})
meshes = append(meshes, stk)
}
}
return dd.Merge(meshes...)
}