Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Shaders' started by Wolfos, May 11, 2013.

  1. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    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?
     
  2. R-Type

    R-Type

    Joined:
    Oct 31, 2012
    Posts:
    44
    As you post here, I assume you need it in the shader? Pass it as material properties to your shader.
     
    Last edited: May 11, 2013
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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:

    Code (csharp):
    1. newUV = TRANSFORM_TEX(oldUV, _MainTex); // replace with the name of your texture
    2. // also remember to declare the float4 _MainTex_ST; variable before using the macro
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    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.

    Code (csharp):
    1. // Transforms float2 UV by scale/bias property (new method)
    2. #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
    On top of that, it could be rewritten like this:
    Code (csharp):
    1. #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.

    :*(
     
    Last edited: May 12, 2013
    KyryloKuzyk and BrainSlugs83 like this.
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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? :)
     
  6. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    Yep, thanks Aras! That was surprisingly easy.
     
  7. tra

    tra

    Joined:
    Mar 21, 2013
    Posts:
    32
    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
     
  8. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    Someone requested I share my edited shader, so here it is.

    http://pastebin.com/izmp8PzW

    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.
     
    Last edited: Dec 29, 2013