# DepthState

Class · category: Graphics

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/platform/graphics/depth-state.js#L26

DepthState is a descriptor that defines how the depth value of the fragment is used by the
rendering pipeline. A depth state can be set on a material using [Material#depthState](https://api.playcanvas.com/engine/classes/Material.md#depthstate),
or in some cases on the graphics device using [GraphicsDevice#setDepthState](https://api.playcanvas.com/engine/classes/GraphicsDevice.md#setdepthstate).

For the best performance, do not modify depth state after it has been created, but create
multiple depth states and assign them to the material or graphics device as needed.

## Constructors

### constructor

```ts
new DepthState(func?: number, write?: boolean)
```

Create a new Depth State instance.

**Parameters**

- `func` (`number`, optional, default `FUNC_LESSEQUAL`): Controls how the depth of the fragment is compared against the
  current depth contained in the depth buffer. See [DepthState#func](https://api.playcanvas.com/engine/classes/DepthState.md#func) for details.
  Defaults to [FUNC_LESSEQUAL](https://api.playcanvas.com/engine/variables/FUNC_LESSEQUAL.md).
- `write` (`boolean`, optional, default `true`): If true, depth values are written to the depth buffer of the
  currently active render target. Defaults to true.

## Properties

### key

```ts
key: number = 0
```

A unique number representing the depth state. You can use this number to quickly compare
two depth states for equality. The key is always maintained valid without a dirty flag,
to avoid condition check at runtime, considering these change rarely.

### DEFAULT

```ts
static readonly DEFAULT: DepthState
```

A default depth state that has the depth testing function set to [FUNC_LESSEQUAL](https://api.playcanvas.com/engine/variables/FUNC_LESSEQUAL.md) and
depth writes enabled.

### NODEPTH

```ts
static readonly NODEPTH: DepthState
```

A depth state that always passes the fragment but does not write depth to the depth buffer.

### WRITEDEPTH

```ts
static readonly WRITEDEPTH: DepthState
```

A depth state that always passes the fragment and writes depth to the depth buffer.

## Accessors

### depthBias

```ts
get depthBias(): number
set depthBias(value: number)
```

Gets the constant depth bias added to each fragment's depth.

### depthBiasSlope

```ts
get depthBiasSlope(): number
set depthBiasSlope(value: number)
```

Gets the depth bias that scales with the fragment's slope.

### func

```ts
get func(): number
set func(value: number)
```

Gets the depth testing function.

### test

```ts
get test(): boolean
set test(value: boolean)
```

Gets whether depth testing is performed.

### write

```ts
get write(): boolean
set write(value: boolean)
```

Gets whether depth writing is performed.

## Methods

### clone

```ts
clone(): DepthState
```

Returns an identical copy of the specified depth state.

**Returns** [`DepthState`](https://api.playcanvas.com/engine/classes/DepthState.md): The result of the cloning.

### copy

```ts
copy(rhs: DepthState): DepthState
```

Copies the contents of a source depth state to this depth state.

**Parameters**

- `rhs` ([`DepthState`](https://api.playcanvas.com/engine/classes/DepthState.md)): A depth state to copy from.

**Returns** [`DepthState`](https://api.playcanvas.com/engine/classes/DepthState.md): Self for chaining.

### equals

```ts
equals(rhs: DepthState): boolean
```

Reports whether two DepthStates are equal.

**Parameters**

- `rhs` ([`DepthState`](https://api.playcanvas.com/engine/classes/DepthState.md)): The depth state to compare to.

**Returns** `boolean`: True if the depth states are equal and false otherwise.
