- Home /
Implement fog settings independent of the Scene Render settings.
I have a split screen ship game where each player has there own camera. I know how to change the render settings through script. However, I want to implement a fog for each camera that is independent fromt he scene render settings. For example, if one boat goes underwater that boat's camera displays a thick underwater fog, while the boat above the water still has the thin above water fog. Is this possible? What is the best way to implement?
Thanks in advance.
Answer by Tetrad · Jun 09, 2010 at 07:55 PM
float previousFogDensity;
void OnPreRender() { previousFogDensity = RenderSettings.fogDensity; if( / is this camera below water / ) { RenderSettings.fogDensity = / whatevever your underwater value is /; } }
void OnPostRender() { RenderSettings.fogDensity = previousFogDensity; }
This script assumes that your normal fog density is what you want for above water fog to be.
How you determine whether or not your camera is above water is very dependent on what your game does. The easiest thing to do is to probably have a script hookup to your player object (or equivalent) and just query that to see if it is or isn't above water. Or if you set it up that your water is always below zero on the Y axis you can just check the camera's position.
This was basically what I used to finally get it to work. All I did was place this line above the if statement.
previousFogDensity = RenderSettings.fogDensity;
Now I have both my separate fog settings displaying for each camera. Thanks!
Answer by Eric5h5 · Jun 09, 2010 at 05:09 PM
One way is to use OnPreRender / OnPostRender for the cameras, like this.
Thanks for the response. I found that script before. But I would really like to keep some fog above water rather than none at all. It looks much better. Any other ideas?
It's the same basic script, but ins$$anonymous$$d of changing rendersettings.fog to true or false, you're setting rendersettings.fogdensity on a per camera script.
Answer by mlkielb 1 · Jun 09, 2010 at 07:45 PM
I get thick fog underwater, but no fog above water. Scene render settings are set to .003 fog. My foglayer script sets the scene render setting to .1 density when testing. However, even when the foglayer isn't on the fog density stays at .1. This is the script of my uwfoglayer:
If the uwfoglayer is enabled the camera should revet back to scene render settings right? Any suggestions on how to get the fog to work above the water? I tried adding a second foglayer to the camera that sets the fogDensity to .003 when above water. But the fog does not appear at all.
private var revertFogState = false;</p> <p>function OnPreRender () { revertFogState = RenderSettings.fog; RenderSettings.fog = enabled; }</p> <p>function OnPostRender () { RenderSettings.fog = revertFogState; RenderSettings.fogDensity = .1; }</p> <p>@script AddComponentMenu ("Rendering/Fog Layer") @script RequireComponent (Camera)
Answer by mlkielb 1 · Jun 09, 2010 at 08:40 PM
I tried this. But it has a fatal error and freezes unity everytime I try to run th app.
private var previousFogDensity;
function OnPreRender(){
if (transform.Find("boat_model/uboat/parascope/parascopeview").position.y < 0){
previousFogDensity = RenderSettings.fogDensity;
RenderSettings.fogDensity = .1;
}
}
function OnPostRender(){
RenderSettings.fogDensity = previousFogDensity; }
@script AddComponentMenu ("Rendering/Fog Layer") @script RequireComponent (Camera)
Try using a different method to deter$$anonymous$$e if you're above or below water. Using transform.Find is really slow anyway.
Your answer
Follow this Question
Related Questions
Adjustable Fog In Settings Scene 0 Answers
Skybox Pixelation problem 6 Answers
Unity - How to hide the Far Clipping Planes of camera? 1 Answer
Always Render/Draw FPS Weapon 1 Answer
How to change camera visibility after certain distance? 0 Answers