MeshBuffer


public final class MeshBuffer implements AutoCloseable


Container holding raw vertex and index data for custom meshes.

A MeshBuffer represents a compilation of one or more vertex buffers and an index buffer in the underlying rendering engine. The vertex buffers contain the vertex data according to the provided VertexLayout, and the index buffer contains the indices of the vertices that form the primitives of the mesh.

The raw buffer data is provided during the creation of the MeshBuffer and is immediately copied into the rendering engine's memory. As a result, the MeshBuffer does not retain references to the original ByteBuffer objects, which can be modified or released by the application after creation.

Summary

Nested types

public final class MeshBuffer.MeshUsage

Describes the intended usage and mutability of a MeshBuffer.

Public methods

void

Closes the given MeshBuffer.

static final @NonNull MeshBuffer
@MainThread
create(
    @NonNull Session session,
    @NonNull VertexLayout vertexLayout,
    @NonNull List<@NonNull ByteBufferRegion> vertexData,
    @NonNull ByteBufferRegion indexData
)

Creates a new static MeshBuffer.

static final @NonNull MeshBuffer
@RequiresSpatialApi(value = 4)
@MainThread
createDynamic(
    @NonNull Session session,
    @NonNull VertexLayout vertexLayout,
    @IntRange(from = 1) int vertexCount,
    @IntRange(from = 1) int indexCount,
    List<ByteBufferRegion> vertexData,
    ByteBufferRegion indexData
)

Creates a new dynamic MeshBuffer supporting runtime updates.

final @NonNull MeshBuffer.MeshUsage

MeshUsage indicating the usage mode and mutability of this buffer

final @NonNull VertexLayout

VertexLayout describing the structure of the vertex data

final void
@MainThread
updateIndexData(
    @NonNull ByteBufferRegion indexData,
    @IntRange(from = 0) int offsetInBytes
)

Updates index data in the MeshBuffer.

final void
@MainThread
updateVertexData(
    @IntRange(from = 0) int bufferIndex,
    @NonNull ByteBufferRegion vertexData,
    @IntRange(from = 0) int offsetInBytes
)

Updates vertex data for a specific buffer index in the MeshBuffer.

Public methods

close

Added in 1.0.0
@MainThread
public void close()

Closes the given MeshBuffer.

The MeshBuffer can be explicitly closed at anytime or garbage collected. An exception will be thrown if the MeshBuffer is used after being closed.

Throws
IllegalStateException

if the resource has already been closed.

create

Added in 1.0.0
@MainThread
public static final @NonNull MeshBuffer create(
    @NonNull Session session,
    @NonNull VertexLayout vertexLayout,
    @NonNull List<@NonNull ByteBufferRegion> vertexData,
    @NonNull ByteBufferRegion indexData
)

Creates a new static MeshBuffer.

The raw buffer data is copied immediately into rendering engine memory. The original ByteBuffer objects can be modified or released after creation without affecting the mesh buffer.

Parameters
@NonNull Session session

session to use for creating the MeshBuffer

@NonNull VertexLayout vertexLayout

layout of the vertices in the vertex buffer(s)

@NonNull List<@NonNull ByteBufferRegion> vertexData

vertex data regions, one for each buffer index in vertexLayout

@NonNull ByteBufferRegion indexData

index data region containing 32-bit unsigned indices

Returns
@NonNull MeshBuffer

a new static MeshBuffer

Throws
IllegalArgumentException

if vertexData does not contain a buffer for each buffer index in vertexLayout, or if any ByteBufferRegion is empty

createDynamic

Added in 1.1.0-alpha01
@RequiresSpatialApi(value = 4)
@MainThread
public static final @NonNull MeshBuffer createDynamic(
    @NonNull Session session,
    @NonNull VertexLayout vertexLayout,
    @IntRange(from = 1) int vertexCount,
    @IntRange(from = 1) int indexCount,
    List<ByteBufferRegion> vertexData,
    ByteBufferRegion indexData
)

Creates a new dynamic MeshBuffer supporting runtime updates.

The vertexCount and indexCount parameters define the fixed maximum capacity of the underlying GPU buffers. The allocated buffer cannot be resized after creation. Any runtime updates via updateVertexData or updateIndexData that exceed these capacities will throw an IllegalArgumentException.

If initial vertex or index data is omitted or partially provided (smaller than the allocated capacities), the unpopulated regions will be filled with zeros.

Note: Automatic bounding box calculation only considers the initial vertex data supplied at creation time and is never recalculated on subsequent dynamic updates. When working with dynamic meshes, set a bounding box on the mesh that covers all possible vertex positions across the mesh's lifetime.

Parameters
@NonNull Session session

session to use for creating the MeshBuffer

@NonNull VertexLayout vertexLayout

layout of the vertices in the vertex buffer(s)

@IntRange(from = 1) int vertexCount

fixed maximum capacity of vertices allocated in memory

@IntRange(from = 1) int indexCount

fixed maximum capacity of indices allocated in memory

List<ByteBufferRegion> vertexData

optional list of ByteBufferRegion containing initial vertex data, one for each buffer index in vertexLayout

ByteBufferRegion indexData

optional ByteBufferRegion containing initial 32-bit index data

Returns
@NonNull MeshBuffer

a new MeshBuffer

Throws
IllegalArgumentException

if vertexCount or indexCount is non-positive, or if vertexData is provided and its size does not match the number of buffers in vertexLayout

getMeshUsage

Added in 1.1.0-alpha01
public final @NonNull MeshBuffer.MeshUsage getMeshUsage()

MeshUsage indicating the usage mode and mutability of this buffer

getVertexLayout

Added in 1.0.0
public final @NonNull VertexLayout getVertexLayout()

VertexLayout describing the structure of the vertex data

updateIndexData

Added in 1.1.0-alpha01
@MainThread
public final void updateIndexData(
    @NonNull ByteBufferRegion indexData,
    @IntRange(from = 0) int offsetInBytes
)

Updates index data in the MeshBuffer.

This operation is only allowed if MeshBuffer.meshUsage is MeshUsage.DYNAMIC.

Parameters
@NonNull ByteBufferRegion indexData

source ByteBufferRegion containing the new 32-bit index data

@IntRange(from = 0) int offsetInBytes

starting offset in bytes in the target buffer where the data should be written

Throws
IllegalStateException

if the buffer is closed, or was not created as dynamic

IllegalArgumentException

if offsetInBytes plus indexData size exceeds the pre-allocated capacity

updateVertexData

Added in 1.1.0-alpha01
@MainThread
public final void updateVertexData(
    @IntRange(from = 0) int bufferIndex,
    @NonNull ByteBufferRegion vertexData,
    @IntRange(from = 0) int offsetInBytes
)

Updates vertex data for a specific buffer index in the MeshBuffer.

This operation is only allowed if MeshBuffer.meshUsage is MeshUsage.DYNAMIC.

Parameters
@IntRange(from = 0) int bufferIndex

index of the vertex buffer to update in VertexLayout.buffers

@NonNull ByteBufferRegion vertexData

source ByteBufferRegion containing the new vertex data

@IntRange(from = 0) int offsetInBytes

starting offset in bytes in the target buffer where the data should be written

Throws
IllegalStateException

if the buffer is closed, or was not created as dynamic

IllegalArgumentException

if bufferIndex is out of bounds, or if offsetInBytes plus vertexData size exceeds the pre-allocated capacity