How to get texture size in fragment shader?

I’m trying to get texture size in fragment shader but all I got is an error all the time. Things I’ve tried so far;

uniform sampler2D texture_splat;

textureSize(texture_splat)
textureSize(texture_splat, 1.0)

textureSize(texture_splat, 1);

Hey, GLSL is quite strict, try this instead of 10:

vec2 size = vec2(10.0);

Basically 10 is an int, whereas 10.0 is a float.

Edit: I think this may need updating too:

ivec2 decalSize = textureSize(texture_splat, 1);

Since according to the GLSL spec it returns an int vector.

2 Likes

Note that this will only work on WebGL2, but not WebGL1. If you need compatibility there too, you might need to use uniforms.

3 Likes

Thank you guys!

you might need to use uniforms.

You mean I need to pass the size as hard-coded data?

Not hardcoded, pass texture.width and texture.height for example.

1 Like