Screen Position from ANOTHER Cam

If I want to get the screen position vector of a mesh to use as UV, but as seen from another camera, how do I do it?

Either I intercept the normal screenspace function and chug my other camera into it…

Or I output the screen position as a UV texture from my other cameras view and input into the other shader… I don’t know if that is at all possible.

Any ideas are highly appreciated.

The way you get the current camera’s screen space UV is by using the current view projection matrix to transform the world position into an adjusted clip space position. If you want the screen space UV of another camera you need to calculate the view projection matrix of that camera in script and pass it to the shader.

Thanks! Are there any resources that describes this in a bit more detail?

Look spot on to what I was looking for, thanks a million!

You’ll have to forgive my ignorance, but I was unable to implement that example you gave on the other post. I tried implementing it in the shader graph by making a “custom function”, but I’m not sure how to get the vertex positions in there.

So I tried to write a shader, which I have almost null experience of. When I save it, the console is complaining about it having “inconsistent endings” and loads of other stuff. Obviously I’m doing something fundamentally wrong. When I go back into the shader the editor adds some lines which are commented with “Exclusing shader from DirectX11…” (paraphrasing).

I did this by basically taking unity’s example of vertex positions, and making a frankenstein with your example, stuffing things in where they seemed to belong. Unity - Manual: Providing vertex data to vertex programs

Oh, and I am using URP. Not sure if that makes any difference.

Shader "VertexInputSimple" {
    SubShader{
        Pass {
            CGPROGRAM

            sampler2D _ProjectedTex;
            float4x4 _CustomMatrix_VP;

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct v2f {
                float4 pos;
                float4 projUV;
            };

            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                float4 worldPos = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1.0));
                o.projUV = ComputeScreenPos(mul(_CustomMatrix_VP, worldPos));
                return o;
            }

            half4 frag(v2f i) : SV_Target
            {
              float2 uv = i.projUV.xy / i.projUV.w;
              return tex2D(_ProjectedTex, uv);
            }
            ENDCG
        }
    }
}

You don’t need a custom function, or vertex positions, or any of that.

You need the world position, you need that view position matrix, and that’s about it.

Multiply matrix by the world position.
Divide the xy by w.
Multiply by 0.5, add 0.5.

done.

Managed to get it to work in shader graph! Can’t thank you enough. I’m posting a video on this so I’ll get back to you so you’ll know what you contributed to :slight_smile: