# Atmospheric fog colour material expression broken

**URL:** <https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452>\
**Category:** Asset Creation\
**Tags:** Atmospheric-Fog, unreal-engine, question\
**Created:** [June 3, 2016, 1:17am UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452 "2016-06-03T01:17:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antidamage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/antidamage/32/918692_2.png) [@Antidamage](https://forums.unrealengine.com/u/Antidamage)\
**Post date:** [June 3, 2016, 1:17am UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/1 "2016-06-03T01:17:02Z")

</div>

The atmospheric fog colour material expression is broken in 4.12. I’m using it to light my scene so it looks extremely messed up right now.

This is an example of how I use it in a material: [https://i.imgur.com/JrPmc60.png](https://i.imgur.com/JrPmc60.png)

In 4.11: [https://i.imgur.com/vaQESPO.png](https://i.imgur.com/vaQESPO.png)

Same scene, sun position and fog settings in 4.12: [https://i.imgur.com/6YUKpi6.jpg](https://i.imgur.com/6YUKpi6.jpg)

Note that in the 4.12 screenshot the sun setting is equivalent to “midday” but the sample colour is coming back black.

I’ve tried remaking the atmospheric fog and sunlight with no effect. I’ve also tried different sampling distances with no effect. It just seems to be broken and returning black.

Even the actual sky colour seems quite broken.

4.11: [https://i.imgur.com/9TUCnhM.png](https://i.imgur.com/9TUCnhM.png)  
Exact same settings 4.12: [https://i.imgur.com/1Jg7SZ0.jpg](https://i.imgur.com/1Jg7SZ0.jpg)  
After a moment the sky refreshed in the editor and looked correct again, but the sample colour is still black.

---

<div class="post-metadata">

**Author:** ![Antidamage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/antidamage/32/918692_2.png) [@Antidamage](https://forums.unrealengine.com/u/Antidamage)\
**Post date:** [June 4, 2016, 6:25am UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/2 "2016-06-04T06:25:00Z")

</div>

Also: I’ve verified the engine and tried it in a new empty project. Same result.

---

<div class="post-metadata">

**Author:** ![jubanka](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jubanka](https://forums.unrealengine.com/u/jubanka)\
**Post date:** [June 6, 2016, 6:50pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/3 "2016-06-06T18:50:40Z")

</div>

Similar strange behavior happens with the Atmospheric Fog actor, as well as the material expression you mention in 4.12. To verify, just drop an Atmospheric Fog actor in an empty level in 4.11 and 4.12: In 4.12 there is visible banding across the horizon, which can be exaggerated by cranking up the Altitude Scale property of the actor from 1.0 to 1000.

I have traced the unwanted behavior in the AtmosphereCommon.usf shader file (source: Engine/Shaders/AtmosphereCommon.usf, in the release: c:/Program Files/Unreal Engine/4.12/Engine/Shaders/AtmosphereCommon.usf) in commit b9b38fe97bc4b403c2c7071a8130cf8e2e3c874e, which adds the abs() function to 4 places to guard against negative values before taking the square root.

In my testing, it is enough to replace the following lines in function GetAtmosphericFog(), around line 556:

```
   float Mu = (ViewPosition, V) / Radius;

    float T = -Radius * Mu - sqrt(abs(Radius * Radius * (Mu * Mu - 1.0) + RadiusGround * RadiusGround));

```

with these:

```
    float Mu = (ViewPosition, V) / Radius;
    float MuClamped = (Mu < 1.f) ? 1.f : Mu;

    float T = -Radius * Mu - sqrt(abs(Radius * Radius * (MuClamped * MuClamped - 1.0) + RadiusGround * RadiusGround));

```

Making that change and triggering a shader recompile (Ctrl+Shift+.) fixes the problem.

I can prepare a pull request, if required.

_edit:_  
The inline if of MuClamped can be replaced by the more efficient max(1.f, Mu \* Mu - 1.0) directly in the T calculation, and all other abs() calls too probably (didn’t check).

---

<div class="post-metadata">

**Author:** ![Tim\_Hobson](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tim\_Hobson](https://forums.unrealengine.com/u/Tim_Hobson)\
**Post date:** [June 8, 2016, 2:00pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/4 "2016-06-08T14:00:54Z")

</div>

There appears to be a ticket in for this to be investigated already with UE-30844.

@jubanka

Thank you for the clearly written out information and changes that address this. I’ve added the notes to the ticket for this to be looked at more.

---

<div class="post-metadata">

**Author:** ![jubanka](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jubanka](https://forums.unrealengine.com/u/jubanka)\
**Post date:** [June 8, 2016, 2:41pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/5 "2016-06-08T14:41:38Z")

</div>

Thanks for your efforts, .

---

<div class="post-metadata">

**Author:** ![Antidamage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/antidamage/32/918692_2.png) [@Antidamage](https://forums.unrealengine.com/u/Antidamage)\
**Post date:** [June 10, 2016, 1:10am UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/6 "2016-06-10T01:10:51Z")

</div>

Heck, thanks for yours Jubanka!

I’ll run a custom build of the engine that addresses this for now until they can release the fix.

---

<div class="post-metadata">

**Author:** ![Antidamage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/antidamage/32/918692_2.png) [@Antidamage](https://forums.unrealengine.com/u/Antidamage)\
**Post date:** [July 26, 2016, 9:48pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/7 "2016-07-26T21:48:39Z")

</div>

> **[The Unreal Engine Issues and Bug Tracker](https://issues.unrealengine.com/issue/UE-30844)**
>
> Welcome to the Unreal Engine Issue and Bug Tracker. You can check the status of an issue and search for existing bugs. See latest bug fixes too.

I’m not seeing the issue. Can it be added please? This is a world-ending bug for those of us using global lighting systems.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0df12f74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0df12f74](https://forums.unrealengine.com/u/anonymous_user_0df12f74)\
**Post date:** [July 28, 2016, 3:20pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/8 "2016-07-28T15:20:16Z")

</div>

Hey ,

Stephen just added [UE-30844](https://issues.unrealengine.com/issue/UE-30844) to the public bug tracker and I noticed it says “Backlogged”. The AtmosphericFog actor itself is broken in 4.12, place it in your level and the issue appears immediately without changing any settings. I’m a bit dumbfounded that such an important actor would be backlogged when there is a simple 2 line fix that can be done in a usf file posted above.

I have fixed it on my end for my project, but anyone who uses my community project will have to manually edit the usf file on their end too. I know from experience with a (optional) custom DOF shader file we added that not all users are comfortable editing/overwriting engine files, and I will end up getting a lot of complaints that my project is broken.

Could this fix please be added into 4.13? I will create a PR for it if it’s really necessary, it’s only 2 lines long.

Thanks for your help as always . =)

---

<div class="post-metadata">

**Author:** ![Antidamage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/antidamage/32/918692_2.png) [@Antidamage](https://forums.unrealengine.com/u/Antidamage)\
**Post date:** [July 28, 2016, 8:40pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/9 "2016-07-28T20:40:22Z")

</div>

Do the PR, get the Engine Contributor badge. 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0df12f74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0df12f74](https://forums.unrealengine.com/u/anonymous_user_0df12f74)\
**Post date:** [July 28, 2016, 9:13pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/10 "2016-07-28T21:13:00Z")

</div>

Good thinking, can never have enough badges!

Ok I just submitted it now, here’s the link to the PR:

[https://github.com/EpicGames/UnrealEngine/pull/2640](https://github.com/EpicGames/UnrealEngine/pull/2640)

Thanks, hope it can make it in by 4.13! =)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0df12f74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0df12f74](https://forums.unrealengine.com/u/anonymous_user_0df12f74)\
**Post date:** [July 28, 2016, 9:45pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/11 "2016-07-28T21:45:27Z")

</div>

Update:

I removed the PR as I noticed the line I was editing (558) in AtmosphereCommon.usf (Release version) was modified recently on the branch. The line of code was changed from:

```
float T = -Radius * Mu - sqrt(abs(Radius * Radius * (Mu * Mu - 1.0) + RadiusGround * RadiusGround));

```

to (now line 565):

```
float T = -Radius * Mu - sqrt(Radius * Radius * (Mu * Mu - 1.0) + RadiusGround * RadiusGround);

```

I am not sure whether this is a change to fix this issue or if it is irrelevant. There have been a few changes to the file recently so I will build locally from to see if it has been fixed by an alternate method. If not I will re-open the Pull Request.

Will update asap.

---

<div class="post-metadata">

**Author:** ![jubanka](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jubanka](https://forums.unrealengine.com/u/jubanka)\
**Post date:** [July 29, 2016, 7:49am UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/12 "2016-07-29T07:49:41Z")

</div>

Judging by the commit logs and changesets, the issue seems to be fixed both in and 4.13.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0df12f74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0df12f74](https://forums.unrealengine.com/u/anonymous_user_0df12f74)\
**Post date:** [July 29, 2016, 3:32pm UTC](https://forums.unrealengine.com/t/atmospheric-fog-colour-material-expression-broken/359452/13 "2016-07-29T15:32:21Z")

</div>

Good news!

I can confirm that this issue has been fixed in the branch, absolutely no issues with it anymore.

The changes made occurred before the recent branching of 4.13 (probably should have built that one instead), but I compared the shaders on Git and they are the same so it will be in 4.13.

Looks like the change was made for the Metal API, causing a regression from 4.11. The new code includes a #IF COMPILER\_METAL check for the affected line/

Thank you guys! Sorry for my mini rant , was just a bit surprised by it. =)

EDIT: Should probably update [UE-30488](https://issues.unrealengine.com/issue/UE-30844) when you guys have a chance!

For reference here is a link to [CL #3063329](https://github.com/EpicGames/UnrealEngine/commit/016eb36528dfd25d254120cac5b34145359462ba) where the change occurred.

Thanks again.
