-
Notifications
You must be signed in to change notification settings - Fork 11
/
blur.go
165 lines (156 loc) · 5.01 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
// 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"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/log"
"github.com/divVerent/aaaaxy/internal/shader"
)
var (
debugUseShaders = flag.Bool("debug_use_shaders", true, "enable use of custom shaders")
drawBlurs = flag.Bool("draw_blurs", true, "perform blur effects; requires draw_visibility_mask")
)
func blurImageFixedFunction(img, tmp, out *ebiten.Image, size int, scale, darken float64) {
opts := ebiten.DrawImageOptions{
CompositeMode: ebiten.CompositeModeLighter,
Filter: ebiten.FilterNearest,
}
size++
// Only power-of-two blurs look good with this approach, so let's scale down the blur as much as needed.
for size&(size-1) != 0 {
size--
}
src := img
opts.ColorM.Scale(1, 1, 1, 0.5)
for size > 1 {
size /= 2
tmp.Fill(color.NRGBA{R: 0, G: 0, B: 0, A: 0})
opts.CompositeMode = ebiten.CompositeModeCopy
opts.GeoM.Reset()
opts.GeoM.Translate(-float64(size), 0)
tmp.DrawImage(src, &opts)
opts.CompositeMode = ebiten.CompositeModeLighter
opts.GeoM.Reset()
opts.GeoM.Translate(float64(size), 0)
tmp.DrawImage(src, &opts)
src = out
if size <= 1 {
opts.ColorM.Scale(1, 1, 1, scale)
opts.ColorM.Translate(-darken, -darken, -darken, 0)
}
out.Fill(color.NRGBA{R: 0, G: 0, B: 0, A: 0})
opts.CompositeMode = ebiten.CompositeModeCopy
opts.GeoM.Reset()
opts.GeoM.Translate(0, -float64(size))
out.DrawImage(tmp, &opts)
opts.CompositeMode = ebiten.CompositeModeLighter
opts.GeoM.Reset()
opts.GeoM.Translate(0, float64(size))
out.DrawImage(tmp, &opts)
}
}
func BlurExpandImage(img, tmp, 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(img, tmp, out, size, scale, darken, 1.0)
}
var (
blurBroken = false
)
func BlurImage(img, tmp, out *ebiten.Image, size int, scale, darken, blurFade float64) {
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.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 || !*debugUseShaders {
blurImageFixedFunction(img, tmp, 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", 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(img, tmp, out, size, scale, darken)
return
}
w, h := img.Size()
centerScale := 1.0 / (2*float64(size)*blurFade + 1)
otherScale := blurFade * centerScale
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,
},
})
}