Thursday, August 6, 2015

Depth Based Fog, For When Linear Doesn't Cut It

Phew! Managed a passable fog. After figuring out that subtracting the viewpoint distance from the start of the fog instead of the beginning was the cause of my previous issue (and thanks to some HLSL debugging that I discovered),  a problem still existed: The fog seemed to rely too much on the placement of the object itself rather than the per-pixel fog I was looking for. DX9 and DX10 seemed to have that type of thing built in, but DX11 required it to be built from scratch and NOTHING on the Internet had it. It was quite disappointing and I was stuck with calculating vertex values with some very odd results where the floor wasn't nearly as foggy as the wall I was right next to.

However, I managed a bit of a small step in the right direction with depth values. By taking the depth value and forming it to a linear set of values between 0.0f and 1.0f (transform to NDC coordinates, then work backwards according to the (1/z - 1/near / 1/far - 1/near) for depth calculation, I finally got the values I was looking for (with a bit of clamping). By multiplying said fog by a set constant value, the objects would fade into grey based on their depth values in the scene. This worked well, even with the vertices that weren't well spread out on the plane. I'm thinking with a floor or terrain with more vertices, such a problem won't be present. The skybox is still there, but is also interpolated with the foggy color to seem more natural compared to the rest of the objects.


Before I start working on something snow related, I may want to replace the gigantic wall with something more realistic like a building. That will be the next step.

No comments:

Post a Comment