Game.Rendering.NetMeshHelpers
Assembly:
Likely the main game assembly (Game).
Namespace: Game.Rendering
Type: static class
Base: System.Object
Summary:
Utility helpers for creating default road-related meshes (lane, roundabout, node, edge) and for caching mesh geometry into ECS-friendly DynamicBuffers for use with the game's rendering/data pipeline. Includes a Burst-compiled nested job (CacheMeshDataJob) that converts GeometryAsset data into runtime DynamicBuffers (MeshVertex, MeshNormal, MeshTangent, MeshUV0, MeshIndex) attached to an Entity. Also contains helper routines to construct meshes procedurally used by the road system.
Fields
-
private static readonly Unity.Mathematics.float3 v_left
Left direction constant used to set normals/tangents when building procedural meshes. -
private static readonly Unity.Mathematics.float3 v_up
Up direction constant used to set normals/tangents when building procedural meshes. -
private static readonly Unity.Mathematics.float3 v_right
Right direction constant used to set normals/tangents when building procedural meshes. -
private static readonly Unity.Mathematics.float3 v_down
Down direction constant used to set normals/tangents when building procedural meshes. -
private static readonly Unity.Mathematics.float3 v_forward
Forward direction constant used to set normals/tangents when building procedural meshes. -
private static readonly Unity.Mathematics.float3 v_backward
Backward direction constant used to set normals/tangents when building procedural meshes.
(These readonly vectors are used throughout the mesh construction routines to orient face normals and tangents consistently.)
Properties
- (None)
This static helper class has no public properties.
Constructors
- (None)
NetMeshHelpers is a static class — it has no instance constructors.
Methods
-
public static Mesh CreateDefaultLaneMesh()
Creates a procedural Mesh representing a default lane geometry. Produces vertices, normals, tangents, UVs and triangle indices for a rectangular prism-like lane section. Returns a UnityEngine.Mesh with name "Default lane". -
public static Mesh CreateDefaultRoundaboutMesh()
Creates a procedural Mesh for the default roundabout geometry used by the road system. Builds vertices, normals, tangents, color/u-v vectors and indices, then calls CreateMesh to return a Mesh named "Default roundabout". -
public static Mesh CreateDefaultNodeMesh()
Creates a procedural Mesh used for junction/nodes. Similar to other CreateDefault* methods but with topology and UV/color packing appropriate for nodes. Returns a Mesh named "Default node". -
public static Mesh CreateDefaultEdgeMesh()
Creates a procedural Mesh used for edges (road segments that are not lanes). Returns a Mesh named "Default edge". -
public static Unity.Jobs.JobHandle CacheMeshData(GeometryAsset meshData, Entity entity, EntityManager entityManager, EntityCommandBuffer commandBuffer)
Schedules a Burst-compiled job (CacheMeshDataJob) that reads a GeometryAsset (asset-compressed geometry) and writes unpacked vertex attributes and indices into DynamicBuffers on the specified Entity. Before scheduling the job, this method fills the Entity's MeshMaterial buffer entries (m_StartIndex, m_IndexCount, m_StartVertex, m_VertexCount) based on sub-mesh descriptors. Returns the job's JobHandle so callers can wait or combine it. -
public static void CacheMeshData(Mesh mesh, Entity entity, EntityManager entityManager, EntityCommandBuffer commandBuffer)
Synchronously reads a UnityEngine.Mesh via Mesh.AcquireReadOnlyMeshData, extracts vertex attributes and indices, and writes them into DynamicBuffers on the given Entity. Also fills MeshMaterial buffer entries for submeshes. This is the CPU path for non-GeometryAsset meshes. -
public static void UncacheMeshData(Entity entity, EntityCommandBuffer commandBuffer)
Removes the mesh-related DynamicBuffer components (MeshVertex, MeshNormal, MeshTangent, MeshUV0, MeshIndex) from the specified Entity via the command buffer. Use to free cached mesh data from an entity. -
private static void AddVertex(Vector3[] vertices, Vector3[] normals, Vector4[] tangents, Vector2[] uvs, ref int vertexIndex, float3 position, float3 normal, float3 tangent, float2 uv)
Internal utility used by CreateDefaultLaneMesh to append a vertex with given attributes. Writes position, normal, tangent (packed into Vector4), and UV; then increments vertexIndex. -
private static void AddQuad(int[] indices, ref int indexIndex, int a, int b, int c, int d)
Internal utility to append two triangles (quad) to the indices array in a consistent winding (a, b, c) and (c, d, a). Advances indexIndex accordingly. -
private static Mesh CreateMesh(string name, Vector3[] vertices, Vector3[] normals, Vector4[] tangents, Color32[] colors, Vector4[] uvs, int[] indices)
Helper that constructs a UnityEngine.Mesh from provided attribute arrays, sets bounds to a large default (1000 units) and returns the created Mesh. -
private static void AddVertex(Vector3[] vertices, Vector3[] normals, Vector4[] tangents, Color32[] colors, Vector4[] uvs, ref int vertexIndex, float3 normal, float3 tangent, float2 uv, int2 m, float tx, float y, float tz)
Overload used by the roundabout/node/edge mesh builders. It encodes two bytes of metadata (m.x, m.y) into colors and packs additional data into the Vector4 UV storage (uv.x, uv.y, 0.5f, 0f). Sets the vertex position from tx/y/tz and increments vertexIndex.
Nested types: CacheMeshDataJob (private struct)
private struct CacheMeshDataJob : IJob
Burst-compiled job type used by CacheMeshData(GeometryAsset, ...). Its purpose is to unpack compressed/asset geometry (GeometryAsset.Data) to DynamicBuffers attached to an Entity.
Key fields:
- Various NativeSlicepublic GeometryAsset.Data m_Data
— the GeometryAsset raw data to unpack (if IsValid).
- public Entity m_Entity
— the entity to which buffers are added.
- public IndexFormat m_IndexFormat
, public int m_VertexCount
, public int m_IndexCount
— used for fallback unpacking.
- public VertexAttributeFormat m_PositionsFormat
, m_NormalsFormat
, m_TangentsFormat
, m_TexCoords0Format
and corresponding dimension fields for proper unpacking in fallback case.
- public EntityCommandBuffer m_CommandBuffer
— used to add DynamicBuffers to the entity inside the job (ECB is used here rather than EntityManager for thread-safety).
Behavior (Execute): - Adds DynamicBuffers to the entity for MeshVertex, MeshNormal, MeshTangent, MeshUV0 and MeshIndex via the EntityCommandBuffer. - If m_Data.IsValid is true (the GeometryAsset case), it: - Calculates total vertex and index counts via GeometryAsset.GetAllVertexCount/GetAllIndicesCount and resizes buffers accordingly. - Iterates over meshData.meshCount; for each submesh it retrieves attribute formats/dimensions and raw attribute data slices and indices, then calls static Unpack methods on MeshVertex/MeshNormal/MeshTangent/MeshUV0/MeshIndex to decode into the DynamicBuffers. - Throws exceptions if required attributes are missing (position, normal, tangent, texcoord0). - If m_Data.IsValid is false, it uses the provided NativeSlices/NativeArrays and format/dimension metadata to call Unpack for a single mesh (fallback path).
Notes: - The job is Burst compiled and uses Native container safety restrictions lifted in some fields when writing into provided buffers. - The job prepares ECS buffers that later rendering or other systems expect.
Usage Example
// Example: create a default lane mesh and cache it onto an entity.
// Assumes you have an Entity `meshEntity`, an EntityManager `entityManager`, and an EntityCommandBuffer `ecb`
// Also assumes required DynamicBuffer<MeshMaterial> already exists on `meshEntity`.
using Unity.Entities;
using UnityEngine;
using Game.Rendering;
// 1) create the mesh (runtime only; could also use a GeometryAsset)
Mesh laneMesh = NetMeshHelpers.CreateDefaultLaneMesh();
// 2) synchronously cache mesh data into ECS buffers for immediate use
NetMeshHelpers.CacheMeshData(laneMesh, meshEntity, entityManager, ecb);
// OR: if you have a GeometryAsset (asset-backed mesh) and want async job path:
// GeometryAsset geomAsset = ...;
// JobHandle handle = NetMeshHelpers.CacheMeshData(geomAsset, meshEntity, entityManager, ecb);
// // Combine or complete handle as needed:
// handle.Complete();
Notes and tips: - When using CacheMeshData(GeometryAsset,...), the method returns a JobHandle — make sure to manage dependencies or complete the handle before reading the buffers on the main thread. - The created procedural meshes set large bounds (1000 units), which is fine for these default primitives used by the road renderer, but if you customize mesh generation consider tightening bounds for frustum and occlusion culling correctness. - The CacheMeshData job requires MeshMaterial buffer elements to exist and filled appropriately (the method fills them when scheduling). If adapting for other ECS setups, ensure MeshMaterial is present and sized to submesh count.