examples/shader: Add lighting example

Fixes #1234
This commit is contained in:
Hajime Hoshi 2020-07-17 00:20:00 +09:00
parent 5506491c03
commit a3f62559c6
12 changed files with 54 additions and 22 deletions

View File

@ -33,6 +33,8 @@
//go:generate file2byteslice -package=mascot -input=./images/mascot/out01.png -output=./images/mascot/out01.go -var=Out01_png
//go:generate file2byteslice -package=mascot -input=./images/mascot/out02.png -output=./images/mascot/out02.go -var=Out02_png
//go:generate file2byteslice -package=mascot -input=./images/mascot/out03.png -output=./images/mascot/out03.go -var=Out03_png
//go:generate file2byteslice -package=shader -input=./images/shader/gopher.png -output=./images/shader/gopher.go -var=Gopher_png
//go:generate file2byteslice -package=shader -input=./images/shader/normal.png -output=./images/shader/normal.go -var=Normal_png
//go:generate file2byteslice -package=platformer -input=./images/platformer/background.png -output=./images/platformer/background.go -var=Background_png
//go:generate file2byteslice -package=platformer -input=./images/platformer/left.png -output=./images/platformer/left.go -var=Left_png
//go:generate file2byteslice -package=platformer -input=./images/platformer/mainchar.png -output=./images/platformer/mainchar.go -var=MainChar_png

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -17,7 +17,7 @@
package main
var Time float
var Mouse vec2
var Cursor vec2
// viewportSize is a predefined function.
@ -31,7 +31,7 @@ func Vertex(position vec2, texCoord vec2, color vec4) vec4 {
}
func Fragment(position vec4) vec4 {
pos := position.xy/viewportSize() + Mouse/viewportSize()/4
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)

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 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")
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")

View File

@ -15,4 +15,4 @@
package main
//go:generate file2byteslice -package=main -input=default.go -output=default_go.go -var=default_go
//go:generate file2byteslice -package=main -input=image.go -output=image_go.go -var=image_go
//go:generate file2byteslice -package=main -input=lighting.go -output=lighting_go.go -var=lighting_go

View File

@ -1,6 +0,0 @@
// Code generated by file2byteslice. DO NOT EDIT.
// (gofmt is fine after generating)
package main
var image_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\treturn texture0At(texCoord)\n}\n")

View File

@ -29,5 +29,10 @@ func Vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2) {
}
func Fragment(position vec4, texCoord vec2) vec4 {
return texture0At(texCoord)
lightpos := vec3(Cursor, 50)
lightdir := normalize(lightpos - position.xyz)
normal := normalize(texture1At(texCoord) - 0.5)
ambient := 0.25
diffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir))
return texture0At(texCoord) * (ambient + diffuse)
}

View File

@ -0,0 +1,6 @@
// Code generated by file2byteslice. DO NOT EDIT.
// (gofmt is fine after generating)
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")

View File

@ -19,12 +19,12 @@ package main
import (
"bytes"
"image"
_ "image/jpeg"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
resources "github.com/hajimehoshi/ebiten/examples/resources/images/shader"
"github.com/hajimehoshi/ebiten/inpututil"
)
@ -33,7 +33,10 @@ const (
screenHeight = 480
)
var gophersImage *ebiten.Image
var (
gopherImage *ebiten.Image
normalImage *ebiten.Image
)
func init() {
// Decode image from a byte slice instead of a file so that
@ -45,16 +48,24 @@ func init() {
// This works even on browsers.
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
// This also works on browsers.
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
img, _, err := image.Decode(bytes.NewReader(resources.Gopher_png))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
gopherImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
func init() {
img, _, err := image.Decode(bytes.NewReader(resources.Normal_png))
if err != nil {
log.Fatal(err)
}
normalImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
var shaderSrcs = [][]byte{
default_go,
image_go,
lighting_go,
}
type Game struct {
@ -93,6 +104,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
return
}
sw, sh := gopherImage.Size()
w, h := screen.Size()
vs := []ebiten.Vertex{
{
@ -104,20 +116,20 @@ func (g *Game) Draw(screen *ebiten.Image) {
{
DstX: float32(w),
DstY: 0,
SrcX: float32(w),
SrcX: float32(sw),
SrcY: 0,
},
{
DstX: 0,
DstY: float32(h),
SrcX: 0,
SrcY: float32(h),
SrcY: float32(sh),
},
{
DstX: float32(w),
DstY: float32(h),
SrcX: float32(w),
SrcY: float32(h),
SrcX: float32(sw),
SrcY: float32(sh),
},
}
is := []uint16{0, 1, 2, 1, 2, 3}
@ -130,7 +142,8 @@ func (g *Game) Draw(screen *ebiten.Image) {
[]float32{float32(cx), float32(cy)}, // Cursor
}
if g.idx != 0 {
op.Images[0] = gophersImage
op.Images[0] = gopherImage
op.Images[1] = normalImage
}
screen.DrawTrianglesWithShader(vs, is, s, op)