---
title: "normalize: WGSL and GLSL examples, uses and input limits"
description: "Normalization preserves a nonzero vector's direction while changing its magnitude to unit length."
url: https://typeshade.dev/reference/functions/normalize/
language: en
---

# `normalize`

Function

Normalization preserves a nonzero vector's direction while changing its magnitude to unit length.

## Explore the function

`y = normalize(vec2(x, 1.)).x`

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

`y` = 1.25 `y` = -1.25 `x` = -3 `x` = 3

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:

- The graph shows the horizontal component after normalizing a vector with a fixed vertical component.

**Full shader source**

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

normalize-plot.shade.ts

```ts
"use typeshade";

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

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

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

  return normalize(vec2(x, 1.)).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 before calculations that require directions independent of magnitude.

Example calculation: `normalize(vec2(1.0, 1.)).x ≈ 0.707107`

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

## Inputs and limits

Conditions in this example: x ∈ ℝ

A zero vector has no unique direction. This example keeps the vertical component nonzero.

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

- [`length`](https://typeshade.dev/reference/functions/length/)
- [`distance`](https://typeshade.dev/reference/functions/distance/)
- [`dot`](https://typeshade.dev/reference/functions/dot/)

**WGSL**

`normalize(a)`

**GLSL ES 3.00**

`normalize(a)`

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

Returns a unit vector in the same direction as the input, computed by dividing each component by the vector's length.

## Syntax

```ts
declare function normalize<V extends vec2 | vec3 | vec4>(a0: V): V
declare function normalize<T extends Numeric | Vec64Any>(a: T): T
```

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

**`Vec64Any`**

`type Vec64Any = vec2f64 | vec3f64 | vec4f64`

**`Numeric`**

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

## Parameters

- `a` `V``T`

## Return value

`V``T`

## Description

For the zero vector the GPU result is undefined, and the CPU oracle returns NaN in each component. Also accepts `f64` operands.

## Targets

**WGSL**

`normalize(a)`

**GLSL ES 3.00**

`normalize(a)`

## Examples

- [`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/path-tracer/path-tracer.shade.ts`](https://typeshade.dev/guide/examples/path-tracer/)
- [`gallery/rt-renderer-class/rt-renderer-class.shade.ts`](https://typeshade.dev/guide/examples/rt-renderer-class/)
- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)
- [`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/optional-fields/optional-fields.shade.ts`](https://typeshade.dev/guide/examples/optional-fields/)
- [`gallery/optional-values/optional-values.shade.ts`](https://typeshade.dev/guide/examples/optional-values/)
- [`gallery/ray-class/ray-class.shade.ts`](https://typeshade.dev/guide/examples/ray-class/)

## See also

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