(SOLVED) Is it possible to draw a sprite's entire Atlas to a render target?

@jhonny.goransson @britzl Thank you both for your answers! I actually also just stumbled across this old thread: Shader - UVs based on whole atlas? - #3 by ross.grams that made me realize the problem is actually not as complicated as I thought. If I pass the center point of the sprite to the vp, I can figure out which of the vertices is currently being evaluated based on its displacement from the center point. Then I can just assign the texcoord for that vertex to be the appropriate corner of the full texture, and set the vertex position to match the dimensions of the full texture.

#define ATLAS_HALF_WIDTH 256.0
#define ATLAS_HALF_HEIGHT 256.0

uniform highp mat4 view_proj;

// updated with the position of the sprite (center point)
uniform highp vec4 pos;

// positions are in world space
attribute highp vec4   position;
attribute mediump vec2 texcoord0;
attribute lowp float   page_index;

varying mediump vec2 var_texcoord0;
varying lowp float   var_page_index;

void main()
{
    vec4 temp_pos = vec4(position.xyz, 1.0);
    
    var_texcoord0  = texcoord0;
    vec2 diff = position.xy - pos.xy;
    if (diff.x < 0.0) {
        var_texcoord0.x = 0.0;
        temp_pos.x = pos.x - ATLAS_HALF_WIDTH;
    } else {
        var_texcoord0.x = 1.0;
        temp_pos.x = pos.x + ATLAS_HALF_WIDTH;
    }

    if (diff.y < 0.0) {
        var_texcoord0.y = 0.0;
        temp_pos.y = pos.y - ATLAS_HALF_HEIGHT;
    } else {
        var_texcoord0.y = 1.0;
        temp_pos.y = pos.y + ATLAS_HALF_HEIGHT;
    }
    
    var_page_index = page_index;
    gl_Position    = view_proj * temp_pos;
}

Based on my preliminary testing it seems to work, so I just need to set up the whole system. Thanks again!

3 Likes