When rendering, unity can push certaint values into the shader automatically. It is also possible to push some values to the shader by script, though it requires extra work and the usage of shaders or materials is not maintenance free. So: Is there any option that would make unity automatically push texture size to the shader?
I found out, it’s (for sampler named _MainTex)
uniform float4 _MainTex_TexelSize;
According to documentation https://docs.unity3d.com/Manual/SL-PropertiesInPrograms.html
Texture size
{TextureName}_TexelSize - a float4 property contains texture size information:
x contains 1.0/width
y contains 1.0/height
z contains width
w contains height
So wen you define sampler, define texelSize as well like
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
and then just use _MainTex_TexelSize.xy properties
No.
Well, I’m pretty sure no. Graphics cards and shaders use only percentage-based UV coords to look up texture values – text2D(0.5, 0.5) means the middle color. It’s a black box. The shader code purposely never knows or cares about the size or settings of the actual texture being used.