/
shader.go
57 lines (46 loc) · 1.53 KB
/
shader.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
/*
Copyright (C) 2021 Alexander Lunsford
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
)
var whiteFadeShader *ebiten.Shader
var noiseImg *ebiten.Image
func init() {
noiseImg = ebiten.NewImage(SCR_WIDTH, SCR_HEIGHT)
noisePixels := make([]byte, SCR_WIDTH*SCR_HEIGHT*4)
for i := 0; i < SCR_HEIGHT*SCR_WIDTH; i++ {
noisePixels[i*4+0] = byte(rand.Intn(255))
noisePixels[i*4+1] = byte(rand.Intn(255))
noisePixels[i*4+2] = byte(rand.Intn(255))
noisePixels[i*4+3] = 255
}
noiseImg.WritePixels(noisePixels)
var err error
whiteFadeShader, err = ebiten.NewShader([]byte(`
package main
var Coverage float
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
diffuse := imageSrc0UnsafeAt(texCoord)
noise := imageSrc1UnsafeAt(texCoord)
mask := step(1.0 - Coverage, noise.r)
return min(diffuse + mask, vec4(0.9, 0.9, 0.9, 1.0))
}
`))
if err != nil {
println(err)
panic(err)
}
}