Skip to content

Game.Prefabs.MeshNormal

Assembly:
Game (Game.dll)

Namespace:
Game.Prefabs

Type:
struct

Base:
System.ValueType, Unity.Entities.IBufferElementData

Summary:
MeshNormal is an ECS buffer element that stores a vertex normal as a float3 (m_Normal). It is marked with [InternalBufferCapacity(0)] so it is intended to be used as a dynamic buffer on an entity. The struct provides static Unpack helper methods to convert raw vertex attribute byte data (NativeSlice) into a DynamicBuffer or NativeArray. The Unpack implementations handle several vertex attribute encodings: - VertexAttributeFormat.Float32 with dimension == 3: direct copy of float3 values. - VertexAttributeFormat.Float16: uses NativeMath.ArrayHalfToFloat to expand half floats to full floats. - VertexAttributeFormat.SNorm16 with dimension == 2: treats data as octahedral-encoded normals and calls NativeMath.ArrayOctahedralToNormals. If an unsupported format/dimension combination is provided, Unpack throws an Exception.


Fields

  • public float3 m_Normal
    Holds the normal vector for a single vertex (x,y,z). This is the data stored per buffer element.

Properties

  • None

Constructors

  • public MeshNormal(float3 normal)
    Initializes a MeshNormal with the provided float3 normal.

Methods

  • public static void Unpack(NativeSlice<byte> src, DynamicBuffer<MeshNormal> dst, int count, VertexAttributeFormat format, int dimension)
    Copies/unpacks vertex normal data from a byte slice into a DynamicBuffer. Resizes the destination buffer to the requested count and forwards to the NativeArray-based overload.

Parameters: - src: Source bytes containing vertex attribute data. - dst: Destination dynamic buffer to receive MeshNormal elements. - count: Number of elements to unpack. - format: VertexAttributeFormat describing how data is encoded. - dimension: Number of components per attribute (usually 2 or 3 for normals).

Behavior: - Resizes dst to count and delegates to the NativeArray overload.

  • public unsafe static void Unpack(NativeSlice<byte> src, NativeArray<MeshNormal> dst, int count, VertexAttributeFormat format, int dimension)
    Performs the actual unpacking into a NativeArray. Handles multiple encoding formats:

Behavior by format: - Float32 && dimension == 3: calls src.SliceConvert().CopyTo(dst) for fast direct copy. - Float16: calls NativeMath.ArrayHalfToFloat to convert half-precision floats to float3. - SNorm16 && dimension == 2: calls NativeMath.ArrayOctahedralToNormals to decode octahedral-encoded normals into float3. - Otherwise: throws Exception with message "Unsupported source normals format/dimension in Unpack {format} {dimension}".

Notes: - This overload is unsafe and uses GetUnsafeReadOnlyPtr / GetUnsafePtr to pass raw pointers to NativeMath helpers. - count and dimension must match the actual data layout in src.

Usage Example

// Example: unpacking normals into a DynamicBuffer<MeshNormal> inside a system or component
using Unity.Collections;
using UnityEngine.Rendering;
using Unity.Entities;
using Unity.Mathematics;

void ExampleUnpack(EntityManager em, Entity entity, NativeSlice<byte> vertexData, int vertexCount, VertexAttributeFormat format, int dimension)
{
    var buffer = em.GetBuffer<MeshNormal>(entity); // ensure the entity has a DynamicBuffer<MeshNormal>
    MeshNormal.Unpack(vertexData, buffer, vertexCount, format, dimension);
}

// Example: unpacking into a preallocated NativeArray<MeshNormal>
void ExampleUnpackNativeArray(NativeSlice<byte> vertexData, NativeArray<MeshNormal> outArray, int vertexCount, VertexAttributeFormat format, int dimension)
{
    MeshNormal.Unpack(vertexData, outArray, vertexCount, format, dimension);
}

{{ Additional notes: - The struct is intended for use with Unity's Entities (ECS) DynamicBuffer semantics. - When using Float16/SNorm16 paths the code relies on native helpers (NativeMath) to perform format-specific conversions; ensure those helpers are available in the runtime. - For SNorm16 octahedral decoding, the source data is expected to be a 2-component packed representation (dimension == 2). - Keep alignment and count consistent with the original mesh vertex layout to avoid out-of-bounds memory reads. }}