Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Our response to Improbable’s blog post (and why you can keep working on your SpatialOS game). Read more here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

What is _MainTex_ST ?

Discussion in 'Shaders' started by alex wang, Jun 15, 2009.

  1. alex wang

    alex wang

    Joined:
    Jun 5, 2009
    Posts:
    9
    Hi,

    I found a lot of CG shader programs has a float4 variable with the name _MainTex_ST. Is this just for the helper function TRANSFORM_TEX in the UnityCG.cginc?

    And the TRANSFORM_TEX is defined as:
    Code (csharp):
    1.  
    2. #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
    3.  
    Then can I think all _ST parameters are provided by Unity engine? Or maybe the xy means tilting and zw means offset?

    Thanks a lot!
     
  2. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,528
    Yes, indeed for any texture property, Unity provides value for float4 with "_ST" suffix. The x,y contains texture scale, and z,w contains translation (offset).
     
  3. drhenry

    drhenry

    Joined:
    Jan 13, 2010
    Posts:
    9
    And how does Unity compute what values to put into this scale and offset variable?
    Is this only needed for usage of texRECT, where texture coordinates in [0,1]x[0,1] have to be mapped to integer coordinates in [0, texWidth]x[0, texHeight]?
    I stumbled over it in SSAO code...
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,600
    I would assume from the offset and tiling values on the material as they represent texture scale and texture offset
     
  5. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    949
    What is the smallest appropriate data type for `_MainTex_ST` for mobile platform? Is it appropriate to use `half4` instead of `float4`?
     
  6. ABerlemont

    ABerlemont

    Joined:
    Sep 27, 2012
    Posts:
    23
    Can someone give an example of how to use that key word ?

    Unity is throwing :
    Shader error in 'Geometry/DistanceGeomCut': '_MainTex_ST' : undeclared identifier; 'xy' : vector field selection out of range; 'zw' : vector field selection out of range at line 110 (on opengl)

    And I think it's because I don't use TRANSFORM_TEX properly.

    Thanks !
     
  7. ABerlemont

    ABerlemont

    Joined:
    Sep 27, 2012
    Posts:
    23
    Piyush_Pandey likes this.
  8. Piyush_Pandey

    Piyush_Pandey

    Joined:
    Aug 29, 2014
    Posts:
    2
  9. ian_unity431

    ian_unity431

    Joined:
    Oct 12, 2018
    Posts:
    4
    hey @Aras, how would you recommend going about programming a keyframe animation for _MainTex_ST.
    I'm trying this out, but can't seem to get about the 'Object' limitation.

    AnimationClip clip = new AnimationClip();

    EditorCurveBinding binding = new EditorCurveBinding();
    binding.type = typeof(MeshRenderer);
    binding.path = "";
    binding.propertyName = textureManager.materialGroups[0].texturePropertyName + "_ST";

    ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[Data.Length];
    for (int i = 0; i < (Data.Length); i++)
    {
    Vector2 value = Data.textureOffset;// Stores a Vector2 of the material offset I want to save in an animation.

    keyFrames = new ObjectReferenceKeyframe();
    keyFrames.time = Data.time;
    keyFrames.value = value; // a Vector2 is obviously not a UnityEngine.Object.
    }
    AnimationUtility.SetObjectReferenceCurve(clip, binding, keyFrames);