-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsand_stroke_fast.go
102 lines (83 loc) · 1.78 KB
/
sand_stroke_fast.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
package main
import (
"image"
colorlib "image/color"
. "github.com/buchanae/ink/dd"
"github.com/buchanae/ink/gfx"
"github.com/buchanae/ink/math"
"github.com/buchanae/ink/rand"
)
const (
Lines = 20
Strokes = 20
N = 5000
W = 1 / float32(N)
Amp = 1000
// min/max y-axis starting position
MinY = 0.1
MaxY = 0.9
D = 0.0040
MaxD = 0.2
AlphaOffset = -0.3
)
func Ink(doc gfx.Doc) {
rand.SeedNow()
palette := rand.Palette()
lines := make([]float32, Lines)
for i := range lines {
lines[i] = rand.Range(MinY, MaxY)
}
for j := 0; j < Strokes; j++ {
y := lines[rand.Intn(len(lines))]
heights := make([]float32, N)
h := rand.Range(0.01, 0.1)
for i := range heights {
h += rand.Range(-D, D)
h = math.Clamp(h, 0, MaxD)
heights[i] = h
}
img := makeHeightMap(heights)
heightmap := doc.NewImage(img)
s := &gfx.Shader{
Name: "Stroke",
Vert: gfx.DefaultVert,
Frag: Frag,
Mesh: Rect{
XY{0, y - MaxD},
XY{1, y + MaxD},
}.Fill(),
Attrs: gfx.Attrs{
"u_heightmap": heightmap,
"u_color": rand.Color(palette),
"u_alpha_offset": float32(AlphaOffset),
},
}
s.Draw(doc)
}
}
const Frag = `
#version 330 core
uniform vec4 u_color;
uniform sampler2D u_heightmap;
uniform float u_alpha_offset;
in vec2 v_uv;
out vec4 color;
void main() {
float h = texture(u_heightmap, v_uv).r;
float d = abs(v_uv.y - 0.5);
float a = step(d, h) * (1-h*2) + u_alpha_offset;
color = vec4(u_color.rgb, a);
}
`
// TODO want to easily create a texture without
// involving the "image" library
func makeHeightMap(heights []float32) *image.Gray {
r := image.Rect(0, 0, len(heights), 1)
img := image.NewGray(r)
for i, h := range heights {
img.SetGray(i, 0, colorlib.Gray{
Y: uint8(h * Amp),
})
}
return img
}