Monday, May 4, 2009

LOD Calculations

The other day a coworker was asking me some modeling questions and I started spilling my philosophy like usual. He said he aims for 5 LODs (Levels of Detail) on his models. In my opinion, in the work we do, 5 is overkill. In fact, having too many LODs can hurt performance because that's 5 checks per model to determine which LOD to use (unless its a hierarchy driven LOD system, which maybe I'll get into in another post).
The basic idea of LODs is that as a model moves further away from your observer, the model's complexity decreases so you can gain some performance.
We eventually started talking about how to model smartly with appropriate LODs. I asked him what distances he was using in relation to his polygon counts.

The most basic concept for an LOD is that you switch a High Level Of Detail (HLOD) model with a simpler, lower detail model (fewer polygons generally speaking). The switch is usually instantaneous at a specified distance from the viewer. For instance, you may have a tree which is HLOD close to the camera. At 100 feet say, the models swaps out with a simpler model. And so on as you move further away. Eventually you can't see the model at all, so it can be completely hidden. The question you are probably asking is, how do you know when to switch those models?

The answer I've come up with requires some assumptions, some math and some basic information about your scene setup. I generally aim for 3 LODs at most, but this is arguable depending on model usage. Personally, I think less is better because it makes the modeler do more with less so to speak.

Here's what you need to determine:
- What is your desired Field of View (FOV)?
- What is your screen resolution?
- What is the size of your geometry?
- How big should the model be on screen when it switches LODs?

What is your desired Field of View (FOV)? I wont get into what FOV is here, but its basically the "zoom" of your camera. A narrow field of view, or smaller number, means the view is more telescopic, like binoculars. A large value, less than 180 degrees, is like a fish eye lens. To put it in perspective (no pun intended), in the first person shooter Call of Duty 4 the default FOV is 65 degrees. Call this value F.

What is your screen resolution? We could get fancy here, but I'm only interested in the horizontal number of pixels. A HD monitor would be 1920 pixels wide. Call this value R.

What is the important size of your geometry, S? Now models are not square, so what do I mean by size? Well, that's sorta up to you. Personally, I usually take into consideration how I expect to see the model. An airplane for instance might be long and wide, but not much height. Personally, I usually just take the largest value in relation to how I expect to see the model. For an airplane in a dogfight game I would use the wingspan, but for a game where you are on the ground, I would use the height since at larger distances that's how you'll most likely see them. Call this value S.

When should the LODs switch, or to put it another way, at what pixel width do you want the model to switch out? Call this value P.

Now here is the math part, I'll use a 25 foot tall tree with a diameter of around 8 feet as an example. The important size is its width because that is the bulk of the model, so S = 8. We'll start out by determining when can we hide the model completely? Let's assume we want to see that tree model until its screen presence is only, at most, 2 pixels wide. At that distance, we feel we can remove the model from the scene completely. At what distance would that tree model be represented by, at most, 2 pixels on the screen? Here's how you determine that distance:

Distance = S * R / ( P * 2 * tan( FOV / 2 ) )

For our 25 x 8 foot tree model:

Distance = 8ft * 1920pixels / ( 2pixels * 2 * ( 65deg / 2 ) )
Distance = 6028 feet

Wow! That's really far. So do you really want that tree to be visible up to that distance? If so, what does the model need to look like to represent that tree at that distance? Not much, its only 2 pixels big at this distance after all. You'd have a hard time distinguishing a 500 polygon model from a single triangle. Ultimately that's my point. In my experience, people model their low level of detail modes with too much detail. This method allows some insight into how to model as well as determining LOD ranges.

So now what? You need to determine the amount of detail you want on the screen and at what distance it is important to show it. If your LODs aren't constructed this way, you could be wasting a lot of performance.

For the tree, I assumed that when it reached 10% of the screen width (192 pixels), I want it to be its highest LOD. That corresponds with roughly 60 ft distance. From there to 2.5% screen width (50 pixels) is the medium LOD at 250 ft. The lowest LOD goes from there to 5 pixels, turning off at 2500 feet. Its debatable if you would want to divide this 3rd LOD further, vs trade polygons between the bottom two LODs. What I mean is that as the lowest LOD fades out to the distance, its polygons are less important. Depending on how you expect to see and use this tree model, you may want another LOD, or alternatively you can make the middle LOD last longer, essentailly allowing you to drastically reduce the lowest level of detail.

That brings me to my other point. This formula can also provide a modeler with a sense of what is important. Details that a modeler may include can be justifiably removed if their size in relation to their screen presence proves them useless, especially regarding reduction for LODs.

Of course, many engines today do more than just calculate specified distances. They can balance based on performance, allowing more detail if the frame rate doesn't suffer.


No comments: