Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Last active April 25, 2020 13:59
Show Gist options
  • Save hajimehoshi/1e2453fe8bd0673ea35ceb9d9c5c4045 to your computer and use it in GitHub Desktop.
Save hajimehoshi/1e2453fe8bd0673ea35ceb9d9c5c4045 to your computer and use it in GitHub Desktop.
A Go-like language for shaders
package main
type VertexIn struct {
Vertex vec2
Tex vec2
TexRegion vec4
ColorScale vec4
}
type VertexOut struct {
Tex vec2
TexRegion vec4
ColorScale vec4
}
// Vertex is a vertex shader.
//
// The first argument is attribute values, and the second is a uniform value.
func Vertex(in VertexIn, viewportSize vec2) (pos vec4, out VertexOut) {
projectionMatrix := mat4(
2.0/viewportSize.x, 0, 0, -1,
0, -2.0/viewportSize.y, 0, 1,
0, 0, 1, 0,
0, 0, 0, 1,
)
pos := projectionMatrix * vec4(in.Vertex, 0, 1)
vo := VertexOutput{
Tex: in.TexRegion,
TexRegion: in.TexRegion,
ColorScale: in.ColorScale,
}
return pos, vo
}
// Fragment is a fragment shader.
//
// The third and following arguments are uniform values.
func Fragment(vertexPos vec4, out VertexOut, texture sampler2d, sourceSize vec2, colorMatrixBody vec4, colorMatrixTransition vec4, scale float) vec4 {
pos := out.Tex
var color vec4
if out.TexRegion[0] <= pos.x && out.TexRegion[1] <= pos.y &&
pos.x < out.TexRegion[2] && pos.y < out.TexRegion[3] {
color = at(texture, pos) // Better name?
}
color.rgb /= color.a + (1.0 - sign(color.a))
color = (colorMatrixBody * color) + colorMatrixTranslation
color *= out.ColorScale
color.rgb *= color.a
return color
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment