Skip to content

Game.NetCompositionMeshRefSystem

Assembly: Game
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
NetCompositionMeshRefSystem is an ECS system used by the game to create and assign shared mesh entities for road/track ("net") compositions. It inspects entities with NetCompositionData and NetCompositionPiece buffers, computes a hash and aggregated mesh metadata (materials, LOD bias, index factor, shadow bias, default layers, etc.), attempts to reuse existing mesh entities (including LOD variants and rotated equivalents), or creates new mesh entities (with NetCompositionMeshData, NetCompositionPiece buffer, MeshMaterial buffer and related components). Work is performed inside a Burst-compiled IJob (CompositionMeshRefJob) and scheduled with dependency handling via a ModificationBarrier4 and the NetCompositionMeshSystem which provides existing mesh entity lookups. The job also builds LOD chains and performs detailed equality checks that account for piece flags, section flags, offsets and possible rotation (mirror) when allowed.


Fields

  • private struct NewMeshData
    Internal helper struct used by the job to temporarily store newly-created mesh entity information (entity, composition flags, pointer to pieces buffer, piece count) while comparing/looking up compositions.

  • [BurstCompile] private struct CompositionMeshRefJob : IJob
    The main job that iterates chunks of NetCompositionData entities and performs the heavy work: hashing, searching for existing mesh entities, creating new mesh entities, copying piece/material buffers, calculating aggregate mesh data and initializing LODs. Contains many helper methods (CopyMeshPieces, CalculatePieceData, InitializeLods, TryFindComposition, GetHashCode, Equals, GetCompositionFlagMask, GetSectionFlagMask).

  • private struct TypeHandle
    Container for all EntityTypeHandle / ComponentTypeHandle / BufferLookup / ComponentLookup handles used by the job. Populated by __AssignHandles.

  • private NetCompositionMeshSystem m_NetCompositionMeshSystem
    Reference to the NetCompositionMeshSystem used to access existing mesh entities (mappings from composition hash to existing mesh entity) and to register readers.

  • private ModificationBarrier4 m_ModificationBarrier
    Command buffer barrier used to create entities and set components/buffers safely from the job.

  • private EntityQuery m_CompositionQuery
    An EntityQuery that selects entities that contain NetCompositionData and NetCompositionPiece and have a NetCompositionMeshRef and Created — used to find compositions that need mesh references.

  • private EntityArchetype m_MeshArchetype
    Archetype used when creating new mesh entities. Includes components such as NetCompositionMeshData, NetCompositionPiece, MeshMaterial, BatchGroup, Created and Updated.

  • private TypeHandle __TypeHandle
    Instance of the TypeHandle struct used to cache handle references for the job.

Properties

  • This type exposes no public properties.

Constructors

  • public NetCompositionMeshRefSystem()
    Default constructor. The system is created by the world / ECS bootstrap; OnCreate handles initialization.

Methods

  • [Preserve] protected override void OnCreate()
    Initializes the system: obtains references to NetCompositionMeshSystem and ModificationBarrier4, builds the m_CompositionQuery to find compositions to process, creates the m_MeshArchetype used for newly-created mesh entities, and calls RequireForUpdate to ensure the system only runs when relevant entities exist.

  • [Preserve] protected override void OnUpdate()
    Prepares the chunk list for the composition query, constructs and schedules the CompositionMeshRefJob with required lookups and buffers, handles dependencies between the created job, the NetCompositionMeshSystem reader registration, and the modification barrier. Disposes the temporary chunk list after scheduling and sets base.Dependency to the scheduled job handle.

  • protected override void OnCreateForCompiler()
    Compiler helper used by the generated code path to assign queries and handles. Calls __AssignQueries and TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal method (generated pattern) to ensure any query building for the compiler path is assigned. Here it creates and immediately disposes an EntityQueryBuilder (no runtime effect in this compiled file).

  • CompositionMeshRefJob.Execute() : void
    The job entry point. Iterates the provided archetype chunks, computes composition hashes and tries to reuse existing mesh entities (via m_MeshEntities and the newMeshes cache). If not found, creates a new mesh entity, copies piece/material buffers, calculates aggregate mesh data, registers the new mesh in the temporary map, and initializes LODs recursively.

  • CompositionMeshRefJob.CopyMeshPieces(DynamicBuffer source, DynamicBuffer target) : void
    Copies only pieces that actually have meshes and are not hidden, compacting them into the target buffer.

  • CompositionMeshRefJob.CalculatePieceData(DynamicBuffer pieces, DynamicBuffer materials, out MeshLayer defaultLayers, out MeshFlags meshFlags, out float indexFactor, out float lodBias, out float shadowBias) : void
    Aggregates per-piece data into overall mesh metrics (default layers, mesh flags, average LOD bias, average shadow bias, indexFactor heuristic) and builds a unique materials list for the composition.

  • CompositionMeshRefJob.InitializeLods(Entity mesh, NetCompositionMeshData meshData, DynamicBuffer pieces, NativeParallelMultiHashMap newMeshes, ref NativeList tempPieces, ref NativeList tempPieces2) : void
    Builds LOD entries for a mesh entity by checking per-piece LOD buffers, building per-LOD piece lists, reusing or creating mesh entities for each LOD level, and recursively initializing their LODs.

  • CompositionMeshRefJob.TryFindComposition(NativeParallelMultiHashMap newMeshes, int hash, CompositionFlags flags, void* pieces, int pieceCount, ref NativeList tempPieces, out Entity entity, out bool rotate) : bool
    Attempts to locate an existing mesh entity matching the composition by checking m_MeshEntities (existing meshes from NetCompositionMeshSystem) and the newMeshes cache created during this execution. Supports detection of rotated equivalents when allowed by flags. Returns the matching entity and whether a rotation (mirror) must be applied.

  • CompositionMeshRefJob.GetHashCode(CompositionFlags flags, NativeArray pieces, out bool hasMesh) : int
    Produces a hash based on a mask of composition flags and piece entity references that have meshes. Also sets hasMesh to indicate whether any piece contributes mesh geometry.

  • CompositionMeshRefJob.Equals(CompositionFlags flags1, CompositionFlags flags2, void pieces1, void pieces2, int pieceCount1, int pieceCount2, ref NativeList tempPieces, bool rotate) : bool
    Performs a detailed equality check between two compositions (pieces lists + flags). Considers piece identity, section flags, offsets (within 0.1f), asymmetric mesh flags and possible rotation equivalence. Uses a temporary list to match and remove pairs; returns true only if all pieces match.

  • CompositionMeshRefJob.GetCompositionFlagMask(CompositionFlags flags) : CompositionFlags.General
    Returns the subset of composition flags relevant to composition identity (Node and Roundabout flags are preserved — used in hashing and quick equality checks).

  • CompositionMeshRefJob.GetSectionFlagMask(NetCompositionPiece piece) : NetSectionFlags
    Returns the subset of section flags relevant for matching (Median, Left, Right, AlignCenter, HalfLength).

  • public NetCompositionMeshRefSystem()
    Public parameterless constructor (presented earlier in Constructors).

Usage Example

// Example: system initialization happens automatically by the world.
// Typical behavior you can expect: the system will run and populate
// NetCompositionMeshRef components for NetCompositionData entities.
// If you need to interact with the mesh system at runtime, request the system:

protected override void OnCreate()
{
    base.OnCreate();

    // Example: get the mesh ref system (similar to what this system does)
    var meshSystem = World.GetOrCreateSystemManaged<NetCompositionMeshSystem>();

    // You can query existing mesh entities via that system or let
    // NetCompositionMeshRefSystem create shared mesh entities automatically.
}

Notes and tips: - The heavy lifting is done inside a Burst-compiled job that uses unsafe pointers and temporary native containers; ensure jobs have appropriate dependency registration and that the modification barrier is present to accept command buffer writes. - The system attempts to reuse mesh entities when possible (including mirrored compositions when allowed), reducing memory and draw call duplication. - When modding, be careful altering NetCompositionPiece, flag enums or MeshData structures — these directly affect hashing and equality checks and will change reuse behavior.