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 Render texture with depth for default fog issue

Discussion in 'General Graphics' started by aNormalGuy, Apr 20, 2021.

  1. aNormalGuy

    aNormalGuy

    Joined:
    Jul 22, 2013
    Posts:
    36
    So in our game you can walk through portals between areas.

    I used Sebastian League Portals video as a start:




    The problem: The default fog from the Light Settings in Unity isn't being rendered through the portal, as you can see it pop when you walk through it. The reason it pops is because the main camera that's on the player is the one that sees the fog and not the portal camera.

    My goal: Have the camera that renders what the portal sees also make sure the fog is rendered on the actual plane/render texture.

    Here's the code for the portal shader, I'm no shader expert but it seems pretty straight forward with taking the color and applying it to the material.
    Code (CSharp):
    1. Shader "Custom/Portal"
    2. {
    3.     Properties
    4.     {
    5.         _InactiveColour ("Inactive Colour", Color) = (1, 1, 1, 1)
    6.         _Color("Color", Color) = (1, 0, 0, 0.4)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         Cull Off
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.             };
    25.  
    26.             struct v2f
    27.             {
    28.                 float4 vertex : SV_POSITION;
    29.                 float4 screenPos : TEXCOORD0;
    30.             };
    31.  
    32.             sampler2D _MainTex;
    33.             float4 _InactiveColour;
    34.             int displayMask = 0; // set to 1 to display texture, otherwise will draw test colour
    35.             float4 _Color;
    36.            
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = UnityObjectToClipPos(v.vertex);
    42.                 o.screenPos = ComputeScreenPos(o.vertex);
    43.                 return o;
    44.             }
    45.  
    46.             fixed4 frag (v2f i) : SV_Target
    47.             {
    48.                 float2 uv = i.screenPos.xy / i.screenPos.w;
    49.                 fixed4 portalCol = tex2D(_MainTex, uv);
    50.                 portalCol = portalCol * _Color;
    51.                 return portalCol;
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56.     Fallback "Standard" // for shadows
    57. }
    Does anybody know how I could make this work?
     
  2. BluebsTopCat

    BluebsTopCat

    Joined:
    Aug 29, 2019
    Posts:
    1
    Sorry for bumping a dead thread, but did you ever find a solution to this? I'm having the same issue.
     
  3. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    725
    Pretty sure I got this working in the past by changing settings/enabling/disabling fog in OnPreRender for the respective cameras.