Does the shader have a built-in function to get the texture size?

:melting_face: :wave: Does the shader have a built-in function to get the texture size? I’m trying to draw a circle on a texture, the texture is in sprite.

I tried it in different ways, sometimes it turns out to be a stretched circle, sometimes it’s not visible at all, sometimes only part of the circle is visible.

vec4 drawCircle(){
    vec2 uv = (uv0 * 2.0 - uv0.xy)/uv0.y;
    float v = length(uv);
     v -= 0.5;
    return vec4(v,v,v, 1.0);
  }
vec4 drawCircle(){
    vec2 uv = (uv0 * 2.0 - cc_nativeSize.xy)/cc_nativeSize.y;
    float v = length(uv);
     v -= 0.5;
    return vec4(v,v,v, 1.0);
  }

Try it “textureSize” API

1 Like

don’t work –
vec2 sizeTexture = textureSize(cc_spriteTexture, 0);
error

ERROR: 0:47: 'texture2DSize' : no matching overloaded function found

ERROR: 0:47: '=' : dimension mismatch

ERROR: 0:47: '=' : cannot convert from 'const mediump float' to 'highp 2-component vector of float'

I only get the circle when the game is not running.

vec4 drawCircle(){
    
    vec2 uv = uv0 - 0.5;
    uv.y *= (cc_nativeSize.x / cc_nativeSize.y)*2.;
    uv.x *= (cc_nativeSize.x / cc_nativeSize.y);
    uv *= 3.;
    float d = length(uv);
    return vec4(d, d, d, 1.);
  }

If I start the game there is no circle.

I have these settings for spiteFrame (circle)

Assuming you’re using the latest versions of GLSL, you can get texture size with the “textureSize()”

Example code here…

uniform sampler2D myTexture; // Uniform for the texture

void main() {
//Note: if you don’t specify a LOD, glsl will default to 0 automatically
ivec2 texSize = textureSize(myTexture, 0); // Get size of the texture at LOD 0
int width = texSize.x; // Width of the texture
int height = texSize.y; // Height of the texture

//variable declarations above to demonstrate use of `width` and `height` for your calculations

}

1 Like