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

View file

@ -14,9 +14,6 @@ type Rectangle struct {
// rotate around(relatively of position, not absolute). // rotate around(relatively of position, not absolute).
T Transform T Transform
// Width and height. // Width and height.
// In fact are needed only to specify
// relation of width and height.
// Change transform to actually change things.
W, H Float W, H Float
} }
@ -24,8 +21,15 @@ type Rectangle struct {
// The type describes rectangle that can be drawn. // The type describes rectangle that can be drawn.
type DrawableRectangle struct { type DrawableRectangle struct {
Rectangle Rectangle
Shader *Shader Shader *Shader
// Solid color of the rectangle.
// Will be ignored if the Shader
// field is not nil.
Color Color Color Color
Options ShaderOptions
Visible bool Visible bool
} }
@ -67,11 +71,11 @@ func (r DrawableRectangle) Draw(
rm := e.Camera().RealMatrix(e, true) rm := e.Camera().RealMatrix(e, true)
m := t.Matrix(e) m := t.Matrix(e)
m.Concat(rm) m.Concat(rm)
// Draw solid color if no shader. // Draw solid color if no shader.
if r.Shader == nil { if r.Shader == nil {
img := NewImage(1, 1) img := NewImage(1, 1)
img.Set(0, 0, r.Color) img.Set(0, 0, r.Color)
opts := &ebiten.DrawImageOptions{ opts := &ebiten.DrawImageOptions{
GeoM: m, GeoM: m,
} }
@ -79,20 +83,20 @@ func (r DrawableRectangle) Draw(
return return
} }
v := 1 // Use the Color as base image if no is provided.
opts := &ebiten.DrawRectShaderOptions{ if r.Options.Images[0] == nil {
GeoM: m, r.Options.Images[0] = NewImage(1, 1)
Images: [4]*Image{ r.Options.Images[0].Set(0, 0, r.Color)
NewImage(v, v),
nil,
nil,
nil,
},
} }
//w := int(r.W * r.T.S.X) // Drawing with shader.
//h := int(r.H * r.T.S.Y) 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 Shader = ebiten.Shader
type ShaderOptions struct {
Uniforms map[string] any
Images [4]*Image
}
var ( var (
// The shader is for example only. // The shader is for example only.
@ -15,14 +19,14 @@ var (
func Fragment(position vec4, texCoord vec2, color vec4) vec4 { func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
//ts := imageSrcTextureSize() //ts := imageSrcTextureSize()
_, size := imageSrcRegionOnTexture() //_, size := imageSrcRegionOnTexture()
/*return vec4( /*return vec4(
position.y/size.y, position.y/size.y,
position.y/size.y, position.y/size.y,
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 px := int(position.x / size.x) % 5
if py >= 1 && px >= 1 { if py >= 1 && px >= 1 {
return vec4( return vec4(
@ -31,14 +35,16 @@ var (
0, 0,
1, 1,
) )
} }*/
return vec4( ret := vec4(
0,
1,
0, 0,
sin(position.x),
sin(position.y),
1, 1,
) )
return imageSrc0UnsafeAt(texCoord) * (ret)
} }
`)) `))
) )