Skip to content

Game.Prefabs.MeshUV0

Assembly:
Game

Namespace:
Game.Prefabs

Type:
struct

Base:
IBufferElementData

Summary:
MeshUV0 is an ECS buffer element that holds the UV0 coordinates for a mesh vertex as a float2 (Unity.Mathematics.float2). It's marked with [InternalBufferCapacity(0)] and is intended to be used as the element type of a DynamicBuffer. The type provides helper Unpack methods to decode vertex UV data from a packed byte source (NativeSlice) into the buffer or a NativeArray, handling common vertex attribute formats (32-bit floats and 16-bit half-floats).


Fields

  • public float2 m_Uv
    Holds the UV0 coordinate for a single vertex as a float2 (x = u, y = v). This is the value stored per buffer element.

Properties

  • This type has no properties.

Constructors

  • public MeshUV0(float2 uv)
    Constructs a MeshUV0 instance and initializes m_Uv with the provided float2.

Methods

  • public static void Unpack(NativeSlice<byte> src, DynamicBuffer<MeshUV0> dst, int count, VertexAttributeFormat format, int dimension)
    Resizes the destination DynamicBuffer to count and delegates to the NativeArray overload to perform the unpack. Use this when you have an ECS DynamicBuffer to receive UV data.

  • public unsafe static void Unpack(NativeSlice<byte> src, NativeArray<MeshUV0> dst, int count, VertexAttributeFormat format, int dimension)
    Unpacks vertex UV data from src into dst. Behavior:

  • If format == VertexAttributeFormat.Float32 and dimension == 2, the method treats the source layout as float2 and performs a fast copy using src.SliceConvert<MeshUV0>().CopyTo(dst).
  • If format == VertexAttributeFormat.Float16, it calls NativeMath.ArrayHalfToFloat (from Colossal.AssetPipeline.Native) to convert half-floats to full floats and write them into dst. The call requests 2 components per element (float2).
  • For any other format/dimension combination the method throws an Exception: "Unsupported source UV0 format/dimension in Unpack {format} {dimension}".
  • The method uses unsafe pointer access (GetUnsafeReadOnlyPtr/GetUnsafePtr) for the half->float conversion, so it requires appropriate safety/permission when used in multithreaded or Burst contexts.

Notes and considerations: - The Unpack methods expect UV data to be either 2-component 32-bit floats or 2-component 16-bit halves. Passing different dimensions or formats will cause an exception. - The Float32 path uses SliceConvert which assumes the source byte layout matches the MeshUV0 struct layout (float2). If data is tightly packed as float2, this is a fast memcpy-like path. - The Float16 path performs CPU-side conversion via NativeMath.ArrayHalfToFloat; this may be more expensive but is necessary when assets store half-floats.

Usage Example

// Example: unpack UV0 bytes into an entity's DynamicBuffer<MeshUV0>
Entity entity = /* get or create entity with a DynamicBuffer<MeshUV0> */;
DynamicBuffer<MeshUV0> uvBuffer = entityManager.GetBuffer<MeshUV0>(entity);

// srcBytes is a NativeArray<byte> or memory region containing packed UV0 data
NativeSlice<byte> srcSlice = new NativeSlice<byte>(srcBytes);

// count = number of vertices/elements to unpack
int count = vertexCount;

// format and dimension must match the source data (e.g., Float16 with 2 components)
MeshUV0.Unpack(srcSlice, uvBuffer, count, VertexAttributeFormat.Float16, 2);