Last active
June 26, 2022 21:31
-
-
Save Zyko0/0edb18e9923481a766667d65c0bc8c15 to your computer and use it in GitHub Desktop.
DrawRectShader without image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"github.com/hajimehoshi/ebiten/v2" | |
) | |
var ( | |
s *ebiten.Shader | |
) | |
func init() { | |
var err error | |
s, err = ebiten.NewShader([]byte(` | |
package main | |
var ( | |
Time float | |
Color vec3 | |
) | |
func Fragment(pos vec4, tex vec2, color vec4) vec4 { | |
if tex.x > 0.5 && tex.y > 0.5 { | |
return vec4(sin(Time)*Color.rgb, 1.) | |
} | |
return vec4(1., 1., 1., 1.) | |
}`)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func DrawRectShaderNoImage(dst *ebiten.Image, x, y, width, height int, shader *ebiten.Shader, options *ebiten.DrawRectShaderOptions) { | |
// options.Images ignored // Note: this can be removed | |
// options.GeoM ignored // Note: Idk how to rotate or multiply by matrix on individual vertices, prob can be found in ebiten's internals | |
// Note: we're ignoring color but we could pass some as arguments for them to be interpolated or "fixed" if the same in the 4 vertices | |
vertices := []ebiten.Vertex{ | |
{ | |
DstX: float32(x), | |
DstY: float32(y), | |
SrcX: 0, | |
SrcY: 0, | |
}, | |
{ | |
DstX: float32(x + width), | |
DstY: float32(y), | |
SrcX: 1, | |
SrcY: 0, | |
}, | |
{ | |
DstX: float32(x), | |
DstY: float32(y + height), | |
SrcX: 0, | |
SrcY: 1, | |
}, | |
{ | |
DstX: float32(x + width), | |
DstY: float32(y + height), | |
SrcX: 1, | |
SrcY: 1, | |
}, | |
} | |
indices := []uint16{0, 1, 2, 1, 2, 3} | |
dst.DrawTrianglesShader(vertices, indices, shader, &ebiten.DrawTrianglesShaderOptions{ | |
CompositeMode: options.CompositeMode, | |
Uniforms: options.Uniforms, | |
}) | |
} | |
type Game struct { | |
ticks uint64 | |
} | |
func (g *Game) Update() error { | |
g.ticks++ | |
return nil | |
} | |
func (g *Game) Draw(screen *ebiten.Image) { | |
DrawRectShaderNoImage(screen, 100, 100, 100, 100, s, &ebiten.DrawRectShaderOptions{ | |
CompositeMode: ebiten.CompositeModeSourceOver, | |
Uniforms: map[string]interface{}{ | |
"Time": float32(g.ticks%60) / 60, | |
"Color": []float32{0.5, 0.5, 0}, | |
}, | |
}) | |
} | |
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { | |
return 1920, 1080 | |
} | |
func main() { | |
ebiten.RunGame(&Game{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment