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).
As documentation says _MainTex_TexelSize
contains information about texture size and _MainTex_ST
contains information about tiling. So it seems simple. I need multiply uv.x
and (_MainTex_TexelSize.z * _MainTex_ST.x)
.
There is my shader:
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
}
}
}
But this shader don't work as I expect.
I am trying to debug this shader in runtime but getting weird results. Texel size and tiling capacity variable have 1
as value.
So I need your help. What am I doing wrong? Is there some other way to get texture size for LineRenderer
in Unity?