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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How do I turn off Fog on a specific camera using URP?

Discussion in 'Universal Render Pipeline' started by Stephen1701, Dec 13, 2022.

  1. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    I've got 2 cameras set up in my scene, and one of them renders to a render texture.
    This camera is rendering fog, which I don't want it to do.
    I've seen solutions which mention Pre and Post render, but they don't work with URP.
    How can I turn off fog for just this one camera?

    Thanks in advance.
     
  2. o1o101

    o1o101

    Joined:
    Jan 19, 2014
    Posts:
    637
    You probably want to disable built in fog all together and to do fog at the post process level instead. This way you could easily disable it on one of the cameras.
    I'd suggest checking out the store for some fog solutions which are done in post, there are lots.

    Built in fog is at the shader level, so one other option would be to use shader variants with fog stripped from them on the second camera, likely with a URP render feature. This shouldn't be too hard but the post process route is easier.
     
    Stephen1701 likes this.
  3. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Thanks for your suggestion, much appreciated. I will take a look at doing it with post processing
     
  4. revolute

    revolute

    Joined:
    Jul 28, 2014
    Posts:
    50
    There are events fired when a camera is about to render.
    Code (CSharp):
    1. RenderPipelineManager.beginCameraRendering
    is an action defined as
    Code (CSharp):
    1. public static event Action<ScriptableRenderContext, Camera> beginCameraRendering;
    under namespace
    Code (CSharp):
    1. UnityEngine.Rendering
    Pair it with endCameraRendering event to revert certain actions.
    Code (CSharp):
    1. RenderSettings.fog = false;
    inside the event and that camera will not render fog.
     
    DannyBoyOnline and Stephen1701 like this.
  5. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Thank you for your reply. Will this work with URP? I think I read somewhere that camera events don't work on URP? Or did I get that wrong?
     
  6. revolute

    revolute

    Joined:
    Jul 28, 2014
    Posts:
    50
    This is how we are doing it in our game. I have used this for URP since 7.2 and its still working in 12.7.
     
    Stephen1701 likes this.
  7. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Thanks for your help. I'll give it a go and report back :)
     
  8. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    33
    Did you ever get this to work? My fog turns off for all Cameras
     
    Stephen1701 likes this.
  9. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Sorry I didn't get around to try it. I've been wrestling with the Input System ever since.
     
  10. revolute

    revolute

    Joined:
    Jul 28, 2014
    Posts:
    50
    I feel like you haven't done a check for which camera to turn off. There is a camera parameter which you can compare to check if it is the camera that you wish to control.
     
    Stephen1701 likes this.
  11. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    I've had a look but don't really understand how to use it.
    I'
    I'm working on a different way around it using the tutorial at https://docs.unity3d.com/Packages/c...@12.1/manual/using-begincamerarendering.htmlI may have it working, I'll report back tomorrow.
     
    Last edited: Apr 12, 2023
  12. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    OK I have this working.
    This below picture shows the result.
    upload_2023-4-13_7-54-40.png
    In my scene I have
    Red Fog
    1 cube
    2 cameras
    1 render texture.
    Both cameras are looking at the cube, and camera 2 is outputting to a render texture. Camera 2 is named "Main Camera No Fog"

    NOTE: The fog being turned off is not reflected in the scene view or camera preview, only in the game view

    The script below is what I've attached to the cube in the scene, although it could be attached to any objects.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. public class URPCallbackExample : MonoBehaviour
    7. {
    8.     // Unity calls this method automatically when it enables this component
    9.     private void OnEnable()
    10.     {
    11.         // Add WriteLogMessage as a delegate of the RenderPipelineManager.beginCameraRendering event
    12.         RenderPipelineManager.beginCameraRendering += BeginRender;
    13.         RenderPipelineManager.endCameraRendering += EndRender;
    14.     }
    15.  
    16.     // Unity calls this method automatically when it disables this component
    17.     private void OnDisable()
    18.     {
    19.         // Remove WriteLogMessage as a delegate of the  RenderPipelineManager.beginCameraRendering event
    20.         RenderPipelineManager.beginCameraRendering -= BeginRender;
    21.         RenderPipelineManager.endCameraRendering -= EndRender;
    22.     }
    23.  
    24.     // When this method is a delegate of RenderPipeline.beginCameraRendering event, Unity calls this method every time it raises the beginCameraRendering event
    25.     void BeginRender(ScriptableRenderContext context, Camera camera)
    26.     {
    27.         // Write text to the console
    28.         Debug.Log($"Beginning rendering the camera: {camera.name}");
    29.  
    30.         if(camera.name == "Main Camera No Fog")
    31.         {
    32.             Debug.Log("Turn fog off");
    33.             RenderSettings.fog = false;
    34.         }
    35.          
    36.     }
    37.  
    38.     void EndRender(ScriptableRenderContext context, Camera camera)
    39.     {
    40.         Debug.Log($"Ending rendering the camera: {camera.name}");
    41.         if (camera.name == "Main Camera No Fog")
    42.         {
    43.             Debug.Log("Turn fog on");
    44.             RenderSettings.fog = true;
    45.         }
    46.     }
    47. }
     
    Last edited: Apr 13, 2023
    MontanaAnton, MostHated and comocc like this.