GLSL and Shadertoy shader skills for Claude Code, Codex, Cursor, Copilot and Gemini CLI. 186 functions, SDF raymarching, procedural terrain, water, clouds and generative art, with a bundler, checker and 11 examples.
Python agent-skillsclaude-codecodexcomputer-graphicscursorgemini-cligenerative-artgithub-copilotglslnoiseprocedural-artprocedural-generationraymarchingshadersshadertoysigned-distance-functionsterrain-generationwebglwebgl2
// Procedural material swatches: six texture-free patterns on one board.
// Shows: two-level domain warping (marble), exact Voronoi borders (cracked earth), hex tiles with bevels,
// Truchet weaves, smooth Voronoi (organic cells) and a box-filtered checker. Each tile uses explicit pixel-size
// filtering instead of fwidth, so it stays valid inside branches.
// @render time=2.0
#define DEBUG_MODE 0 // 0 final | 3 tile ids
// Library code below is bundled from shader-skills (MIT): https://github.com/gokayfem/shader-skills
// ---- shader-skills library: common ----
const float TAU = 6.28318530717959;
// Clamp to [0,1].
float saturate(float x) { return clamp(x, 0.0, 1.0); }
vec3 saturate(vec3 x) { return clamp(x, 0.0, 1.0); }
// Squared length.
float dot2(vec2 v) { return dot(v, v); }
float dot2(vec3 v) { return dot(v, v); }
// ---- shader-skills library: hash ----
// PCG-style 1D integer hash.
uint pcgHash(uint v) {
uint state = v * 747796405u + 2891336453u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
// 3D integer hash, three decorrelated outputs.
uvec3 pcg3d(uvec3 v) {
v = v * 1664525u + 1013904223u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
v ^= v >> 16u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
return v;
}
// Map a uint to [0,1) using the top 24 bits, so the result can never round up to 1.0.
float uintToUnit(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }
vec2 uintToUnit(uvec2 h) { return vec2(h >> 8u) * (1.0 / 16777216.0); }
vec3 uintToUnit(uvec3 h) { return vec3(h >> 8u) * (1.0 / 16777216.0); }
vec4 uintToUnit(uvec4 h) { return vec4(h >> 8u) * (1.0 / 16777216.0); }
// Random float in [0,1) for an integer-valued 2D input.
float hash12(vec2 p) {
uvec2 q = uvec2(ivec2(floor(p)));
return uintToUnit(pcgHash(q.x + pcgHash(q.y)));
}
// Random vec2 in [0,1)^2 for an integer-valued 2D input.
vec2 hash22(vec2 p) {
ivec2 q = ivec2(floor(p));
return uintToUnit(pcg3d(uvec3(q.x, q.y, 0x2545u))).xy;
}
// Random vec3 in [0,1)^3 for an integer-valued 3D input.
vec3 hash33(vec3 p) { return uintToUnit(pcg3d(uvec3(ivec3(floor(p))))); }
// ---- shader-skills library: noise ----
// Quintic fade 6t^5 - 15t^4 + 10t^3: C2-continuous interpolation weights for lattice noise.
vec2 quinticFade(vec2 f) { return f * f * f * (f * (f * 6.0 - 15.0) + 10.0); }
vec3 quinticFade(vec3 f) { return f * f * f * (f * (f * 6.0 - 15.0) + 10.0); }
// Derivative of quinticFade: 30 t^2 (t - 1)^2.
vec2 quinticFadeDeriv(vec2 f) { return 30.0 * f * f * (f * (f - 2.0) + 1.0); }
vec3 quinticFadeDeriv(vec3 f) { return 30.0 * f * f * (f * (f - 2.0) + 1.0); }
// Unit gradient for a 2D lattice point.
vec2 latticeGradient(vec2 cell) {
float a = TAU * hash12(cell);
return vec2(cos(a), sin(a));
}
// 2D gradient (Perlin-style) noise with analytic derivatives. Returns vec3(value, d/dx, d/dy). Gradient noise has less grid-aligned blockiness than value noise. Prefer it for fBm.
vec3 gradientNoiseD(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = quinticFade(f), du = quinticFadeDeriv(f);
vec2 ga = latticeGradient(i);
vec2 gb = latticeGradient(i + vec2(1.0, 0.0));
vec2 gc = latticeGradient(i + vec2(0.0, 1.0));
vec2 gd = latticeGradient(i + vec2(1.0, 1.0));
float va = dot(ga, f);
float vb = dot(gb, f - vec2(1.0, 0.0));
float vc = dot(gc, f - vec2(0.0, 1.0));
float vd = dot(gd, f - vec2(1.0, 1.0));
float k = va - vb - vc + vd;
float value = va + u.x * (vb - va) + u.y * (vc - va) + u.x * u.y * k;
vec2 deriv = ga + u.x * (gb - ga) + u.y * (gc - ga) + u.x * u.y * (ga - gb - gc + gd)
+ du * (vec2(vb - va, vc - va) + u.yx * k);
return vec3(value, deriv);
}
// 2D gradient noise.
float gradientNoise(vec2 p) { return gradientNoiseD(p).x; }
// Dot product of a hashed 3D gradient with the offset to its lattice corner.
float latticeGradDot(vec3 cell, vec3 corner, vec3 f) {
vec3 g = hash33(cell + corner) * 2.0 - 1.0;
return dot(g, f - corner);
}
// 3D gradient noise.
float gradientNoise(vec3 p) {
vec3 i = floor(p), f = fract(p);
vec3 u = quinticFade(f);
return mix(mix(mix(latticeGradDot(i, vec3(0, 0, 0), f), latticeGradDot(i, vec3(1, 0, 0), f), u.x),
mix(latticeGradDot(i, vec3(0, 1, 0), f), latticeGradDot(i, vec3(1, 1, 0), f), u.x), u.y),
mix(mix(latticeGradDot(i, vec3(0, 0, 1), f), latticeGradDot(i, vec3(1, 0, 1), f), u.x),
mix(latticeGradDot(i, vec3(0, 1, 1), f), latticeGradDot(i, vec3(1, 1, 1), f), u.x), u.y), u.z);
}
// 3D gradient noise with analytic derivatives. Returns vec4(value, d/dx, d/dy, d/dz); value equals gradientNoise(p). The trilinear blend is expanded as a polynomial in the fade weights, so the gradient is the same blend of the corner gradients plus the chain-rule term of the fade.
vec4 gradientNoiseD(vec3 p) {
vec3 i = floor(p), f = fract(p);
vec3 u = quinticFade(f), du = quinticFadeDeriv(f);
vec3 g000 = hash33(i) * 2.0 - 1.0;
vec3 g100 = hash33(i + vec3(1, 0, 0)) * 2.0 - 1.0;
vec3 g010 = hash33(i + vec3(0, 1, 0)) * 2.0 - 1.0;
vec3 g110 = hash33(i + vec3(1, 1, 0)) * 2.0 - 1.0;
vec3 g001 = hash33(i + vec3(0, 0, 1)) * 2.0 - 1.0;
vec3 g101 = hash33(i + vec3(1, 0, 1)) * 2.0 - 1.0;
vec3 g011 = hash33(i + vec3(0, 1, 1)) * 2.0 - 1.0;
vec3 g111 = hash33(i + vec3(1, 1, 1)) * 2.0 - 1.0;
float v000 = dot(g000, f);
float v100 = dot(g100, f - vec3(1, 0, 0));
float v010 = dot(g010, f - vec3(0, 1, 0));
float v110 = dot(g110, f - vec3(1, 1, 0));
float v001 = dot(g001, f - vec3(0, 0, 1));
float v101 = dot(g101, f - vec3(1, 0, 1));
float v011 = dot(g011, f - vec3(0, 1, 1));
float v111 = dot(g111, f - vec3(1, 1, 1));
// value = v000 + kx u.x + ky u.y + kz u.z + kxy u.x u.y + kyz u.y u.z + kzx u.z u.x + kxyz u.x u.y u.z
float kx = v100 - v000, ky = v010 - v000, kz = v001 - v000;
float kxy = v000 - v100 - v010 + v110;
float kyz = v000 - v010 - v001 + v011;
float kzx = v000 - v100 - v001 + v101;
float kxyz = -v000 + v100 + v010 - v110 + v001 - v101 - v011 + v111;
float value = v000 + kx * u.x + ky * u.y + kz * u.z
+ kxy * u.x * u.y + kyz * u.y * u.z + kzx * u.z * u.x + kxyz * u.x * u.y * u.z;
vec3 grad = g000 + (g100 - g000) * u.x + (g010 - g000) * u.y + (g001 - g000) * u.z
+ (g000 - g100 - g010 + g110) * u.x * u.y
+ (g000 - g010 - g001 + g011) * u.y * u.z
+ (g000 - g100 - g001 + g101) * u.z * u.x
+ (-g000 + g100 + g010 - g110 + g001 - g101 - g011 + g111) * u.x * u.y * u.z
+ du * vec3(kx + kxy * u.y + kzx * u.z + kxyz * u.y * u.z,
ky + kxy * u.x + kyz * u.z + kxyz * u.x * u.z,
kz + kyz * u.y + kzx * u.x + kxyz * u.x * u.y);
return vec4(value, grad);
}
const int FBM_MAX_OCTAVES = 12;
// Fractional Brownian motion over 2D gradient noise. Each octave is rotated to hide lattice alignment. persistence 0.5 is the classic look (H = 1). Lower is smoother, higher is rougher.
float fbm(vec2 p, int octaves, float lacunarity, float persistence) {
const mat2 octaveRotation = mat2(0.8, 0.6, -0.6, 0.8);
float sum = 0.0, amp = 1.0, norm = 0.0;
for (int i = 0; i < FBM_MAX_OCTAVES; i++) {
if (i >= octaves) break;
sum += amp * gradientNoise(p);
norm += amp;
amp *= persistence;
p = lacunarity * (octaveRotation * p) + vec2(17.13, -3.71);
}
return 1.4 * sum / norm;
}
// 2D fBm with the usual defaults (lacunarity 2.01 so octave peaks never re-align, persistence 0.5).
float fbm(vec2 p, int octaves) { return fbm(p, octaves, 2.01, 0.5); }
// fBm over 3D gradient noise.
float fbm(vec3 p, int octaves, float lacunarity, float persistence) {
const mat3 octaveRotation = mat3(0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64);
float sum = 0.0, amp = 1.0, norm = 0.0;
for (int i = 0; i < FBM_MAX_OCTAVES; i++) {
if (i >= octaves) break;
sum += amp * gradientNoise(p);
norm += amp;
amp *= persistence;
p = lacunarity * (octaveRotation * p) + vec3(17.13, -3.71, 5.29);
}
return sum / norm;
}
// Two-level domain warp f(p + s*r), r = fbm(p + s*q), q = fbm(p). Writes the intermediate warp vectors so they can drive color. Cost is 5 fBm calls.
float warpedFbm(vec2 p, float strength, int octaves, out vec2 q, out vec2 r) {
q = vec2(fbm(p, octaves), fbm(p + vec2(3.1, 7.4), octaves));
r = vec2(fbm(p + strength * q + vec2(9.7, 2.6), octaves),
fbm(p + strength * q + vec2(6.3, 1.9), octaves));
return fbm(p + strength * r, octaves);
}
// Exact distance to Voronoi cell borders, found in two passes: nearest point, then nearest bisector. Returns vec3(borderDistance, vectorToNearestPoint). Unlike F2-F1, the border distance is a true distance, so lines have constant width.
vec3 voronoiBorder(vec2 p, float jitter) {
vec2 ip = floor(p), fp = fract(p);
vec2 mg = vec2(0.0), mr = vec2(0.0);
float md = 8.0;
for (int j = -1; j <= 1; j++)
for (int i = -1; i <= 1; i++) {
vec2 g = vec2(float(i), float(j));
vec2 r = g + jitter * hash22(ip + g) - fp;
float d = dot(r, r);
if (d < md) { md = d; mr = r; mg = g; }
}
md = 8.0;
for (int j = -2; j <= 2; j++)
for (int i = -2; i <= 2; i++) {
vec2 g = mg + vec2(float(i), float(j));
vec2 r = g + jitter * hash22(ip + g) - fp;
if (dot2(mr - r) > 1e-5) {
md = min(md, dot(0.5 * (mr + r), normalize(r - mr)));
}
}
return vec3(md, mr);
}
// Smooth Voronoi: a soft-min of distances to feature points. Cells become blobs with no creases. k controls smoothness (large = sharper; around 8-32). Searches 5x5 cells so low k has no seams.
float smoothVoronoi(vec2 p, float k) {
vec2 ip = floor(p), fp = fract(p);
float acc = 0.0;
for (int j = -2; j <= 2; j++)
for (int i = -2; i <= 2; i++) {
vec2 g = vec2(float(i), float(j));
vec2 r = g + hash22(ip + g) - fp;
acc += exp2(-k * length(r));
}
return -log2(acc) / k;
}
// ---- shader-skills library: pattern ----
// Triangle wave, the integral of a square wave. Building block for box-filtered patterns.
vec2 triangleWave(vec2 x) { return 1.0 - 2.0 * abs(fract(x * 0.5) - 0.5); }
// Box-filtered checkerboard with unit squares: 0 or 1 up close, fading to 0.5 where squares are smaller than a pixel. Compute the footprint before any branching: w = fwidth(p) (or max(abs(dFdx(p)), abs(dFdy(p)))).
float filteredChecker(vec2 p, vec2 w) {
w = max(w, 1e-4);
vec2 i = (triangleWave(p + 0.5 * w) - triangleWave(p - 0.5 * w)) / w;
return 0.5 - 0.5 * i.x * i.y;
}
// Hexagonal tiling with unit spacing between neighboring centers. Neighbors sit left/right, so hexes are pointy-top. Returns vec4(local offset from the hex center, integer-valued hex id).
vec4 hexGrid(vec2 p) {
const vec2 s = vec2(1.0, 1.7320508);
vec2 a = mod(p, s) - 0.5 * s;
vec2 b = mod(p - 0.5 * s, s) - 0.5 * s;
vec2 local = (dot(a, a) < dot(b, b)) ? a : b;
vec2 center = p - local;
vec2 id = floor(vec2(center.x * 2.0, center.y * 1.1547005) + 0.5);
return vec4(local, id);
}
// Distance from a hexGrid local coordinate to its hexagon's edge (0.5 at the center, 0 on the edge).
float hexEdgeDistance(vec2 local) {
vec2 a = abs(local);
return 0.5 - max(a.x, dot(a, vec2(0.5, 0.8660254)));
}
// Truchet tiling of quarter-circle arcs, randomly flipped per cell. Returns signed distance to arcs of half-width w.
float truchetArcs(vec2 p, float w) {
vec2 id = floor(p);
vec2 f = fract(p) - 0.5;
if (hash12(id) < 0.5) f.x = -f.x;
vec2 q = f - 0.5 * sign(f.x + f.y + 1e-6);
return abs(length(q) - 0.5) - w;
}
// ---- shader-skills library: color ----
// Exact sRGB decode (EOTF): encoded [0,1] -> linear.
vec3 srgbToLinear(vec3 c) {
c = max(c, 0.0);
return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c));
}
// Exact sRGB encode (OETF): linear -> display [0,1]. Apply exactly once, after tonemapping. pow(c, 1/2.2) is visibly wrong near black (0.001 encodes to 0.013 here vs 0.043 with 2.2).
vec3 linearToSrgb(vec3 c) {
c = max(c, 0.0);
return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));
}
// Linear sRGB -> Oklab (L in [0,1], a/b around [-0.4, 0.4]). Matrices from Bjorn Ottosson (public domain / MIT).
vec3 linearSrgbToOklab(vec3 c) {
vec3 lms = c * mat3(0.4122214708, 0.5363325363, 0.0514459929,
0.2119034982, 0.6806995451, 0.1073969566,
0.0883024619, 0.2817188376, 0.6299787005);
lms = sign(lms) * pow(abs(lms), vec3(1.0 / 3.0));
return lms * mat3(0.2104542553, 0.7936177850, -0.0040720468,
1.9779984951, -2.4285922050, 0.4505937099,
0.0259040371, 0.7827717662, -0.8086757660);
}
// Oklab -> linear sRGB. The result can fall outside [0,1] for saturated colors, so clamp after tonemapping.
vec3 oklabToLinearSrgb(vec3 lab) {
vec3 lms = lab * mat3(1.0, 0.3963377774, 0.2158037573,
1.0, -0.1055613458, -0.0638541728,
1.0, -0.0894841775, -1.2914855480);
lms = lms * lms * lms;
return lms * mat3(4.0767416621, -3.3077115913, 0.2309699292,
-1.2684380046, 2.6097574011, -0.3413193965,
-0.0041960863, -0.7034186147, 1.7076147010);
}
// Mix two linear colors in Oklab. Avoids the muddy mid-tones of RGB lerps and the hue detours of HSV.
vec3 mixOklab(vec3 a, vec3 b, float t) {
return oklabToLinearSrgb(mix(linearSrgbToOklab(a), linearSrgbToOklab(b), t));
}
// OkLCh (lightness, chroma, hue in radians) -> linear sRGB. Hue sweeps keep lightness constant. L about 0.4-0.9, C about 0.05-0.2 stays mostly in gamut.
vec3 oklchToLinearSrgb(vec3 lch) {
return oklabToLinearSrgb(vec3(lch.x, lch.y * cos(lch.z), lch.y * sin(lch.z)));
}
// ---- shader-skills library: post ----
// Antialiased fill of a 2D SDF: 1 inside, 0 outside, box-filtered across one pixel. Uses screen derivatives, so call it outside non-uniform branches and loops. For a fixed pixel size use fillAA(d, px).
float fillAA(float d) {
float w = length(vec2(dFdx(d), dFdy(d)));
return clamp(0.5 - d / max(w, 1e-6), 0.0, 1.0);
}
// Antialiased fill with an explicit pixel size px (e.g. 2.0 / iResolution.y for the standard uv). Safe in loops and branches.
float fillAA(float d, float px) { return clamp(0.5 - d / px, 0.0, 1.0); }
// Triangular-PDF dither of +-1 LSB for 8-bit output. Add AFTER linearToSrgb, right before writing fragColor.
vec3 ditherTPDF(vec2 fragCoord, int frame) {
vec2 r = hash22(vec2(floor(fragCoord) + 7919.0 * float(frame % 1024)));
return vec3((r.x + r.y - 1.0) / 255.0);
}
// ---- shader-skills library: debug ----
// Distinct color per integer id.
vec3 debugIdColor(float id) {
return 0.55 + 0.45 * cos(TAU * (0.618034 * id + vec3(0.0, 0.33, 0.67)));
}
// ---- shader ----
vec3 marble(vec2 p) {
vec2 q, r;
float f = warpedFbm(p * 1.5, 3.0, 5, q, r);
vec3 col = mixOklab(srgbToLinear(vec3(0.92, 0.90, 0.86)), srgbToLinear(vec3(0.35, 0.38, 0.45)), saturate(length(q) * 0.9));
col = mix(col, srgbToLinear(vec3(0.62, 0.42, 0.30)), saturate(r.y * r.y * 2.0));
return col * (0.85 + 0.25 * f);
}
vec3 crackedEarth(vec2 p, float px) {
vec3 v = voronoiBorder(p * 4.0, 0.9);
vec2 cell = floor(p * 4.0 + v.yz);
float tint = hash12(cell);
vec3 col = mix(srgbToLinear(vec3(0.62, 0.44, 0.30)), srgbToLinear(vec3(0.75, 0.58, 0.40)), tint);
col *= 0.75 + 0.25 * smoothstep(0.0, 0.25, v.x); // plates darken toward their curled edges
float plate = fillAA(0.03 - v.x, px * 4.0); // cracks: border distance < 0.03 cell units
return mix(srgbToLinear(vec3(0.12, 0.08, 0.06)), col, plate);
}
vec3 hexTiles(vec2 p, float px) {
vec4 h = hexGrid(p * 5.0);
float edge = hexEdgeDistance(h.xy);
vec3 base = oklchToLinearSrgb(vec3(0.7, 0.09, 3.6 + 0.8 * hash12(h.zw)));
float bevel = smoothstep(0.0, 0.12, edge);
return mix(vec3(0.02), base * (0.7 + 0.3 * bevel), smoothstep(0.03 - px * 5.0, 0.03 + px * 5.0, edge));
}
vec3 truchet(vec2 p, float px) {
float d = truchetArcs(p * 5.0, 0.12) / 5.0;
float stripe = truchetArcs(p * 5.0, 0.03) / 5.0;
vec3 col = srgbToLinear(vec3(0.08, 0.10, 0.16));
col = mix(col, srgbToLinear(vec3(0.95, 0.75, 0.30)), fillAA(d, px));
return mix(col, srgbToLinear(vec3(0.55, 0.20, 0.10)), fillAA(stripe, px));
}
vec3 organicCells(vec2 p) {
float s = smoothVoronoi(p * 5.0, 12.0);
vec3 col = mixOklab(srgbToLinear(vec3(0.95, 0.55, 0.55)), srgbToLinear(vec3(0.35, 0.05, 0.15)), saturate(s * 1.6));
return col * (0.6 + 0.4 * smoothstep(0.1, 0.5, s));
}
vec3 checker(vec2 p, float px) {
// perspective floor: the filtered checker stays grey instead of moire in the distance
float z = 1.0 / max(0.55 - p.y, 0.02);
vec2 floorUV = vec2(p.x * z, z) * 1.5;
float c = filteredChecker(floorUV, vec2(px * z * z * 1.5 + px * z * 1.5));
return mix(vec3(0.05), vec3(0.8), c) * smoothstep(0.55, 0.3, p.y);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.y;
float px = 1.0 / iResolution.y;
float aspect = iResolution.x / iResolution.y;
vec2 grid = vec2(3.0, 2.0);
vec2 tileSize = vec2(aspect, 1.0) / grid;
vec2 id = floor(uv / tileSize);
vec2 local = (uv - id * tileSize) / tileSize.y; // 0..(tile aspect) x 0..1
vec2 p = local - 0.5 * vec2(tileSize.x / tileSize.y, 1.0);
float k = id.x + 3.0 * (1.0 - id.y);
float lpx = px / tileSize.y;
vec3 col;
if (k < 0.5) col = marble(p + 0.05 * iTime);
else if (k < 1.5) col = crackedEarth(p, lpx);
else if (k < 2.5) col = hexTiles(p, lpx);
else if (k < 3.5) col = truchet(p, lpx);
else if (k < 4.5) col = organicCells(p + vec2(0.0, 0.03 * iTime));
else col = checker(p, lpx);
// tile frame
vec2 e = min(local, vec2(tileSize.x / tileSize.y, 1.0) - local);
col *= smoothstep(0.0, 0.02, min(e.x, e.y));
#if DEBUG_MODE == 3
col = debugIdColor(k);
#endif
fragColor = vec4(linearToSrgb(col) + ditherTPDF(fragCoord, iFrame), 1.0);
}