Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Project a render texure onto a 2d plane

Discussion in 'Shaders' started by FathomMaster, May 6, 2021.

  1. FathomMaster

    FathomMaster

    Joined:
    Jun 20, 2018
    Posts:
    6
    Hi, I am fairly new to Shaders, so bear with me please.

    In my scene I have 2d objects. I want to take an existing render texture and project it directly onto those planes so that the render texture only shows where those objects are.

    So far I have failed to get the coordinates for the objects properly. I have access to the render texture, but it shows up squished and in the completely wrong space. I would show you code, but it's such a mess due to me not knowing what I'm doing that it would be better to start from scratch.

    My searches have not turned up anything that would help me (that I can comprehend at this point).

    Thanks
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    You want screen space UVs.
    Code (csharp):
    1. struct v2f {
    2.     // other stuff, like the pos and uv
    3.  
    4.     // use a number for the TEXCOORD# that isn't already used
    5.     float4 projPos : TEXCOORD4;
    6. };
    7.  
    8. // in the vertex function
    9. o.projPos = ComputeScreenPos (o.vertex);
    10.  
    11. // in the fragment shader
    12. float2 screenUV = i.projPos.xy / i.projPos.w;
    13. fixed4 myColor = tex2D(_MyRenderTexture, screenUV);
     
    Westland likes this.
  3. FathomMaster

    FathomMaster

    Joined:
    Jun 20, 2018
    Posts:
    6
    Thank you so much! I knew it something small eluding me. It works beautifully now.