Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by $$anonymous$$ · Dec 06, 2016 at 04:34 AM · shaderstandard-assetsanti-aliasingimage-effectsupside-down

Why is Global Fog flipping image upside-down?

I installed the latest version of Unity (5.5.0) and re-imported Global Fog effect from the Standard Assets. It was working just fine before, but after the re-import it is flipping the image upside-down when I build the game (Target Platform: Windows).

The problem is related to the Global Fog shader. I saw this document https://docs.unity3d.com/Manual/SL-PlatformDifferences.html and it says that I have to standardize the the coordinates by manually flipping the image, using the following code sample:

if UNITY_UV_STARTS_AT_TOP

f (_MainTex_TexelSize.y < 0) uv.y = 1-uv.y; #endif

But the shader is already using it. Here it isthe shader script:

Shader "Hidden/GlobalFog" { Properties { _MainTex ("Base (RGB)", 2D) = "black" {} }

CGINCLUDE

 #include "UnityCG.cginc"

 uniform sampler2D _MainTex;
 uniform sampler2D_float _CameraDepthTexture;
 
 // x = fog height
 // y = FdotC (CameraY-FogHeight)
 // z = k (FdotC > 0.0)
 // w = a/2
 uniform float4 _HeightParams;
 
 // x = start distance
 uniform float4 _DistanceParams;
 
 int4 _SceneFogMode; // x = fog mode, y = use radial flag
 float4 _SceneFogParams;
 #ifndef UNITY_APPLY_FOG
 half4 unity_FogColor;
 half4 unity_FogDensity;
 #endif    

 uniform float4 _MainTex_TexelSize;
 
 // for fast world space reconstruction
 uniform float4x4 _FrustumCornersWS;
 uniform float4 _CameraWS;

 struct appdata_fog
 {
     float4 vertex : POSITION;
     half2 texcoord : TEXCOORD0;
 };

 struct v2f {
     float4 pos : SV_POSITION;
     float2 uv : TEXCOORD0;
     float2 uv_depth : TEXCOORD1;
     float4 interpolatedRay : TEXCOORD2;
 };
 
 v2f vert (appdata_fog v)
 {
     v2f o;
     v.vertex.z = 0.1;
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
     o.uv = v.texcoord.xy;
     o.uv_depth = v.texcoord.xy;
     
     #if UNITY_UV_STARTS_AT_TOP
     if (_MainTex_TexelSize.y < 0)
         o.uv.y = 1-o.uv.y;
     #endif                
     
     int frustumIndex = v.texcoord.x + (2 * o.uv.y);
     o.interpolatedRay = _FrustumCornersWS[frustumIndex];
     o.interpolatedRay.w = frustumIndex;
     
     return o;
 }
 
 // Applies one of standard fog formulas, given fog coordinate (i.e. distance)
 half ComputeFogFactor (float coord)
 {
     float fogFac = 0.0;
     if (_SceneFogMode.x == 1) // linear
     {
         // factor = (end-z)/(end-start) = z * (-1/(end-start)) + (end/(end-start))
         fogFac = coord * _SceneFogParams.z + _SceneFogParams.w;
     }
     if (_SceneFogMode.x == 2) // exp
     {
         // factor = exp(-density*z)
         fogFac = _SceneFogParams.y * coord; fogFac = exp2(-fogFac);
     }
     if (_SceneFogMode.x == 3) // exp2
     {
         // factor = exp(-(density*z)^2)
         fogFac = _SceneFogParams.x * coord; fogFac = exp2(-fogFac*fogFac);
     }
     return saturate(fogFac);
 }

 // Distance-based fog
 float ComputeDistance (float3 camDir, float zdepth)
 {
     float dist; 
     if (_SceneFogMode.y == 1)
         dist = length(camDir);
     else
         dist = zdepth * _ProjectionParams.z;
     // Built-in fog starts at near plane, so match that by
     // subtracting the near value. Not a perfect approximation
     // if near plane is very large, but good enough.
     dist -= _ProjectionParams.y;
     return dist;
 }

 // Linear half-space fog, from https://www.terathon.com/lengyel/Lengyel-UnifiedFog.pdf
 float ComputeHalfSpace (float3 wsDir)
 {
     float3 wpos = _CameraWS + wsDir;
     float FH = _HeightParams.x;
     float3 C = _CameraWS;
     float3 V = wsDir;
     float3 P = wpos;
     float3 aV = _HeightParams.w * V;
     float FdotC = _HeightParams.y;
     float k = _HeightParams.z;
     float FdotP = P.y-FH;
     float FdotV = wsDir.y;
     float c1 = k * (FdotP + FdotC);
     float c2 = (1-2*k) * FdotP;
     float g = min(c2, 0.0);
     g = -length(aV) * (c1 - g * g / abs(FdotV+1.0e-5f));
     return g;
 }

 half4 ComputeFog (v2f i, bool distance, bool height) : SV_Target
 {
     half4 sceneColor = tex2D(_MainTex, UnityStereoTransformScreenSpaceTex(i.uv));
     
     // Reconstruct world space position & direction
     // towards this screen pixel.
     float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(i.uv_depth));
     float dpth = Linear01Depth(rawDepth);
     float4 wsDir = dpth * i.interpolatedRay;
     float4 wsPos = _CameraWS + wsDir;

     // Compute fog distance
     float g = _DistanceParams.x;
     if (distance)
         g += ComputeDistance (wsDir, dpth);
     if (height)
         g += ComputeHalfSpace (wsDir);

     // Compute fog amount
     half fogFac = ComputeFogFactor (max(0.0,g));
     // Do not fog skybox
     if (dpth == _DistanceParams.y)
         fogFac = 1.0;
     //return fogFac; // for debugging
     
     // Lerp between fog color & original scene color
     // by fog amount
     return lerp (unity_FogColor, sceneColor, fogFac);
 }

ENDCG

SubShader { ZTest Always Cull Off ZWrite Off Fog { Mode Off }

 // 0: distance + height
 Pass
 {
     CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
     half4 frag (v2f i) : SV_Target { return ComputeFog (i, true, true); }
     ENDCG
 }
 // 1: distance
 Pass
 {
     CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
     half4 frag (v2f i) : SV_Target { return ComputeFog (i, true, false); }
     ENDCG
 }
 // 2: height
 Pass
 {
     CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
     half4 frag (v2f i) : SV_Target { return ComputeFog (i, false, true); }
     ENDCG
 }

}

Fallback off

}

What should I do to fix this issue? Please Help! Thank You!

Comment
Add comment · Show 1
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 kyzy_4399 · Feb 17, 2017 at 10:02 PM 0
Share

I got the same problem, still wonder how to fix it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kornica3d · Feb 26, 2017 at 06:28 PM

Hey there. Its a known bug. There is a thread where they have a solution - you just have to replace your GlobalFog file with earlier one, or you can update your unity to latest version...

https://forum.unity3d.com/threads/unity-5-4-global-fog-now-flips-transparent-objects-when-using-single-pass.429366/#post-2807256

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
avatar image
0

Answer by Eimantas1 · Jun 13, 2017 at 07:00 PM

Although the fog in 5.4 is very crappy and does not even affect my grass :/

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

7 People are following this question.

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

Related Questions

How to create an imageEffect that blurs each pixel differently? 1 Answer

Motion Blur effect on Windows with multiple cameras 1 Answer

Make objects monochrome, and when I come closer, they get color 1 Answer

screen flipped upside down [image effect] 0 Answers

Are multiple Unity Pro "Image Effects" auto-combined into a single (or as few as possible) quad shaders? 1 Answer


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