ebiten: Remove user-defined vertex shaders

Fixes #1253
This commit is contained in:
Hajime Hoshi 2020-07-20 02:15:06 +09:00
parent 29d53edf1d
commit 1217db3b1e
7 changed files with 55 additions and 45 deletions

View File

@ -21,21 +21,12 @@ var Cursor 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 {
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
pos := position.xy/viewportSize() + Cursor/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)
clr := 0.0
clr += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10)
clr += sin(pos.y*sin(Time/10)*40) + cos(pos.x*sin(Time/25)*40)
clr += sin(pos.x*sin(Time/5)*10) + sin(pos.y*sin(Time/35)*80)
clr *= sin(Time/10) * 0.5
return vec4(clr, clr*0.5, sin(clr+Time/3)*0.75, 1)
}

View File

@ -3,4 +3,4 @@
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 Cursor 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() + Cursor/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")
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 Cursor vec2\n\n// viewportSize is a predefined function.\n\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tpos := position.xy/viewportSize() + Cursor/viewportSize()/4\n\tclr := 0.0\n\tclr += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10)\n\tclr += sin(pos.y*sin(Time/10)*40) + cos(pos.x*sin(Time/25)*40)\n\tclr += sin(pos.x*sin(Time/5)*10) + sin(pos.y*sin(Time/35)*80)\n\tclr *= sin(Time/10) * 0.5\n\treturn vec4(clr, clr*0.5, sin(clr+Time/3)*0.75, 1)\n}\n")

View File

@ -19,16 +19,7 @@ package main
var Time float
var Cursor vec2
func Vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2) {
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), texCoord
}
func Fragment(position vec4, texCoord vec2) vec4 {
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
lightpos := vec3(Cursor, 50)
lightdir := normalize(lightpos - position.xyz)
normal := normalize(texture1At(texCoord) - 0.5)

View File

@ -3,4 +3,4 @@
package main
var lighting_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 Cursor vec2\n\nfunc Vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2) {\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), texCoord\n}\n\nfunc Fragment(position vec4, texCoord vec2) vec4 {\n\tlightpos := vec3(Cursor, 50)\n\tlightdir := normalize(lightpos - position.xyz)\n\tnormal := normalize(texture1At(texCoord) - 0.5)\n\tambient := 0.25\n\tdiffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir))\n\treturn texture0At(texCoord) * (ambient + diffuse)\n}\n")
var lighting_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 Cursor vec2\n\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tlightpos := vec3(Cursor, 50)\n\tlightdir := normalize(lightpos - position.xyz)\n\tnormal := normalize(texture1At(texCoord) - 0.5)\n\tambient := 0.25\n\tdiffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir))\n\treturn texture0At(texCoord) * (ambient + diffuse)\n}\n")

View File

@ -108,28 +108,44 @@ func (g *Game) Draw(screen *ebiten.Image) {
w, h := screen.Size()
vs := []ebiten.Vertex{
{
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: float32(w),
DstY: 0,
SrcX: float32(sw),
SrcY: 0,
DstX: float32(w),
DstY: 0,
SrcX: float32(sw),
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: 0,
DstY: float32(h),
SrcX: 0,
SrcY: float32(sh),
DstX: 0,
DstY: float32(h),
SrcX: 0,
SrcY: float32(sh),
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: float32(w),
DstY: float32(h),
SrcX: float32(sw),
SrcY: float32(sh),
DstX: float32(w),
DstY: float32(h),
SrcX: float32(sw),
SrcY: float32(sh),
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
}
is := []uint16{0, 1, 2, 1, 2, 3}

View File

@ -95,6 +95,9 @@ type block struct {
ir shaderir.Block
}
// TODO: Consider variable/attrbutes!
// Probably creating a local variable for them automatically would be nice!
// However, what if the name is shadowing arguments? -> cause the error!
func (b *block) findLocalVariable(name string) (int, shaderir.Type, bool) {
idx := 0
for outer := b.outer; outer != nil; outer = outer.outer {

View File

@ -34,6 +34,15 @@ var __viewportSize vec2
func viewportSize() vec2 {
return __viewportSize
}
func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, 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), texCoord, color
}
`
for i := 1; i < graphics.ShaderImageNum; i++ {
@ -72,7 +81,7 @@ func NewShader(src []byte) (*Shader, error) {
}
// TODO: Create a pseudo vertex entrypoint to treat the attribute values correctly.
s, err := shader.Compile(fs, f, "Vertex", "Fragment", graphics.ShaderImageNum)
s, err := shader.Compile(fs, f, "__vertex", "Fragment", graphics.ShaderImageNum)
if err != nil {
return nil, err
}