- Home /
Is it possible to disable fog on a per-camera basis?
In a game I'm working on we have our main camera in the world which we would like to have fog enabled. We also have a handful of other cameras that are in the world that render other things in the same scene to either a render texture or directly on top of the world (minimaps, etc). These cameras are all enabled at the same time for the final scene composition.
Is it possible to make it so that some cameras (or rather all the cameras other than the main camera) render the world without the fog enabled?
Edit:
I slightly modified the script that Eric posted a link to to more suit my purposes. This class will let you specifically enable or disable fog on a specific camera instead of just enabling it.
using UnityEngine;
[RequireComponent( typeof( Camera ) )] public class CameraFogSetting : MonoBehaviour { [SerializeField] bool enableFog = true;
bool previousFogState;
void OnPreRender()
{
previousFogState = RenderSettings.fog;
RenderSettings.fog = enableFog;
}
void OnPostRender()
{
RenderSettings.fog = previousFogState;
}
}
Answer by Eric5h5 · Feb 04, 2010 at 11:47 PM
Yes, it's possible; use this script.
dead link. i have 25 scenes i need to enable fog 1 by 1. How can i enable it once in all scenes ?
The above link is broken. this answer should be voted down. I would but I don't have enough reputation points I guess
The answer is from 2010. A lot has changed since then (like old domains) so does that really warrant a downvote? Did you scroll down to the other answer (2014) with the updated link?
Answer by Eric5h5 · Sep 22, 2014 at 05:35 PM
I recently tried this technique on Unity 2019.1.0b9 but it didn't work. The fog keeps in. It worked on Unity 2018.3.10
That script is in JavaScript which is no longer supported.
Use this one:
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class NoFogOnCamera : $$anonymous$$onoBehaviour
{
public bool AllowFog = false;
private bool FogOn;
private void OnPreRender()
{
FogOn = RenderSettings.fog;
RenderSettings.fog = AllowFog;
}
private void OnPostRender()
{
RenderSettings.fog = FogOn;
}
}
Add to a Camera. Set AllowFog in the Inspector to Turn on Fog for this camera only. Otherwise false turns it off for this camera only.
Your answer
Follow this Question
Related Questions
I'm trying to get a picture in game and display it on a plane 2 Answers
dynamic hole in layer / texture / camera 0 Answers
Image Effect Scripts on a separate object from Camera 1 Answer
Why does camera not register color until objects are close up? 0 Answers
Diagonal lines across texture when viewed from afar 1 Answer