How to convert World Position to Screen UV Coordinate?

I found there’s a matrix named WorldToClip in FViewUniformShaderParameters.
So I searched View.WorldToClip in built-in shaders, I found this in DistanceFieldObjectCulling.usf as follow:

OutPosition = mul(float4(WorldPosition, 1), View.WorldToClip);

I also found this in DeferredDecal.usf :

float4 CSHitPos = mul(float4(WSPosition, 1), View.WorldToClip);
OutDepth = CSHitPos.z / CSHitPos.w;

So I tried to use WorldToClip in my own shader as follow.

    float2 WorldToPixelCoordinate(float3 WorldPosition)
    {
        float4 PixelCoordinate = mul(float4(WorldPosition, 1.0f), View.WorldToClip);
        return PixelCoordinate.xy / PixelCoordinate.w;
    }

It was worked!
Though I’m not sure if it can project correctly, the results were visible at least…

4 Likes