# MeshBuffer

Artifact: [androidx.xr.scenecore:scenecore](https://developer.android.com/jetpack/androidx/releases/xr-scenecore) [View Source](https://cs.android.com/search?q=file:androidx/xr/scenecore/MeshBuffer.kt+class:androidx.xr.scenecore.MeshBuffer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

*** ** * ** ***


```
public final class MeshBuffer implements AutoCloseable
```

<br />

*** ** * ** ***

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 `https://developer.android.com/reference/androidx/xr/scenecore/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 `https://developer.android.com/reference/java/nio/ByteBuffer.html` objects, which can be modified or released by the application after creation.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.MeshUsage` Describes the intended usage and mutability of a `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`. |

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#close()()` Closes the given `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#create(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout vertexLayout, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion> vertexData, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion indexData )` Creates a new static `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` | `@https://developer.android.com/reference/androidx/xr/runtime/RequiresSpatialApi(value = 4) @https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout vertexLayout, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int vertexCount, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int indexCount, https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion> vertexData, https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion indexData )` Creates a new dynamic `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` supporting runtime updates. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.MeshUsage` | `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#getMeshUsage()()` `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.MeshUsage` indicating the usage mode and mutability of this buffer |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout` | `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#getVertexLayout()()` `https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout` describing the structure of the vertex data |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateIndexData(androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion indexData, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int offsetInBytes )` Updates index data in the `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int bufferIndex, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion vertexData, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int offsetInBytes )` Updates vertex data for a specific buffer index in the `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`. |

## Public methods

### close

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public void close()
```

Closes the given `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`.

The `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` can be explicitly closed at anytime or garbage collected. An exception will be thrown if the `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` is used after being closed.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the resource has already been closed. |

### create

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#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 `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`.

The raw buffer data is copied immediately into rendering engine memory. The original `https://developer.android.com/reference/java/nio/ByteBuffer.html` objects can be modified or released after creation without affecting the mesh buffer.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session` | session to use for creating the MeshBuffer |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout vertexLayout` | layout of the vertices in the vertex buffer(s) |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion> vertexData` | vertex data regions, one for each buffer index in `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#create(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion indexData` | index data region containing 32-bit unsigned indices |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` | a new static `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#create(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` does not contain a buffer for each buffer index in `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#create(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)`, or if any `https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion` is empty |

### createDynamic

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#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 `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` supporting runtime updates.

The `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` and `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` parameters define the fixed maximum capacity of the underlying GPU buffers. The allocated buffer cannot be resized after creation. Any runtime updates via `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` or `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateIndexData(androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` that exceed these capacities will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session` | session to use for creating the MeshBuffer |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout vertexLayout` | layout of the vertices in the vertex buffer(s) |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int vertexCount` | fixed maximum capacity of vertices allocated in memory |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int indexCount` | fixed maximum capacity of indices allocated in memory |
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion> vertexData` | optional list of `https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion` containing initial vertex data, one for each buffer index in `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` |
| `https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion indexData` | optional `https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion` containing initial 32-bit index data |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` | a new `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer` |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` or `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` is non-positive, or if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` is provided and its size does not match the number of buffers in `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.Companion#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` |

### getMeshUsage

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

```
public final @NonNull MeshBuffer.MeshUsage getMeshUsage()
```

`https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.MeshUsage` indicating the usage mode and mutability of this buffer

### getVertexLayout

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final @NonNull VertexLayout getVertexLayout()
```

`https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout` describing the structure of the vertex data

### updateIndexData

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

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

Updates index data in the `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`.

This operation is only allowed if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#meshUsage()` is `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.MeshUsage#DYNAMIC()`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion indexData` | source `https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion` containing the new 32-bit index data |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int offsetInBytes` | starting offset in bytes in the target buffer where the data should be written |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the buffer is closed, or was not created as dynamic |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateIndexData(androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` plus `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateIndexData(androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` size exceeds the pre-allocated capacity |

### updateVertexData

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#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 `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer`.

This operation is only allowed if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#meshUsage()` is `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer.MeshUsage#DYNAMIC()`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int bufferIndex` | index of the vertex buffer to update in `https://developer.android.com/reference/androidx/xr/scenecore/VertexLayout#buffers()` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion vertexData` | source `https://developer.android.com/reference/androidx/xr/scenecore/ByteBufferRegion` containing the new vertex data |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int offsetInBytes` | starting offset in bytes in the target buffer where the data should be written |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the buffer is closed, or was not created as dynamic |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` is out of bounds, or if `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` plus `https://developer.android.com/reference/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` size exceeds the pre-allocated capacity |