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