Skip to main content

StaticObjectCombiner

A class in Root.Township, the global namespace.

Bake-time tool on static prefabs that merges LOD groups, joins meshes by material, removes hidden or underground triangles and flattens colliders, based on its functions flags. Runs during static batching, not in a live game.

Constructors​

MemberWhat it is
StaticObjectCombiner()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
protected StaticObjectCombiner.CombineFunctions functionsFlags for which bake steps to run (LOD join, mesh join, occlusion, collider joining and flattening, terrain occlusion).
7 private and internal fields
MemberWhat it is
private Vector3[] additionalCheckPointsExtra local-space points used as view points when finding hidden triangles.
private bool btnBakeInspector button that runs a preview bake.
private const string ImpostorKeyShader name part (Impostor) used to skip impostor renderers.
private bool isForcingJoinedMeshCollidersConvexMakes the joined mesh colliders convex.
private bool isIgnoringAutomatedMarkingRuns collider joining and terrain occlusion even during automated static batching, and joins layers with a single collider.
private float[] joinedLODValuesScreen transition heights applied to the joined LOD group's levels.
private static readonly Logger LoggerNLog logger for this class.

Properties​

MemberWhat it is
static bool IsAutomatedCall { get; set; }Static flag for automated batch runs. While set, collider joining and terrain collider steps are skipped unless the object opts in.
static bool IsTerrainBakingLocked { get; set; }Static switch that turns off all terrain-based triangle removal.
protected virtual bool IsCleaningOnJoin { get; }Whether empty child objects are deleted after joining. True by default.
protected virtual bool IsSettingToTerrainLayer { get; }Whether joined meshes are moved to the Terrain layer. True by default.

Events​

MemberWhat it is
static event Action<IEnumerable<Renderer>> OnCreatedRenderersStatic one-shot event raised with the renderers a mesh join created. Cleared after each join.
event Action<GameObject> PostBakeablesRaised after the object's build bakeables have been baked, before any combining.

Methods​

MemberWhat it is
static void AutoLOD(GameObject baseObject)Merges all child LOD groups into a single LOD group on the base object, taking the largest transition height per level.
virtual void Bake()Runs every enabled bake step on this object.
static bool CleanRecursive(Transform parent)Deletes child objects that have nothing but a transform. True if the given object was deleted.
static Mesh ExcludeOccludedTriangles(Mesh mesh, List<RemovingTriangle> trianglesToRemove)Returns a copy of a mesh with the listed triangles removed.
static void FlattenPrimitiveColliders(Transform transform)Moves box colliders onto shared child objects grouped by layer and rotation, so there are fewer collider objects.
static void GatherRenderers(GameObject prefab, LODGroup lodGroup, Dictionary<MaterialLOD, List<MeshFilter>> meshesByMaterial, Func<Renderer, bool> validateRenderer = null)Groups the prefab's mesh filters by material and LOD level, skipping impostors and parts on other rigidbodies.
static List<GameObject> JoinMeshes(Transform baseTransform, LODGroup lodGroup, KeyValuePair<MaterialLOD, List<MeshFilter>> pair, out List<Mesh> newMeshes, bool isCleaningUp = false)Combines a group of meshes with the same material and LOD into new mesh objects under the base transform.
void SetupFull()Turns on every bake step.
protected virtual bool ValidateRenderer(Renderer renderer)Subclass filter for which renderers may be joined. Allows all by default.
10 private and internal methods
MemberWhat it is
private IEnumerable<Vector3> AdditionalCheckPoints() (iterator)Yields additionalCheckPoints in world space, as extra view points for the triangle occlusion check.
private static bool AnyChecks(bool[] checks, List<int> triangles, int i)True when all three vertices of a triangle passed the check (the name says any, the code needs all).
private static int GetLODLevel(Renderer renderer, LOD[] lods)Returns which LOD level of the group a renderer is in, or -1.
private static void GetMesh(List<Vector3> vertices, List<int> triangles, ref bool needsNormal, List<Vector3> normals, ref bool needsTangents, List<Vector4> tangents, ref bool needsUV, List<Vector2> uv, ref bool needsUV2, List<Vector4> uv2, ref bool needsUV3, List<Vector4> uv3, ref bool needsUV4, List<Vector4> uv4, ref bool needsColor, List<Color> colors, MaterialLOD materialLOD, List<Mesh> meshes, bool hasLOD, int finalLayer, List<GameObject> result, Transform baseTransform, List<Renderer> joinedRenderers)Builds a mesh from the gathered vertex lists, clears them, and adds a renderer object for it under the base transform.
private static StaticObjectCombiner.Space GetNewParent(int layer, List<StaticObjectCombiner.Space> spaces, Quaternion rotation, Transform parent)Creates a new collider parent Space for a layer and rotation and adds it to the list.
private bool IsValidateTerrain(Object terrain)True when a terrain exists, terrain baking isn't locked and TerrainOccludesTriangles is set.
private void JoinMeshColliders()Merges child mesh colliders into one collision mesh per layer, strips occluded triangles and destroys the originals.
private void OnDrawGizmosSelected()Empty.
private static bool TriangleValidation(Vector3 a, Vector3 b, Vector3 c, Matrix4x4 localToWorld, Terrain terrain)True when any edge of the triangle crosses the terrain (temporarily moved to layer 3 for the linecast).
private static bool Validation(Vector3 worldVertex, Matrix4x4 localToWorld, Terrain terrain)True when a vertex is above the terrain height at its position.