---
title: "mix / lerp: WGSL and GLSL examples, uses and input limits"
description: "Linear interpolation uses a weight to move between two endpoints. Use it for blending values, colours, and positions."
url: https://typeshade.dev/reference/functions/mix/
language: en
---

# `mix`

Function

Linear interpolation uses a weight to move between two endpoints.

**Related names in other languages**

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

- HLSL: `lerp`

[Targets](https://typeshade.dev/reference/functions/mix/#targets)

## Explore the function

`y = mix(0., k, x)`

![mix graph. A curve shows the output and a highlighted point follows the selected input.](https://typeshade.dev/stills/reference-function-mix.webp)

`y` = 2.25 `y` = -0.25 `x` = 0 `x` = 1

The blue curve shows the output. The orange point marks the selected input.

Wheel to zoom; drag to pan, or rotate in 3D. Keyboard: arrows, +/-, Home to reset.

The axis labels show the displayed range. Values outside this window are not drawn.

Explore the result:

- Change k, the second endpoint, then move x, the interpolation weight.

**Full shader source**

[Open in the Playground](https://typeshade.dev/playground/)

mix-plot.shade.ts

```ts
"use typeshade";

import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";

class Uniforms extends PlotUniforms { k: f32; }
declare const u: uniform<Uniforms>;

// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {

  return mix(0., u.k, x);
}

function trajectory(t: f32): vec3 {
  return vec3(t, sample(t), 0.);
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  const flat = plot2D(sample, uv, u, false);
  if (u.plotBlend <= 0.) { return flat; }
  const space = plot3D(trajectory, uv, u, false);
  return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
```

Filled by the page every frame, with no control: `resolution`, `plotCenter`, `plotSpan`, `plotRotation`, `plotBlend`.

## When to use it

Use it for blending values, colours, and positions.

Example calculation: `mix(0., 1.0, 0.25) ≈ 0.25`

The reference result is rounded for readability. Shader output follows the precision rules of its target.

## Inputs and limits

Conditions in this example: x ∈ ℝ

Weights outside \[ 0,1 \] extrapolate beyond the endpoints unless explicitly constrained.

For shader input rules and precision, consult [WGSL](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`min`](https://typeshade.dev/reference/functions/min/)
- [`max`](https://typeshade.dev/reference/functions/max/)
- [`clamp`](https://typeshade.dev/reference/functions/clamp/)
- [`saturate`](https://typeshade.dev/reference/functions/saturate/)

**WGSL**

`mix(a, b, c)`

**GLSL ES 3.00**

`mix(a, b, c)`

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns `a * (1 - t) + b * t`.

## Syntax

```ts
declare function mix<V extends vec2 | vec3 | vec4>(a0: V, a1: V, a2: (f32)): V
declare function mix<V extends vec2 | vec3 | vec4>(a0: V, a1: V, a2: V): V
declare function mix<P0 extends number, P1 extends number, P2 extends number>(a0: P0, a1: P1, a2: P2): ScalarOf<P0, P1, P2>
declare function mix(a: vec2, b: vec2, t: number): vec2
declare function mix(a: vec3, b: vec3, t: number): vec3
declare function mix(a: vec4, b: vec4, t: number): vec4
declare function mix(a: vec2f64, b: vec2f64, t: number): vec2f64
declare function mix(a: vec3f64, b: vec3f64, t: number): vec3f64
declare function mix(a: vec4f64, b: vec4f64, t: number): vec4f64
declare function mix<T extends Numeric>(a: T, b: T, t: T): T
```

A `number` in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

`type f32 = number & { readonly [f32Tag]?: true }`

A `number` you write as a type is inferred from its uses, and `f32`, `i32` and `u32` fix a width; [the TypeScript constructs page](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## Parameters

- `a` `V``P0``vec2``vec3``vec4``vec2f64``vec3f64``vec4f64``T`
- `b` `V``P1``vec2``vec3``vec4``vec2f64``vec3f64``vec4f64``T`
- `t` `(f32)``V``P2``number``T`

## Return value

`V``ScalarOf<P0, P1, P2>``vec2``vec3``vec4``vec2f64``vec3f64``vec4f64``T`

## Description

`t` is a scalar or the same shape as `a` and `b`. `f64` vectors accept an `f32` blend factor.

Evaluated in `f64` by the CPU oracle.

## Targets

**WGSL**

`mix(a, b, c)`

**GLSL ES 3.00**

`mix(a, b, c)`

## Examples

- [`path/palette/palette.shade.ts`](https://typeshade.dev/guide/examples/palette/)
- [`path/ray-tracer/ray-tracer.shade.ts`](https://typeshade.dev/guide/examples/ray-tracer/)
- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/guide/examples/capsule-corp-namek-class/)
- [`gallery/domain-warp-twin/domain-warp-twin.shade.ts`](https://typeshade.dev/guide/examples/domain-warp-twin/)
- [`gallery/hillshade-twin/hillshade-twin.shade.ts`](https://typeshade.dev/guide/examples/hillshade-twin/)
- [`gallery/julia-twin/julia-twin.shade.ts`](https://typeshade.dev/guide/examples/julia-twin/)
- [`gallery/kaleidoscope-twin/kaleidoscope-twin.shade.ts`](https://typeshade.dev/guide/examples/kaleidoscope-twin/)
- [`gallery/ocean-twin/ocean-twin.shade.ts`](https://typeshade.dev/guide/examples/ocean-twin/)
- [`gallery/rt-renderer-class/rt-renderer-class.shade.ts`](https://typeshade.dev/guide/examples/rt-renderer-class/)
- [`gallery/starfield-twin/starfield-twin.shade.ts`](https://typeshade.dev/guide/examples/starfield-twin/)
- [`gallery/tunnel-twin/tunnel-twin.shade.ts`](https://typeshade.dev/guide/examples/tunnel-twin/)
- [`gallery/fp64-burning-ship-twin/fp64-burning-ship-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-burning-ship-twin/)
- [`gallery/fp64-cancellation-twin/fp64-cancellation-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-cancellation-twin/)
- [`gallery/fp64-checker-plane-twin/fp64-checker-plane-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-checker-plane-twin/)
- [`gallery/fp64-clock-twin/fp64-clock-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-clock-twin/)
- [`gallery/fp64-julia-twin/fp64-julia-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-julia-twin/)
- [`gallery/fp64-loran-twin/fp64-loran-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-loran-twin/)
- [`gallery/fp64-newton-twin/fp64-newton-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-newton-twin/)
- [`gallery/fp64-rtc-twin/fp64-rtc-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-rtc-twin/)
- [`gallery/fp64-sine-sweep-twin/fp64-sine-sweep-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-sine-sweep-twin/)
- [`gallery/array-methods/array-methods.shade.ts`](https://typeshade.dev/guide/examples/array-methods/)
- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)
- [`gallery/block-scope/block-scope.shade.ts`](https://typeshade.dev/guide/examples/block-scope/)
- [`gallery/class-builder/class-builder.shade.ts`](https://typeshade.dev/guide/examples/class-builder/)
- [`gallery/class-parts/class-parts.shade.ts`](https://typeshade.dev/guide/examples/class-parts/)
- [`gallery/class-syntax/class-syntax.shade.ts`](https://typeshade.dev/guide/examples/class-syntax/)
- [`gallery/closures/closures.shade.ts`](https://typeshade.dev/guide/examples/closures/)
- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/guide/examples/cube-array-gather/)
- [`gallery/cube-env/cube-env.shade.ts`](https://typeshade.dev/guide/examples/cube-env/)
- [`gallery/cutout/cutout.shade.ts`](https://typeshade.dev/guide/examples/cutout/)
- [`gallery/default-args/default-args.shade.ts`](https://typeshade.dev/guide/examples/default-args/)
- [`gallery/gradient-twin/gradient-twin.shade.ts`](https://typeshade.dev/guide/examples/gradient-twin/)
- [`gallery/higher-order/higher-order.shade.ts`](https://typeshade.dev/guide/examples/higher-order/)
- [`gallery/imported-noise/imported-noise.shade.ts`](https://typeshade.dev/guide/examples/imported-noise/)
- [`gallery/pick-composite/pick-composite.shade.ts`](https://typeshade.dev/guide/examples/pick-composite/)
- [`gallery/reference-parameters/reference-parameters.shade.ts`](https://typeshade.dev/guide/examples/reference-parameters/)
- [`gallery/rng-method/rng-method.shade.ts`](https://typeshade.dev/guide/examples/rng-method/)
- [`gallery/shadow-compare/shadow-compare.shade.ts`](https://typeshade.dev/guide/examples/shadow-compare/)
- [`gallery/textured-quad/textured-quad.shade.ts`](https://typeshade.dev/guide/examples/textured-quad/)
- [`gallery/uniform-array/uniform-array.shade.ts`](https://typeshade.dev/guide/examples/uniform-array/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#maths)
- [`mix()` in `typeshade/core/ir`](https://typeshade.dev/api/mix/)
