2018-06-18 01:46:09 -05:00
|
|
|
package pixelgl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/faiface/glhf"
|
|
|
|
"github.com/faiface/mainthread"
|
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
// GLShader is a type to assist with managing a canvas's underlying
|
|
|
|
// shader configuration. This allows for customization of shaders on
|
|
|
|
// a per canvas basis.
|
2018-06-18 01:46:09 -05:00
|
|
|
type (
|
|
|
|
GLShader struct {
|
|
|
|
s *glhf.Shader
|
|
|
|
vf, uf glhf.AttrFormat
|
|
|
|
vs, fs string
|
|
|
|
|
|
|
|
uniforms []gsUniformAttr
|
|
|
|
|
|
|
|
uniformDefaults struct {
|
|
|
|
transform mgl32.Mat3
|
|
|
|
colormask mgl32.Vec4
|
|
|
|
bounds mgl32.Vec4
|
|
|
|
texbounds mgl32.Vec4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gsUniformAttr struct {
|
|
|
|
Name string
|
|
|
|
Type AttrType
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
// reinitialize GLShader data and recompile the underlying gl shader object
|
|
|
|
func (gs *GLShader) update() {
|
2018-06-18 01:46:09 -05:00
|
|
|
gs.uf = nil
|
|
|
|
for _, u := range gs.uniforms {
|
|
|
|
gs.uf = append(gs.uf, glhf.Attr{
|
|
|
|
Name: u.Name,
|
|
|
|
Type: glhf.AttrType(u.Type),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var shader *glhf.Shader
|
|
|
|
mainthread.Call(func() {
|
|
|
|
var err error
|
|
|
|
shader, err = glhf.NewShader(
|
|
|
|
gs.vf,
|
|
|
|
gs.uf,
|
|
|
|
gs.vs,
|
|
|
|
gs.fs,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "failed to create Canvas, there's a bug in the shader"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
gs.s = shader
|
|
|
|
}
|
2018-06-18 19:20:27 -05:00
|
|
|
|
|
|
|
// gets the uniform index from GLShader
|
|
|
|
func (gs *GLShader) getUniform(Name string) int {
|
2018-06-18 01:46:09 -05:00
|
|
|
for i, u := range gs.uniforms {
|
|
|
|
if u.Name == Name {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
2018-06-18 19:20:27 -05:00
|
|
|
|
|
|
|
// AddUniform appends a custom uniform name and value to the shader
|
|
|
|
//
|
|
|
|
// To add a time uniform for example:
|
|
|
|
//
|
|
|
|
// utime := float32(time.Since(starttime)).Seconds())
|
|
|
|
// mycanvas.shader.AddUniform("u_time", &utime)
|
|
|
|
//
|
2018-06-18 01:46:09 -05:00
|
|
|
func (gs *GLShader) AddUniform(Name string, Value interface{}) {
|
2018-06-18 19:20:27 -05:00
|
|
|
Type := getAttrType(Value)
|
|
|
|
if loc := gs.getUniform(Name); loc > -1 {
|
2018-06-18 01:46:09 -05:00
|
|
|
gs.uniforms[loc].Name = Name
|
|
|
|
gs.uniforms[loc].Type = Type
|
|
|
|
gs.uniforms[loc].Value = Value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gs.uniforms = append(gs.uniforms, gsUniformAttr{
|
|
|
|
Name: Name,
|
|
|
|
Type: Type,
|
|
|
|
Value: Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
// Sets up a base shader with everything needed for a pixel
|
|
|
|
// canvas to render correctly. The defaults can be overridden
|
|
|
|
// by simply using AddUniform()
|
2018-06-18 01:46:09 -05:00
|
|
|
func baseShader(c *Canvas) {
|
|
|
|
gs := &GLShader{
|
|
|
|
vf: defaultCanvasVertexFormat,
|
|
|
|
vs: defaultCanvasVertexShader,
|
|
|
|
fs: baseCanvasFragmentShader,
|
|
|
|
}
|
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
gs.AddUniform("u_transform", &gs.uniformDefaults.transform)
|
|
|
|
gs.AddUniform("u_colormask", &gs.uniformDefaults.colormask)
|
|
|
|
gs.AddUniform("u_bounds", &gs.uniformDefaults.bounds)
|
|
|
|
gs.AddUniform("u_texbounds", &gs.uniformDefaults.texbounds)
|
2018-06-18 01:46:09 -05:00
|
|
|
|
|
|
|
c.shader = gs
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultCanvasVertexShader = `
|
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
in vec2 position;
|
|
|
|
in vec4 color;
|
|
|
|
in vec2 texCoords;
|
|
|
|
in float intensity;
|
|
|
|
|
|
|
|
out vec4 Color;
|
2018-06-18 19:20:27 -05:00
|
|
|
out vec2 texcoords;
|
2018-06-18 01:46:09 -05:00
|
|
|
out float Intensity;
|
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
uniform mat3 u_transform;
|
|
|
|
uniform vec4 u_bounds;
|
2018-06-18 01:46:09 -05:00
|
|
|
|
|
|
|
void main() {
|
2018-06-18 19:20:27 -05:00
|
|
|
vec2 transPos = (u_transform * vec3(position, 1.0)).xy;
|
|
|
|
vec2 normPos = (transPos - u_bounds.xy) / u_bounds.zw * 2 - vec2(1, 1);
|
2018-06-18 01:46:09 -05:00
|
|
|
gl_Position = vec4(normPos, 0.0, 1.0);
|
|
|
|
Color = color;
|
2018-06-18 19:20:27 -05:00
|
|
|
texcoords = texCoords;
|
2018-06-18 01:46:09 -05:00
|
|
|
Intensity = intensity;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var baseCanvasFragmentShader = `
|
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
in vec4 Color;
|
2018-06-18 19:20:27 -05:00
|
|
|
in vec2 texcoords;
|
2018-06-18 01:46:09 -05:00
|
|
|
in float Intensity;
|
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
out vec4 fragColor;
|
2018-06-18 01:46:09 -05:00
|
|
|
|
2018-06-18 19:20:27 -05:00
|
|
|
uniform vec4 u_colormask;
|
|
|
|
uniform vec4 u_texbounds;
|
|
|
|
uniform sampler2D u_texture;
|
2018-06-18 01:46:09 -05:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
if (Intensity == 0) {
|
2018-06-18 19:20:27 -05:00
|
|
|
fragColor = u_colormask * Color;
|
2018-06-18 01:46:09 -05:00
|
|
|
} else {
|
2018-06-18 19:20:27 -05:00
|
|
|
fragColor = vec4(0, 0, 0, 0);
|
|
|
|
fragColor += (1 - Intensity) * Color;
|
|
|
|
vec2 t = (texcoords - u_texbounds.xy) / u_texbounds.zw;
|
|
|
|
fragColor += Intensity * Color * texture(u_texture, t);
|
|
|
|
fragColor *= u_colormask;
|
2018-06-18 01:46:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|