Intrinsics2 min read

dot

Computes the dot product of two vectors.

Reading Time
2 min
Word Count
222
Sections
12
Try It Live

Test dot in a live shader

Open the playground, start from a visual preset, and wire dot into the fragment stage to see how it behaves with real values.

Open Playground
bwsl
dot :: (vecN a, vecN b) -> float {...}

The dot function computes the dot product (inner product) of two vectors, returning a scalar value equal to the sum of the component-wise products. This is the foundation of nearly all lighting calculations in shaders.

Where vecN can be float2, float3, or float4.

Example - Diffuse Lighting

Parameters

ParameterTypeDescription
avecNFirst vector
bvecNSecond vector

Return Value

Returns the dot product: a.x*b.x + a.y*b.y + ... for all components.

Common Use Cases

Specular Highlights (Blinn-Phong)

The dot product of normal and half-vector creates shiny reflections.

Fresnel Effect

The dot product of normal and view direction creates view-dependent effects.

Half-Lambert (Soft Diffuse)

Wrap the dot product for softer, more stylized lighting.

Toon Shading

Quantize the dot product for cel-shaded looks.

Rim Lighting

Backlit glow effect using inverted view dot.

Hemisphere Lighting

Blend between sky and ground colors based on normal direction.

Length Optimization

dot(v, v) gives the squared length of a vector. When comparing distances, compare squared values to avoid the expensive sqrt in length().

Geometric Interpretation

For unit vectors, dot(a, b) equals cos(θ) where θ is the angle between them. Result is 1 when parallel, 0 when perpendicular, -1 when opposite.

Compiled Output

When compiled to GLSL:

glsl
dot(a, b)

When compiled to HLSL:

hlsl
dot(a, b)

When compiled to Metal:

metal
dot(a, b)

See Also