title: How can I get texture size in shader?
body: <p>I am trying to implement shader for a LineRenderer (Unity component). And i need to get value from 0 to 1 for x axis in frag shader, that means position in local texture coordinates (0 for pixel on the left side of texture and 1 for right side border). </p>
  
  <p>As documentation says <code>_MainTex_TexelSize</code> contains information about texture size and <code>_MainTex_ST</code> contains information about tiling. So it seems simple. I need multiply <code>uv.x</code> and <code>(_MainTex_TexelSize.z * _MainTex_ST.x)</code>.</p>
  
  <p>There is my shader:</p>
  
  <pre><code>Shader "LineRendering/Test"
  {
      Properties
      {
          [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
      }
  
      SubShader
      {
          Tags
          {
              "Queue" = "Transparent"
              "IgnoreProjector" = "True"
              "RenderType" = "Transparent"
              "PreviewType" = "Plane"
              "CanUseSpriteAtlas" = "True"
          }
  
          LOD 200
  
          Cull Off
          Lighting Off
          ZWrite Off
          Fog { Mode Off }
          Blend One OneMinusSrcAlpha
  
          Pass
          {
              CGPROGRAM
  
              #pragma target 3.0          
              #pragma vertex vert
              #pragma fragment frag
              #pragma enable_d3d11_debug_symbols
  
              #include "UnityCG.cginc"
              #include "noiseSimplex.cginc"
  
              struct appdata_t
              {
                  fixed4 vertex : POSITION;
                  fixed2 uv : TEXCOORD0;
              };
  
              struct v2f
              {
                  fixed4 vertex : SV_POSITION;
                  fixed2 uv : TEXCOORD0;
                  fixed2 srcPos : TEXCOORD1;
                  fixed2 texelSize : TEXCOORD2;
                  fixed2 tilingCapacity : TEXCOORD3;
              };
  
              uniform fixed4 _MainTex_TexelSize, _MainTex_ST;
  
              v2f vert(appdata_t IN)
              {
                  v2f OUT;
  
                  OUT.vertex = UnityObjectToClipPos(IN.vertex);
                  OUT.uv = IN.uv;
                  //OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
                  //OUT.uv = fixed2(IN.uv.x * (_MainTex_ST.x + _MainTex_ST.z), (IN.uv.y +  * _MainTex_ST.y));
                  //OUT.uv = (IN.uv * _MainTex_ST.xy + _MainTex_ST.zw);               
  
                  OUT.texelSize = fixed2(_MainTex_TexelSize.z, _MainTex_TexelSize.w);
                  OUT.tilingCapacity = fixed2(_MainTex_ST.x, _MainTex_ST.y);
  
                  return OUT;
              }
  
              fixed4 frag(v2f IN) : COLOR
              {
                  fixed4 output;
                  output.a = 1;
  
                  fixed relativeWidth = IN.uv.x / (IN.texelSize.x * IN.tilingCapacity.x);             
  
                  //fixed relativeWidth = IN.uv.x / (_MainTex_TexelSize.z * _MainTex_ST.x);                   
  
                  output.rgb = fixed3(relativeWidth, relativeWidth, relativeWidth);
  
                  return output;
              }
  
              ENDCG
          }
      }
  }
  </code></pre>
  
  <p>But this shader don't work as I expect. </p>
  
  <p><a href="https://i.stack.imgur.com/iEetL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iEetL.png" alt="How it&#39;s looks like"></a></p>
  
  <p>I am trying to debug this shader in runtime but getting weird results. Texel size and tiling capacity variable have <code>1</code> as value.</p>
  
  <p><a href="https://i.stack.imgur.com/mmt0j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mmt0j.png" alt="Shader debug."></a></p>
  
  <p>So I need your help. What am I doing wrong? Is there some other way to get texture size for <code>LineRenderer</code> in Unity?</p>