mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
examples/shader: Embed the shader file
This commit is contained in:
parent
d0df5950b5
commit
841085863b
41
examples/shader/default.go
Normal file
41
examples/shader/default.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2020 The Ebiten Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
var Time float
|
||||||
|
var Mouse vec2
|
||||||
|
|
||||||
|
// viewportSize is a predefined function.
|
||||||
|
|
||||||
|
func Vertex(position vec2, texCoord vec2, color vec4) vec4 {
|
||||||
|
return mat4(
|
||||||
|
2/viewportSize().x, 0, 0, 0,
|
||||||
|
0, 2/viewportSize().y, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
-1, -1, 0, 1,
|
||||||
|
) * vec4(position, 0, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fragment(position vec4) vec4 {
|
||||||
|
pos := position.xy/viewportSize() + Mouse/viewportSize()/4
|
||||||
|
color := 0.0
|
||||||
|
color += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10)
|
||||||
|
color += sin(pos.y*sin(Time/10)*40) + cos(pos.x*sin(Time/25)*40)
|
||||||
|
color += sin(pos.x*sin(Time/5)*10) + sin(pos.y*sin(Time/35)*80)
|
||||||
|
color *= sin(Time/10) * 0.5
|
||||||
|
return vec4(color, color*0.5, sin(color+Time/3)*0.75, 1)
|
||||||
|
}
|
6
examples/shader/default_go.go
Normal file
6
examples/shader/default_go.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// Code generated by file2byteslice. DO NOT EDIT.
|
||||||
|
// (gofmt is fine after generating)
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
var default_go = []byte("// Copyright 2020 The Ebiten Authors\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// +build ignore\n\npackage main\n\nvar Time float\nvar Mouse vec2\n\n// viewportSize is a predefined function.\n\nfunc Vertex(position vec2, texCoord vec2, color vec4) vec4 {\n\treturn mat4(\n\t\t2/viewportSize().x, 0, 0, 0,\n\t\t0, 2/viewportSize().y, 0, 0,\n\t\t0, 0, 1, 0,\n\t\t-1, -1, 0, 1,\n\t) * vec4(position, 0, 1)\n}\n\nfunc Fragment(position vec4) vec4 {\n\tpos := position.xy/viewportSize() + Mouse/viewportSize()/4\n\tcolor := 0.0\n\tcolor += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10)\n\tcolor += sin(pos.y*sin(Time/10)*40) + cos(pos.x*sin(Time/25)*40)\n\tcolor += sin(pos.x*sin(Time/5)*10) + sin(pos.y*sin(Time/35)*80)\n\tcolor *= sin(Time/10) * 0.5\n\treturn vec4(color, color*0.5, sin(color+Time/3)*0.75, 1)\n}\n")
|
17
examples/shader/generate.go
Normal file
17
examples/shader/generate.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright 2020 The Ebiten Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
//go:generate file2byteslice -package=main -input=default.go -output=default_go.go -var=default_go
|
@ -27,50 +27,37 @@ const (
|
|||||||
screenHeight = 480
|
screenHeight = 480
|
||||||
)
|
)
|
||||||
|
|
||||||
const shaderSrc = `package main
|
var shaderSrcs = [][]byte{
|
||||||
|
default_go,
|
||||||
var Time float
|
|
||||||
var Mouse vec2
|
|
||||||
|
|
||||||
// viewportSize is a predefined function.
|
|
||||||
|
|
||||||
func Vertex(position vec2, texCoord vec2, color vec4) vec4 {
|
|
||||||
return mat4(
|
|
||||||
2/viewportSize().x, 0, 0, 0,
|
|
||||||
0, 2/viewportSize().y, 0, 0,
|
|
||||||
0, 0, 1, 0,
|
|
||||||
-1, -1, 0, 1,
|
|
||||||
) * vec4(position, 0, 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fragment(position vec4) vec4 {
|
|
||||||
pos := position.xy / viewportSize() + Mouse / viewportSize() / 4
|
|
||||||
color := 0.0
|
|
||||||
color += sin(pos.x * cos(Time / 15) * 80) + cos(pos.y * cos(Time / 15) * 10)
|
|
||||||
color += sin(pos.y * sin(Time / 10) * 40) + cos(pos.x * sin(Time / 25) * 40)
|
|
||||||
color += sin(pos.x * sin(Time / 5) * 10) + sin(pos.y * sin(Time / 35) * 80)
|
|
||||||
color *= sin(Time / 10) * 0.5
|
|
||||||
return vec4(color, color * 0.5, sin(color + Time / 3 ) * 0.75, 1)
|
|
||||||
}`
|
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
shader *ebiten.Shader
|
shaders map[int]*ebiten.Shader
|
||||||
time int
|
idx int
|
||||||
|
time int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Update(screen *ebiten.Image) error {
|
func (g *Game) Update(screen *ebiten.Image) error {
|
||||||
g.time++
|
g.time++
|
||||||
if g.shader == nil {
|
if g.shaders == nil {
|
||||||
var err error
|
g.shaders = map[int]*ebiten.Shader{}
|
||||||
g.shader, err = ebiten.NewShader([]byte(shaderSrc))
|
}
|
||||||
|
if _, ok := g.shaders[g.idx]; !ok {
|
||||||
|
s, err := ebiten.NewShader([]byte(shaderSrcs[g.idx]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
g.shaders[g.idx] = s
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Draw(screen *ebiten.Image) {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
|
s, ok := g.shaders[g.idx]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w, h := screen.Size()
|
w, h := screen.Size()
|
||||||
vs := []ebiten.Vertex{
|
vs := []ebiten.Vertex{
|
||||||
{
|
{
|
||||||
@ -99,7 +86,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||||||
float32(g.time) / 60, // time
|
float32(g.time) / 60, // time
|
||||||
[]float32{float32(cx), float32(cy)}, // cursor
|
[]float32{float32(cx), float32(cy)}, // cursor
|
||||||
}
|
}
|
||||||
screen.DrawTrianglesWithShader(vs, is, g.shader, op)
|
screen.DrawTrianglesWithShader(vs, is, s, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
|
Loading…
Reference in New Issue
Block a user