---
title: "Math"
description: "The arithmetic you write your own paint with."
group: "space"
groupTitle: "Shapes, frames & warps"
categoryId: "math"
categorySlug: "math"
wordCount: 47
curated: true
sourceUrl: "https://github.com/shader-effects-inc/shaders/blob/main/packages/core/src/std/math.ts"
editUrl: "https://github.com/shader-effects-inc/shaders/edit/main/packages/core/docs/std/math.md"
---

# Math

The arithmetic you write your own paint with. Every word takes expressions or plain numbers
and returns an expression the GPU works out once per pixel: `mul(color, coverage)` fades a
color, `smoothstep(a, b, dist)` turns a distance into a soft mask, `mix(a, b, t)` blends two
colors. Props reach you as `params.uniforms.<name>` (or `uniformOf(p('name'), params)`), a
frame gives you the coordinate, and the words compose by nesting until you return a [`vec4`](/docs/primitives/math/vec4)
color from `paint:`. Wrap anything you read twice in [`local`](/docs/primitives/math/local) so it is computed once.

```ts
import {math} from 'shaders/std'
```

## Reach for it when

| When you need | Use |
|---|---|
| combining numbers, colors or coordinates | [`add`](/docs/primitives/math/add), [`sub`](/docs/primitives/math/sub), [`mul`](/docs/primitives/math/mul), [`div`](/docs/primitives/math/div) |
| blending two colors or values by an amount | [`mix`](/docs/primitives/math/mix) with [`splat3`](/docs/primitives/math/splat3) for colors |
| a soft edge from a distance | [`smoothstep`](/docs/primitives/math/smoothstep); [`step`](/docs/primitives/math/step) for a hard one |
| keeping a value in range | [`clamp`](/docs/primitives/math/clamp), [`min`](/docs/primitives/math/min), [`max`](/docs/primitives/math/max) |
| a value you read more than once | [`local`](/docs/primitives/math/local) |
| the distance from a center or between points | [`length`](/docs/primitives/math/length), [`distance`](/docs/primitives/math/distance) |
| a pattern that repeats across the canvas | [`fract`](/docs/primitives/math/fract) and [`floor`](/docs/primitives/math/floor) on a scaled coordinate |
| a wave or a rotation over time | [`sin`](/docs/primitives/math/sin), [`cos`](/docs/primitives/math/cos), [`rotate2`](/docs/primitives/math/rotate2) |
| an angle around a center | [`atan2`](/docs/primitives/math/atan2) |
| a smooth falloff or a bell shape | [`exp`](/docs/primitives/math/exp), [`gaussBell`](/docs/primitives/math/gaussbell) |
| a hinge that bends instead of creasing | [`softPlus`](/docs/primitives/math/softplus) |
| picking one of two values per pixel | [`select`](/docs/primitives/math/select) with [`lt`](/docs/primitives/math/lt), [`gt`](/docs/primitives/math/gt), [`le`](/docs/primitives/math/le), [`ge`](/docs/primitives/math/ge) |
| several motions that must not move in step | [`hashPhase`](/docs/primitives/math/hashphase) |
| the final color | [`vec4`](/docs/primitives/math/vec4) from a [`vec3`](/docs/primitives/math/vec3) and an alpha |

## Example

```ts
import {defineShader, p, uniformOf, animatedTime, math, frames, transformColor, transformPosition} from 'shaders/std'

const {add, sub, mul, sin, smoothstep, mix, splat3, local, vec4} = math

// Rings: concentric waves spreading from a draggable center, blended between two colors.
export const Rings = defineShader({
  name: 'Rings',
  animatedTime: {speed: 'speed'},
  props: {
    center: {default: {x: 0.5, y: 0.5}, transform: transformPosition},
    colorA: {default: '#0b1026', transform: transformColor},
    colorB: {default: '#7dd3fc', transform: transformColor},
    frequency: {default: 24},
    softness: {default: 0.25},
    speed: {default: 1},
  },
  paint: (params) => {
    const u = (name: string) => uniformOf(p(name), params)
    const {dist} = frames.centredFrame({center: p('center')})(params, frames.surfaceOf(params))
    // A −1..1 wave along the distance, moved into 0..1 and computed once.
    const wave = local(add(mul(sin(sub(mul(dist, u('frequency')), animatedTime(params))), 0.5), 0.5), 'wave')
    // softness 0 is a hard ring, 0.5 a smooth gradient between rings.
    const band = smoothstep(sub(0.5, u('softness')), add(0.5, u('softness')), wave)
    const rgb = mix(u('colorA').member('rgb'), u('colorB').member('rgb'), splat3(band))
    return vec4(rgb, 1)
  },
})
```

## Words

- [`add`](/docs/primitives/math/add) — The sum of two values.
- [`sub`](/docs/primitives/math/sub) — The first value minus the second.
- [`mul`](/docs/primitives/math/mul) — The product of two values.
- [`div`](/docs/primitives/math/div) — The first value divided by the second.
- [`mix`](/docs/primitives/math/mix) — A blend from `a` to `b` by `t`, where 0 gives `a` and 1 gives `b`.
- [`smoothstep`](/docs/primitives/math/smoothstep) — A soft edge: 0 below `e0`, 1 above `e1`, an S-curve between.
- [`clamp`](/docs/primitives/math/clamp) — The value limited to the range `lo`..`hi`.
- [`local`](/docs/primitives/math/local) — A value computed once per pixel and shared by everything that reads it.
- [`vec4`](/docs/primitives/math/vec4) — A 4D vector, usually the final color with alpha.
- [`vec3`](/docs/primitives/math/vec3) — A 3D vector from three values, often a color without alpha.
- [`vec2`](/docs/primitives/math/vec2) — A 2D vector from two values.
- [`splat3`](/docs/primitives/math/splat3) — One number copied into all three channels of a vec3.
- [`length`](/docs/primitives/math/length) — The length of a vector.
- [`distance`](/docs/primitives/math/distance) — The distance between two points.
- [`abs`](/docs/primitives/math/abs) — The value without its sign.
- [`fract`](/docs/primitives/math/fract) — The fractional part of a value, 0..1.
- [`floor`](/docs/primitives/math/floor) — The value rounded down to a whole number.
- [`min`](/docs/primitives/math/min) — The smaller of two values.
- [`max`](/docs/primitives/math/max) — The larger of two values.
- [`sin`](/docs/primitives/math/sin) — The sine of an angle in radians, −1..1.
- [`cos`](/docs/primitives/math/cos) — The cosine of an angle in radians, −1..1.
- [`step`](/docs/primitives/math/step) — A hard edge: 0 when `x` is below `edge`, 1 from `edge` on.
- [`pow`](/docs/primitives/math/pow) — The first value raised to the power of the second.
- [`exp`](/docs/primitives/math/exp) — e raised to the value.
- [`sqrt`](/docs/primitives/math/sqrt) — The square root of a value.
- [`neg`](/docs/primitives/math/neg) — The value with its sign flipped.
- [`select`](/docs/primitives/math/select) — `ifTrue` when the condition holds, otherwise `ifFalse`.
- [`lt`](/docs/primitives/math/lt) — True when `a` is less than `b`.
- [`gt`](/docs/primitives/math/gt) — True when `a` is greater than `b`.
- [`le`](/docs/primitives/math/le) — True when `a` is less than or equal to `b`.
- [`ge`](/docs/primitives/math/ge) — True when `a` is greater than or equal to `b`.
- [`atan2`](/docs/primitives/math/atan2) — The angle of the point (x, y) in radians, −π..π.
- [`dot`](/docs/primitives/math/dot) — The dot product of two vectors.
- [`normalize`](/docs/primitives/math/normalize) — The vector scaled to length 1.
- [`rotate2`](/docs/primitives/math/rotate2) — A 2D point rotated around the origin.
- [`gaussBell`](/docs/primitives/math/gaussbell) — A bell curve: 1 at zero, falling toward 0 on both sides.
- [`softPlus`](/docs/primitives/math/softplus) — A soft hinge: about 0 for values well below zero, about `x` above, rounded near zero.
- [`hashPhase`](/docs/primitives/math/hashphase) — A random but stable angle in radians, 0..2π, from a seed.
- [`sign`](/docs/primitives/math/sign) — −1, 0 or 1 by the sign of the value.
- [`ceil`](/docs/primitives/math/ceil) — The value rounded up to a whole number.
- [`log`](/docs/primitives/math/log) — The natural logarithm of a value.
- [`exp2`](/docs/primitives/math/exp2) — 2 raised to the value.
- [`tan`](/docs/primitives/math/tan) — The tangent of an angle in radians.
- [`cross`](/docs/primitives/math/cross) — The cross product of two 3D vectors.
- [`reflect`](/docs/primitives/math/reflect) — The direction `i` bounced off a surface with unit normal `n`.
- [`refract`](/docs/primitives/math/refract) — The direction `i` bent through a surface with unit normal `n`.
- [`float`](/docs/primitives/math/float) — A plain number as an expression.