# BlendState

Class · category: Graphics

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

BlendState is a descriptor that defines how output of fragment shader is written and blended
into render target. A blend state can be set on a material using [Material#blendState](https://api.playcanvas.com/engine/classes/Material.md#blendstate),
or in some cases on the graphics device using [GraphicsDevice#setBlendState](https://api.playcanvas.com/engine/classes/GraphicsDevice.md#setblendstate).

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

By default the blend state applies to all color attachments of the render target. When multiple
color attachments are used, individual attachments can be given an independent blend state using
[BlendState#setAttachment](https://api.playcanvas.com/engine/classes/BlendState.md#setattachment). This requires [GraphicsDevice#supportsIndependentBlending](https://api.playcanvas.com/engine/classes/GraphicsDevice.md#supportsindependentblending) -
on devices without support, the state of the attachment 0 is used for all attachments.

## Constructors

### constructor

```ts
new BlendState(blend?: boolean, colorOp?: number, colorSrcFactor?: number, colorDstFactor?: number, alphaOp?: number, alphaSrcFactor?: number, alphaDstFactor?: number, redWrite?: boolean, greenWrite?: boolean, blueWrite?: boolean, alphaWrite?: boolean)
```

Create a new BlendState instance.

All factor parameters can take the following values:

- [BLENDMODE_ZERO](https://api.playcanvas.com/engine/variables/BLENDMODE_ZERO.md)
- [BLENDMODE_ONE](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE.md)
- [BLENDMODE_SRC_COLOR](https://api.playcanvas.com/engine/variables/BLENDMODE_SRC_COLOR.md)
- [BLENDMODE_ONE_MINUS_SRC_COLOR](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_SRC_COLOR.md)
- [BLENDMODE_DST_COLOR](https://api.playcanvas.com/engine/variables/BLENDMODE_DST_COLOR.md)
- [BLENDMODE_ONE_MINUS_DST_COLOR](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_DST_COLOR.md)
- [BLENDMODE_SRC_ALPHA](https://api.playcanvas.com/engine/variables/BLENDMODE_SRC_ALPHA.md)
- [BLENDMODE_SRC_ALPHA_SATURATE](https://api.playcanvas.com/engine/variables/BLENDMODE_SRC_ALPHA_SATURATE.md)
- [BLENDMODE_ONE_MINUS_SRC_ALPHA](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_SRC_ALPHA.md)
- [BLENDMODE_DST_ALPHA](https://api.playcanvas.com/engine/variables/BLENDMODE_DST_ALPHA.md)
- [BLENDMODE_ONE_MINUS_DST_ALPHA](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_DST_ALPHA.md)
- [BLENDMODE_CONSTANT](https://api.playcanvas.com/engine/variables/BLENDMODE_CONSTANT.md)
- [BLENDMODE_ONE_MINUS_CONSTANT](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_CONSTANT.md)
- [BLENDMODE_SRC1_COLOR](https://api.playcanvas.com/engine/variables/BLENDMODE_SRC1_COLOR.md)
- [BLENDMODE_ONE_MINUS_SRC1_COLOR](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_SRC1_COLOR.md)
- [BLENDMODE_SRC1_ALPHA](https://api.playcanvas.com/engine/variables/BLENDMODE_SRC1_ALPHA.md)
- [BLENDMODE_ONE_MINUS_SRC1_ALPHA](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE_MINUS_SRC1_ALPHA.md)

All op parameters can take the following values:

- [BLENDEQUATION_ADD](https://api.playcanvas.com/engine/variables/BLENDEQUATION_ADD.md)
- [BLENDEQUATION_SUBTRACT](https://api.playcanvas.com/engine/variables/BLENDEQUATION_SUBTRACT.md)
- [BLENDEQUATION_REVERSE_SUBTRACT](https://api.playcanvas.com/engine/variables/BLENDEQUATION_REVERSE_SUBTRACT.md)
- [BLENDEQUATION_MIN](https://api.playcanvas.com/engine/variables/BLENDEQUATION_MIN.md)
- [BLENDEQUATION_MAX](https://api.playcanvas.com/engine/variables/BLENDEQUATION_MAX.md)

**Parameters**

- `blend` (`boolean`, optional, default `false`): Enables or disables blending. Defaults to false.
- `colorOp` (`number`, optional, default `BLENDEQUATION_ADD`): Configures color blending operation. Defaults to
  [BLENDEQUATION_ADD](https://api.playcanvas.com/engine/variables/BLENDEQUATION_ADD.md).
- `colorSrcFactor` (`number`, optional, default `BLENDMODE_ONE`): Configures source color blending factor. Defaults to
  [BLENDMODE_ONE](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE.md).
- `colorDstFactor` (`number`, optional, default `BLENDMODE_ZERO`): Configures destination color blending factor. Defaults to
  [BLENDMODE_ZERO](https://api.playcanvas.com/engine/variables/BLENDMODE_ZERO.md).
- `alphaOp` (`number`, optional): Configures alpha blending operation. Defaults to
  [BLENDEQUATION_ADD](https://api.playcanvas.com/engine/variables/BLENDEQUATION_ADD.md).
- `alphaSrcFactor` (`number`, optional): Configures source alpha blending factor. Defaults to
  [BLENDMODE_ONE](https://api.playcanvas.com/engine/variables/BLENDMODE_ONE.md).
- `alphaDstFactor` (`number`, optional): Configures destination alpha blending factor. Defaults to
  [BLENDMODE_ZERO](https://api.playcanvas.com/engine/variables/BLENDMODE_ZERO.md).
- `redWrite` (`boolean`, optional, default `true`): True to enable writing of the red channel and false otherwise.
  Defaults to true.
- `greenWrite` (`boolean`, optional, default `true`): True to enable writing of the green channel and false
  otherwise. Defaults to true.
- `blueWrite` (`boolean`, optional, default `true`): True to enable writing of the blue channel and false otherwise.
  Defaults to true.
- `alphaWrite` (`boolean`, optional, default `true`): True to enable writing of the alpha channel and false
  otherwise. Defaults to true.

## Properties

### ADDBLEND

```ts
static readonly ADDBLEND: BlendState
```

A blend state that does simple additive blending.

### ALPHABLEND

```ts
static readonly ALPHABLEND: BlendState
```

A blend state that does simple translucency using alpha channel.

### NOBLEND

```ts
static readonly NOBLEND: BlendState
```

A blend state that has blending disabled and writes to all color channels.

### NOWRITE

```ts
static readonly NOWRITE: BlendState
```

A blend state that does not write to color channels.

## Accessors

### blend

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

Gets whether blending is enabled.

### hasAttachmentOverrides

```ts
get hasAttachmentOverrides(): boolean
```

Gets whether any color attachment has been given an independent blend state using
[BlendState#setAttachment](https://api.playcanvas.com/engine/classes/BlendState.md#setattachment).

## Methods

### clearAttachment

```ts
clearAttachment(index: number): void
```

Removes the independent blend state of the specified color attachment, making it follow
attachment 0 again.

**Parameters**

- `index` (`number`): The index of the color attachment, in 1 to 7 range.

### clone

```ts
clone(): BlendState
```

Returns an identical copy of the specified blend state.

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

### copy

```ts
copy(rhs: BlendState): BlendState
```

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

**Parameters**

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

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

### equals

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

Reports whether two BlendStates are equal.

**Parameters**

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

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

### getAttachment

```ts
getAttachment(index: number, dst: BlendState): BlendState
```

Stores the blend state of the specified color attachment in the supplied blend state. When
the attachment does not have an independent blend state, the state of attachment 0 is stored.

**Parameters**

- `index` (`number`): The index of the color attachment, in 0 to 7 range.
- `dst` ([`BlendState`](https://api.playcanvas.com/engine/classes/BlendState.md)): The blend state to store the result in. This avoids allocations, as
  a single instance can be reused.

**Returns** [`BlendState`](https://api.playcanvas.com/engine/classes/BlendState.md): The supplied dst, for chaining.

### setAttachment

```ts
setAttachment(index: number, src: BlendState | null): void
```

Assigns an independent blend state to the specified color attachment. The blend state of the
supplied source is copied, and so subsequent changes to either the source or to attachment 0 do
not affect it. An attachment which has not been assigned an independent state instead follows
attachment 0.

Note that this requires [GraphicsDevice#supportsIndependentBlending](https://api.playcanvas.com/engine/classes/GraphicsDevice.md#supportsindependentblending) - on devices
without support, the state of attachment 0 is used for all attachments.

**Parameters**

- `index` (`number`): The index of the color attachment, in 1 to 7 range. Attachment 0 is
  configured using the other functions and properties of this class.
- `src` ([`BlendState`](https://api.playcanvas.com/engine/classes/BlendState.md) `| null`): The blend state to copy from, or null to make the attachment
  follow attachment 0 again.

**Example**

```ts
// attachment 1 keeps the blending of attachment 0, but does not write any channels
const state = material.blendState.clone();
const noWrite = state.clone();
noWrite.setColorWrite(false, false, false, false);
state.setAttachment(1, noWrite);
```
