Skip to content

Shading

@ripl/3d

Flat shading utilities based on face normals and light direction.

Overview

Functions: computeFaceNormal · computeFaceBrightness · shadeFaceColor

computeFaceNormal function

Computes the surface normal of a face from its first three vertices via the cross product.

ts
function computeFaceNormal(vertices: Vector3[]): Vector3

Parameters:

NameTypeDescription
verticesVector3[]

Returns: Vector3


computeFaceBrightness function

Computes a 0–1 brightness value for a face given its normal and a light direction.

ts
function computeFaceBrightness(normal: Vector3, lightDirection: Vector3, normalized?: boolean): number

Parameters:

NameTypeDescription
normalVector3
lightDirectionVector3
normalizedboolean | undefined

Returns: number


shadeFaceColor function

Shades a color by a brightness factor (0–1), darkening or lightening the RGB channels.

ts
function shadeFaceColor(baseColor: string, brightness: number): string;

Parameters:

NameTypeDescription
baseColorstring
brightnessnumber

Returns: string