Here is a basic sort of fake volume fog, you can arrange the fog to disappear at altitudes. Code (csharp): Shader "Fog-Altitude" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _FogNear ("Fog Near", range (0,100)) = 5 _FogFar ("Fog Far", range (0,100)) = 10 _FogAltScale ("Fog Alt. Scale", range (0,100)) = 10 _FogThinning ("Fog Thinning", range (0,100)) = 100 _FogColor ("Fog Color", Color) = (0.5,0.5,0.5,1) } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert vertex:vert float4 _Color; sampler2D _MainTex; float _FogNear, _FogFar, _FogAltScale, _FogThinning; float4 _FogColor; struct Input { float2 uv_MainTex; float3 viewDir; float4 pos; }; void vert (inout appdata_full v, out Input o) { //o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.pos = mul(_Object2World, v.vertex); } void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D (_MainTex, IN.uv_MainTex); float d = length(IN.viewDir); float l = saturate((d - _FogNear) / (_FogFar - _FogNear) / clamp(IN.pos.y / _FogAltScale + 1, 1, _FogThinning)); //o.Albedo = c.rgb * _Color.rgb; o.Albedo = lerp(c.rgb * _Color.rgb, _FogColor, l); o.Alpha = c.a * _Color.a; } ENDCG } FallBack "Diffuse" } EDIT: Anything below y 0 is considered fully fogged, above is calculated according to FogAltitudeScale and fogthinning values All the input values about fog are real world values (fog near, fog far, fog altitude..etc.)
The shaders are for learning purposes at the moment (my own self learning purposes ) i feel more comfortable with these now,and i think i will make usable sets of these shaders then i might add those.
I've considered doing this one myself once but wasn't ever sure how to deal with transparency within a scene. How does this fare with transparent objects?
I dont think i will ever need alpha for such an effect, but anyways here it is: Code (csharp): Shader "Fog-Altitude" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _FogNear ("Fog Near", range (0,50)) = 5 _FogFar ("Fog Far", range (0,50)) = 10 _FogAltScale ("Fog Alt. Scale", range (0,10)) = 10 _FogThinning ("Fog Thinning", range (0,100)) = 100 _FogColor ("Fog Color", Color) = (0.5,0.5,0.5,1) } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma vertex vert #pragma surface surf Lambert alpha float4 _Color; sampler2D _MainTex; float _FogNear, _FogFar, _FogAltScale, _FogThinning; float4 _FogColor; struct Input { float2 uv_MainTex; float3 viewDir; float4 pos; }; void vert (inout appdata_full v, out Input o) { //o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.pos = mul(_Object2World, v.vertex); } void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D (_MainTex, IN.uv_MainTex); float d = length(IN.viewDir); float l = saturate((d - _FogNear) / (_FogFar - _FogNear) / clamp(IN.pos.y / _FogAltScale + 1, 1, _FogThinning)); //o.Albedo = c.rgb * _Color.rgb; o.Albedo = lerp(c.rgb * _Color.rgb, _FogColor, l); o.Alpha = lerp(c.a * _Color.a, _FogColor.a, l); } ENDCG } FallBack "Diffuse" }
Wow, that's great!! Just what I was thinking about. But I thought about if it's possible to modify Unity fog. As far as I understand you've made a shader for individual objects? What's the best way to use this? Can it automatically fog the whole scene? Sorry if I ask stupid questions I'm new to all this. Thanx
It is per object, if you want to use it in whole scene, you will need to change any shaders currently in scene and add the fog part to it. Or you may ask a feature request to unity to include a smiliar method for vertical fog.