GLSL Shader Functions Reference

This page is a quick reference for the built-in GLSL libraries available in MadMapper Materials, Surface FX, and Laser Materials. These libraries can also be used through MadAI.

All three libraries are included by adding the appropriate #include statement after your ISF header and before your shader code.

Libraries Overview

Library Include Statement Purpose
MadCommon #include "MadCommon.glsl" Constants, color conversion, and utility functions
MadNoise #include "MadNoise.glsl" 2D and 3D procedural noise generation
MadSDF #include "MadSDF.glsl" 2D shape drawing using Signed Distance Fields

All shaders are compiled using GLSL version 150 core with OpenGL 3.2 contexts.

MadCommon.glsl

Common constants and color utility functions.

#include "MadCommon.glsl"

Constants

Name Value
PI 3.1415926535897932384626433832795

Shortcut Macros

Macro Equivalent
saturate(x) clamp( x, 0.0, 1.0 )
lerp(x,y,t) mix( x, y, t )

Functions

Function Description
float luma( vec3 color ) Returns the luminance of an RGB color
float luminance( vec3 color ) Returns the luminance of an RGB color
vec3 linearToRgb( vec3 color, vec3 range ) Luminance-based linear to RGB conversion
vec3 rgbToLinear( vec3 color, vec3 range ) Luminance-based RGB to linear conversion
vec3 applyContrastSaturationBrightness( vec3 color, float contrast, float saturation, float brightness ) Adjusts contrast, saturation, and brightness. 1.0 = 100%, 0.5 = 50%, 1.5 = 150%
vec3 rgb2hsv( vec3 color ) Converts RGB to HSV
vec3 hsv2rgb( vec3 color ) Converts HSV to RGB
float rgb2hue( vec3 color ) Returns the hue value from an RGB color

MadNoise.glsl

A procedural noise library by Simon Geilfus. Provides a wide range of 2D and 3D noise functions for use in Materials, Surface FX, and Laser Materials.

#include "MadNoise.glsl"

Performance Optimization

Most noise functions can be optimized using a lookup texture. To enable this, define NOISE_TEXTURE_BASED before the include and add the noiseLUT.png texture to your IMPORTED section:

#define NOISE_TEXTURE_BASED
#include "MadNoise.glsl"
 
json header
 
"IMPORTED": [
    {
        "NAME": "noiseLUT",
        "PATH": "noiseLUT.png",
        "GL_TEXTURE_MIN_FILTER": "LINEAR",
        "GL_TEXTURE_MAG_FILTER": "LINEAR",
        "GL_TEXTURE_WRAP": "REPEAT"
    }
]
 

Value Noise

Smooth, interpolated noise. Output range: [0, 1]

Function Description
float vnoise( vec2 p ) 2D value noise
float vnoise( vec3 p ) 3D value noise
vec3 dvnoise( vec2 p ) 2D value noise with derivatives
vec4 dvnoise( vec3 p ) 3D value noise with derivatives

Simplex Noise

Gradient-based noise with fewer directional artifacts than value noise. Output range: [-1, 1]

Function Description
float noise( vec2 p ) 2D simplex noise
float noise( vec3 p ) 3D simplex noise
vec3 dnoise( vec2 p ) 2D simplex noise with derivatives
vec4 dnoise( vec3 p ) 3D simplex noise with derivatives

Worley / Cellular Noise

Cell-based noise useful for organic textures. Output range: [0, 1]

Function Description
float worleyNoise( vec2 p ) 2D cellular noise (default)
float worleyNoise( vec3 p ) 3D cellular noise (default)
vec2 worleyNoise( vec2 p ) 2D cellular noise returning F1 and F2 (requires NOISE_WORLEY_ASHIMA_IMPL)
vec2 worleyNoise( vec3 p ) 3D cellular noise returning F1 and F2 (requires NOISE_WORLEY_ASHIMA_IMPL)
vec3 dWorleyNoise( vec2 p ) 2D cellular noise with derivatives
vec4 dWorleyNoise( vec3 p ) 3D cellular noise with derivatives

Flow Noise

Simplex noise with rotating gradients, useful for animated fluid effects. Output range: [-1, 1]

Function Description
float flowNoise( vec2 pos, float rot ) 2D flow noise
vec3 dFlowNoise( vec2 pos, float rot ) 2D flow noise with derivatives

Billowed Noise

Absolute value of simplex noise, creating soft cloud-like shapes. Output range: [0, 1]

Function Description
float billowedNoise( vec2 p ) 2D billowed noise
float billowedNoise( vec3 p ) 3D billowed noise
vec3 dBillowedNoise( vec2 p ) 2D billowed noise with derivatives
vec4 dBillowedNoise( vec3 p ) 3D billowed noise with derivatives

Ridged Noise

Inverted billowed noise creating sharp ridge-like features. Output range: [0, 1]

Function Description
float ridgedNoise( vec2 p ) 2D ridged noise
float ridgedNoise( vec2 p, float ridge ) 2D ridged noise with custom ridge value
float ridgedNoise( vec3 p ) 3D ridged noise
float ridgedNoise( vec3 p, float ridge ) 3D ridged noise with custom ridge value
vec3 dRidgedNoise( vec2 p ) 2D ridged noise with derivatives
vec3 dRidgedNoise( vec2 p, float ridge ) 2D ridged noise with derivatives and custom ridge
vec4 dRidgedNoise( vec3 p ) 3D ridged noise with derivatives
vec4 dRidgedNoise( vec3 p, float ridge ) 3D ridged noise with derivatives and custom ridge

Curl Noise

Divergence-free noise useful for particle movement and fluid simulation. Output range: [-1, 1]

Function Description
vec2 curlNoise( vec2 v ) 2D curl of simplex noise
vec2 curlNoise( vec2 v, float t ) 2D curl of simplex flow noise (animated)

Fractal Brownian Motion (fBm)

Layered simplex noise at multiple octaves for natural-looking complexity. Output range: [-1, 1]

Function Description
float fBm( vec2 p ) 2D fBm (default octaves)
float fBm( vec2 p, int octaves, float lacunarity, float gain ) 2D fBm with custom parameters
float fBm( vec3 p ) 3D fBm (default octaves)
float fBm( vec3 p, int octaves, float lacunarity, float gain ) 3D fBm with custom parameters
vec3 dfBm( vec2 p ) 2D fBm with derivatives
vec3 dfBm( vec2 p, int octaves, float lacunarity, float gain ) 2D fBm with derivatives and custom parameters
vec4 dfBm( vec3 p ) 3D fBm with derivatives
vec4 dfBm( vec3 p, int octaves, float lacunarity, float gain ) 3D fBm with derivatives and custom parameters

Billowy Turbulence

Fractal sum of billowed noise. Output range: [-1, 1]

Function Description
float billowyTurbulence( vec2 p ) 2D billowy turbulence
float billowyTurbulence( vec2 p, int octaves, float lacunarity, float gain ) 2D with custom parameters
float billowyTurbulence( vec3 p ) 3D billowy turbulence
float billowyTurbulence( vec3 p, int octaves, float lacunarity, float gain ) 3D with custom parameters
vec3 dBillowyTurbulence( vec2 p ) 2D with derivatives
vec3 dBillowyTurbulence( vec2 p, int octaves, float lacunarity, float gain ) 2D with derivatives and custom parameters
vec4 dBillowyTurbulence( vec3 p ) 3D with derivatives
vec4 dBillowyTurbulence( vec3 p, int octaves, float lacunarity, float gain ) 3D with derivatives and custom parameters

Ridged Multi-Fractal

Fractal sum of ridged noise, great for terrain and rock-like textures. Output range: [-1, 1]

Function Description
float ridgedMF( vec2 p ) 2D ridged multi-fractal
float ridgedMF( vec2 p, float ridgeOffset ) 2D with ridge offset
float ridgedMF( vec2 p, float ridgeOffset, int octaves, float lacunarity, float gain ) 2D fully parameterized
float ridgedMF( vec3 p ) 3D ridged multi-fractal
float ridgedMF( vec3 p, float ridgeOffset ) 3D with ridge offset
float ridgedMF( vec3 p, float ridgeOffset, int octaves, float lacunarity, float gain ) 3D fully parameterized
vec3 dRidgedMF( vec2 p ) 2D with derivatives
vec3 dRidgedMF( vec2 p, float ridgeOffset ) 2D with derivatives and ridge offset
vec3 dRidgedMF( vec2 p, float ridgeOffset, int octaves, float lacunarity, float gain ) 2D fully parameterized with derivatives
vec4 dRidgedMF( vec3 p ) 3D with derivatives
vec4 dRidgedMF( vec3 p, float ridgeOffset ) 3D with derivatives and ridge offset
vec4 dRidgedMF( vec3 p, float ridgeOffset, int octaves, float lacunarity, float gain ) 3D fully parameterized with derivatives

MadSDF.glsl

A 2D Signed Distance Fields library by Simon Geilfus. Allows fast, clean, resolution-independent 2D shape rendering in Materials and Surface FX.

#include "MadSDF.glsl"

SDF functions work in three stages: set up a domain, create a distance field from a primitive, then color the result. For a full tutorial with visual examples, see the MadSDF documentation on GitHub.

Distance Field Primitives

Function Description
float circle( vec2 p, float radius ) Circle at origin
float rectangle( vec2 p, float radius ); Circle at position
float rectangle( vec2 p, vec2 size ) Box at origin
float rectangle( vec2 p, vec2 size ) Rectangle at origin
float rectangle( vec2 p, vec2 center, float radius ) Box at position
float rectangle( vec2 p, vec2 center, vec2 size ) Rectangle at position
float roundedRectangle( vec2 p, vec2 size, float cornerRadius ) Rounded rectangle at origin
float roundedRectangle( vec2 p, vec2 center, vec2 size, float cornerRadius ) Rounded rectangle at position
float triangle( vec2 p, float radius ) Triangle at origin
float triangle( vec2 p, vec2 center, float radius ) Triangle at position
float hexagon( vec2 p, float radius ) Hexagon at origin
float hexagon( vec2 p, vec2 center, float radius ) Hexagon at position
float line( vec2 p, vec2 start, vec2 end, float width ) Line segment
float arc( vec2 p, float radius, float angle, float width ) Arc

Coloring Functions

Function Description
vec3 fill( vec3 background, vec3 color, float dist ) Fills the interior of a shape
vec4 fill( vec4 background, vec4 color, float dist ) Fill with alpha
vec3 stroke( vec3 background, vec3 color, float dist, float width ) Draws the outline of a shape
vec4 stroke( vec4 background, vec4 color, float dist, float width ) Stroke with alpha
vec3 glow( vec3 color, float dist, float width ) Additive glow effect (use with +=)
vec4 glow( vec4 color, float dist, float width ) Glow with alpha

Distance Operators

Combine two distance fields using boolean-style operations.

Function Description
float blend( float d1, float d2 ) Union of two shapes
float smoothBlend( float d1, float d2, float k ) Smooth union with blending factor k
float subtract( float d1, float d2 ) Subtract second shape from first
float intersect( float d1, float d2 ) Intersection of two shapes

2D Transforms

Function Description
vec2 translate( vec2 p, vec2 t ) Move the domain
vec2 rotate( vec2 p, float a ) Rotate the domain (radians)
vec2 transform( vec2 p, mat2 m ) Apply a 2x2 matrix transform

Domain Transforms

Repeat and tile shapes across the domain. These are applied before creating the distance field.

Function Description
vec2 repeat( vec2 p, vec2 offset ) Repeat every offset
vec2 repeat( vec2 p, vec2 offset, out vec2 cellId ) Repeat with cell ID output
vec2 repeat( vec2 p, vec2 offset, vec2 a, vec2 b ) Repeat with row offset (brick pattern)
vec2 repeat( vec2 p, vec2 offset, vec2 a, vec2 b, out vec2 cellId ) Brick pattern with cell ID
vec2 repeatRadial( vec2 p, float angle ) Radial repetition
vec2 repeatRadial( vec2 p, float angle, out vec2 cellId ) Radial repetition with cell ID
vec2 repeatRadial( vec2 p, float angle, float angleOffset ) Radial repetition with start offset
vec2 repeatRadial( vec2 p, float angle, float angleOffset, out vec2 cellId ) Radial with offset and cell ID

Tip: The cellId variants let you apply per-cell variation using noise or random functions — for example, varying size or color across a grid of repeated shapes.

Quick Usage Example

A simple Material using all three libraries:

 
/*{
    "CREDIT": "Your Name",
    "DESCRIPTION": "Noisy glowing circle",
    "INPUTS": [
        {"LABEL": "Radius", "NAME": "mat_radius", "TYPE": "float", "DEFAULT": 0.25, "MIN": 0.0, "MAX": 0.5}
    ]
}*/

#include "MadCommon.glsl"
#include "MadNoise.glsl"
#include "MadSDF.glsl"

vec4 materialColorForPixel(vec2 texCoord)
{
    vec2 uv = texCoord - vec2(0.5);

    // Distort the domain with noise
    float n = noise(uv * 10.0 + TIME) * 0.02;

    // Create a circle using SDF
    float sdf = circle(uv + n, mat_radius);

    // Color it using MadCommon's hsv2rgb
    vec3 col = hsv2rgb(vec3(TIME * 0.1, 0.8, 1.0));

    vec3 color = vec3(0.0);
    color = fill(color, col, sdf);
    color += glow(col * 0.5, sdf, 0.1);

    return vec4(color, 1.0);
}

Related Documentation

For deeper coverage of the shader system beyond this function reference, see: