-
Notifications
You must be signed in to change notification settings - Fork 11
/
blur.go
197 lines (185 loc) · 6.02 KB
/
blur.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
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package engine
import (
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/log"
"github.com/divVerent/aaaaxy/internal/offscreen"
"github.com/divVerent/aaaaxy/internal/shader"
)
var (
drawBlurs = flag.Bool("draw_blurs", true, "perform blur effects; requires draw_visibility_mask")
)
func blurPassFixedFunction(img, out *ebiten.Image, mode ebiten.CompositeMode, dx, dy int, scale, darken float64) {
opts := ebiten.DrawImageOptions{
CompositeMode: mode,
Filter: ebiten.FilterNearest,
}
opts.ColorM.Scale(1, 1, 1, scale)
opts.ColorM.Translate(-darken, -darken, -darken, 0)
opts.GeoM.Translate(float64(dx), float64(dy))
out.DrawImage(img, &opts)
}
func blurImageFixedFunction(name string, img, out *ebiten.Image, size int, scale, darken float64) {
// Only power-of-two blurs look good with this approach, so let's scale down the blur as much as needed.
size++
for size&(size-1) != 0 {
size--
}
w, h := img.Size()
if offscreen.AvoidReuse() {
src := img // Only in first pass. Otherwise it is a temp image.
for size > 1 {
size /= 2
tmp := offscreen.New(fmt.Sprintf("%s.Horiz.%d", name, size), w, h)
blurPassFixedFunction(src, tmp, ebiten.CompositeModeCopy, -size, 0, 0.5, 0)
blurPassFixedFunction(src, tmp, ebiten.CompositeModeLighter, size, 0, 0.5, 0)
if src != img {
// Not first pass.
offscreen.Dispose(src)
}
dst := out
dstScale := 0.5
dstDarken := 0.0
if size > 1 {
// Not last pass.
dst = offscreen.New(fmt.Sprintf("%s.Vert.%d", name, size), w, h)
} else {
// Last pass.
dstScale *= scale
dstDarken = darken
dst = out
}
blurPassFixedFunction(tmp, dst, ebiten.CompositeModeCopy, -size, 0, dstScale, dstDarken)
blurPassFixedFunction(tmp, dst, ebiten.CompositeModeLighter, size, 0, dstScale, dstDarken)
offscreen.Dispose(tmp)
src = dst
}
} else {
tmp := offscreen.New(name, w, h)
src := img
for size > 1 {
size /= 2
blurPassFixedFunction(src, tmp, ebiten.CompositeModeCopy, -size, 0, 0.5, 0)
blurPassFixedFunction(src, tmp, ebiten.CompositeModeLighter, size, 0, 0.5, 0)
dstScale := 0.5
dstDarken := 0.0
if size <= 1 {
dstScale *= scale
dstDarken = darken
}
blurPassFixedFunction(tmp, out, ebiten.CompositeModeCopy, -size, 0, dstScale, dstDarken)
blurPassFixedFunction(tmp, out, ebiten.CompositeModeLighter, size, 0, dstScale, dstDarken)
src = out
}
offscreen.Dispose(tmp)
}
}
func BlurExpandImage(name string, img, out *ebiten.Image, blurSize, expandSize int, scale, darken float64) {
// Blurring and expanding can be done in a single step by doing a regular blur then scaling up at the last step.
if !*drawBlurs {
blurSize = 0
}
size := blurSize + expandSize
scale *= (2*float64(size) + 1) / (2*float64(blurSize) + 1)
BlurImage(name, img, out, size, scale, darken, 1.0)
}
var (
blurBroken = false
)
func BlurImage(name string, img, out *ebiten.Image, size int, scale, darken, blurFade float64) {
w, h := img.Size()
scale *= scale * blurFade
scale += 1 - blurFade
darken *= blurFade
if !*drawBlurs && scale <= 1 {
// Blurs can be globally turned off.
if img == out {
if scale == 1.0 && darken == 0.0 {
return
}
options := &ebiten.DrawImageOptions{
CompositeMode: ebiten.CompositeModeCopy,
Filter: ebiten.FilterNearest,
}
tmp := offscreen.New(name, w, h)
defer offscreen.Dispose(tmp)
tmp.DrawImage(img, options)
options.ColorM.Scale(scale, scale, scale, 1.0)
options.ColorM.Translate(-darken, -darken, -darken, 0.0)
out.DrawImage(tmp, options)
} else {
options := &ebiten.DrawImageOptions{
CompositeMode: ebiten.CompositeModeCopy,
Filter: ebiten.FilterNearest,
}
options.ColorM.Scale(scale, scale, scale, 1.0)
options.ColorM.Translate(-darken, -darken, -darken, 0.0)
out.DrawImage(img, options)
}
return
}
if blurBroken {
blurImageFixedFunction(name, img, out, size, scale, darken)
return
}
// Too bad we can't have integer uniforms, so we need to templatize this
// shader instead. Should be faster than having conditionals inside the
// shader code.
blurShader, err := shader.Load("blur.kage.tmpl", map[string]string{
"Size": fmt.Sprint(size),
})
if err != nil {
log.Errorf("BROKEN RENDERER, WILL FALLBACK: could not load blur shader: %v", err)
blurBroken = true
blurImageFixedFunction(name, img, out, size, scale, darken)
return
}
centerScale := 1.0 / (2*float64(size)*blurFade + 1)
otherScale := blurFade * centerScale
tmp := offscreen.New(fmt.Sprintf("%s.Horiz", name), w, h)
defer offscreen.Dispose(tmp)
tmp.DrawRectShader(w, h, blurShader, &ebiten.DrawRectShaderOptions{
CompositeMode: ebiten.CompositeModeCopy,
Uniforms: map[string]interface{}{
"Step": []float32{1 / float32(w), 0},
"CenterScale": float32(centerScale),
"OtherScale": float32(otherScale),
"Add": []float32{float32(-darken), float32(-darken), float32(-darken), 0.0},
},
Images: [4]*ebiten.Image{
img,
nil,
nil,
nil,
},
})
out.DrawRectShader(w, h, blurShader, &ebiten.DrawRectShaderOptions{
CompositeMode: ebiten.CompositeModeCopy,
Uniforms: map[string]interface{}{
"Step": []float32{0, 1 / float32(h)},
"CenterScale": float32(centerScale * scale),
"OtherScale": float32(otherScale * scale),
"Add": []float32{float32(-darken), float32(-darken), float32(-darken), 0.0},
},
Images: [4]*ebiten.Image{
tmp,
nil,
nil,
nil,
},
})
}