---
title: "refract: WGSL and GLSL examples, uses and input limits"
description: "Refraction changes a ray direction as it crosses a boundary between materials with different refractive indices. Use it for transmission through water or glass."
url: https://typeshade.dev/reference/functions/refract/
language: en
---

# `refract`

Function

Refraction changes a ray direction as it crosses a boundary between materials with different refractive indices.

## Compare examples

Compare equal refractive indices with bending and total internal reflection. The latter returns a zero vector.

**Model assumptions**`i = vec3(0.6, -0.8, 0); n = vec3(0, 1, 0)`

**Example 1`refract(i, n, 1)`**

**Result**

`vec3(0.6, -0.8, 0)`

**Example 2`refract(i, n, 0.5)`**

**Result**

`vec3(0.3, -0.9539392, 0)`

**Example 3`refract(i, n, 2)`**

**Result**

`vec3(0, 0, 0)`

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

## When to use it

Use it for transmission through water or glass.

## Inputs and limits

Normalize the incident direction and normal. The ratio is incident index divided by transmitted index; orient the normal consistently with the incident ray.

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

- [`reflect`](https://typeshade.dev/reference/functions/reflect/)
- [`normalize`](https://typeshade.dev/reference/functions/normalize/)
- [`dot`](https://typeshade.dev/reference/functions/dot/)

**WGSL**

`refract(a, b, c)`

**GLSL ES 3.00**

`refract(a, b, c)`

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

Refracts the incident vector `i` through a surface with normal `n` and ratio of indices `eta`; returns the zero vector on total internal reflection.

## Syntax

```ts
declare function refract<V extends vec2 | vec3 | vec4>(a0: V, a1: V, a2: (f32)): V
declare function refract<T extends Numeric>(i: T, n: T, eta: number): 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.

**`Numeric`**

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

## Parameters

- `i` `V``T`
- `n` `V``T`
- `eta` `(f32)``number`

## Return value

`V``T`

## Description

Both vectors should be normalized.

## Targets

**WGSL**

`refract(a, b, c)`

**GLSL ES 3.00**

`refract(a, b, c)`

## Examples

- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#geometry)
