Calculate Correct Z-Clip Distance

By: Shy - [08/11/2017] - Source Engine

Has this ever happened to you!?

If so you may be suffering from an incorrect far z clip plane! If you have used hammer for more than a day, you should be somewhat familiar with fog and far z clip. The vast majority of tutorials on fog only mention something like "the z clip should be more than your fog end", while this is technically correct no one takes into account the way source draws fog. The far z clip plane takes anything farther away from the player than its value and stops drawling it. This drastically helps performance and render times. So as long as you set your far z clip plane to a higher value than your fog you should be alright, right? In some instances yes, smaller maps where you will never really see wide open areas of land may not benefit from having a correct z clip distance. Larger open maps will require a correct distance or else you will get visual artifacts. Here are a few more examples.

Figure 1

Figure 2

In figure 1 you can see clipped off objects to the right of the screen. When you turn towards those objects (figure 2) they will be covered with fog. These are real world examples from the development of rp_hogwarts_sbs. I have created an example map to demonstrate this issue further. You can download it here.

Figure 3

Figure 4

In figure 3 you will notice huge chunks of land missing on the left and right sides of the screen. When looking 45 degrees to the right in figure 4, the missing areas will be covered with fog. In this test map the Fog End value is 8192 and the Far Z Clip Plane value is 8500. These are settings that someone would likely use. 8500 is quite a lot more than the fog end distance so most people would assume they are safe. But looking at the images, it is clear something is wrong.

So why does this happen anyway?

The Source Engine drawls fog in a very basic way. Fog is like an infinitely long perfectly straight wall placed at a distance (Fog End) from the player. The z clip plane however, is like a perfect sphere around the player. This means there will be areas that the z clip plane will cut off, where the fog is not yet visible. Here is a basic drawling to give you a better idea of the concept.

Notice how there are areas where the orange circle is not behind the blue line. Those areas will be visible to the player but the fog will not hide them. This will let the player be able to see through world, and that is not something you want.

How to Calculate the Correct Z Clip Distance

To get the correct z clip distance you only need 2 numbers: your fog end distance and your FOV. Default source FOV max is 90 degrees, however games like gmod let you bring the FOV to 100 or more. Using the Simple formula below you can calculate the exact distance the z clip should be minimum.

Okay, don't get too frightened just yet. Remember the default source fov max is 90. Half of 90 is 45 and the tangent of 45 is always 1. Meaning the simplified formula would be Sqrt(FogEnd^2 + FogEnd^2) However if you are mapping for gmod or other games, you need to take into account the full formula : Sqrt((tan(FOV/2)*FogEnd)^2 + FogEnd^2) If you have a decent calculator you can put in exactly that and get your distance. (You want to be in degrees mode.) This number will be the recommended minimum distance for your z clip in most cases. You should always round it up to a simple number (ex. 1500 from 1438).

In the test map with a fog end of 8192 the minimum z clip distance for 90 FOV would be 11585. I would round this up to 12000 but really any amount between the two will give a good result in the majority of cases. With an FOV of 100 and fog end at 8192 the z clip should be 12745. Lets look at the test map again with the correct settings now.

The entire field of view is now covered with fog and you can see no holes in the world that wouldn't be covered by fog. This is using 11585 as the z clip distance. If this were a real map, you would want to have a 3d skybox with skybox fog that matches and a horizon. This would blend the fog into the 2d skybox creating a seamless transition.

To demonstrate why the calculated value is the recommended minimum in most cases I have lowered the z clip distance by 512 to demonstrate the ill effects.

Figure 5

It may be hard to notice but the far corners of the world are being cut off when looking 12 degrees left or right. In figure 5 looking 12 degrees left will cut the right most bit of land off. This may or may not be an issue at all depending on your map which leads us into the final topic.

Ignore everything I just said.

Okay, don't ignore EVERYTHING but all of this info is only applicable to certain types maps. You may have a map that you can bring the z clip down to be right on top of your fog end distance. You may have a map that uses models with strange origin points that a calculated z clip distance may still cut off. You may be using skybox world projection to create an LOD inside of a large map, in which case you want your z clip lower than your fog distance! Play testing your map is the only way to get the true value for your map. You should calculate the distance as a starting point and work from there. Always remember:

A lower Z clip distance will give players better FPS and will give you faster compile times.