- Home /
Global fog problem
Hi. I've been trying tomake my game visually appealing lately, and an effect we believed would lift the graphics and depth perception a lot was fog. We liked the default fog under render settings, but found it did nothing to the skybox. Hence, we decided to try out the Global Fog script. The result was... nothing short of a disaster. Does anyone know what's going on?
Answer by aldonaletto · Jul 09, 2013 at 03:48 PM
I don't know what's wrong with this Global Fog script (could you provide a link to it?), but found myself a solution to extend the fog to the skybox: I modified the original Skybox shader so that the fog vanishes towards the sky (fog intensity inversely proportional to the sine of the pixel's elevation angle).
You can save this shader in Assets or a subfolder, then change your current skybox shader to "Skybox With Fog":
Shader "RenderFX/Skybox With Fog" {
Properties {
_Fog ("Fog Intensity", Range(0.33,5.0)) = 1.0
_FrontTex ("Front (+Z)", 2D) = "white" {}
_BackTex ("Back (-Z)", 2D) = "white" {}
_LeftTex ("Left (+X)", 2D) = "white" {}
_RightTex ("Right (-X)", 2D) = "white" {}
_UpTex ("Up (+Y)", 2D) = "white" {}
_DownTex ("Down (-Y)", 2D) = "white" {}
}
SubShader {
Tags { "Queue"="Background" "RenderType"="Background" }
Cull Off ZWrite Off Fog { Mode Off }
CGINCLUDE
#include "UnityCG.cginc"
half4 unity_FogColor; // fog color defined by RenderSettings
fixed _Fog;
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 dir: TEXCOORD1;
};
v2f vert (appdata_t v){
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
o.dir = mul(_Object2World, v.vertex);
return o;
}
fixed4 skybox_frag (v2f i, sampler2D sky){
fixed4 tex = tex2D(sky, i.texcoord); // get skybox texel
half fog = saturate(1 - normalize(i.dir).y / _Fog); // fog vanishes upwards
tex = lerp(tex, unity_FogColor, fog); // blend skybox with fog
return tex;
}
ENDCG
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _FrontTex;
fixed4 frag (v2f i) : COLOR { return skybox_frag(i, _FrontTex); }
ENDCG
}
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _BackTex;
fixed4 frag (v2f i) : COLOR { return skybox_frag(i, _BackTex); }
ENDCG
}
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _LeftTex;
fixed4 frag (v2f i) : COLOR { return skybox_frag(i, _LeftTex); }
ENDCG
}
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _RightTex;
fixed4 frag (v2f i) : COLOR { return skybox_frag(i, _RightTex); }
ENDCG
}
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _UpTex;
fixed4 frag (v2f i) : COLOR { return skybox_frag(i, _UpTex); }
ENDCG
}
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _DownTex;
fixed4 frag (v2f i) : COLOR { return skybox_frag(i, _DownTex); }
ENDCG
}
}
}
Well, that's just the standard Global Fog script that's included with Unity, if that's any help for you. Thanks for the code, anyway, it'll be brilliantly helpful.
Hi.. thanks for answer. but it is not working in mobile device(android). is there anything you can change to make it work in mobile device?
Answer by X05E · May 30, 2015 at 04:16 PM
At least in Unity 5, you don't need any script for fog (and if you can do it without script, better go for it)
It seems like skymaps are not rendering fog so, I created a dome/sphere and it's working for me. Try to apply your sky to a model (dome or box) and see what happens. BTW, remember to reverse normals, otherwise you will see through the sky polygons.
Answer by SmokinBoots · Nov 07, 2016 at 08:07 AM
I'm having a similar problem. My global fog is clouding out objects as planned, but it is not affecting the horizon. I intend to creat a "hellbender"-style environment with a planar cloud deck, 3d terrain and moderately thick fog as seen here: