Hi, I’ve created UI shader which needs to handle world position of vertex. On Camera Space Canvas everything works fine but on Screen Space Canvas position of vertex is different.
So from shader like:
v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.vertexPos = v.vertex;
OUT.worldPos = mul(unity_ObjectToWorld, v.vertex);
OUT.vertex = UnityObjectToClipPos(v.vertex);
return OUT;
}
half4 frag(v2f IN) : SV_Target
{
half4 color = frac(length(IN.worldPos.xy - _Pos.xy) / 50); //on example screen _Pos is (0,0)
color.a = 1;
#ifdef UNITY_UI_CLIP_RECT
color.a *= UnityGet2DClipping(IN.vertexPos.xy, _ClipRect);
#endif
return color;
}
On scene view I have (it’s proper one, there is world pos (0,0)):
But on game view (it’s wrong):
Any idea how to fix this? I thought about offsetting value in shader but it will still make that on scene view or game view value will be wrong. I cannot even handle different previews because there is no that kind of define.