{{tag>graphic occlusion optimization}}
[[:start|Start Page]] >> [[:gamedev|Game Development with the Drag[en]gine]] >> **Occlusion Meshes**
Occlusion culling is an important feature of a game engine. Although graphic cards become faster and more powerful they still struggle with complex scene filled with many objects. One possible solution is to reduce the complexity of object at a distance using **[[gamedev:lod|LOD Systems]]**. But even with the best LODing the number of triangles rendered in a scene explodes with the scene complexity. A good example is a city scene where lots of buildings and props are located one behind the other. To render such a scene effective **invisible objects have to be skipped**. Calculating though which objects are invisible is a difficult and time consuming task. The Drag[en]gine provides the artist with a possibility to help the Graphic Module to figure out hidden elements without much extra work. For this purpose **occlusion meshes** can be used.
====== Occlusion Mesh Resource ======
An occlusion mesh is an extra resource object defining a low polygon mesh blocking the view. Each face in the mesh marks a view blocking polygon in space. In contrary to **[[dragengine:modules:model|model resources]]** resources occlusion meshes can be composed of faces with **more than 3 corners** as long as all corners of the face are **coplanar**. Faces can be double sided or single sided. By default faces are double sided.
Faces are not required to share vertices nor are they required to connect to each other or to defined a closed manifold mesh. You are free to place faces in an occlusion mesh the way you see fit. For most models creating the occlusion mesh is fast as it contains only a handful of faces taking no time to create.
Occlusion meshes support animation as do **[[dragengine:modules:model|models]]**. You can define bones and weights for vertices the same way. Occlusion meshes without bone weights are called **static occlusion meshes** while those with bone weights are called **dynamic occlusion meshes**.
===== Double/Single Sided Faces =====
A **double sided** face blocks the view from both directions. This is the choice for walls of any kind. Use double sided also for faces on room walls where no geometry is located outside. The reason is that Graphic Modules can decide to use occlusion meshes also for shadow casting optimization and missing a double sided face on such walls can result in unexpected behavior.
On the other hand **single sided** faces block the view only from the front side. The front side of the face is the side where the normal is pointing at you. From the back side the single sided face does not block the view. This can be used for situations where a texture blocks the view only from one side but not the other. An example would be a police interrogation room half-mirror window where you can see the inside from the outside but not vice versa. Using a double sided face the objects in the inside can be incorrectly culled.
====== Usage with Components ======
Occlusion meshes are stored in own **[[dragengine:modules:occmesh|resource files]]**. This allows to reuse occlusion meshes across different objects. You can export occlusion meshes using the Blender3D export scripts. Occlusion meshes are typically not found floating alone in a scene but belong to actual scene geometry. For this reason you attach occlusion meshes to **components**. In the **[[gamedev:editors:world|World Editor]]** you can do this by using the **occlusionMesh** object property where you set the path to the occlusion mesh to use. The name of this property can be different for special element classes if required.
Occlusion meshes assigned to components are moved, rotated and scaled together with the component. This way dynamic objects moving in the scene work equally well with occlusion meshes as do static objects. A typical use for this are doors where culling of props in a room is possible while the door is closed. If the occlusion mesh is a **dynamic occlusion mesh** and the component has a matching **[[dragengine:modules:rig|rig]]** object assigned (bones in the rig and occlusion mesh match partially or fully) then the occlusion mesh deforms alongside the model. This can be used for multi-part doors with special shapes or open-closing patterns. This can be also used for large deformable objects to provide as much culling oppurtunity to the Graphic Module without causing wrong culling due to the occlusion mesh sticking out of the model.
====== Best Practice ======
* In general the less faces an occlusion mesh has the faster it is for culling no matter what culling technique the Graphic Module uses.
* Use double sided faces whenever possible. Single sided are for special situations providing only half the culling oppurtinities and can even lead to unexpected behavior.
* Use occlusion meshes only on larger objects suiting well for blocking the view in a large scale. Small occlusion meshes or thin occlusion meshes are usually inefficient.
* Avoid using one huge occlusion mesh but instead use more smaller ones. This allows Graphic Modules to skip occlusion meshes not affecting the view improving performance.
====== Blender3D Export Script ======
The Blender3D Export Scripts allow to export a mesh object as an occlusion mesh file (*.occmesh). You create the mesh the same way as you would create a mesh for exporting as a model. The difference is how materials are used. For occlusion meshes you need 1 or 2 materials which represent the **double sided** and **single sided** faces. You can reuse the materials for various meshes. A good name for them is "**occmesh double**" and "**occmesh single**". Use the "**Double Sided**" property in the "**Drag[en]gine Texture**" object panel to mark the material either as single or double sided for use with occlusion meshes. Assign the double sided faces to the double sided marked material and the single sided faces to the single sided marked material. If done correctly the exporter write the occlusion mesh with the right faces.
For the time being the export script does not support the new N-Gon polygons included in Blender since 2.65 . Hence for the time being only 3-4 corner faces can be exported. N-Gons will be ignored. This will be fixed later on.
{{ :gamedev:occmesh_blender3d.png |Blender materials for use with occlusion meshes}}
Blender materials for use with occlusion meshes. If you have no single sided faces you can use only the left material.
====== Examples ======
{{ :gamedev:occmesh1.jpg |Building facades with occlusion meshes for each segment}}
Building facades with occlusion meshes for each segment. The base object is selected to highlight a single occlusion mesh. Occlusion meshes are pre-assigned to facade elements. You can just use facade elements without having to worry about occlusion meshes. You can still replace them using the **occlusionMesh** object property if required.