Model Caching Best Practices
This guide goes through when and how to use model caching for entities and non-entity models to improve the performance of your game.
When to use?
The model caching is useful in the case where you have a high amount of entities (especially units) where only a small fraction of them is visible at (or most) times to the main camera.
Every frame, Cameras in Unity perform culling operations that exclude Renderers that do not fall within the Camera's view frustum from being rendered.
The benefits that model caching in the RTS Engine bring is elevating the need for Unity to make calculations for the entity models that are usually composed of multiple child objects and in the case of large amount of unit entities, model caching allows to only enable animators on units that are visible by the main camera.
The rule here should be is to disable model caching in the Model Cache Manager by default. And once performance becomes an issues and optimizations are required, you can enable model caching and see if that brings any benefits.
Model Caching with Grid Search
When model caching is enabled, you have two options:
- Have the Use Grid Search field disabled in the Model Cache Manager inspector making each entity determine whether it is visible by the main camera or not for it to be rendered or not every time the main camera position is updated.
- Have the Use Grid Search field enabled in the Model Cache Manager inspector making the Grid Search Handler do the computations to determine what entities are visible by the main camera. If a grid search cell is visible by the main camera then all entities that belong to that cell are rendered and vice-versa. Using the grid search for model caching is more efficient as less calculations are required every time the main camera changes its position.
In most cases, using the grid search to handle model caching is much more efficient but it is recommended to test both options to determine what brings more performance benefits in your case.
In all cases, you can select the game object that holds the Grid Search Handler component and in the scene view, you'd see the cells that are currently visible by the camera and the ones that are not. In case model caching is enabled via grid search, only the entities that are in the visible search cells are rendered.
Model Caching FAQ
1. Why are some entities on the edge of the screen keep disappearing/appearing when moving the main camera?
When enabled and you have the issue of the entities disappearing/appearing on the edges of the screen, you should increase the margin at which the camera visibility boundaries operate in relation to the base terrain. You can find the margin field in the Terrain Manager component inspector. Increase it until it guarantees that entity models that are just outside the main camera visibility are also rendered so that as soon as you move closer or away, they wouldn't 'magically' appear or disappear.
Pick the margin applied to the top right and top left corners of the camera view (Upper Margin) and the one applied to the bottom left and right corners (Bottom Margin). In the case where the main camera is not directly looking down at the map, different margin values for the upper and bottom boundaries would need to be set. For example, when the main camera has a 45 degree angle on the X axis rotation, a much higher bottom margin is required in case model caching is enabled in order to avoid the issue where entity model objects appear/disappear quickly near the bottom edges of the camera view.