Search Unity

[Shader Graph] Does _TexelSize and _ST is handle ?

Discussion in 'Shaders' started by FlorentFal, Mar 18, 2019.

  1. FlorentFal

    FlorentFal

    Joined:
    Nov 20, 2015
    Posts:
    55
    Hi, I would known if there is a way to access _TexelSize / _ST properties of a texture in a ShaderGraph ?
    For example is it possible to get _MainTex_TexelSize and _MainTex_ST values ?

    What about creating a Vector4 property and set its reference value to "_MainTex_TexelSize" ?

    Thanks

    Florent
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Yep, this works. It's a little silly they don't have this as an output of of the texture node, but there are a ton of built in values the Shader Graph simply doesn't give easy access to.

    Edit: It looks like there's a Texel Size node in LWRP 5.2.3! Not sure what version it was added for, but it's there now, so the hack isn't necessary.

    One note, I would pretend the _TexName_ST is dead. By default Shader Graph does not expose the scale and offset for texture properties (they're always using [NoScaleOffset]) and there's no TRANSFORM_TEX equivalent node that auto references references the _ST value. The value is still there, and can still be set via script or the debug inspector, and then referenced using the same unexposed Shader Graph property hack, there just isn't any meaningful benefit to doing so over using a custom Vector property.

    Actually, there is one benefit to not using the _ST value; if you do create a _MainTex_ST property, the graph preview goes pink and your shader while it works, produces a "redefinition" error as it appears _MainTex_ST is already being defined. :rolleyes:
     
  3. Kmiecis

    Kmiecis

    Joined:
    Oct 11, 2017
    Posts:
    12
    Okay so it seems like 'Tiling and Offset' in Shader Graph, according to manual at https://docs.unity3d.com/Packages/com.unity.shadergraph@6.9/manual/Tiling-And-Offset-Node.html, is written like:
    Code (CSharp):
    1. void Unity_TilingAndOffset_float(float2 UV, float2 Tiling, float2 Offset, out float2 Out)
    2. {
    3.     Out = UV * Tiling + Offset;
    4. }
    And according to 'UnityCG.cginc' TRANSFORM_TEX looks like:
    Code (CSharp):
    1. #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
    Therefore we can assume that one is implementation of the other and can safely use them, which i sure did.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    They're both modifying the UV by a scale and offset, but the difference is the
    _TexName_ST
    variables were something that Unity was exposing on each texture property, and automatically setting on the material rather than being unique material properties on their own. For Shader Graph you can do the same scale and offset, but you need to define and set those properties yourself. You can't easily make use of the
    _TexName_ST
    itself.