Hi there! As the title says, I’ve been trying to normalize var_texcoord0 for sprites. The solution I have in mind is to attach a script to the game object that will feed the shader with the data it needs to normalize it.
This is the fragment shader i’m using for testing:
varying mediump vec4 position;
varying mediump vec2 var_texcoord0;
uniform lowp sampler2D texture_sampler;
uniform mediump vec4 nomalized_sprite_ps;
void main()
{
vec2 invertedTexCoords = vec2(var_texcoord0.x, 1 - var_texcoord0.y);
vec2 normalizedTexCoords = (invertedTexCoords - nomalized_sprite_ps.xy) / nomalized_sprite_ps.zw;
gl_FragColor = vec4(normalizedTexCoords.xy,0,1);
}
And this is the script:
local atlasSize = vmath.vector3(512, 256, 0); --atlas.width, atlas.height
local sprite_ps = vmath.vector4(128,0, 64,64); --offset.x, offset.y, size.x, size.y
--TODO: check sprite rotation
sprite_ps = vmath.vector4(
sprite_ps.x / atlasSize.x, sprite_ps.y / atlasSize.y,
sprite_ps.z / atlasSize.x, sprite_ps.w / atlasSize.y)
go.set("#sprite", "nomalized_sprite_ps", sprite_ps)
And this is the output:
So, the math is working!
Now I just need to figure out how to remove the hardcoded values for the atlas size (512,256), sprite offset (128,0), sprite size (64,64) and also the sprite rotation within the atlas.
JCash pointed me to the resource.get_atlas method, but I couldn’t find the data I need. I can only find the size in the atlas.animations table, which looks like:
1 = [table: 0x01d9a8d9ebc0]
{
"id" = "asteroid1"
"height" = 32
"fps" = 30
"playback" = 0
"frame_start" = 1
"frame_end" = 2
"flip_horizontal" = false
"width" = 32
"flip_vertical" = false
}
So, am I missing anything? Does anyone know how can I get the missing values?
Thanks!