blog games developers documentation portfolio gallery

More in this category:

SetUpLODLevelsWith LODSwitcher()

Mesh[] GameObject.SetUpLODLevelsWithLODSwitcher(float[] lodScreenSizes, float[] maxWeights, bool recalcNormals, float removeSmallParts)



Extension to GameObject.
This function will generate LOD meshes and add a LODSwitcher component to the gameObject.

maxWeights is an array of compression values for each LOD level to be generated. The number of elements should be identical to lodScreenSizes. SetUpLODLevels() uses the array {0.65f, 1f, 1.5f} as default

lodScreenSizes is an array of floats that will be copied into the LODSwitcher component. It tells the LODSwitcher when to switch between LOD levels. The number of elements should be the same as maxWeights. SetUpLODLevels uses the array {0.6f, 0.3f, 0.15f} as default.

recalcNormals is a bool that is best set to true unless your original mesh has specific normals that should be left alone.

removeSmallParts is a float to control the removal of small parts of the mesh. Like buttons on a shirt or bolts on a machine. The default value is 1. To remove only very small parts use a smaller value, to remove bigger parts as well use a bigger value. A value of 0 will skip the removal of small parts entirely.

The mesh compression algorithm uses many internal steps to identify key features of the mesh like corners, edges and jumps in the UV map. Basically, the number of triangles is reduced by repeatedly joining two adjacent triangles. The smaller the triangle, the more likely it is to be merged. To prevent deforming the mesh, it takes many values into account like the directions of the normals, UV stretch, individual triangle area versus combined triangle area, etc. Groups of triangles are identified by following adjacent triangles and identical vertices. When the surface area of a group of triangles is small enough, the entire group is removed. This will result in small details disappearing in the LOD levels, like a door handle or a small screw.

When you get unwanted holes in your mesh or when you are not happy with the result, Try using other values for float[] maxWeights.

The result of the second LOD mesh using maxWeights {0.5f, 1.1f} is not the same as using maxWeights {1.1f} or maxWeights {0.3f, 0.56, 1.1f}. This is because the original mesh is first compressed with maxWeights[0]. For performance reasons the result of this compression is then compressed with maxWeights[1], etc.

This function will add a LODSwitcher component to the gameObject, fill it with the generated LOD meshes and copy lodScreenSizes into it.

It returns an array with the newly generated LOD Meshes. This array also includes the original mesh which is used for LOD 0.

Example:

public class Tester : MonoBehaviour {

void OnMouseUpAsButton() {
// switch LOD 0 to LOD 1 at relative screen size of 0.6
// switch LOD 1 to LOD 2 at relative screen size of 0.3
// compress the original mesh with 0.5 for LOD1 and 0.9 for LOD 2.
gameObject.SetUpLODLevelsWithLODSwitcher(new float[2] {0.6f, 0.3f}, new float[2] {0.5f, 0.9f}, 1f );

Debug.Log("This object is now game ready and has 2 LOD levels (3 if you count LOD 0)");
Debug.Log("The relative screen sizes at which the LOD levels switch are 0.6 and 0.3");
}
}






follow us