# Mesh

Class · extends [`RefCountedObject`](https://api.playcanvas.com/engine/classes/RefCountedObject.md) · category: Graphics

Source: https://github.com/playcanvas/engine/blob/b5b983982a9860d21e0c1dafb2f85f72e2c01afb/src/scene/mesh.js#L196

A graphical primitive. The mesh is defined by a [VertexBuffer](https://api.playcanvas.com/engine/classes/VertexBuffer.md) and an optional
[IndexBuffer](https://api.playcanvas.com/engine/classes/IndexBuffer.md). It also contains a primitive definition which controls the type of the
primitive and the portion of the vertex or index buffer to use.

## Mesh APIs
There are two ways a mesh can be generated or updated.

### Simple Mesh API
[Mesh](https://api.playcanvas.com/engine/classes/Mesh.md) class provides interfaces such as [setPositions](https://api.playcanvas.com/engine/classes/Mesh.md#setpositions) and [setUvs](https://api.playcanvas.com/engine/classes/Mesh.md#setuvs) that
provide a simple way to provide vertex and index data for the Mesh, and hiding the complexity
of creating the [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md). This is the recommended interface to use.

A simple example which creates a Mesh with 3 vertices, containing position coordinates only, to
form a single triangle.

```javascript
const mesh = new Mesh(device);
const positions = [
    0, 0, 0, // pos 0
    1, 0, 0, // pos 1
    1, 1, 0  // pos 2
];
mesh.setPositions(positions);
mesh.update();
```

An example which creates a Mesh with 4 vertices, containing position and uv coordinates in
channel 0, and an index buffer to form two triangles. Float32Array is used for positions and uvs.

```javascript
const mesh = new Mesh(device);
const positions = new Float32Array([
    0, 0, 0, // pos 0
    1, 0, 0, // pos 1
    1, 1, 0, // pos 2
    0, 1, 0  // pos 3
]);
const uvs = new Float32Array([
    0, 1  // uv 3
    1, 1, // uv 2
    1, 0, // uv 1
    0, 0, // uv 0
]);
const indices = [
    0, 1, 2, // triangle 0
    0, 2, 3  // triangle 1
];
mesh.setPositions(positions);
mesh.setNormals(calculateNormals(positions, indices));
mesh.setUvs(0, uvs);
mesh.setIndices(indices);
mesh.update();
```

This example demonstrates that vertex attributes such as position and normals, and also indices
can be provided using Arrays ([]) and also Typed Arrays (Float32Array and similar). Note that
typed arrays have higher performance, and are generally recommended for per-frame operations or
larger meshes, but their construction using new operator is costly operation. If you only need
to operate on a small number of vertices or indices, consider using Arrays to avoid the overhead
associated with allocating Typed Arrays.

Follow these links for more complex examples showing the functionality.

- [https://playcanvas.github.io/#graphics/mesh-decals](https://playcanvas.github.io/#graphics/mesh-decals)
- [https://playcanvas.github.io/#graphics/mesh-deformation](https://playcanvas.github.io/#graphics/mesh-deformation)
- [https://playcanvas.github.io/#graphics/mesh-generation](https://playcanvas.github.io/#graphics/mesh-generation)
- [https://playcanvas.github.io/#graphics/point-cloud-simulation](https://playcanvas.github.io/#graphics/point-cloud-simulation)

### Update Vertex and Index buffers
This allows greater flexibility, but is more complex to use. It allows more advanced setups, for
example sharing a Vertex or Index Buffer between multiple meshes. See [VertexBuffer](https://api.playcanvas.com/engine/classes/VertexBuffer.md),
[IndexBuffer](https://api.playcanvas.com/engine/classes/IndexBuffer.md) and [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md) for details.

## Constructors

### constructor

```ts
new Mesh(graphicsDevice: GraphicsDevice, options?: object)
```

Create a new Mesh instance.

**Parameters**

- `graphicsDevice` ([`GraphicsDevice`](https://api.playcanvas.com/engine/classes/GraphicsDevice.md)): The graphics device used to manage this mesh.
- `options` (`object`, optional): Object for passing optional arguments.
    - `options.storageIndex` (`boolean`, optional): Defines if the index buffer can be used as
      a storage buffer by a compute shader. Defaults to false. Only supported on WebGPU.
    - `options.storageVertex` (`boolean`, optional): Defines if the vertex buffer can be used as
      a storage buffer by a compute shader. Defaults to false. Only supported on WebGPU.

## Properties

### indexBuffer

```ts
indexBuffer: IndexBuffer[]
```

An array of index buffers. For unindexed meshes, this array can be empty. The first index
buffer in the array is used by [MeshInstance](https://api.playcanvas.com/engine/classes/MeshInstance.md)s with a `renderStyle` property set to
[RENDERSTYLE_SOLID](https://api.playcanvas.com/engine/variables/RENDERSTYLE_SOLID.md). The second index buffer in the array is used if `renderStyle` is
set to [RENDERSTYLE_WIREFRAME](https://api.playcanvas.com/engine/variables/RENDERSTYLE_WIREFRAME.md).

### primitive

```ts
primitive: { base: number; baseVertex: number; count: number; indexed?: boolean; type: number }[]
```

Array of primitive objects defining how vertex (and index) data in the mesh should be
interpreted by the graphics device.

- `type` is the type of primitive to render. Can be:

  - [PRIMITIVE_POINTS](https://api.playcanvas.com/engine/variables/PRIMITIVE_POINTS.md)
  - [PRIMITIVE_LINES](https://api.playcanvas.com/engine/variables/PRIMITIVE_LINES.md)
  - [PRIMITIVE_LINELOOP](https://api.playcanvas.com/engine/variables/PRIMITIVE_LINELOOP.md)
  - [PRIMITIVE_LINESTRIP](https://api.playcanvas.com/engine/variables/PRIMITIVE_LINESTRIP.md)
  - [PRIMITIVE_TRIANGLES](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRIANGLES.md)
  - [PRIMITIVE_TRISTRIP](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRISTRIP.md)
  - [PRIMITIVE_TRIFAN](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRIFAN.md)

- `base` is the offset of the first index or vertex to dispatch in the draw call.
- `baseVertex` is the number added to each index value before indexing into the vertex buffers. (supported only in WebGPU, ignored in WebGL2)
- `count` is the number of indices or vertices to dispatch in the draw call.
- `indexed` specifies whether to interpret the primitive as indexed, thereby using the
currently set index buffer.

### skin

```ts
skin: Skin | null = null
```

The skin data (if any) that drives skinned mesh animations for this mesh.

### vertexBuffer

```ts
vertexBuffer: VertexBuffer = null
```

The vertex buffer holding the vertex data of the mesh.

## Accessors

### aabb

```ts
get aabb(): BoundingBox
set aabb(aabb: BoundingBox)
```

Gets the axis-aligned bounding box for the object space vertices of this mesh.

### morph

```ts
get morph(): Morph | null
set morph(morph: Morph | null)
```

Gets the morph data that drives morph target animations for this mesh.

## Methods

### clear

```ts
clear(verticesDynamic?: boolean, indicesDynamic?: boolean, maxVertices?: number, maxIndices?: number): void
```

Clears the mesh of existing vertices and indices and resets the [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md)
associated with the mesh. This call is typically followed by calls to methods such as
[setPositions](https://api.playcanvas.com/engine/classes/Mesh.md#setpositions), [setVertexStream](https://api.playcanvas.com/engine/classes/Mesh.md#setvertexstream) or [setIndices](https://api.playcanvas.com/engine/classes/Mesh.md#setindices) and finally
[update](https://api.playcanvas.com/engine/classes/Mesh.md#update) to rebuild the mesh, allowing different [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md).

**Parameters**

- `verticesDynamic` (`boolean`, optional): Indicates the [VertexBuffer](https://api.playcanvas.com/engine/classes/VertexBuffer.md) should be created
  with [BUFFER_DYNAMIC](https://api.playcanvas.com/engine/variables/BUFFER_DYNAMIC.md) usage. If not specified, [BUFFER_STATIC](https://api.playcanvas.com/engine/variables/BUFFER_STATIC.md) is used.
- `indicesDynamic` (`boolean`, optional): Indicates the [IndexBuffer](https://api.playcanvas.com/engine/classes/IndexBuffer.md) should be created with
  [BUFFER_DYNAMIC](https://api.playcanvas.com/engine/variables/BUFFER_DYNAMIC.md) usage. If not specified, [BUFFER_STATIC](https://api.playcanvas.com/engine/variables/BUFFER_STATIC.md) is used.
- `maxVertices` (`number`, optional, default `0`): A [VertexBuffer](https://api.playcanvas.com/engine/classes/VertexBuffer.md) will be allocated with at least
  maxVertices, allowing additional vertices to be added to it without the allocation. If no
  value is provided, a size to fit the provided vertices will be allocated.
- `maxIndices` (`number`, optional, default `0`): An [IndexBuffer](https://api.playcanvas.com/engine/classes/IndexBuffer.md) will be allocated with at least
  maxIndices, allowing additional indices to be added to it without the allocation. If no
  value is provided, a size to fit the provided indices will be allocated.

### destroy

```ts
destroy(): void
```

Destroys the [VertexBuffer](https://api.playcanvas.com/engine/classes/VertexBuffer.md) and [IndexBuffer](https://api.playcanvas.com/engine/classes/IndexBuffer.md)s associated with the mesh. This is
normally called by [Model#destroy](https://api.playcanvas.com/engine/classes/Model.md#destroy) and does not need to be called manually.

### getColors

```ts
getColors(colors: NumericArray): number
```

Gets the vertex color data.

**Parameters**

- `colors` ([`NumericArray`](https://api.playcanvas.com/engine/types/NumericArray.md)): An array to populate with the vertex data. When
  typed array is supplied, enough space needs to be reserved, otherwise only partial data is
  copied.

**Returns** `number`: Returns the number of vertices populated.

### getIndices

```ts
getIndices(indices: number[] | Uint8Array<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>): number
```

Gets the index data.

**Parameters**

- `indices` (`number[] | Uint8Array<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>`): An array to populate with the
  index data. When a typed array is supplied, enough space needs to be reserved, otherwise
  only partial data is copied.

**Returns** `number`: Returns the number of indices populated.

### getNormals

```ts
getNormals(normals: NumericArray): number
```

Gets the vertex normals data.

**Parameters**

- `normals` ([`NumericArray`](https://api.playcanvas.com/engine/types/NumericArray.md)): An array to populate with the vertex data. When
  typed array is supplied, enough space needs to be reserved, otherwise only partial data is
  copied.

**Returns** `number`: Returns the number of vertices populated.

### getPositions

```ts
getPositions(positions: NumericArray): number
```

Gets the vertex positions data.

**Parameters**

- `positions` ([`NumericArray`](https://api.playcanvas.com/engine/types/NumericArray.md)): An array to populate with the vertex data.
  When typed array is supplied, enough space needs to be reserved, otherwise only partial data
  is copied.

**Returns** `number`: Returns the number of vertices populated.

### getUvs

```ts
getUvs(channel: number, uvs: NumericArray): number
```

Gets the vertex uv data.

**Parameters**

- `channel` (`number`): The uv channel in [0..7] range.
- `uvs` ([`NumericArray`](https://api.playcanvas.com/engine/types/NumericArray.md)): An array to populate with the vertex data. When
  typed array is supplied, enough space needs to be reserved, otherwise only partial data is
  copied.

**Returns** `number`: Returns the number of vertices populated.

### getVertexStream

```ts
getVertexStream(semantic: string, data: NumericArray): number
```

Gets the vertex data corresponding to a semantic.

**Parameters**

- `semantic` (`string`): The semantic of the vertex element to get. For supported
  semantics, see SEMANTIC_* in [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md).
- `data` ([`NumericArray`](https://api.playcanvas.com/engine/types/NumericArray.md)): An array to populate with the vertex data. When
  typed array is supplied, enough space needs to be reserved, otherwise only partial data is
  copied.

**Returns** `number`: Returns the number of vertices populated.

### setColors

```ts
setColors(colors: ArrayLike<number>, componentCount?: number, numVertices?: number): void
```

Sets the vertex color array. Colors are stored using [TYPE_FLOAT32](https://api.playcanvas.com/engine/variables/TYPE_FLOAT32.md) format, which is
useful for HDR colors.

**Parameters**

- `colors` (`ArrayLike<number>`): Vertex data containing colors.
- `componentCount` (`number`, optional, default `GeometryData.DEFAULT_COMPONENTS_COLORS`): The number of values that form a single color element.
  Defaults to 4 if not specified, corresponding to r, g, b and a.
- `numVertices` (`number`, optional): The number of vertices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.

### setColors32

```ts
setColors32(colors: ArrayLike<number>, numVertices?: number): void
```

Sets the vertex color array. Colors are stored using [TYPE_UINT8](https://api.playcanvas.com/engine/variables/TYPE_UINT8.md) format, which is
useful for LDR colors. Values in the array are expected in [0..255] range, and are mapped to
[0..1] range in the shader.

**Parameters**

- `colors` (`ArrayLike<number>`): Vertex data containing colors. The array is
  expected to contain 4 components per vertex, corresponding to r, g, b and a.
- `numVertices` (`number`, optional): The number of vertices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.

### setIndices

```ts
setIndices(indices: number[] | Uint8Array<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>, numIndices?: number): void
```

Sets the index array. Indices are stored using 16-bit format by default, unless more than
65535 vertices are specified, in which case 32-bit format is used.

**Parameters**

- `indices` (`number[] | Uint8Array<ArrayBufferLike> | Uint16Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>`): The array of indices that
  define primitives (lines, triangles, etc.).
- `numIndices` (`number`, optional): The number of indices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.

### setNormals

```ts
setNormals(normals: ArrayLike<number>, componentCount?: number, numVertices?: number): void
```

Sets the vertex normals array. Normals are stored using [TYPE_FLOAT32](https://api.playcanvas.com/engine/variables/TYPE_FLOAT32.md) format.

**Parameters**

- `normals` (`ArrayLike<number>`): Vertex data containing normals.
- `componentCount` (`number`, optional, default `GeometryData.DEFAULT_COMPONENTS_NORMAL`): The number of values that form a single normal element.
  Defaults to 3 if not specified, corresponding to x, y and z direction.
- `numVertices` (`number`, optional): The number of vertices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.

### setPositions

```ts
setPositions(positions: ArrayLike<number>, componentCount?: number, numVertices?: number): void
```

Sets the vertex positions array. Vertices are stored using [TYPE_FLOAT32](https://api.playcanvas.com/engine/variables/TYPE_FLOAT32.md) format.

**Parameters**

- `positions` (`ArrayLike<number>`): Vertex data containing positions.
- `componentCount` (`number`, optional, default `GeometryData.DEFAULT_COMPONENTS_POSITION`): The number of values that form a single position element.
  Defaults to 3 if not specified, corresponding to x, y and z coordinates.
- `numVertices` (`number`, optional): The number of vertices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.

### setUvs

```ts
setUvs(channel: number, uvs: ArrayLike<number>, componentCount?: number, numVertices?: number): void
```

Sets the vertex uv array. Uvs are stored using [TYPE_FLOAT32](https://api.playcanvas.com/engine/variables/TYPE_FLOAT32.md) format.

**Parameters**

- `channel` (`number`): The uv channel in [0..7] range.
- `uvs` (`ArrayLike<number>`): Vertex data containing uv-coordinates.
- `componentCount` (`number`, optional, default `GeometryData.DEFAULT_COMPONENTS_UV`): The number of values that form a single uv element.
  Defaults to 2 if not specified, corresponding to u and v coordinates.
- `numVertices` (`number`, optional): The number of vertices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.

### setVertexStream

```ts
setVertexStream(semantic: string, data: ArrayLike<number>, componentCount: number, numVertices?: number, dataType?: number, dataTypeNormalize?: boolean, asInt?: boolean): void
```

Sets the vertex data for any supported semantic.

**Parameters**

- `semantic` (`string`): The meaning of the vertex element. For supported semantics, see
  SEMANTIC_* in [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md).
- `data` (`ArrayLike<number>`): Vertex data for the specified semantic.
- `componentCount` (`number`): The number of values that form a single Vertex element. For
  example when setting a 3D position represented by 3 numbers per vertex, number 3 should be
  specified.
- `numVertices` (`number`, optional): The number of vertices to be used from data array. If not
  provided, the whole data array is used. This allows to use only part of the data array.
- `dataType` (`number`, optional, default `TYPE_FLOAT32`): The format of data when stored in the [VertexBuffer](https://api.playcanvas.com/engine/classes/VertexBuffer.md), see
  TYPE_* in [VertexFormat](https://api.playcanvas.com/engine/classes/VertexFormat.md). When not specified, [TYPE_FLOAT32](https://api.playcanvas.com/engine/variables/TYPE_FLOAT32.md) is used.
- `dataTypeNormalize` (`boolean`, optional, default `false`): If true, vertex attribute data will be mapped from a
  0 to 255 range down to 0 to 1 when fed to a shader. If false, vertex attribute data is left
  unchanged. If this property is unspecified, false is assumed.
- `asInt` (`boolean`, optional, default `false`): If true, vertex attribute data will be accessible as integer
  numbers in shader code. Defaults to false, which means that vertex attribute data will be
  accessible as floating point numbers. Can be only used with INT and UINT data types.

### update

```ts
update(primitiveType?: number, updateBoundingBox?: boolean): void
```

Applies any changes to vertex stream and indices to mesh. This allocates or reallocates
[vertexBuffer](https://api.playcanvas.com/engine/classes/Mesh.md#vertexbuffer) or [indexBuffer](https://api.playcanvas.com/engine/classes/Mesh.md#indexbuffer) to fit all provided vertices and indices, and
fills them with data.

**Parameters**

- `primitiveType` (`number`, optional, default `PRIMITIVE_TRIANGLES`): The type of primitive to render. Can be:

  - [PRIMITIVE_POINTS](https://api.playcanvas.com/engine/variables/PRIMITIVE_POINTS.md)
  - [PRIMITIVE_LINES](https://api.playcanvas.com/engine/variables/PRIMITIVE_LINES.md)
  - [PRIMITIVE_LINELOOP](https://api.playcanvas.com/engine/variables/PRIMITIVE_LINELOOP.md)
  - [PRIMITIVE_LINESTRIP](https://api.playcanvas.com/engine/variables/PRIMITIVE_LINESTRIP.md)
  - [PRIMITIVE_TRIANGLES](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRIANGLES.md)
  - [PRIMITIVE_TRISTRIP](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRISTRIP.md)
  - [PRIMITIVE_TRIFAN](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRIFAN.md)

  Defaults to [PRIMITIVE_TRIANGLES](https://api.playcanvas.com/engine/variables/PRIMITIVE_TRIANGLES.md) if not specified.
- `updateBoundingBox` (`boolean`, optional, default `true`): True to update bounding box. Bounding box is updated
  only if positions were set since last time update was called, and `componentCount` for
  position was 3, otherwise bounding box is not updated. See [setPositions](https://api.playcanvas.com/engine/classes/Mesh.md#setpositions). Defaults to
  true if not specified. Set this to false to avoid update of the bounding box and use aabb
  property to set it instead.

### fromGeometry

```ts
static fromGeometry(graphicsDevice: GraphicsDevice, geometry: Geometry, options?: object): Mesh
```

Create a new Mesh instance from [Geometry](https://api.playcanvas.com/engine/classes/Geometry.md) object.

**Parameters**

- `graphicsDevice` ([`GraphicsDevice`](https://api.playcanvas.com/engine/classes/GraphicsDevice.md)): The graphics device used to manage this mesh.
- `geometry` ([`Geometry`](https://api.playcanvas.com/engine/classes/Geometry.md)): The geometry object to create the mesh from.
- `options` (`object`, optional, default `{}`): An object that specifies optional inputs for the function as follows:
    - `options.storageIndex` (`boolean`, optional): Defines if the index buffer of the mesh can be used as
      a storage buffer by a compute shader. Defaults to false. Only supported on WebGPU.
    - `options.storageVertex` (`boolean`, optional): Defines if the vertex buffer of the mesh can be used as
      a storage buffer by a compute shader. Defaults to false. Only supported on WebGPU.

**Returns** [`Mesh`](https://api.playcanvas.com/engine/classes/Mesh.md): A new mesh.

## Inherited from [RefCountedObject](https://api.playcanvas.com/engine/classes/RefCountedObject.md)

- `get refCount(): number`
- `decRefCount(): void`
- `incRefCount(): void`
