Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Hogge · Jul 09, 2013 at 02:59 PM · shaderskyboxfog

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? alt text

volumetric.png (168.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

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).

alt text

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
     }
 }    
 
 }



skyboxfog.png (140.9 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hogge · Jul 09, 2013 at 07:36 PM 0
Share

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.

avatar image Patel-Sagar · Aug 19, 2014 at 01:10 PM 0
Share

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?

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KarlKarl2000 · May 23, 2017 at 03:48 PM 0
Share

@X05E This worked for what I needed. Thanks!

avatar image
0

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: hellbender screenshot

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I prevent GlobalFog from obscuring particle shaders? 1 Answer

Represent Land 3D Area End 0 Answers

Setting fog color on alpha blended to be fog color 1 Answer

How i can create vertical Fog by Shader Graph 0 Answers

How do I get objects to render against the skybox? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges