Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

_MainTex_TexelSize what's the meaning?

Discussion in 'Shaders' started by iverson, Nov 1, 2011.

  1. iverson

    iverson

    Joined:
    Dec 27, 2008
    Posts:
    81
    uniform float4 _MainTex_TexelSize
    where is the value of the float4 _MainTexelSize from?
     
  2. Deleted User

    Deleted User

    Guest

    It's set by the application if it's present in the shader - it is the size of a texel of the texture in question, in other words, if it's a 1k x 1k texture, both x and y will be 1.0/1024.0
     
    Pagica likes this.
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    it's the easy way you get your 0..1 uv range from the texture size. It should go up to1/ 2048 or 1/4096 in unity though?
     
  4. psantoki

    psantoki

    Joined:
    Oct 24, 2012
    Posts:
    17
    _MainTex_TexelSize is set by magic sauce in the darkness of Unity's source code. It follows the dimensions of the _MainTex sampler and has these contents:

    Vector4(1 / width, 1 / height, width, height)
     
  5. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Oh wow, that's awesome. I didn't know it did that :D
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,356
    There's more to it than that: _TexelSize.y is negative when it belongs to a RenderTexture that has been flipped vertically by D3D anti-aliasing. Unfortunately, even after years of documentation complaints and forum posts, this is the only thing that is documented about _TexelSize.
     
  7. Arhspros

    Arhspros

    Joined:
    Nov 11, 2014
    Posts:
    14
    I'm just trying to understand the meaning of this "TexelSize" variable,thanks guys
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,356
    Is there something more specific about the first three responses in this thread that doesn't make sense?
     
  9. MatrixTan

    MatrixTan

    Joined:
    Apr 7, 2015
    Posts:
    6
    I can't find the description in documents. is there url ?
     
  10. blueknee

    blueknee

    Joined:
    Apr 5, 2014
    Posts:
    8
  11. gferrand_UofM

    gferrand_UofM

    Joined:
    Jan 19, 2016
    Posts:
    8
    unfortunately the way _TexelSize was defined, it doesn't scale up to 3D textures
     
  12. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    I cannot seem to use _MainTex_TexelSize. When I use it, it just says that it doesn't exist. How do I use it? In for example Unity's default Sprites/Default shader (which I have copied from latest beta)?
     
  13. howong

    howong

    Joined:
    Oct 9, 2017
    Posts:
    6
    I think it seems to work. You'd need to declare the variable to access the associated property value. Put this above the frag function if that's where you refer to it:
    float4 _MainTex_TexelSize;
     
  14. AlejUb

    AlejUb

    Joined:
    Mar 11, 2019
    Posts:
    25
    How do we get the size of the volume depth though (for a 3D texture)?
    We have to manually feed it?
     
  15. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,767
    Feed it in manually.
     
    IgorAherne and AlejUb like this.
  16. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    If you are using HLSL you can do this:
    Code (CSharp):
    1. uint3 textureDimensions = 0;
    2. _MainTex.GetDimensions(0,textureDimensions.x,textureDimensions.y,textureDimensions.z);
    I don't think it is possible in CG.
     
  17. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,315
    There is no more CG in Unity, hasn't been for maaany years. It is all treated as HLSL despite the .cginc and CGINCLUDE naming. What matters though is the shader model you're targeting your shader code, as GetDimensions() did not exist in HLSL before SM4.0 (or before DX10 to be more specific). So you'd want a
    #pragma target 4.0
    at least where Texture2D exists, prior to that it was just the sampler bound
    sampler2D
    types.

    DX9 was removed from Unity in 2017.3 though, so there isn't much worry about that function being unsupported in your shader code now.
     
  18. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,767
    * Unless you're targeting OpenGLES 2.0 or 3.0 in which case there's no equivalent function.
     
    Invertex likes this.
  19. zzxiang

    zzxiang

    Joined:
    Aug 29, 2015
    Posts:
    3
    Is the _TexelSize suffix also available in compute shader?

    I defined the following two variables in my compute shader

    Code (Shader):
    1. Texture<float4> _MyTexture;
    2. float4 _MyTexture_TexelSize;
    and set _MyTexture in C# code

    Code (CSharp):
    1. ComputeShader myComputeShader;
    2. Texture2D myTexture;
    3. // ...
    4. myComputeShader = myComputeShader.SetTexture(0, "_MyTexture", myTexture);
    But _MyTexture_TexelSize seems to not containing the size information of my texture.
     
unityunity