Skip to content

Game.Prefabs.MeshTangent

Assembly: Game
Namespace: Game.Prefabs

Type: struct

Base: System.ValueType, Unity.Entities.IBufferElementData

Summary:
MeshTangent is an ECS buffer element that holds a vertex tangent as a float4 (xyz tangent + w handedness). It provides constructors and specialized Unpack helpers to convert raw vertex attribute byte data (NativeSlice) into either a DynamicBuffer or a NativeArray. The Unpack methods support several source vertex attribute encodings (native float32, half-floats, and octahedral-compressed tangents) and use unsafe/low-level conversions in the Colossal/NativeMath helpers for performance. This struct is intended for use in Unity ECS codepaths that import or process mesh vertex tangent data in Cities: Skylines 2 modding.


Fields

  • public Unity.Mathematics.float4 m_Tangent
    Stores the tangent vector as float4: xyz is the tangent direction, w is typically the handedness/bit used to reconstruct bitangent.

Properties

  • None
    This struct exposes its data directly via the public field and provides static helper methods for populating buffers/arrays.

Constructors

  • public MeshTangent(float4 tangent)
    Creates a MeshTangent wrapping the provided float4 tangent value.

Methods

  • public static void Unpack(NativeSlice<byte> src, DynamicBuffer<MeshTangent> dst, int count, VertexAttributeFormat format, int dimension)
    Resizes the destination dynamic buffer to hold count elements and delegates to the NativeArray overload. Use this when you have an ECS DynamicBuffer to populate.

  • public unsafe static void Unpack(NativeSlice<byte> src, NativeArray<MeshTangent> dst, int count, VertexAttributeFormat format, int dimension)
    Core unpack implementation. Supported behaviors:

  • If format == VertexAttributeFormat.Float32 and dimension == 4: performs a SliceConvert() and CopyTo(dst) for a fast direct copy.
  • If format == VertexAttributeFormat.Float16: calls NativeMath.ArrayHalfToFloat to convert half-precision components to full float components and writes into dst (destination is 4-component float per element). This supports source dimensions up to the provided dimension parameter.
  • If format == VertexAttributeFormat.Float32 and dimension == 1: treats source as octahedral-compressed tangents and calls NativeMath.ArrayOctahedralToTangents to expand into float4 tangents.
  • Otherwise: throws an Exception indicating an unsupported format/dimension combination.

Notes on safety and performance: - The method uses unsafe pointer operations (GetUnsafeReadOnlyPtr / GetUnsafePtr) and Colossal.AssetPipeline.Native.NativeMath conversion helpers for performance; the caller must ensure src contains at least count * dimension * sizeof(element) bytes and that dst has capacity for count elements. - The struct is annotated with [InternalBufferCapacity(0)], so DynamicBuffer elements are stored out-of-line.

Usage Example

// Populate a DynamicBuffer<MeshTangent> (inside an ECS system where you have the buffer)
NativeSlice<byte> srcBytes = /* obtained from mesh vertex buffer */;
DynamicBuffer<MeshTangent> tangents = /* get from entity */;
int count = vertexCount;
VertexAttributeFormat format = /* from mesh import (e.g. VertexAttributeFormat.Float32) */;
int dimension = /* number of components per vertex attribute (1..4) */;

MeshTangent.Unpack(srcBytes, tangents, count, format, dimension);

// OR populate a NativeArray<MeshTangent>
NativeArray<MeshTangent> array = new NativeArray<MeshTangent>(count, Allocator.Temp);
MeshTangent.Unpack(srcBytes, array, count, format, dimension);
// use array...
array.Dispose();

Additional remarks: - Supported common combinations: (Float32, 4) direct copy, (Float32, 1) octahedral compressed tangents, and Float16 with various dimensions (converted to float4). - If you hit the thrown Exception, verify the mesh vertex tangent attribute format and dimension before calling Unpack. - Requires Unity.Entities, Unity.Collections, Unity.Mathematics and the Colossal.AssetPipeline.Native NativeMath helpers available in the game's native asset pipeline.