Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question World space position into Screen Space Overlay Canvas

Discussion in 'Shaders' started by MODev, Sep 26, 2022.

  1. MODev

    MODev

    Joined:
    Jul 30, 2013
    Posts:
    229
    Hey, I've made tool that allows to show certain effect on SS Overlay canvas and also allows use Rect Transform to select center of this effect (let's say it's just vertical line) in world space.
    So I do:
    Code (CSharp):
    1. if (abs(IN.worldPos.x - _Pos.x) / _LineWidth < 1.0) //it's just an simplified example!
    2.      return half(1,0,0,1);
    3. else
    4.     return (0,0,0,0);
    IN.worldPos is just mul(unity_ObjectToWorld, float4(v.vertex.xyz, 0)) + _SSCanvasOffset;

    SSCanvasOffset is calculated middle of SSOverlay Canvas (because even if it's overlay it still has to have position.

    Example:
    Scene view:
    upload_2022-9-26_18-40-32.png

    Game view (4k UHD):
    upload_2022-9-26_18-42-43.png
    As you can see on 4k effect is offseted and wider. Generally the smaller size is set offset is smaller and width is looking more similar to white image.

    Any idea why is that happening? I thought that what is in scene view is the same in game view (just another viewport parameters) but seems that there is difference
     

    Attached Files: