How to get the texture's tiling and offset variables?

I noticed that the tessellation shaders don’t have tiling, and I would like to access the tiling and offset variables in the texture to change this. How do I do that?

As you post here, I assume you need it in the shader? Pass it as material properties to your shader.

For a texture called “X” (e.g. “_MainTex”), Unity will setup a float4 variable called “X_ST” (e.g. “_MainTex_ST”) with scale offset values. There’s a helper macro in UnityCG.cginc to compute UV with scale offset applied:

newUV = TRANSFORM_TEX(oldUV, _MainTex); // replace with the name of your texture
// also remember to declare the float4 _MainTex_ST; variable before using the macro
3 Likes

Confusing name!

You called the “T” part “offset”, the macro calls it “bias”, but the “T” suggests “Translation”, I think. The Also, the name of the macro suggests that you’re transforming the red and green components of a texture, not UVs.

// Transforms float2 UV by scale/bias property (new method)
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

On top of that, it could be rewritten like this:

#define TRANSFORM_TEX(tex,name) (tex.st * name##_ST.st + name##_ST.zw)

As for the “S” portion, the only place I know of where it is not referred to as “scale” is the Inspector for a Material, where it’s most visible to most people.

:*(

2 Likes

Well, yes. “ST” here stands for “scale translate”, and at some point the material inspector did call them that. Then based upon the number of confused users the inspector part got renamed to “tiling offset” (I think?), but it was too late to change the shader name property without breaking all existing shaders.

Just accept that it’s called “ST” and move on, mkay? :slight_smile:

Yep, thanks Aras! That was surprisingly easy.

Hi,
I am trying to make tiling in shader using texture atlas. I modified the uvs in the mesh to show selected sprite(a portion of my texture atlas) then I added your formula to shader. Then I modified vertex color info in the mesh and I use vertex color as scale/offset input. But all it gives me is just scaling of the texture without any multiplication(tiling) of original texture. Its like zoom_in or zoom_out in texture atlas.
I am sure that making this in shader requires alot more calculations in fragment shader than just adding scale and offset to the selected Sprite.

I am not good in shaders so maybe i did something wrong but your example was simple enough (1 line of code) so i think there is no place to mistake.
Perhaps there are specific definitions needed to achieve this but I dont have the necessary knowledge.

Could you please direct me to some example or give a hint … or maybe stop my experiments telling me “you can’t tile a fragment of texture like that;)”

Regards,
Piotr

Someone requested I share my edited shader, so here it is.

The only additions I made were:
line 38 - initialize scale/transform variable (the one Aras was talking about)
line 42 - Multiplied displacement offset by the scale/transform variable.