examples/shader: bug fix: wrong usages of position

Closes #2638
Updates #1431
This commit is contained in:
Hajime Hoshi 2023-04-13 01:48:56 +09:00
parent dad36c8de8
commit 96f9ea1b2e
4 changed files with 22 additions and 13 deletions

View File

@ -21,6 +21,7 @@ var Cursor vec2
var ScreenSize vec2 var ScreenSize vec2
func Fragment(position vec4, texCoord vec2, color vec4) vec4 { func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
// Note that the value of position.xy is indeterministic as the destination image is somewhere on a texture atlas.
pos := position.xy/imageDstTextureSize() + Cursor/ScreenSize/4 pos := position.xy/imageDstTextureSize() + Cursor/ScreenSize/4
clr := 0.0 clr := 0.0
clr += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10) clr += sin(pos.x*cos(Time/15)*80) + cos(pos.y*cos(Time/15)*10)

View File

@ -21,8 +21,12 @@ var Cursor vec2
var ScreenSize vec2 var ScreenSize vec2
func Fragment(position vec4, texCoord vec2, color vec4) vec4 { func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
srcOrigin, srcSize := imageSrcRegionOnTexture()
pos := (texCoord - srcOrigin) / srcSize
pos *= ScreenSize
lightpos := vec3(Cursor, 50) lightpos := vec3(Cursor, 50)
lightdir := normalize(lightpos - position.xyz) lightdir := normalize(lightpos - vec3(pos, 0))
normal := normalize(imageSrc1UnsafeAt(texCoord) - 0.5) normal := normalize(imageSrc1UnsafeAt(texCoord) - 0.5)
const ambient = 0.25 const ambient = 0.25
diffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir)) diffuse := 0.75 * max(0.0, dot(normal.xyz, lightdir))

View File

@ -21,7 +21,11 @@ var Cursor vec2
var ScreenSize 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) srcOrigin, srcSize := imageSrcRegionOnTexture()
pos := (texCoord - srcOrigin) / srcSize
pos *= ScreenSize
dir := normalize(pos - Cursor)
clr := imageSrc2UnsafeAt(texCoord) clr := imageSrc2UnsafeAt(texCoord)
samples := [...]float{ samples := [...]float{
@ -34,7 +38,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
} }
sum /= 10 + 1 sum /= 10 + 1
dist := distance(position.xy, Cursor) dist := distance(pos, Cursor)
t := clamp(dist/256, 0, 1) t := clamp(dist/256, 0, 1)
return mix(clr, sum, t) return mix(clr, sum, t)
} }

View File

@ -21,22 +21,22 @@ var Cursor vec2
var ScreenSize vec2 var ScreenSize vec2
func Fragment(position vec4, texCoord vec2, color vec4) vec4 { func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
srcsize := imageSrcTextureSize() srcOrigin, srcSize := imageSrcRegionOnTexture()
ypx := texCoord.y * srcsize.y pos := (texCoord - srcOrigin) / srcSize
pos *= ScreenSize
border := ScreenSize.y*0.6 + 4*cos(Time*3+ypx/10) border := ScreenSize.y*0.6 + 4*cos(Time*3+pos.y/10)
if position.y < border { if pos.y < border {
return imageSrc2UnsafeAt(texCoord) return imageSrc2UnsafeAt(texCoord)
} }
rorigin, _ := imageSrcRegionOnTexture() srcTexSize := imageSrcTextureSize()
xoffset := (4 / srcTexSize.x) * cos(Time*3+pos.y/10)
xoffset := (4 / srcsize.x) * cos(Time*3+ypx/10) yoffset := (20 / srcTexSize.y) * (1.0 + cos(Time*3+pos.y/40))
yoffset := (20 / srcsize.y) * (1.0 + cos(Time*3+ypx/40)) bordertex := border / srcTexSize.y
bordertex := border / srcsize.y
clr := imageSrc2At(vec2( clr := imageSrc2At(vec2(
texCoord.x+xoffset, texCoord.x+xoffset,
-(texCoord.y+yoffset-rorigin.y)+bordertex*2+rorigin.y, -(texCoord.y+yoffset-srcOrigin.y)+bordertex*2+srcOrigin.y,
)).rgb )).rgb
overlay := vec3(0.5, 1, 1) overlay := vec3(0.5, 1, 1)