Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Resolved Projecting clipped _CameraOpaqueTexture to plane.

Discussion in 'Shaders' started by Gaminart, Mar 11, 2021.

  1. Gaminart

    Gaminart

    Joined:
    Dec 9, 2018
    Posts:
    33
    I've been messing with projecting stuff based on uvs, screen position, etc.

    This is what I want: A clipped _CameraOpaqueTexture, projected on a plane.

    The image below is me simply projecting the plane uvs cause I'm scared to do anything else.
    I've looked around for solutions but have found none.

    I'd love to understand how to achieve the clipped projection of the _CameraOpaqueTexture on the plane!

    CODE BELOW

    upload_2021-3-11_11-31-34.png

    Code (CSharp):
    1. Shader "Unlit/Water"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (0,0,0,0)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    11.  
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.  
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             sampler2D _MainTex;
    24.             sampler2D _CameraDepthTexture;
    25.             sampler2D _CameraOpaqueTexture;
    26.             float4 _MainTex_ST;
    27.             float4 _Color;
    28.  
    29.  
    30.  
    31.             struct appdata
    32.             {
    33.                 float4 vertex : POSITION;
    34.                 float2 uv : TEXCOORD0;
    35.             };
    36.  
    37.             struct v2f
    38.             {
    39.                 float4 vertex : SV_POSITION;
    40.                 float2 uv : TEXCOORD0;
    41.                 float4 screenPos : TEXCOORD1;
    42.             };
    43.  
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.screenPos = ComputeScreenPos(o.vertex);
    50.                 return o;
    51.             }
    52.  
    53.             float4 frag (v2f i) : SV_Target
    54.             {
    55.                 return tex2D(_CameraOpaqueTexture, i.uv);
    56.             }
    57.  
    58.             ENDCG
    59.         }
    60.     }
    61. }
    62.  
     
    Last edited: Mar 11, 2021
  2. Gaminart

    Gaminart

    Joined:
    Dec 9, 2018
    Posts:
    33
    https://www.ronja-tutorials.com/post/039-screenspace-texture/

    This article might be the solution!

    // Normalized coordinates of pixel positions within our screen (direction is world space).
    // w-component is the worldspace depth of the pixel being drawn .
    float2 uv = i.screenPos.xy / i.screenPos.w;
    return tex2D(_CameraOpaqueTexture, uv);

    The given code projects the texture based on screen position and it fits perfectly.
     
    Last edited: Mar 11, 2021
unityunity