Skip to main content

Complete Function Reference

Comprehensive reference for all mathematical functions and operations in TSL.

Mathematical Functions

Trigonometric Functions

FunctionDescriptionParametersReturn TypeVector Support
sin(x)Sinefloat|vec*Input type
cos(x)Cosinefloat|vec*Input type
tan(x)Tangentfloat|vec*Input type
asin(x)Arcsinefloat|vec*Input type
acos(x)Arccosinefloat|vec*Input type
atan(x)Arctangentfloat|vec*Input type
atan2(y, x)Two-argument arctangentfloat|vec*, float|vec*Higher precision type

Mathematical Background: Trigonometric functions relate angles to ratios of triangle sides.

  • sin() and cos() oscillate between -1 and 1
  • Useful for creating wave patterns and circular motion
  • Input angles are in radians (π ≈ 3.14159)
// Basic trigonometry
const angle = uniform(0, 'angle')
const wave = sin(angle.mul(PI))
const circle = vec2(cos(angle), sin(angle))

// Multiple frequencies for complex waves
const multiWave = sin(angle.mul(3)).add(cos(angle.mul(5)).mul(0.5))

Exponential Functions

FunctionDescriptionParametersReturn TypeVector Support
pow(x, y)Powerfloat|vec*, float|vec*Higher precision type
pow2(x)Squarefloat|vec*Input type
pow3(x)Cubefloat|vec*Input type
pow4(x)Fourth powerfloat|vec*Input type
sqrt(x)Square rootfloat|vec*Input type
inverseSqrt(x)Inverse square rootfloat|vec*Input type
exp(x)Natural exponentialfloat|vec*Input type
exp2(x)Base-2 exponentialfloat|vec*Input type
log(x)Natural logarithmfloat|vec*Input type
log2(x)Base-2 logarithmfloat|vec*Input type

Mathematical Background: Exponential functions model growth and decay.

  • pow(x, y) raises x to the power y
  • sqrt(x) finds the number that when multiplied by itself equals x
  • exp(x) is e^x, where e ≈ 2.718
// Power and exponential functions
const distance = length(position)
const falloff = exp(distance.negate())
const brightness = pow(dot(normal, lightDir), shininess)
const octaves = log2(resolution)

Common Mathematical Functions

FunctionDescriptionParametersReturn TypeVector Support
abs(x)Absolute valuefloat|vec*Input type
sign(x)Sign extractionfloat|vec*Input type
floor(x)Floor functionfloat|vec*Input type
ceil(x)Ceiling functionfloat|vec*Input type
round(x)Round to nearestfloat|vec*Input type
fract(x)Fractional partfloat|vec*Input type
trunc(x)Truncatefloat|vec*Input type
mod(x, y)Modulofloat|vec*, float|vec*Higher precision type

Mathematical Background: These functions modify numbers in specific ways.

  • abs(x) removes the negative sign: abs(-5) = 5
  • floor(x) rounds down: floor(3.7) = 3
  • fract(x) gives the decimal part: fract(3.7) = 0.7
// Basic math function combinations
const pattern = fract(position.mul(10))
const stepped = floor(value.mul(8)).div(8)
const pingPong = abs(fract(time.mul(0.5)).mul(2).sub(1))

Interpolation Functions

FunctionDescriptionParametersReturn TypeVector Support
min(x, y)Minimumfloat|vec*, float|vec*Higher precision type
max(x, y)Maximumfloat|vec*, float|vec*Higher precision type
clamp(x, min, max)Clamp to rangefloat|vec*, float|vec*, float|vec*Higher precision type
saturate(x)Clamp to 0-1float|vec*Input type
mix(x, y, a)Linear interpolationfloat|vec*, float|vec*, float|vec*Higher precision type
step(edge, x)Step functionfloat|vec*, float|vec*Higher precision type
smoothstep(a, b, x)Smooth stepfloat|vec*, float|vec*, float|vec*Higher precision type

Mathematical Background: Interpolation blends between values.

  • mix(a, b, t) blends: when t=0 returns a, when t=1 returns b
  • step(edge, x) returns 0 if x < edge, otherwise 1
  • smoothstep() creates smooth transitions instead of sharp jumps
// Interpolation for smooth transitions
const gradient = smoothstep(0.2, 0.8, position.y)
const masked = mix(colorA, colorB, gradient)
const threshold = step(0.5, noise)

Vector Functions

Vector Operations

FunctionDescriptionParametersReturn TypeNotes
length(x)Vector lengthvec*floatEuclidean norm
distance(x, y)Distance between pointsvec*, vec*floatEuclidean distance
dot(x, y)Dot productvec*, vec*floatScalar product
cross(x, y)Cross productvec3, vec3vec33D only
normalize(x)Unit vectorvec*Input typeLength = 1
lengthSq(x)Squared lengthvec*floatFaster than length

Mathematical Background: Vector operations work with directions and magnitudes.

  • length() measures how far a point is from origin
  • dot() measures how much two vectors point in the same direction
  • normalize() makes a vector length 1 while keeping its direction
// Vector operations for lighting
const lightDir = normalize(lightPos.sub(worldPos))
const intensity = max(0, dot(normal, lightDir))
const reflection = reflect(viewDir.negate(), normal)

Vector Utilities

FunctionDescriptionParametersReturn TypeNotes
reflect(I, N)Reflection vectorvec*, vec*Input typeMirror reflection
refract(I, N, eta)Refraction vectorvec*, vec*, floatInput typeSnell's law
faceforward(N, I, Nref)Orient normalvec*, vec*, vec*Input typeConsistent orientation
// Physical vector calculations
const reflected = reflect(incident, normal)
const refracted = refract(incident, normal, ior)
const oriented = faceforward(normal, viewDir, geometryNormal)

Comparison and Logical Functions

Comparison Operations

MethodSymbolReturn TypeVector Support
.equal(x)==bool|bvec*
.notEqual(x)!=bool|bvec*
.lessThan(x)<bool|bvec*
.greaterThan(x)>bool|bvec*
.lessThanEqual(x)<=bool|bvec*
.greaterThanEqual(x)>=bool|bvec*

Logical Operations

MethodSymbolReturn TypeVector Support
.and(x)&&bool|bvec*
.or(x)||bool|bvec*
.not()!bool|bvec*
.xor(x)^^bool|bvec*

Boolean Vector Functions

FunctionDescriptionParametersReturn TypeNotes
all(x)All components truebvec*boolLogical AND
any(x)Any component truebvec*boolLogical OR
not(x)Component-wise NOTbvec*Input typeLogical NOT
// Logical operations
const inBounds = all(position.greaterThan(vec3(0)).and(position.lessThan(vec3(1))))
const hasColor = any(color.greaterThan(vec3(0)))

Derivative Functions

FunctionDescriptionParametersReturn TypeFragment Only
dFdx(x)X-direction derivativefloat|vec*Input type
dFdy(x)Y-direction derivativefloat|vec*Input type
fwidth(x)Derivative widthfloat|vec*Input type

Mathematical Background: Derivatives measure how fast something changes.

  • dFdx() shows how much a value changes between neighboring pixels horizontally
  • dFdy() shows vertical change
  • Used for anti-aliasing and normal calculation
// Derivatives for surface normals and anti-aliasing
const normalFromHeight = normalize(vec3(dFdx(heightmap).negate(), dFdy(heightmap).negate(), 1))

const antialiasing = smoothstep(0, fwidth(pattern), pattern)

Utility Functions

Custom Utility Functions

FunctionDescriptionParametersReturn TypeNotes
oneMinus(x)One minus xfloat|vec*Input type1 - x
negate(x)Negate valuefloat|vec*Input type-x
reciprocal(x)Reciprocalfloat|vec*Input type1/x
remap(x, a, b, c, d)Remap rangefloat|vec*, ...Input typeLinear remapping
remapClamp(x, a, b, c, d)Clamped remapfloat|vec*, ...Input typeClamped remapping
// Utility function applications
const inverted = oneMinus(brightness)
const normalized = remap(worldPos.y, -100, 100, 0, 1)
const safety = reciprocal(max(EPSILON, denominator))

Geometric Functions

2D Transformations

FunctionDescriptionParametersReturn TypeNotes
rotate(pos, angle)2D rotationvec2, floatvec2Around origin
scale(pos, factor)2D scalingvec2, vec2|floatvec2Non-uniform scaling
// 2D geometric transformations
const rotated = rotate(uv.sub(0.5), time).add(0.5)
const scaled = scale(uv, vec2(2, 1))

Noise and Random Functions

Pseudorandom Functions

FunctionDescriptionParametersReturn TypeNotes
hash(seed)Hash functionfloat|vec*float0-1 range
range(min, max)Random rangefloat, floatfloatAttribute-based

Mathematical Background: Pseudorandom functions create seemingly random values from deterministic inputs.

// Random function usage
const randomValue = hash(position.add(time))
const randomColor = vec3(hash(position), hash(position.add(1)), hash(position.add(2)))

Oscillator Functions

Wave Generation

FunctionDescriptionParametersReturn TypeNotes
oscSine(t)Sine wavefloatfloat-1 to 1
oscSquare(t)Square wavefloatfloat-1 to 1
oscTriangle(t)Triangle wavefloatfloat-1 to 1
oscSawtooth(t)Sawtooth wavefloatfloat-1 to 1
// Oscillator function combinations
const wave = oscSine(time.mul(2))
.add(oscTriangle(time.mul(4)).mul(0.5))
.add(oscSquare(time.mul(8)).mul(0.25))

Color Space Functions

Color Utilities

FunctionDescriptionParametersReturn TypeNotes
directionToColor(dir)Direction to colorvec3vec3Normal encoding
colorToDirection(col)Color to directionvec3vec3Normal decoding
// Color space conversions
const encoded = directionToColor(normal)
const decoded = colorToDirection(normalTexture)

Blend Mode Functions

Color Blending

FunctionDescriptionParametersReturn TypeNotes
blendBurn(a, b)Burn blendvec3, vec3vec3Color burn
blendDodge(a, b)Dodge blendvec3, vec3vec3Color dodge
blendOverlay(a, b)Overlay blendvec3, vec3vec3Overlay mode
blendScreen(a, b)Screen blendvec3, vec3vec3Screen mode
blendColor(a, b)Normal blendvec3, vec3vec3Alpha blend
// Blend mode applications
const result = blendOverlay(baseColor, overlayColor)
const highlight = blendScreen(color, lightColor)

Control Flow Functions

Conditional Functions

FunctionDescriptionParametersReturn Type
select(condition, trueValue, falseValue)Conditional selectionbool|bvec*, any, anyInput type
// Conditional selection
const color = select(
position.x.greaterThan(0),
vec3(1, 0, 0), // Red
vec3(0, 0, 1) // Blue
)

Function Composition

Combining Functions

Functions can be chained and combined to create complex behaviors:

// Complex function composition
const complexPattern = (position, time) => {
const transformed = rotate(position.mul(2), time.mul(0.5))
const noise1 = hash(transformed.add(time))
const noise2 = hash(transformed.mul(2).add(time.mul(1.3)))

const combined = mix(noise1, noise2, sin(time).mul(0.5).add(0.5))
return smoothstep(0.3, 0.7, combined)
}

Node System provides 150+ mathematical functions covering all aspects of GPU programming, from basic arithmetic to advanced geometric transformations.