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

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.

1 Like

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.

1 Like

Thanks for your suggestion, much appreciated. I will take a look at doing it with post processing

There are events fired when a camera is about to render.
RenderPipelineManager.beginCameraRendering is an action defined as public static event Action<ScriptableRenderContext, Camera> beginCameraRendering; under namespace UnityEngine.Rendering
Pair it with endCameraRendering event to revert certain actions.
RenderSettings.fog = false; inside the event and that camera will not render fog.

2 Likes

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?

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.

1 Like

Thanks for your help. I’ll give it a go and report back :slight_smile:

Did you ever get this to work? My fog turns off for all Cameras

1 Like

Sorry I didn’t get around to try it. I’ve been wrestling with the Input System ever since.

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.

1 Like

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/com.unity.render-pipelines.universal@12.1/manual/using-begincamerarendering.htmlI may have it working, I’ll report back tomorrow.

OK I have this working.
This below picture shows the result.
8945163--1227414--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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class URPCallbackExample : MonoBehaviour
{
    // Unity calls this method automatically when it enables this component
    private void OnEnable()
    {
        // Add WriteLogMessage as a delegate of the RenderPipelineManager.beginCameraRendering event
        RenderPipelineManager.beginCameraRendering += BeginRender;
        RenderPipelineManager.endCameraRendering += EndRender;
    }

    // Unity calls this method automatically when it disables this component
    private void OnDisable()
    {
        // Remove WriteLogMessage as a delegate of the  RenderPipelineManager.beginCameraRendering event
        RenderPipelineManager.beginCameraRendering -= BeginRender;
        RenderPipelineManager.endCameraRendering -= EndRender;
    }

    // When this method is a delegate of RenderPipeline.beginCameraRendering event, Unity calls this method every time it raises the beginCameraRendering event
    void BeginRender(ScriptableRenderContext context, Camera camera)
    {
        // Write text to the console
        Debug.Log($"Beginning rendering the camera: {camera.name}");

        if(camera.name == "Main Camera No Fog")
        {
            Debug.Log("Turn fog off");
            RenderSettings.fog = false;
        }
         
    }

    void EndRender(ScriptableRenderContext context, Camera camera)
    {
        Debug.Log($"Ending rendering the camera: {camera.name}");
        if (camera.name == "Main Camera No Fog")
        {
            Debug.Log("Turn fog on");
            RenderSettings.fog = true;
        }
    }
}
7 Likes

Thank you for this! works perfectly