(Unity shader). Problem with screen position

105 Views Asked by At

So I need to get the objects screen position, and here is the code I use for it:

v2f vert(appdata_full v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = ComputeScreenPos(o.vertex);
    return o;
}

half4 frag(v2f i) : SV_Target
{
    return half4(i.uv-0.5,0,1);
}

The problem is that the fragment shader is calculating screen position for each pixel and I get the result below. enter image description here

But I need the WHOLE object to be colored by the coordinate of its center.

I tried to use old methods, using mul and this (unity_ObjectToWorld._m03_m13_m23) thing, but in the end it comes down to the same problem.

In the project itself, I need to move the image inside the sprite relative to its position: enter image description here

And the code:

v2f vert(appdata_full v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.ws = v.vertex;
    return o;
}

half4 frag(v2f i) : SV_Target
{
    half d = tex2D(_CameraOpaqueTexture, i.uv).r;
    half d1 = tex2D(_CameraOpaqueTexture, i.uv-i.ws/10).r;

    return d1;
}

The problem here is that the image I move gets scaled because of that fact I wrote above.

0

There are 0 best solutions below