How to convert World Position to Screen UV Coordinate?

Hi, everyone, I met this issue when coding my own shader.
I got a World Position (x,y,z), I wanna convert it into Screen View UV coordinate (x,y). I can’t find a matrix or function of current view can do this.

1 Like

Have you tried Project World to Screen?

Also check this tutorial.

1 Like

I’m sorry that I was not describe this issue clearly. I wanna project world position to screen coordinate in .usf file not in a blueprint.

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

float2 UV = ScreenPos * View.ScreenPositionScaleBias.xy + View.ScreenPositionScaleBias.wz;
float2 PixelCoordinate = UV / View.BufferSizeAndInvSize.zw - 0.5;

Must add codes above to convert to Pixel Coordinate.

2 Likes