I have a problem with the
agk_spritesize uniform in a shader:
For example, if I create sprites with the following code:
WH = 500
WW = 500
sW = 250
sH = 20
SetWindowSize(WW, WH, 0 )
SetVirtualResolution(WW,WH)
img = CreateImageColor(255,255,255,255)
spr1 = createSprite(img)
spr2 = createSprite(img)
SetSpriteSize(spr1, sW, sH)
SetSpriteSize(spr2, sW * 1.2, sH)
SetSpritePosition(spr1, WW/2.0 - sW/2.0, WH/2.0)
SetSpritePosition(spr2, WW/2.0 - sW/2.0, WH/2.0 + 1.5*sH)
vsFile as String = "vshader-01.vs"
fsFile as String = "fshader-01.fs"
//shader = LoadShader(vsFile, fsFile)
shader = LoadSpriteShader(fsFile)
SetSpriteShader(spr1, shader)
SetSpriteShader(spr2, shader)
do
Sync()
loop
And I set my fragment shader ("fshader-01.fs"):
varying mediump vec2 uvVarying;
void main()
{
vec4 color = vec4(1.0,0.0,0.0,1.0); //red
float x = uvVarying.x;
float lim_x = 0.65;
if(x > lim_x)
color = vec4(0.0, 1.0, 0.0, 1.0); //green
gl_FragColor = color;
}
My sprites end up like this:
The fragment shader makes it so that "65%" of the length of the sprite is red, and the rest is green.
What I want to get, is to specify a length on the sprite in real coordinates (not
uv space). So I thought if I added the uniform
agk_spritesize, I can use the x value of this vector.
My attempt is this:
uniform vec2 agk_spritesize;
varying mediump vec2 uvVarying;
void main()
{
vec4 color = vec4(1.0,0.0,0.0,1.0); //red
float x = uvVarying.x * agk_spritesize.x;
float lim_x = 125.0;
if(x > lim_x)
color = vec4(0.0, 1.0, 0.0, 1.0); //green
gl_FragColor = color;
}
What I expected, is that this would do so that any part of the sprite that is more than 125.0 "units" from 0.0, would be green.
However, the sprites end up completely red.
It looks like
agk_spritesize.x (and
agk_spritesize.y) is always zero.
Why is that, and what am I doing wrong?