Better interfacing for shaders.

This commit is contained in:
Andrey Parhomenko 2023-05-25 00:42:48 +03:00
parent cc02da52f8
commit f913f95d61
3 changed files with 41 additions and 23 deletions

View file

@ -132,8 +132,8 @@ func main() {
e.Add(1, &Debug{})
e.Add(-1, gx.DrawableRectangle{
Rectangle: gx.Rectangle{
W: 100,
H: 100,
W: 4,
H: 4,
T: gx.T(),
},
Color: gx.Color{
@ -144,6 +144,14 @@ func main() {
},
Visible: true,
Shader: gx.SolidWhiteColorShader,
Options: gx.ShaderOptions{
Images: [4]*gx.Image{
playerImg,
nil,
nil,
nil,
},
},
})
e.Run()
}

View file

@ -14,9 +14,6 @@ type Rectangle struct {
// rotate around(relatively of position, not absolute).
T Transform
// Width and height.
// In fact are needed only to specify
// relation of width and height.
// Change transform to actually change things.
W, H Float
}
@ -24,8 +21,15 @@ type Rectangle struct {
// The type describes rectangle that can be drawn.
type DrawableRectangle struct {
Rectangle
Shader *Shader
// Solid color of the rectangle.
// Will be ignored if the Shader
// field is not nil.
Color Color
Options ShaderOptions
Visible bool
}
@ -67,11 +71,11 @@ func (r DrawableRectangle) Draw(
rm := e.Camera().RealMatrix(e, true)
m := t.Matrix(e)
m.Concat(rm)
// Draw solid color if no shader.
if r.Shader == nil {
img := NewImage(1, 1)
img.Set(0, 0, r.Color)
opts := &ebiten.DrawImageOptions{
GeoM: m,
}
@ -79,20 +83,20 @@ func (r DrawableRectangle) Draw(
return
}
v := 1
opts := &ebiten.DrawRectShaderOptions{
GeoM: m,
Images: [4]*Image{
NewImage(v, v),
nil,
nil,
nil,
},
// Use the Color as base image if no is provided.
if r.Options.Images[0] == nil {
r.Options.Images[0] = NewImage(1, 1)
r.Options.Images[0].Set(0, 0, r.Color)
}
//w := int(r.W * r.T.S.X)
//h := int(r.H * r.T.S.Y)
// Drawing with shader.
opts := &ebiten.DrawRectShaderOptions{
GeoM: m,
Images: r.Options.Images,
Uniforms: r.Options.Uniforms,
}
i.DrawRectShader(v, v, r.Shader, opts)
w, h := r.Options.Images[0].Size()
i.DrawRectShader(w, h, r.Shader, opts)
}

View file

@ -6,6 +6,10 @@ import (
)
type Shader = ebiten.Shader
type ShaderOptions struct {
Uniforms map[string] any
Images [4]*Image
}
var (
// The shader is for example only.
@ -15,14 +19,14 @@ var (
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
//ts := imageSrcTextureSize()
_, size := imageSrcRegionOnTexture()
//_, size := imageSrcRegionOnTexture()
/*return vec4(
position.y/size.y,
position.y/size.y,
position.y/size.y,
position.y/size.y,
)*/
py := int(position.y / size.y) % 5
/*py := int(position.y / size.y) % 5
px := int(position.x / size.x) % 5
if py >= 1 && px >= 1 {
return vec4(
@ -31,14 +35,16 @@ var (
0,
1,
)
}
}*/
return vec4(
0,
1,
ret := vec4(
0,
sin(position.x),
sin(position.y),
1,
)
return imageSrc0UnsafeAt(texCoord) * (ret)
}
`))
)