# 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)

*** ** * ** ***


```
class MeshBuffer : 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/kotlin/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 |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer.MeshUsage` Describes the intended usage and mutability of a `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer`. |

| ### Public companion functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#create(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)( session: https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session, vertexLayout: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/VertexLayout, vertexData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion>, indexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion )` Creates a new static `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer`. |
| `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer` | `@https://developer.android.com/reference/kotlin/androidx/xr/runtime/RequiresSpatialApi(value = 4) @https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#createDynamic(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.Int,kotlin.Int,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)( session: https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session, vertexLayout: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/VertexLayout, vertexCount: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 1) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, indexCount: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 1) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, vertexData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion?>?, indexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion? )` Creates a new dynamic `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer` supporting runtime updates. |

| ### Public functions |
|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#close()()` Closes the given `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#updateIndexData(androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)( indexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion, offsetInBytes: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Updates index data in the `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)( bufferIndex: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, vertexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion, offsetInBytes: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Updates vertex data for a specific buffer index in the `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer`. |

| ### Public properties |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer.MeshUsage` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#meshUsage()` `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer.MeshUsage` indicating the usage mode and mutability of this buffer |
| `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/VertexLayout` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#vertexLayout()` `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/VertexLayout` describing the structure of the vertex data |

## Public companion functions

### create

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

```
@MainThread
fun create(
    session: Session,
    vertexLayout: VertexLayout,
    vertexData: List<ByteBufferRegion>,
    indexData: ByteBufferRegion
): MeshBuffer
```

Creates a new static `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `session: https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session` | session to use for creating the MeshBuffer |
| `vertexLayout: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/VertexLayout` | layout of the vertices in the vertex buffer(s) |
| `vertexData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion>` | vertex data regions, one for each buffer index in `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer.Companion#create(androidx.xr.runtime.Session,androidx.xr.scenecore.VertexLayout,kotlin.collections.List,androidx.xr.scenecore.ByteBufferRegion)` |
| `indexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion` | index data region containing 32-bit unsigned indices |

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

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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
fun createDynamic(
    session: Session,
    vertexLayout: VertexLayout,
    vertexCount: @IntRange(from = 1) Int,
    indexCount: @IntRange(from = 1) Int,
    vertexData: List<ByteBufferRegion?>? = null,
    indexData: ByteBufferRegion? = null
): MeshBuffer
```

Creates a new dynamic `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer` supporting runtime updates.

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

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

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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/kotlin/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)` |

## Public functions

### close

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

```
@MainThread
open fun close(): Unit
```

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

The `https://developer.android.com/reference/kotlin/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/kotlin/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. |

### updateIndexData

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

```
@MainThread
fun updateIndexData(
    indexData: ByteBufferRegion,
    offsetInBytes: @IntRange(from = 0) Int = 0
): Unit
```

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

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

| Parameters |
|---|---|
| `indexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion` | source `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion` containing the new 32-bit index data |
| `offsetInBytes: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | 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/kotlin/androidx/xr/scenecore/MeshBuffer#updateIndexData(androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` plus `https://developer.android.com/reference/kotlin/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
fun updateVertexData(
    bufferIndex: @IntRange(from = 0) Int,
    vertexData: ByteBufferRegion,
    offsetInBytes: @IntRange(from = 0) Int = 0
): Unit
```

Updates vertex data for a specific buffer index in the `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer`.

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

| Parameters |
|---|---|
| `bufferIndex: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | index of the vertex buffer to update in `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/VertexLayout#buffers()` |
| `vertexData: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion` | source `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ByteBufferRegion` containing the new vertex data |
| `offsetInBytes: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | 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/kotlin/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` is out of bounds, or if `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` plus `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/MeshBuffer#updateVertexData(kotlin.Int,androidx.xr.scenecore.ByteBufferRegion,kotlin.Int)` size exceeds the pre-allocated capacity |

## Public properties

### meshUsage

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

```
val meshUsage: MeshBuffer.MeshUsage
```

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

### vertexLayout

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

```
val vertexLayout: VertexLayout
```

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