mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-24 01:42:05 +01:00
examples/shader: Add the chromatic aberration example (#1288)
Updates #1284
This commit is contained in:
parent
d66e48b91f
commit
6e5a9a4534
31
examples/shader/chromaticaberration.go
Normal file
31
examples/shader/chromaticaberration.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// 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 Cursor vec2
|
||||||
|
var ScreenSize vec2
|
||||||
|
|
||||||
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
||||||
|
center := ScreenSize / 2
|
||||||
|
amount := normalize(center-Cursor).x / 100
|
||||||
|
var clr vec3
|
||||||
|
clr.r = texture2At(vec2(texCoord.x+amount, texCoord.y)).r
|
||||||
|
clr.g = texture2At(texCoord).g
|
||||||
|
clr.b = texture2At(vec2(texCoord.x-amount, texCoord.y)).b
|
||||||
|
return vec4(clr, 1.0)
|
||||||
|
}
|
6
examples/shader/chromaticaberration_go.go
Normal file
6
examples/shader/chromaticaberration_go.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// Code generated by file2byteslice. DO NOT EDIT.
|
||||||
|
// (gofmt is fine after generating)
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
var chromaticaberration_go = []byte("// Copyright 2020 The Ebiten Authors\r\n//\r\n// Licensed under the Apache License, Version 2.0 (the \"License\");\r\n// you may not use this file except in compliance with the License.\r\n// You may obtain a copy of the License at\r\n//\r\n// http://www.apache.org/licenses/LICENSE-2.0\r\n//\r\n// Unless required by applicable law or agreed to in writing, software\r\n// distributed under the License is distributed on an \"AS IS\" BASIS,\r\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n// See the License for the specific language governing permissions and\r\n// limitations under the License.\r\n\r\n// +build ignore\r\n\r\npackage main\r\n\r\nvar Time float\r\nvar Cursor vec2\r\nvar ScreenSize vec2\r\n\r\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\r\n\tcenter := ScreenSize / 2\r\n\tamount := normalize(center-Cursor).x / 100\r\n\tvar clr vec3\r\n\tclr.r = texture2At(vec2(texCoord.x+amount, texCoord.y)).r\r\n\tclr.g = texture2At(texCoord).g\r\n\tclr.b = texture2At(vec2(texCoord.x-amount, texCoord.y)).b\r\n\treturn vec4(clr, 1.0)\r\n}\r\n")
|
@ -18,6 +18,7 @@ package main
|
|||||||
|
|
||||||
var Time float
|
var Time float
|
||||||
var Cursor vec2
|
var Cursor vec2
|
||||||
|
var ScreenSize vec2
|
||||||
|
|
||||||
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
||||||
pos := position.xy/textureDstSize() + Cursor/textureDstSize()/4
|
pos := position.xy/textureDstSize() + Cursor/textureDstSize()/4
|
||||||
|
@ -3,4 +3,4 @@
|
|||||||
|
|
||||||
package main
|
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\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tpos := position.xy/textureDstSize() + Cursor/textureDstSize()/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")
|
var default_go = []byte("// Copyright 2020 The Ebiten Authors\r\n//\r\n// Licensed under the Apache License, Version 2.0 (the \"License\");\r\n// you may not use this file except in compliance with the License.\r\n// You may obtain a copy of the License at\r\n//\r\n// http://www.apache.org/licenses/LICENSE-2.0\r\n//\r\n// Unless required by applicable law or agreed to in writing, software\r\n// distributed under the License is distributed on an \"AS IS\" BASIS,\r\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n// See the License for the specific language governing permissions and\r\n// limitations under the License.\r\n\r\n// +build ignore\r\n\r\npackage main\r\n\r\nvar Time float\r\nvar Cursor vec2\r\nvar ScreenSize vec2\r\n\r\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\r\n\tpos := position.xy/textureDstSize() + Cursor/textureDstSize()/4\r\n\tclr := 0.0\r\n\tclr += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10)\r\n\tclr += sin(pos.y*sin(Time/10)*40) + cos(pos.x*sin(Time/25)*40)\r\n\tclr += sin(pos.x*sin(Time/5)*10) + sin(pos.y*sin(Time/35)*80)\r\n\tclr *= sin(Time/10) * 0.5\r\n\treturn vec4(clr, clr*0.5, sin(clr+Time/3)*0.75, 1)\r\n}\r\n")
|
||||||
|
@ -17,3 +17,4 @@ package main
|
|||||||
//go:generate file2byteslice -package=main -input=default.go -output=default_go.go -var=default_go
|
//go:generate file2byteslice -package=main -input=default.go -output=default_go.go -var=default_go
|
||||||
//go:generate file2byteslice -package=main -input=lighting.go -output=lighting_go.go -var=lighting_go
|
//go:generate file2byteslice -package=main -input=lighting.go -output=lighting_go.go -var=lighting_go
|
||||||
//go:generate file2byteslice -package=main -input=radialblur.go -output=radialblur_go.go -var=radialblur_go
|
//go:generate file2byteslice -package=main -input=radialblur.go -output=radialblur_go.go -var=radialblur_go
|
||||||
|
//go:generate file2byteslice -package=main -input=chromaticaberration.go -output=chromaticaberration_go.go -var=chromaticaberration_go
|
||||||
|
@ -18,6 +18,7 @@ package main
|
|||||||
|
|
||||||
var Time float
|
var Time float
|
||||||
var Cursor vec2
|
var Cursor vec2
|
||||||
|
var ScreenSize vec2
|
||||||
|
|
||||||
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
||||||
lightpos := vec3(Cursor, 50)
|
lightpos := vec3(Cursor, 50)
|
||||||
|
@ -3,4 +3,4 @@
|
|||||||
|
|
||||||
package main
|
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 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")
|
var lighting_go = []byte("// Copyright 2020 The Ebiten Authors\r\n//\r\n// Licensed under the Apache License, Version 2.0 (the \"License\");\r\n// you may not use this file except in compliance with the License.\r\n// You may obtain a copy of the License at\r\n//\r\n// http://www.apache.org/licenses/LICENSE-2.0\r\n//\r\n// Unless required by applicable law or agreed to in writing, software\r\n// distributed under the License is distributed on an \"AS IS\" BASIS,\r\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n// See the License for the specific language governing permissions and\r\n// limitations under the License.\r\n\r\n// +build ignore\r\n\r\npackage main\r\n\r\nvar Time float\r\nvar Cursor vec2\r\nvar ScreenSize vec2\r\n\r\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\r\n\tlightpos := vec3(Cursor, 50)\r\n\tlightdir := normalize(lightpos - position.xyz)\r\n\tnormal := normalize(texture1At(texCoord) - 0.5)\r\n\tambient := 0.25\r\n\tdiffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir))\r\n\treturn texture0At(texCoord) * (ambient + diffuse)\r\n}\r\n")
|
||||||
|
@ -76,6 +76,7 @@ var shaderSrcs = [][]byte{
|
|||||||
default_go,
|
default_go,
|
||||||
lighting_go,
|
lighting_go,
|
||||||
radialblur_go,
|
radialblur_go,
|
||||||
|
chromaticaberration_go,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
@ -121,6 +122,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||||||
op.Uniforms = []interface{}{
|
op.Uniforms = []interface{}{
|
||||||
float32(g.time) / 60, // Time
|
float32(g.time) / 60, // Time
|
||||||
[]float32{float32(cx), float32(cy)}, // Cursor
|
[]float32{float32(cx), float32(cy)}, // Cursor
|
||||||
|
[]float32{float32(w), float32(h)}, // ScreenSize
|
||||||
}
|
}
|
||||||
op.Images[0] = gopherImage
|
op.Images[0] = gopherImage
|
||||||
op.Images[1] = normalImage
|
op.Images[1] = normalImage
|
||||||
|
@ -18,6 +18,7 @@ package main
|
|||||||
|
|
||||||
var Time float
|
var Time float
|
||||||
var Cursor vec2
|
var Cursor vec2
|
||||||
|
var ScreenSize vec2
|
||||||
|
|
||||||
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
||||||
dir := normalize(position.xy - Cursor)
|
dir := normalize(position.xy - Cursor)
|
||||||
|
@ -3,4 +3,4 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
var radialblur_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\tdir := normalize(position.xy - Cursor)\n\tclr := texture2At(texCoord)\n\n\tsamples := [10]float{\n\t\t-22, -14, -8, -4, -2, 2, 4, 8, 14, 22,\n\t}\n\t// TODO: Add len(samples)\n\tsum := clr\n\tfor i := 0; i < 10; i++ {\n\t\t// TODO: Consider the source region not to violate the region.\n\t\tsum += texture2At(texCoord + dir*samples[i]/texture2Size())\n\t}\n\tsum /= 10 + 1\n\n\tdist := distance(position.xy, Cursor)\n\tt := clamp(dist/256, 0, 1)\n\treturn mix(clr, sum, t)\n}\n")
|
var radialblur_go = []byte("// Copyright 2020 The Ebiten Authors\r\n//\r\n// Licensed under the Apache License, Version 2.0 (the \"License\");\r\n// you may not use this file except in compliance with the License.\r\n// You may obtain a copy of the License at\r\n//\r\n// http://www.apache.org/licenses/LICENSE-2.0\r\n//\r\n// Unless required by applicable law or agreed to in writing, software\r\n// distributed under the License is distributed on an \"AS IS\" BASIS,\r\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n// See the License for the specific language governing permissions and\r\n// limitations under the License.\r\n\r\n// +build ignore\r\n\r\npackage main\r\n\r\nvar Time float\r\nvar Cursor vec2\r\nvar ScreenSize vec2\r\n\r\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\r\n\tdir := normalize(position.xy - Cursor)\r\n\tclr := texture2At(texCoord)\r\n\r\n\tsamples := [10]float{\r\n\t\t-22, -14, -8, -4, -2, 2, 4, 8, 14, 22,\r\n\t}\r\n\t// TODO: Add len(samples)\r\n\tsum := clr\r\n\tfor i := 0; i < 10; i++ {\r\n\t\t// TODO: Consider the source region not to violate the region.\r\n\t\tsum += texture2At(texCoord + dir*samples[i]/texture2Size())\r\n\t}\r\n\tsum /= 10 + 1\r\n\r\n\tdist := distance(position.xy, Cursor)\r\n\tt := clamp(dist/256, 0, 1)\r\n\treturn mix(clr, sum, t)\r\n}\r\n")
|
||||||
|
Loading…
Reference in New Issue
Block a user