diff --git a/examples/shader/chromaticaberration.go b/examples/shader/chromaticaberration.go index 4716c869e..cb791aff8 100644 --- a/examples/shader/chromaticaberration.go +++ b/examples/shader/chromaticaberration.go @@ -22,10 +22,10 @@ var ImageSize vec2 func Fragment(position vec4, texCoord vec2, color vec4) vec4 { center := ImageSize / 2 - amount := (center - Cursor) / texture2Size() / 10 + amount := (center - Cursor) / image2TextureSize() / 10 var clr vec3 - clr.r = texture2At(vec2(texCoord.x+amount.x, texCoord.y)).r - clr.g = texture2At(texCoord).g - clr.b = texture2At(vec2(texCoord.x-amount.x, texCoord.y)).b + clr.r = image2TextureAt(vec2(texCoord.x+amount.x, texCoord.y)).r + clr.g = image2TextureAt(texCoord).g + clr.b = image2TextureAt(vec2(texCoord.x-amount.x, texCoord.y)).b return vec4(clr, 1.0) } diff --git a/examples/shader/chromaticaberration_go.go b/examples/shader/chromaticaberration_go.go index aa612cee6..330620e54 100644 --- a/examples/shader/chromaticaberration_go.go +++ b/examples/shader/chromaticaberration_go.go @@ -3,4 +3,4 @@ package main -var chromaticaberration_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\nvar ImageSize vec2\n\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tcenter := ImageSize / 2\n\tamount := (center - Cursor) / texture2Size() / 10\n\tvar clr vec3\n\tclr.r = texture2At(vec2(texCoord.x+amount.x, texCoord.y)).r\n\tclr.g = texture2At(texCoord).g\n\tclr.b = texture2At(vec2(texCoord.x-amount.x, texCoord.y)).b\n\treturn vec4(clr, 1.0)\n}\n") +var chromaticaberration_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\nvar ImageSize vec2\n\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tcenter := ImageSize / 2\n\tamount := (center - Cursor) / image2TextureSize() / 10\n\tvar clr vec3\n\tclr.r = image2TextureAt(vec2(texCoord.x+amount.x, texCoord.y)).r\n\tclr.g = image2TextureAt(texCoord).g\n\tclr.b = image2TextureAt(vec2(texCoord.x-amount.x, texCoord.y)).b\n\treturn vec4(clr, 1.0)\n}\n") diff --git a/examples/shader/default.go b/examples/shader/default.go index 67860c803..b726bf4e0 100644 --- a/examples/shader/default.go +++ b/examples/shader/default.go @@ -21,7 +21,7 @@ var Cursor vec2 var ImageSize vec2 func Fragment(position vec4, texCoord vec2, color vec4) vec4 { - pos := position.xy/textureDstSize() + Cursor/textureDstSize()/4 + pos := position.xy/imageDstTextureSize() + Cursor/imageDstTextureSize()/4 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) diff --git a/examples/shader/default_go.go b/examples/shader/default_go.go index ce9222dd5..669a1b06c 100644 --- a/examples/shader/default_go.go +++ b/examples/shader/default_go.go @@ -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\nvar ImageSize 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\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\nvar ImageSize vec2\n\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tpos := position.xy/imageDstTextureSize() + Cursor/imageDstTextureSize()/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") diff --git a/examples/shader/lighting.go b/examples/shader/lighting.go index 259a9874f..1bfaba87b 100644 --- a/examples/shader/lighting.go +++ b/examples/shader/lighting.go @@ -23,8 +23,8 @@ var ImageSize vec2 func Fragment(position vec4, texCoord vec2, color vec4) vec4 { lightpos := vec3(Cursor, 50) lightdir := normalize(lightpos - position.xyz) - normal := normalize(texture1At(texCoord) - 0.5) + normal := normalize(image1TextureAt(texCoord) - 0.5) ambient := 0.25 diffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir)) - return texture0At(texCoord) * (ambient + diffuse) + return image0TextureAt(texCoord) * (ambient + diffuse) } diff --git a/examples/shader/lighting_go.go b/examples/shader/lighting_go.go index 3fa0e18e7..ce8b66601 100644 --- a/examples/shader/lighting_go.go +++ b/examples/shader/lighting_go.go @@ -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\nvar ImageSize 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\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\nvar ImageSize 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(image1TextureAt(texCoord) - 0.5)\n\tambient := 0.25\n\tdiffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir))\n\treturn image0TextureAt(texCoord) * (ambient + diffuse)\n}\n") diff --git a/examples/shader/radialblur.go b/examples/shader/radialblur.go index e76fc26af..6b1e59da0 100644 --- a/examples/shader/radialblur.go +++ b/examples/shader/radialblur.go @@ -22,7 +22,7 @@ var ImageSize vec2 func Fragment(position vec4, texCoord vec2, color vec4) vec4 { dir := normalize(position.xy - Cursor) - clr := texture2At(texCoord) + clr := image2TextureAt(texCoord) samples := [10]float{ -22, -14, -8, -4, -2, 2, 4, 8, 14, 22, @@ -31,7 +31,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { sum := clr for i := 0; i < 10; i++ { // TODO: Consider the source region not to violate the region. - sum += texture2At(texCoord + dir*samples[i]/texture2Size()) + sum += image2TextureAt(texCoord + dir*samples[i]/image2TextureSize()) } sum /= 10 + 1 diff --git a/examples/shader/radialblur_go.go b/examples/shader/radialblur_go.go index d4813c6c1..070899539 100644 --- a/examples/shader/radialblur_go.go +++ b/examples/shader/radialblur_go.go @@ -3,4 +3,4 @@ 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\nvar ImageSize 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\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\nvar ImageSize vec2\n\nfunc Fragment(position vec4, texCoord vec2, color vec4) vec4 {\n\tdir := normalize(position.xy - Cursor)\n\tclr := image2TextureAt(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 += image2TextureAt(texCoord + dir*samples[i]/image2TextureSize())\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") diff --git a/shader.go b/shader.go index 39808937f..54b6f74f5 100644 --- a/shader.go +++ b/shader.go @@ -29,10 +29,10 @@ var shaderSuffix string func init() { shaderSuffix = ` -var __textureDstSize vec2 +var __imageDstTextureSize vec2 -func textureDstSize() vec2 { - return __textureDstSize +func imageDstTextureSize() vec2 { + return __imageDstTextureSize } ` @@ -42,7 +42,7 @@ var __textureSizes [%d]vec2 for i := 0; i < graphics.ShaderImageNum; i++ { shaderSuffix += fmt.Sprintf(` -func texture%[1]dSize() vec2 { +func image%[1]dTextureSize() vec2 { return __textureSizes[%[1]d] } `, i) @@ -59,7 +59,7 @@ var __textureOffsets [%d]vec2 } // __t%d is a special variable for a texture variable. shaderSuffix += fmt.Sprintf(` -func texture%[1]dAt(pos vec2) vec4 { +func image%[1]dTextureAt(pos vec2) vec4 { return texture2D(__t%[1]d, pos%[2]s) } `, i, offset) @@ -68,8 +68,8 @@ func texture%[1]dAt(pos vec2) vec4 { shaderSuffix += ` func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, vec4) { return mat4( - 2/textureDstSize().x, 0, 0, 0, - 0, 2/textureDstSize().y, 0, 0, + 2/imageDstTextureSize().x, 0, 0, 0, + 0, 2/imageDstTextureSize().y, 0, 0, 0, 0, 1, 0, -1, -1, 0, 1, ) * vec4(position, 0, 1), texCoord, color