Game.Rendering.ZoneMeshHelpers
Assembly:
Namespace: Game.Rendering
Type: public static class
Base: System.Object
Summary:
Helper utilities for generating Mesh objects and spatial bounds used to render "zone" tiles/patches in the game. Provides a mesh builder that constructs a regular grid of corner vertices plus one center vertex per cell and the associated triangle indices, a helper to calculate the index buffer size for a given resolution, and a function to compute an axis-aligned Bounds3 covering the generated geometry.
Fields
None
This is a static utility class and defines no instance or static fields.
Properties
None
No public properties.
Constructors
None
Static classes have no constructors exposed.
Methods
public static Mesh CreateMesh(int2 resolution, int2 factor)
Creates a Unity Mesh representing a zone patch with the given resolution (number of cells in x and y) and factor (scaling applied to spacing and UVs).
Implementation details and notes: - Vertex layout: - First section: (resolution.x + 1) * (resolution.y + 1) corner vertices laid out on a regular grid (one vertex per grid corner). - Second section: resolution.x * resolution.y center vertices — one center vertex per cell. - UVs are stored in mesh.uv and are computed from the remaining cell count multiplied by the factor components. Corner and center vertices use slightly different offsets so UVs align with the grid and centers. - Triangle indices: - For each cell, the method emits 4 triangles (making up a quad split from the center to the four surrounding corners). Each triangle uses 3 indices: thus GetIndexCount(resolution) == resolution.x * resolution.y * 4 * 3. - Index ordering follows the code's ordering, producing consistent winding for the mesh faces. - Mesh properties set: - name = $"Zone {resolution.x}x{resolution.y}" - vertices, uv, triangles are assigned. The method does not compute normals or tangents; if your shader requires normals or tangents, call mesh.RecalculateNormals() and/or mesh.RecalculateBounds()/RecalculateTangents() as needed after creation. - Performance / memory: - Vertex count = (resolution.x + 1) * (resolution.y + 1) + resolution.x * resolution.y. - Index count = resolution.x * resolution.y * 4 * 3. - Large resolutions can produce large meshes — ensure you keep resolution reasonable to avoid excessive memory and CPU usage. - Edge cases: - If either resolution.x or resolution.y is zero, the resulting mesh will be degenerate (GetIndexCount will be 0); the method will still allocate arrays accordingly. - Usage considerations: - The mesh is returned ready to be assigned to a MeshFilter or used with Graphics.DrawMesh, but if lighting is required, recalculate normals or provide them explicitly.
public static int GetIndexCount(int2 resolution)
Returns the number of triangle indices that CreateMesh will produce for the given resolution.
Details: - Formula: resolution.x * resolution.y * 4 * 3 - Each cell produces 4 triangles. - Each triangle uses 3 indices. - This is useful to preallocate index arrays or to estimate memory usage.
public static Bounds3 GetBounds(int2 resolution)
Computes a cube-shaped Bounds3 that encloses the geometry for a given resolution.
Details: - The method computes a half-size vector float3(resolution.x * 4f, 0f, resolution.y * 4f) and then sets the y component to the maximum of the x and z components (so the bounds is cubic in extent). - It returns new Bounds3(-halfSize, halfSize) — i.e., a bounds centered at the origin with that half-extent. - Use this bounds when you need spatial culling or to place bounding volumes for the generated mesh.
Usage Example
// Example: create a 16x16 zone mesh, assign to a MeshFilter and ensure normals are available.
using UnityEngine;
using Unity.Mathematics;
using Colossal.Mathematics; // for Bounds3 if needed
public class ZoneMeshExample : MonoBehaviour
{
void Start()
{
int2 resolution = new int2(16, 16);
int2 factor = new int2(1, 1);
Mesh zoneMesh = ZoneMeshHelpers.CreateMesh(resolution, factor);
// If you need normals for lighting:
zoneMesh.RecalculateNormals();
// Assign to MeshFilter on this GameObject
var mf = gameObject.GetComponent<MeshFilter>();
if (mf == null) mf = gameObject.AddComponent<MeshFilter>();
mf.sharedMesh = zoneMesh;
var mr = gameObject.GetComponent<MeshRenderer>();
if (mr == null) mr = gameObject.AddComponent<MeshRenderer>();
// assign material as needed
// Get bounding volume (optional)
Bounds3 b = ZoneMeshHelpers.GetBounds(resolution);
Debug.Log($"Zone bounds: min={b.min}, max={b.max}");
}
}
Notes: - The created mesh name follows the pattern "Zone {width}x{height}". - If you will be modifying the mesh frequently or at runtime, be mindful of Unity's mesh upload costs and consider reusing meshes or using sharedMesh where possible.