refract
On this page

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 assumptionsi = vec3(0.6, -0.8, 0); n = vec3(0, 1, 0)

Example 1refract(i, n, 1)
Result
vec3(0.6, -0.8, 0)
Example 2refract(i, n, 0.5)
Result
vec3(0.3, -0.9539392, 0)
Example 3refract(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 and GLSL ES 3.00.

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

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 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 VT
  • n VT
  • eta (f32)number

Return value

VT

Description

Both vectors should be normalized.

Targets

WGSL
refract(a, b, c)
GLSL ES 3.00
refract(a, b, c)

Examples

See also

Edit this page Report a problem