Diamond PenTile RGB emitter array for close-up cybernetic visors, HUD screens, and holo-panels.
Wind-Sorted Sand Ripples shader
wind-sorted-sand-ripples.glsl · 761 lines · GLSL ES 3.00 · MIT
Engine shader code & wrappers
Cellular & Ripple Fields#version 300 es
// Wind-Sorted Sand Ripples (wind-sorted-sand-ripples.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/granular/lib/09_wind_sorted_ripple_crests.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;
out vec4 fragColor;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// -----------------------------------------------------------------------------
// Hash & Procedural Noise Utilities
// -----------------------------------------------------------------------------
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 rot = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * valueNoise(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// -----------------------------------------------------------------------------
// Cellular Sand Grain Field (Voronoi with dome profile, offset, and cell ID)
// -----------------------------------------------------------------------------
struct GrainSample {
float dome; // 0..1 rounded grain elevation
float edge; // distance to grain boundary
vec2 localDir; // vector from grain center to sample point
vec2 id; // stable cell ID for mineral coloration
};
GrainSample sampleGrains(vec2 p, float scale) {
vec2 sp = p * scale;
vec2 n = floor(sp);
vec2 f = fract(sp);
float minDist = 8.0;
float secondDist = 8.0;
vec2 bestVec = vec2(0.0);
vec2 bestId = vec2(0.0);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 g = vec2(float(i), float(j));
vec2 o = hash22(n + g);
// Keep grain centers jittered inside cell for natural close-packing
vec2 r = g + (0.14 + 0.72 * o) - f;
float d2 = dot(r, r);
if (d2 < minDist) {
secondDist = minDist;
minDist = d2;
bestVec = -r;
bestId = n + g;
} else if (d2 < secondDist) {
secondDist = d2;
}
}
}
float d1 = sqrt(minDist);
float d2 = sqrt(secondDist);
float edge = clamp((d2 - d1) * 1.4, 0.0, 1.0);
// Smooth rounded quartz/hematite pebble dome
float rNorm = clamp(d1 / 0.58, 0.0, 1.0);
float dome = sqrt(max(0.0, 1.0 - rNorm * rNorm)) * smoothstep(0.0, 0.18, edge);
GrainSample gs;
gs.dome = dome;
gs.edge = edge;
gs.localDir = bestVec;
gs.id = bestId;
return gs;
}
// -----------------------------------------------------------------------------
// Aeolian Asymmetric Ripple Field
// -----------------------------------------------------------------------------
// Wind blows along windDir: gentle windward stoss slope, sharp crest, steep leeward slip face
const vec2 WIND_DIR = vec2(0.92388, 0.38268); // ~22.5 deg
const vec2 CROSS_DIR = vec2(-0.38268, 0.92388);
struct RippleInfo {
float height; // world-space ripple height contribution
float normH; // 0..1 normalized ripple height (0=trough, 1=crest)
float crestMask; // 0..1 coarse dark-mineral armor concentration
float leeMask; // 0..1 steep leeward slip-face indicator
float phase; // ripple phase for cross-bedding laminae
};
float asymWaveProfile(float x, float skew, out float dProfile) {
// Phase-warped C-infinity asymmetric wave:
// Rising limb spans ~70% of wavelength (gentle stoss), falling ~30% (steep leeward slip face)
float s1 = skew;
float s2 = 0.22 * skew * skew;
float w = x - s1 * sin(x) - s2 * sin(2.0 * x);
float dw = 1.0 - s1 * cos(x) - 2.0 * s2 * cos(2.0 * x);
float s = sin(w);
float c = cos(w);
float h0 = 0.5 + 0.5 * s;
// Sharpen crest slightly and broaden trough like true wind ripples
float h = mix(h0, pow(max(h0, 0.0), 1.48), 0.62);
h += 0.055 * pow(max(h0, 0.0), 3.6);
h = clamp(h / 1.055, 0.0, 1.0);
dProfile = c * dw; // positive on windward stoss, negative on steep leeward slip face
return h;
}
RippleInfo evalRippleField(vec2 xz, float openAmt) {
float u = dot(xz, WIND_DIR);
float v = dot(xz, CROSS_DIR);
// Organic crest sinuosity & gentle aeolian modulation
float warp = 0.36 * sin(v * 2.15 + 0.65)
+ 0.16 * sin(v * 4.8 - 1.1)
+ 0.42 * (fbm2(vec2(u * 0.65, v * 1.15) + vec2(3.1, 7.4)) - 0.5);
// Primary ripple train (~4.5 crests across the swatch)
float freq = 13.6;
float phA = u * freq + warp * 2.35;
// Subtle secondary phase for natural tuning-fork ripple bifurcation on one side
float bifurMask = smoothstep(-0.35, 0.55, v + 0.25 * sin(u * 2.5));
float phB = u * (freq * 1.08) + warp * 2.15 + 1.15;
float skew = mix(0.56, 0.68, openAmt);
float dA, dB;
float hA = asymWaveProfile(phA, skew, dA);
float hB = asymWaveProfile(phB, skew, dB);
// Softly blend trains in a localized defect zone to keep crests crisp everywhere else
float defectZone = exp(-pow((u - 0.18) * 1.9, 2.0)) * smoothstep(0.05, 0.65, bifurMask);
float normH = mix(hA, hB, defectZone * 0.42);
float dProf = mix(dA, dB, defectZone * 0.42);
// Slight natural crest height undulation along the ridge
float crestMod = 0.86 + 0.26 * fbm2(vec2(u * 1.1, v * 2.4) + vec2(11.3, 4.7));
float amp = mix(0.044, 0.102, openAmt) * crestMod;
// Gentle large-scale dune curvature across the specimen
float macroRoll = 0.014 * sin(u * 1.7 + 0.4) + 0.008 * cos(v * 1.9);
// Leeward slip-face mask (steep negative slope just downwind of crest)
float leeMask = smoothstep(-0.10, -0.85, dProf) * smoothstep(0.12, 0.55, normH);
// Wind-sorted grain-size armor mask: coarse dark heavy minerals armor the crests
// and spill slightly down the top of the leeward slip face
float sortNoise = fbm2(vec2(u * 6.5, v * 5.2) + vec2(2.7, 9.1)) - 0.5;
float armorLow = mix(0.64, 0.48, openAmt);
float armorHigh = mix(0.95, 0.84, openAmt);
float crestMask = smoothstep(armorLow, armorHigh, normH + sortNoise * 0.14 + leeMask * 0.07);
// Boost contrast with uOpen
crestMask = clamp(crestMask * mix(0.76, 1.12, openAmt), 0.0, 1.0);
RippleInfo ri;
ri.height = normH * amp + macroRoll;
ri.normH = normH;
ri.crestMask = crestMask;
ri.leeMask = leeMask;
ri.phase = mix(phA, phB, defectZone * 0.42);
return ri;
}
// -----------------------------------------------------------------------------
// Swatch SDF (Bevel-cut geological sand specimen grounded on y = 0)
// -----------------------------------------------------------------------------
float sdRoundBox2D(vec2 p, vec2 b, float r) {
vec2 q = abs(p) - b + r;
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
const vec2 SWATCH_HALF = vec2(1.16, 0.94);
const float SWATCH_BASE_H = 0.235;
float mapHero(vec3 p) {
float openAmt = clamp(uOpen, 0.0, 1.0);
// Fast bounding-box rejection for distant rays
vec3 bp = abs(p - vec3(0.0, 0.22, 0.0)) - vec3(1.26, 0.30, 1.04);
float dBound = length(max(bp, 0.0)) + min(max(bp.x, max(bp.y, bp.z)), 0.0);
if (dBound > 0.16) {
return dBound;
}
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
// Evaluate ripple surface height
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Subtle real geometric micro-displacement for coarse armored crest grains
float coarseDisp = 0.0;
if (abs(p.y - (SWATCH_BASE_H + ri.height)) < 0.06 && dXZ < 0.04) {
GrainSample cg = sampleGrains(p.xz, 38.0);
coarseDisp = cg.dome * (0.0028 + 0.0072 * ri.crestMask * mix(0.75, 1.25, openAmt));
// Subtle fine-grain ripple micro-roughness in troughs
float fineMicro = valueNoise(p.xz * 95.0) * 0.0014 * (1.0 - ri.crestMask);
coarseDisp += fineMicro;
}
float topH = SWATCH_BASE_H + ri.height + coarseDisp;
// 45-degree clean bevel cut around top perimeter of the specimen
float bevelWidth = 0.048;
float dTop = p.y - topH;
float dSide = dXZ;
float dBot = -p.y;
float dBevel = (dXZ + (p.y - (topH - bevelWidth))) * 0.70710678;
// Tiny bottom chamfer so specimen seats crisply on the bench
float dBotBevel = (dXZ + (-p.y + 0.012)) * 0.70710678;
float d = smax(dSide, dBot, 0.006);
d = smax(d, dTop, 0.007);
d = smax(d, dBevel, 0.007);
d = smax(d, dBotBevel, 0.004);
// Conservative Lipschitz factor for steep leeward slip faces & coarse crest domes
return d * 0.62;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadow, Ambient Occlusion (Strict Zero-Bug Rules)
// -----------------------------------------------------------------------------
vec3 calcNormal(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.0014;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
float softShadowHero(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 64; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
if (res < 0.001 || t > tMax) break;
t += max(h, 0.015);
}
return clamp(res, 0.0, 1.0);
}
float calcAO(vec3 p, vec3 n) {
float occ = 0.0;
float sca = 1.0;
for (int i = 0; i < 5; i++) {
float h = 0.018 + 0.042 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.18, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Softboxes + Negative-Fill Flags
// -----------------------------------------------------------------------------
float softboxRect(vec3 rd, vec3 centerDir, vec3 upHint, vec2 halfSize, float softness) {
vec3 f = normalize(centerDir);
vec3 r = normalize(cross(upHint, f));
vec3 u = cross(f, r);
float d = dot(rd, f);
if (d <= 0.05) return 0.0;
vec2 uv = vec2(dot(rd, r), dot(rd, u)) / d;
vec2 q = abs(uv) - halfSize;
float dist = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
return smoothstep(softness, -softness * 0.25, dist);
}
vec3 studioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient (neutral dark-to-mid studio room)
vec3 zenith = (lightMode == 1) ? vec3(0.26) : vec3(0.25, 0.27, 0.30);
vec3 horizon = (lightMode == 1) ? vec3(0.16) : vec3(0.16, 0.165, 0.175);
vec3 floorBounce = (lightMode == 1) ? vec3(0.11) : vec3(0.12, 0.115, 0.11);
vec3 col = mix(horizon, zenith, smoothstep(0.0, 0.85, max(y, 0.0)));
col = mix(col, floorBounce, smoothstep(0.0, -0.55, y));
// Light-mode specific softboxes
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.85);
rimCol = vec3(1.25);
} else if (lightMode == 2) {
// Grazing raking light across ripples
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(2.75, 2.38, 1.95);
fillCol = vec3(0.62, 0.70, 0.84);
rimCol = vec3(1.15, 1.08, 0.98);
} else if (lightMode == 3) {
// Backlit high-contrast rim studio
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.80, 2.55, 2.25);
fillCol = vec3(0.75, 0.80, 0.90);
rimCol = vec3(1.90, 1.75, 1.55);
} else {
// Mode 0: Hero museum studio lighting
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.45, 2.32, 2.12);
fillCol = vec3(0.74, 0.82, 0.95);
rimCol = vec3(1.35, 1.26, 1.15);
}
float sbKey = softboxRect(rd, keyDir, vec3(0.0, 1.0, 0.0), vec2(0.42, 0.28), 0.14);
float sbFill = softboxRect(rd, fillDir, vec3(0.0, 1.0, 0.0), vec2(0.50, 0.36), 0.20);
float sbRim = softboxRect(rd, rimDir, vec3(0.0, 1.0, 0.0), vec2(0.65, 0.16), 0.12);
float sbTop = softboxRect(rd, vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.55, 0.45), 0.25);
col += keyCol * sbKey * 0.95;
col += fillCol * sbFill * 0.60;
col += rimCol * sbRim * 0.75;
col += ((lightMode == 1) ? vec3(0.48) : vec3(0.46, 0.48, 0.52)) * sbTop;
// Negative-fill black flags to carve crisp specular contrast on hematite/quartz grains
float negFlagL = softboxRect(rd, normalize(vec3(-0.90, 0.12, -0.42)), vec3(0.0, 1.0, 0.0), vec2(0.32, 0.42), 0.10);
float negFlagR = softboxRect(rd, normalize(vec3( 0.88, 0.10, -0.46)), vec3(0.0, 1.0, 0.0), vec2(0.30, 0.40), 0.10);
col *= (1.0 - 0.72 * max(negFlagL, negFlagR));
return col;
}
// -----------------------------------------------------------------------------
// PBR Helpers (GGX + Schlick Fresnel + ACES Tone Mapping)
// -----------------------------------------------------------------------------
float D_GGX(float NoH, float rough) {
float a = rough * rough;
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / max(PI * d * d, 1e-5);
}
float G_Smith(float NoV, float NoL, float rough) {
float r = rough + 1.0;
float k = (r * r) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k);
float gl = NoL / (NoL * (1.0 - k) + k);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalLight(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, vec3 F0, float sssAmt) {
float NoL = max(dot(N, L), 0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(L + V);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F = F_Schlick(VoH, F0);
float D = D_GGX(NoH, rough);
float G = G_Smith(NoV, NoL, rough);
vec3 spec = (D * G * F) / max(4.0 * NoV * NoL, 0.001);
vec3 kD = (1.0 - F);
// Subtle Oren-Nayar / sand multiple-scattering wrap diffuse
float wrapNoL = max((dot(N, L) + 0.18) / 1.18, 0.0);
vec3 diff = (kD * albedo / PI) * mix(NoL, wrapNoL, sssAmt);
return (diff + spec * NoL) * lightCol;
}
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// -----------------------------------------------------------------------------
// Material Evaluation: Wind-Sorted Desert Sand Specimen & Cut Cross-Section
// -----------------------------------------------------------------------------
struct MatParams {
vec3 albedo;
vec3 normal;
float rough;
vec3 F0;
float sss;
float glint;
};
MatParams evalHeroMaterial(vec3 p, vec3 geomN, vec3 V, float openAmt) {
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Detect whether we are on the top wind-sculpted ripple surface vs. bevel-cut side walls
float topMask = smoothstep(0.42, 0.78, geomN.y);
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float bevelMask = smoothstep(-0.065, -0.015, dXZ) * (1.0 - smoothstep(0.75, 0.94, geomN.y));
// --- 1. Coarse Armored Dark-Mineral / Hematite Grains (Crests) ---
GrainSample coarseG = sampleGrains(p.xz, 38.0);
vec3 cid = hash32(coarseG.id + vec2(4.2, 8.9));
// Authentic heavy-mineral crest assemblage: hematite, magnetite, dark basalt, iron-stained quartz
vec3 hematiteCol = vec3(0.17, 0.085, 0.065);
vec3 magnetiteCol = vec3(0.085, 0.085, 0.095);
vec3 jasperCol = vec3(0.32, 0.145, 0.085);
vec3 smokyQCol = vec3(0.28, 0.195, 0.135);
vec3 coarseCol;
float coarseRough;
vec3 coarseF0;
if (cid.x < 0.42) {
coarseCol = mix(hematiteCol, jasperCol, cid.y * 0.45);
coarseRough = mix(0.28, 0.42, cid.z);
coarseF0 = vec3(0.10, 0.08, 0.075);
} else if (cid.x < 0.78) {
coarseCol = mix(magnetiteCol, vec3(0.13, 0.125, 0.13), cid.y);
coarseRough = mix(0.24, 0.38, cid.z);
coarseF0 = vec3(0.12, 0.12, 0.13);
} else {
coarseCol = mix(smokyQCol, jasperCol, cid.y * 0.5);
coarseRough = mix(0.36, 0.50, cid.z);
coarseF0 = vec3(0.06);
}
// Darken micro-crevices between coarse grains
coarseCol *= mix(0.55, 1.08, smoothstep(0.0, 0.35, coarseG.edge));
// --- 2. Fine Pale Golden-Tan Quartz Sand (Troughs & Lower Stoss) ---
GrainSample fineG = sampleGrains(p.xz, 118.0);
vec3 fid = hash32(fineG.id + vec2(19.7, 3.3));
vec3 quartzPale = vec3(0.84, 0.71, 0.52);
vec3 quartzGold = vec3(0.73, 0.57, 0.38);
vec3 quartzWarm = vec3(0.64, 0.47, 0.31);
vec3 fineCol = mix(quartzGold, quartzPale, fid.x);
fineCol = mix(fineCol, quartzWarm, fid.y * 0.45);
// Subtle 5% scattered fine dark mineral specks in the trough sand
float fineSpeck = step(0.95, fid.z);
fineCol = mix(fineCol, vec3(0.16, 0.12, 0.10), fineSpeck * 0.72);
fineCol *= mix(0.76, 1.04, smoothstep(0.0, 0.25, fineG.edge));
// Slightly warmer/brighter sunlit crest-to-trough sorting tint
float uWind = dot(p.xz, WIND_DIR);
float vCross = dot(p.xz, CROSS_DIR);
float streakNoise = fbm2(vec2(uWind * 14.0, vCross * 3.8));
fineCol *= (0.94 + 0.12 * streakNoise);
// Subtle leeward slip-face avalanche darkening & scattered rolled dark grains
float rolledDark = smoothstep(0.74, 0.92, hash12(coarseG.id + 31.4)) * ri.leeMask * mix(0.35, 0.75, openAmt);
float armorBlend = clamp(max(ri.crestMask, rolledDark * 0.85), 0.0, 1.0);
// Crisp grain-scale transition: coarse grains sit physically on top of fine sand
float grainOverlay = smoothstep(0.18, 0.55, armorBlend * (0.62 + 0.72 * coarseG.dome));
vec3 topAlbedo = mix(fineCol, coarseCol, grainOverlay);
float topRough = mix(mix(0.62, 0.76, fid.y), coarseRough, grainOverlay);
vec3 topF0 = mix(vec3(0.042), coarseF0, grainOverlay);
// Micro-normal perturbation from coarse armored grains & fine quartz grains
vec2 coarseGrad = coarseG.localDir * (1.85 * coarseG.dome) * grainOverlay;
vec2 fineGrad = fineG.localDir * (0.92 * fineG.dome) * (1.0 - grainOverlay * 0.7);
// Subtle wind-streak micro-ripple on leeward slip face
float slipMicro = (valueNoise(vec2(uWind * 42.0, vCross * 12.0)) - 0.5) * 0.14 * ri.leeMask;
vec3 topN = normalize(geomN + vec3(coarseGrad.x + fineGrad.x + slipMicro, 0.0, coarseGrad.y + fineGrad.y));
// --- 3. Bevel-Cut Side Wall Cross-Bedding Laminae ---
// Dipping aeolian cross-strata visible on the precision-cut geological specimen walls
float strataCoord = p.y * 44.0 + uWind * 14.5 + 1.4 * fbm2(vec2(uWind * 3.5, vCross * 3.5));
float strataBand = 0.5 + 0.5 * sin(strataCoord);
float heavySeam = smoothstep(0.80, 0.96, sin(strataCoord * 0.5 + 0.9));
vec3 cutQuartz = mix(vec3(0.64, 0.50, 0.34), vec3(0.78, 0.65, 0.47), strataBand);
vec3 cutAlbedo = mix(cutQuartz, vec3(0.22, 0.15, 0.12), heavySeam * mix(0.45, 0.78, openAmt));
// Fine cut-sand grain texture on vertical walls
float cutGrain = valueNoise(vec2(p.x + p.z, p.y) * 140.0);
cutAlbedo *= (0.90 + 0.18 * cutGrain);
// Darken slightly near bottom seating edge for grounded realism
cutAlbedo *= mix(0.84, 1.0, smoothstep(0.0, 0.06, p.y));
// Subtle micro-bump on cut side walls
vec3 cutN = normalize(geomN + (vec3(cutGrain, valueNoise(p.xz * 130.0), cutGrain) - 0.5) * 0.06);
// Combine top surface, bevel, and cut sides
vec3 finalAlbedo = mix(cutAlbedo, topAlbedo, topMask);
// Slightly crisper lighter dust along the clean bevel cut
finalAlbedo = mix(finalAlbedo, mix(cutAlbedo, topAlbedo, 0.65) * 1.03, bevelMask * 0.35);
vec3 finalN = normalize(mix(cutN, topN, topMask));
float finalRough = mix(0.72, topRough, topMask);
vec3 finalF0 = mix(vec3(0.038), topF0, topMask);
// Micro-facet crystalline quartz & hematite specular glints
vec3 glintSeed = hash32((grainOverlay > 0.5 ? coarseG.id : fineG.id) + vec2(71.3, 12.9));
vec3 randFacet = normalize(finalN + (glintSeed - 0.5) * 0.95);
float glint = pow(max(dot(reflect(-V, randFacet), normalize(vec3(-0.6, 0.7, 0.4))), 0.0), 48.0);
glint *= topMask * mix(0.18, 0.48, grainOverlay);
MatParams m;
m.albedo = finalAlbedo;
m.normal = finalN;
m.rough = finalRough;
m.F0 = finalF0;
m.sss = mix(0.10, mix(0.32, 0.08, grainOverlay), topMask);
m.glint = glint;
return m;
}
// -----------------------------------------------------------------------------
// Scene Shading
// -----------------------------------------------------------------------------
vec3 shadeScene(vec3 ro, vec3 rd) {
// Raymarch scene
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
if (!hit) {
return studioEnv(rd, uLightMode) * 0.85;
}
vec3 p = ro + rd * t;
vec3 V = -rd;
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
// Lighting rig matching studioEnv
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
float keyShadowK;
if (uLightMode == 1) {
// Pure neutral grayscale studio light
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.72);
rimCol = vec3(0.95);
keyShadowK = 10.0;
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(3.15, 2.72, 2.22);
fillCol = vec3(0.55, 0.62, 0.74);
rimCol = vec3(0.95, 0.90, 0.82);
keyShadowK = 14.0;
} else if (uLightMode == 3) {
// Backlit studio light
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.90, 2.65, 2.35);
fillCol = vec3(0.88, 0.94, 1.05);
rimCol = vec3(1.45, 1.32, 1.18);
keyShadowK = 11.0;
} else {
// Mode 0: Hero studio light
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.55, 2.40, 2.18);
fillCol = vec3(0.68, 0.76, 0.88);
rimCol = vec3(1.05, 0.98, 0.90);
keyShadowK = 10.5;
}
vec3 col = vec3(0.0);
if (isBench) {
vec3 N = vec3(0.0, 1.0, 0.0);
// Subtle architectural studio slate/mineral bench grain
float bNoise = fbm2(p.xz * 2.2);
float bFine = valueNoise(p.xz * 48.0);
vec3 benchBase = (uLightMode == 1) ? vec3(0.22) : vec3(0.22, 0.215, 0.208);
vec3 albedo = benchBase * (0.93 + 0.12 * bNoise) * (0.97 + 0.06 * bFine);
float rough = mix(0.66, 0.78, bNoise);
vec3 F0 = vec3(0.035);
float shKey = softShadowHero(p + N * 0.014, keyDir, 5.5, keyShadowK);
float shFill = softShadowHero(p + N * 0.014, fillDir, 5.5, 5.0);
float ao = calcAO(p, N);
// Tight contact shadow right beneath the specimen perimeter
float dEdge = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float contactAO = smoothstep(-0.02, 0.14, dEdge);
ao *= mix(0.38, 1.0, contactAO);
col += evalLight(N, V, keyDir, keyCol * shKey, albedo, rough, F0, 0.0);
col += evalLight(N, V, fillDir, fillCol * shFill, albedo, rough, F0, 0.0);
col += evalLight(N, V, rimDir, rimCol * 0.65, albedo, rough, F0, 0.0);
// Studio IBL ambient + subtle reflection on matte bench
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(reflect(-V, N), uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), F0);
col += (albedo * envDiff * 0.42 + envSpec * F * (1.0 - rough) * 0.45) * ao;
// Smooth studio horizon fade into cyclorama backdrop
float distFade = smoothstep(3.2, 11.5, length(p.xz));
col = mix(col, studioEnv(rd, uLightMode) * 0.85, distFade);
} else {
vec3 geomN = calcNormal(p);
float openAmt = clamp(uOpen, 0.0, 1.0);
MatParams mat = evalHeroMaterial(p, geomN, V, openAmt);
vec3 N = mat.normal;
float shKey = softShadowHero(p + geomN * 0.014, keyDir, 4.5, keyShadowK);
float shFill = softShadowHero(p + geomN * 0.014, fillDir, 4.5, 5.5);
float ao = calcAO(p, geomN);
col += evalLight(N, V, keyDir, keyCol * shKey, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, fillDir, fillCol * shFill, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, rimDir, rimCol, mat.albedo, mat.rough, mat.F0, mat.sss);
// Micro-crystal specular glints from key & rim lights
col += keyCol * mat.glint * shKey * 0.22;
// Studio IBL diffuse + specular reflection
vec3 R = reflect(-V, N);
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(R, uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), mat.F0);
col += mat.albedo * envDiff * 0.46 * ao;
col += envSpec * F * mix(0.65, 0.16, mat.rough) * ao;
// Warm inter-grain sand multiple-scattering bounce in shadowed troughs
vec3 sandBounce = (uLightMode == 1) ? vec3(0.055) : vec3(0.075, 0.055, 0.036);
col += mat.albedo * sandBounce * (0.45 + 0.55 * ao);
}
return col;
}
// -----------------------------------------------------------------------------
// Camera Setup & Main
// -----------------------------------------------------------------------------
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / max(uResolution.y, 1.0);
// Subtle gentle breathing motion when uTime advances; exact hero framing at uTime = 0
float yaw = 0.08 * sin(uTime * 0.22);
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x Macro camera inspecting 2-3 ripple crests & coarse-vs-fine grain sorting
ro = vec3(0.42 + 0.12 * sin(yaw), 0.78, 0.92 + 0.08 * cos(yaw));
ta = vec3(-0.04, 0.27, 0.02);
focal = 2.35;
} else {
// Hero 3/4 museum specimen framing (full bevel-cut swatch + shadow cleanly inside frame)
float r = 3.42;
float baseAng = 0.54 + yaw;
ro = vec3(sin(baseAng) * r, 1.92, cos(baseAng) * r);
ta = vec3(0.0, 0.14, 0.0);
focal = 1.58;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);
vec3 col = shadeScene(ro, rd);
// Exposure calibration for ACES (keeps hero luminance comfortably in 15..235)
col *= 0.78;
col = acesToneMap(col);
// Gentle studio vignette
float vig = 1.0 - 0.12 * dot(uv, uv);
col *= vig;
// Linear to sRGB
col = pow(max(col, 0.0), vec3(1.0 / 2.2));
// Pure neutral grayscale output for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
fragColor = vec4(col, 1.0);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Wind-Sorted Sand Ripples — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/granular/lib/09_wind_sorted_ripple_crests.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// -----------------------------------------------------------------------------
// Hash & Procedural Noise Utilities
// -----------------------------------------------------------------------------
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 rot = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * valueNoise(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// -----------------------------------------------------------------------------
// Cellular Sand Grain Field (Voronoi with dome profile, offset, and cell ID)
// -----------------------------------------------------------------------------
struct GrainSample {
float dome; // 0..1 rounded grain elevation
float edge; // distance to grain boundary
vec2 localDir; // vector from grain center to sample point
vec2 id; // stable cell ID for mineral coloration
};
GrainSample sampleGrains(vec2 p, float scale) {
vec2 sp = p * scale;
vec2 n = floor(sp);
vec2 f = fract(sp);
float minDist = 8.0;
float secondDist = 8.0;
vec2 bestVec = vec2(0.0);
vec2 bestId = vec2(0.0);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 g = vec2(float(i), float(j));
vec2 o = hash22(n + g);
// Keep grain centers jittered inside cell for natural close-packing
vec2 r = g + (0.14 + 0.72 * o) - f;
float d2 = dot(r, r);
if (d2 < minDist) {
secondDist = minDist;
minDist = d2;
bestVec = -r;
bestId = n + g;
} else if (d2 < secondDist) {
secondDist = d2;
}
}
}
float d1 = sqrt(minDist);
float d2 = sqrt(secondDist);
float edge = clamp((d2 - d1) * 1.4, 0.0, 1.0);
// Smooth rounded quartz/hematite pebble dome
float rNorm = clamp(d1 / 0.58, 0.0, 1.0);
float dome = sqrt(max(0.0, 1.0 - rNorm * rNorm)) * smoothstep(0.0, 0.18, edge);
GrainSample gs;
gs.dome = dome;
gs.edge = edge;
gs.localDir = bestVec;
gs.id = bestId;
return gs;
}
// -----------------------------------------------------------------------------
// Aeolian Asymmetric Ripple Field
// -----------------------------------------------------------------------------
// Wind blows along windDir: gentle windward stoss slope, sharp crest, steep leeward slip face
const vec2 WIND_DIR = vec2(0.92388, 0.38268); // ~22.5 deg
const vec2 CROSS_DIR = vec2(-0.38268, 0.92388);
struct RippleInfo {
float height; // world-space ripple height contribution
float normH; // 0..1 normalized ripple height (0=trough, 1=crest)
float crestMask; // 0..1 coarse dark-mineral armor concentration
float leeMask; // 0..1 steep leeward slip-face indicator
float phase; // ripple phase for cross-bedding laminae
};
float asymWaveProfile(float x, float skew, out float dProfile) {
// Phase-warped C-infinity asymmetric wave:
// Rising limb spans ~70% of wavelength (gentle stoss), falling ~30% (steep leeward slip face)
float s1 = skew;
float s2 = 0.22 * skew * skew;
float w = x - s1 * sin(x) - s2 * sin(2.0 * x);
float dw = 1.0 - s1 * cos(x) - 2.0 * s2 * cos(2.0 * x);
float s = sin(w);
float c = cos(w);
float h0 = 0.5 + 0.5 * s;
// Sharpen crest slightly and broaden trough like true wind ripples
float h = mix(h0, pow(max(h0, 0.0), 1.48), 0.62);
h += 0.055 * pow(max(h0, 0.0), 3.6);
h = clamp(h / 1.055, 0.0, 1.0);
dProfile = c * dw; // positive on windward stoss, negative on steep leeward slip face
return h;
}
RippleInfo evalRippleField(vec2 xz, float openAmt) {
float u = dot(xz, WIND_DIR);
float v = dot(xz, CROSS_DIR);
// Organic crest sinuosity & gentle aeolian modulation
float warp = 0.36 * sin(v * 2.15 + 0.65)
+ 0.16 * sin(v * 4.8 - 1.1)
+ 0.42 * (fbm2(vec2(u * 0.65, v * 1.15) + vec2(3.1, 7.4)) - 0.5);
// Primary ripple train (~4.5 crests across the swatch)
float freq = 13.6;
float phA = u * freq + warp * 2.35;
// Subtle secondary phase for natural tuning-fork ripple bifurcation on one side
float bifurMask = smoothstep(-0.35, 0.55, v + 0.25 * sin(u * 2.5));
float phB = u * (freq * 1.08) + warp * 2.15 + 1.15;
float skew = mix(0.56, 0.68, openAmt);
float dA, dB;
float hA = asymWaveProfile(phA, skew, dA);
float hB = asymWaveProfile(phB, skew, dB);
// Softly blend trains in a localized defect zone to keep crests crisp everywhere else
float defectZone = exp(-pow((u - 0.18) * 1.9, 2.0)) * smoothstep(0.05, 0.65, bifurMask);
float normH = mix(hA, hB, defectZone * 0.42);
float dProf = mix(dA, dB, defectZone * 0.42);
// Slight natural crest height undulation along the ridge
float crestMod = 0.86 + 0.26 * fbm2(vec2(u * 1.1, v * 2.4) + vec2(11.3, 4.7));
float amp = mix(0.044, 0.102, openAmt) * crestMod;
// Gentle large-scale dune curvature across the specimen
float macroRoll = 0.014 * sin(u * 1.7 + 0.4) + 0.008 * cos(v * 1.9);
// Leeward slip-face mask (steep negative slope just downwind of crest)
float leeMask = smoothstep(-0.10, -0.85, dProf) * smoothstep(0.12, 0.55, normH);
// Wind-sorted grain-size armor mask: coarse dark heavy minerals armor the crests
// and spill slightly down the top of the leeward slip face
float sortNoise = fbm2(vec2(u * 6.5, v * 5.2) + vec2(2.7, 9.1)) - 0.5;
float armorLow = mix(0.64, 0.48, openAmt);
float armorHigh = mix(0.95, 0.84, openAmt);
float crestMask = smoothstep(armorLow, armorHigh, normH + sortNoise * 0.14 + leeMask * 0.07);
// Boost contrast with uOpen
crestMask = clamp(crestMask * mix(0.76, 1.12, openAmt), 0.0, 1.0);
RippleInfo ri;
ri.height = normH * amp + macroRoll;
ri.normH = normH;
ri.crestMask = crestMask;
ri.leeMask = leeMask;
ri.phase = mix(phA, phB, defectZone * 0.42);
return ri;
}
// -----------------------------------------------------------------------------
// Swatch SDF (Bevel-cut geological sand specimen grounded on y = 0)
// -----------------------------------------------------------------------------
float sdRoundBox2D(vec2 p, vec2 b, float r) {
vec2 q = abs(p) - b + r;
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
const vec2 SWATCH_HALF = vec2(1.16, 0.94);
const float SWATCH_BASE_H = 0.235;
float mapHero(vec3 p) {
float openAmt = clamp(uOpen, 0.0, 1.0);
// Fast bounding-box rejection for distant rays
vec3 bp = abs(p - vec3(0.0, 0.22, 0.0)) - vec3(1.26, 0.30, 1.04);
float dBound = length(max(bp, 0.0)) + min(max(bp.x, max(bp.y, bp.z)), 0.0);
if (dBound > 0.16) {
return dBound;
}
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
// Evaluate ripple surface height
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Subtle real geometric micro-displacement for coarse armored crest grains
float coarseDisp = 0.0;
if (abs(p.y - (SWATCH_BASE_H + ri.height)) < 0.06 && dXZ < 0.04) {
GrainSample cg = sampleGrains(p.xz, 38.0);
coarseDisp = cg.dome * (0.0028 + 0.0072 * ri.crestMask * mix(0.75, 1.25, openAmt));
// Subtle fine-grain ripple micro-roughness in troughs
float fineMicro = valueNoise(p.xz * 95.0) * 0.0014 * (1.0 - ri.crestMask);
coarseDisp += fineMicro;
}
float topH = SWATCH_BASE_H + ri.height + coarseDisp;
// 45-degree clean bevel cut around top perimeter of the specimen
float bevelWidth = 0.048;
float dTop = p.y - topH;
float dSide = dXZ;
float dBot = -p.y;
float dBevel = (dXZ + (p.y - (topH - bevelWidth))) * 0.70710678;
// Tiny bottom chamfer so specimen seats crisply on the bench
float dBotBevel = (dXZ + (-p.y + 0.012)) * 0.70710678;
float d = smax(dSide, dBot, 0.006);
d = smax(d, dTop, 0.007);
d = smax(d, dBevel, 0.007);
d = smax(d, dBotBevel, 0.004);
// Conservative Lipschitz factor for steep leeward slip faces & coarse crest domes
return d * 0.62;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadow, Ambient Occlusion (Strict Zero-Bug Rules)
// -----------------------------------------------------------------------------
vec3 calcNormal(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.0014;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
float softShadowHero(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 64; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
if (res < 0.001 || t > tMax) break;
t += max(h, 0.015);
}
return clamp(res, 0.0, 1.0);
}
float calcAO(vec3 p, vec3 n) {
float occ = 0.0;
float sca = 1.0;
for (int i = 0; i < 5; i++) {
float h = 0.018 + 0.042 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.18, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Softboxes + Negative-Fill Flags
// -----------------------------------------------------------------------------
float softboxRect(vec3 rd, vec3 centerDir, vec3 upHint, vec2 halfSize, float softness) {
vec3 f = normalize(centerDir);
vec3 r = normalize(cross(upHint, f));
vec3 u = cross(f, r);
float d = dot(rd, f);
if (d <= 0.05) return 0.0;
vec2 uv = vec2(dot(rd, r), dot(rd, u)) / d;
vec2 q = abs(uv) - halfSize;
float dist = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
return smoothstep(softness, -softness * 0.25, dist);
}
vec3 studioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient (neutral dark-to-mid studio room)
vec3 zenith = (lightMode == 1) ? vec3(0.26) : vec3(0.25, 0.27, 0.30);
vec3 horizon = (lightMode == 1) ? vec3(0.16) : vec3(0.16, 0.165, 0.175);
vec3 floorBounce = (lightMode == 1) ? vec3(0.11) : vec3(0.12, 0.115, 0.11);
vec3 col = mix(horizon, zenith, smoothstep(0.0, 0.85, max(y, 0.0)));
col = mix(col, floorBounce, smoothstep(0.0, -0.55, y));
// Light-mode specific softboxes
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.85);
rimCol = vec3(1.25);
} else if (lightMode == 2) {
// Grazing raking light across ripples
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(2.75, 2.38, 1.95);
fillCol = vec3(0.62, 0.70, 0.84);
rimCol = vec3(1.15, 1.08, 0.98);
} else if (lightMode == 3) {
// Backlit high-contrast rim studio
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.80, 2.55, 2.25);
fillCol = vec3(0.75, 0.80, 0.90);
rimCol = vec3(1.90, 1.75, 1.55);
} else {
// Mode 0: Hero museum studio lighting
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.45, 2.32, 2.12);
fillCol = vec3(0.74, 0.82, 0.95);
rimCol = vec3(1.35, 1.26, 1.15);
}
float sbKey = softboxRect(rd, keyDir, vec3(0.0, 1.0, 0.0), vec2(0.42, 0.28), 0.14);
float sbFill = softboxRect(rd, fillDir, vec3(0.0, 1.0, 0.0), vec2(0.50, 0.36), 0.20);
float sbRim = softboxRect(rd, rimDir, vec3(0.0, 1.0, 0.0), vec2(0.65, 0.16), 0.12);
float sbTop = softboxRect(rd, vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.55, 0.45), 0.25);
col += keyCol * sbKey * 0.95;
col += fillCol * sbFill * 0.60;
col += rimCol * sbRim * 0.75;
col += ((lightMode == 1) ? vec3(0.48) : vec3(0.46, 0.48, 0.52)) * sbTop;
// Negative-fill black flags to carve crisp specular contrast on hematite/quartz grains
float negFlagL = softboxRect(rd, normalize(vec3(-0.90, 0.12, -0.42)), vec3(0.0, 1.0, 0.0), vec2(0.32, 0.42), 0.10);
float negFlagR = softboxRect(rd, normalize(vec3( 0.88, 0.10, -0.46)), vec3(0.0, 1.0, 0.0), vec2(0.30, 0.40), 0.10);
col *= (1.0 - 0.72 * max(negFlagL, negFlagR));
return col;
}
// -----------------------------------------------------------------------------
// PBR Helpers (GGX + Schlick Fresnel + ACES Tone Mapping)
// -----------------------------------------------------------------------------
float D_GGX(float NoH, float rough) {
float a = rough * rough;
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / max(PI * d * d, 1e-5);
}
float G_Smith(float NoV, float NoL, float rough) {
float r = rough + 1.0;
float k = (r * r) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k);
float gl = NoL / (NoL * (1.0 - k) + k);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalLight(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, vec3 F0, float sssAmt) {
float NoL = max(dot(N, L), 0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(L + V);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F = F_Schlick(VoH, F0);
float D = D_GGX(NoH, rough);
float G = G_Smith(NoV, NoL, rough);
vec3 spec = (D * G * F) / max(4.0 * NoV * NoL, 0.001);
vec3 kD = (1.0 - F);
// Subtle Oren-Nayar / sand multiple-scattering wrap diffuse
float wrapNoL = max((dot(N, L) + 0.18) / 1.18, 0.0);
vec3 diff = (kD * albedo / PI) * mix(NoL, wrapNoL, sssAmt);
return (diff + spec * NoL) * lightCol;
}
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// -----------------------------------------------------------------------------
// Material Evaluation: Wind-Sorted Desert Sand Specimen & Cut Cross-Section
// -----------------------------------------------------------------------------
struct MatParams {
vec3 albedo;
vec3 normal;
float rough;
vec3 F0;
float sss;
float glint;
};
MatParams evalHeroMaterial(vec3 p, vec3 geomN, vec3 V, float openAmt) {
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Detect whether we are on the top wind-sculpted ripple surface vs. bevel-cut side walls
float topMask = smoothstep(0.42, 0.78, geomN.y);
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float bevelMask = smoothstep(-0.065, -0.015, dXZ) * (1.0 - smoothstep(0.75, 0.94, geomN.y));
// --- 1. Coarse Armored Dark-Mineral / Hematite Grains (Crests) ---
GrainSample coarseG = sampleGrains(p.xz, 38.0);
vec3 cid = hash32(coarseG.id + vec2(4.2, 8.9));
// Authentic heavy-mineral crest assemblage: hematite, magnetite, dark basalt, iron-stained quartz
vec3 hematiteCol = vec3(0.17, 0.085, 0.065);
vec3 magnetiteCol = vec3(0.085, 0.085, 0.095);
vec3 jasperCol = vec3(0.32, 0.145, 0.085);
vec3 smokyQCol = vec3(0.28, 0.195, 0.135);
vec3 coarseCol;
float coarseRough;
vec3 coarseF0;
if (cid.x < 0.42) {
coarseCol = mix(hematiteCol, jasperCol, cid.y * 0.45);
coarseRough = mix(0.28, 0.42, cid.z);
coarseF0 = vec3(0.10, 0.08, 0.075);
} else if (cid.x < 0.78) {
coarseCol = mix(magnetiteCol, vec3(0.13, 0.125, 0.13), cid.y);
coarseRough = mix(0.24, 0.38, cid.z);
coarseF0 = vec3(0.12, 0.12, 0.13);
} else {
coarseCol = mix(smokyQCol, jasperCol, cid.y * 0.5);
coarseRough = mix(0.36, 0.50, cid.z);
coarseF0 = vec3(0.06);
}
// Darken micro-crevices between coarse grains
coarseCol *= mix(0.55, 1.08, smoothstep(0.0, 0.35, coarseG.edge));
// --- 2. Fine Pale Golden-Tan Quartz Sand (Troughs & Lower Stoss) ---
GrainSample fineG = sampleGrains(p.xz, 118.0);
vec3 fid = hash32(fineG.id + vec2(19.7, 3.3));
vec3 quartzPale = vec3(0.84, 0.71, 0.52);
vec3 quartzGold = vec3(0.73, 0.57, 0.38);
vec3 quartzWarm = vec3(0.64, 0.47, 0.31);
vec3 fineCol = mix(quartzGold, quartzPale, fid.x);
fineCol = mix(fineCol, quartzWarm, fid.y * 0.45);
// Subtle 5% scattered fine dark mineral specks in the trough sand
float fineSpeck = step(0.95, fid.z);
fineCol = mix(fineCol, vec3(0.16, 0.12, 0.10), fineSpeck * 0.72);
fineCol *= mix(0.76, 1.04, smoothstep(0.0, 0.25, fineG.edge));
// Slightly warmer/brighter sunlit crest-to-trough sorting tint
float uWind = dot(p.xz, WIND_DIR);
float vCross = dot(p.xz, CROSS_DIR);
float streakNoise = fbm2(vec2(uWind * 14.0, vCross * 3.8));
fineCol *= (0.94 + 0.12 * streakNoise);
// Subtle leeward slip-face avalanche darkening & scattered rolled dark grains
float rolledDark = smoothstep(0.74, 0.92, hash12(coarseG.id + 31.4)) * ri.leeMask * mix(0.35, 0.75, openAmt);
float armorBlend = clamp(max(ri.crestMask, rolledDark * 0.85), 0.0, 1.0);
// Crisp grain-scale transition: coarse grains sit physically on top of fine sand
float grainOverlay = smoothstep(0.18, 0.55, armorBlend * (0.62 + 0.72 * coarseG.dome));
vec3 topAlbedo = mix(fineCol, coarseCol, grainOverlay);
float topRough = mix(mix(0.62, 0.76, fid.y), coarseRough, grainOverlay);
vec3 topF0 = mix(vec3(0.042), coarseF0, grainOverlay);
// Micro-normal perturbation from coarse armored grains & fine quartz grains
vec2 coarseGrad = coarseG.localDir * (1.85 * coarseG.dome) * grainOverlay;
vec2 fineGrad = fineG.localDir * (0.92 * fineG.dome) * (1.0 - grainOverlay * 0.7);
// Subtle wind-streak micro-ripple on leeward slip face
float slipMicro = (valueNoise(vec2(uWind * 42.0, vCross * 12.0)) - 0.5) * 0.14 * ri.leeMask;
vec3 topN = normalize(geomN + vec3(coarseGrad.x + fineGrad.x + slipMicro, 0.0, coarseGrad.y + fineGrad.y));
// --- 3. Bevel-Cut Side Wall Cross-Bedding Laminae ---
// Dipping aeolian cross-strata visible on the precision-cut geological specimen walls
float strataCoord = p.y * 44.0 + uWind * 14.5 + 1.4 * fbm2(vec2(uWind * 3.5, vCross * 3.5));
float strataBand = 0.5 + 0.5 * sin(strataCoord);
float heavySeam = smoothstep(0.80, 0.96, sin(strataCoord * 0.5 + 0.9));
vec3 cutQuartz = mix(vec3(0.64, 0.50, 0.34), vec3(0.78, 0.65, 0.47), strataBand);
vec3 cutAlbedo = mix(cutQuartz, vec3(0.22, 0.15, 0.12), heavySeam * mix(0.45, 0.78, openAmt));
// Fine cut-sand grain texture on vertical walls
float cutGrain = valueNoise(vec2(p.x + p.z, p.y) * 140.0);
cutAlbedo *= (0.90 + 0.18 * cutGrain);
// Darken slightly near bottom seating edge for grounded realism
cutAlbedo *= mix(0.84, 1.0, smoothstep(0.0, 0.06, p.y));
// Subtle micro-bump on cut side walls
vec3 cutN = normalize(geomN + (vec3(cutGrain, valueNoise(p.xz * 130.0), cutGrain) - 0.5) * 0.06);
// Combine top surface, bevel, and cut sides
vec3 finalAlbedo = mix(cutAlbedo, topAlbedo, topMask);
// Slightly crisper lighter dust along the clean bevel cut
finalAlbedo = mix(finalAlbedo, mix(cutAlbedo, topAlbedo, 0.65) * 1.03, bevelMask * 0.35);
vec3 finalN = normalize(mix(cutN, topN, topMask));
float finalRough = mix(0.72, topRough, topMask);
vec3 finalF0 = mix(vec3(0.038), topF0, topMask);
// Micro-facet crystalline quartz & hematite specular glints
vec3 glintSeed = hash32((grainOverlay > 0.5 ? coarseG.id : fineG.id) + vec2(71.3, 12.9));
vec3 randFacet = normalize(finalN + (glintSeed - 0.5) * 0.95);
float glint = pow(max(dot(reflect(-V, randFacet), normalize(vec3(-0.6, 0.7, 0.4))), 0.0), 48.0);
glint *= topMask * mix(0.18, 0.48, grainOverlay);
MatParams m;
m.albedo = finalAlbedo;
m.normal = finalN;
m.rough = finalRough;
m.F0 = finalF0;
m.sss = mix(0.10, mix(0.32, 0.08, grainOverlay), topMask);
m.glint = glint;
return m;
}
// -----------------------------------------------------------------------------
// Scene Shading
// -----------------------------------------------------------------------------
vec3 shadeScene(vec3 ro, vec3 rd) {
// Raymarch scene
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
if (!hit) {
return studioEnv(rd, uLightMode) * 0.85;
}
vec3 p = ro + rd * t;
vec3 V = -rd;
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
// Lighting rig matching studioEnv
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
float keyShadowK;
if (uLightMode == 1) {
// Pure neutral grayscale studio light
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.72);
rimCol = vec3(0.95);
keyShadowK = 10.0;
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(3.15, 2.72, 2.22);
fillCol = vec3(0.55, 0.62, 0.74);
rimCol = vec3(0.95, 0.90, 0.82);
keyShadowK = 14.0;
} else if (uLightMode == 3) {
// Backlit studio light
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.90, 2.65, 2.35);
fillCol = vec3(0.88, 0.94, 1.05);
rimCol = vec3(1.45, 1.32, 1.18);
keyShadowK = 11.0;
} else {
// Mode 0: Hero studio light
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.55, 2.40, 2.18);
fillCol = vec3(0.68, 0.76, 0.88);
rimCol = vec3(1.05, 0.98, 0.90);
keyShadowK = 10.5;
}
vec3 col = vec3(0.0);
if (isBench) {
vec3 N = vec3(0.0, 1.0, 0.0);
// Subtle architectural studio slate/mineral bench grain
float bNoise = fbm2(p.xz * 2.2);
float bFine = valueNoise(p.xz * 48.0);
vec3 benchBase = (uLightMode == 1) ? vec3(0.22) : vec3(0.22, 0.215, 0.208);
vec3 albedo = benchBase * (0.93 + 0.12 * bNoise) * (0.97 + 0.06 * bFine);
float rough = mix(0.66, 0.78, bNoise);
vec3 F0 = vec3(0.035);
float shKey = softShadowHero(p + N * 0.014, keyDir, 5.5, keyShadowK);
float shFill = softShadowHero(p + N * 0.014, fillDir, 5.5, 5.0);
float ao = calcAO(p, N);
// Tight contact shadow right beneath the specimen perimeter
float dEdge = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float contactAO = smoothstep(-0.02, 0.14, dEdge);
ao *= mix(0.38, 1.0, contactAO);
col += evalLight(N, V, keyDir, keyCol * shKey, albedo, rough, F0, 0.0);
col += evalLight(N, V, fillDir, fillCol * shFill, albedo, rough, F0, 0.0);
col += evalLight(N, V, rimDir, rimCol * 0.65, albedo, rough, F0, 0.0);
// Studio IBL ambient + subtle reflection on matte bench
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(reflect(-V, N), uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), F0);
col += (albedo * envDiff * 0.42 + envSpec * F * (1.0 - rough) * 0.45) * ao;
// Smooth studio horizon fade into cyclorama backdrop
float distFade = smoothstep(3.2, 11.5, length(p.xz));
col = mix(col, studioEnv(rd, uLightMode) * 0.85, distFade);
} else {
vec3 geomN = calcNormal(p);
float openAmt = clamp(uOpen, 0.0, 1.0);
MatParams mat = evalHeroMaterial(p, geomN, V, openAmt);
vec3 N = mat.normal;
float shKey = softShadowHero(p + geomN * 0.014, keyDir, 4.5, keyShadowK);
float shFill = softShadowHero(p + geomN * 0.014, fillDir, 4.5, 5.5);
float ao = calcAO(p, geomN);
col += evalLight(N, V, keyDir, keyCol * shKey, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, fillDir, fillCol * shFill, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, rimDir, rimCol, mat.albedo, mat.rough, mat.F0, mat.sss);
// Micro-crystal specular glints from key & rim lights
col += keyCol * mat.glint * shKey * 0.22;
// Studio IBL diffuse + specular reflection
vec3 R = reflect(-V, N);
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(R, uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), mat.F0);
col += mat.albedo * envDiff * 0.46 * ao;
col += envSpec * F * mix(0.65, 0.16, mat.rough) * ao;
// Warm inter-grain sand multiple-scattering bounce in shadowed troughs
vec3 sandBounce = (uLightMode == 1) ? vec3(0.055) : vec3(0.075, 0.055, 0.036);
col += mat.albedo * sandBounce * (0.45 + 0.55 * ao);
}
return col;
}
// -----------------------------------------------------------------------------
// Camera Setup & Main
// -----------------------------------------------------------------------------
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / max(uResolution.y, 1.0);
// Subtle gentle breathing motion when uTime advances; exact hero framing at uTime = 0
float yaw = 0.08 * sin(uTime * 0.22);
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x Macro camera inspecting 2-3 ripple crests & coarse-vs-fine grain sorting
ro = vec3(0.42 + 0.12 * sin(yaw), 0.78, 0.92 + 0.08 * cos(yaw));
ta = vec3(-0.04, 0.27, 0.02);
focal = 2.35;
} else {
// Hero 3/4 museum specimen framing (full bevel-cut swatch + shadow cleanly inside frame)
float r = 3.42;
float baseAng = 0.54 + yaw;
ro = vec3(sin(baseAng) * r, 1.92, cos(baseAng) * r);
ta = vec3(0.0, 0.14, 0.0);
focal = 1.58;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);
vec3 col = shadeScene(ro, rd);
// Exposure calibration for ACES (keeps hero luminance comfortably in 15..235)
col *= 0.78;
col = acesToneMap(col);
// Gentle studio vignette
float vig = 1.0 - 0.12 * dot(uv, uv);
col *= vig;
// Linear to sRGB
col = pow(max(col, 0.0), vec3(1.0 / 2.2));
// Pure neutral grayscale output for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
fragColor = vec4(col, 1.0);
}
void fragment() {
vec2 tiled_uv = fract(UV * u_tiling);
vec2 frag_xy = vec2(tiled_uv.x, 1.0 - tiled_uv.y) * uResolution;
vec4 study_col = vec4(0.0, 0.0, 0.0, 1.0);
pixelfx_study_eval(frag_xy, study_col);
vec3 tinted = clamp(mix(study_col.rgb, study_col.rgb * u_tint.rgb * 1.35, u_tint_mix), 0.0, 1.0);
float out_alpha = study_col.a * COLOR.a;
if (u_luma_alpha_key) {
float luma = dot(tinted, vec3(0.2126, 0.7152, 0.0722));
out_alpha *= smoothstep(0.035, 0.22, luma);
}
COLOR = vec4(tinted, out_alpha);
}
shader_type spatial;
render_mode cull_disabled;
// Wind-Sorted Sand Ripples — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/granular/lib/09_wind_sorted_ripple_crests.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// -----------------------------------------------------------------------------
// Hash & Procedural Noise Utilities
// -----------------------------------------------------------------------------
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 rot = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * valueNoise(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// -----------------------------------------------------------------------------
// Cellular Sand Grain Field (Voronoi with dome profile, offset, and cell ID)
// -----------------------------------------------------------------------------
struct GrainSample {
float dome; // 0..1 rounded grain elevation
float edge; // distance to grain boundary
vec2 localDir; // vector from grain center to sample point
vec2 id; // stable cell ID for mineral coloration
};
GrainSample sampleGrains(vec2 p, float scale) {
vec2 sp = p * scale;
vec2 n = floor(sp);
vec2 f = fract(sp);
float minDist = 8.0;
float secondDist = 8.0;
vec2 bestVec = vec2(0.0);
vec2 bestId = vec2(0.0);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 g = vec2(float(i), float(j));
vec2 o = hash22(n + g);
// Keep grain centers jittered inside cell for natural close-packing
vec2 r = g + (0.14 + 0.72 * o) - f;
float d2 = dot(r, r);
if (d2 < minDist) {
secondDist = minDist;
minDist = d2;
bestVec = -r;
bestId = n + g;
} else if (d2 < secondDist) {
secondDist = d2;
}
}
}
float d1 = sqrt(minDist);
float d2 = sqrt(secondDist);
float edge = clamp((d2 - d1) * 1.4, 0.0, 1.0);
// Smooth rounded quartz/hematite pebble dome
float rNorm = clamp(d1 / 0.58, 0.0, 1.0);
float dome = sqrt(max(0.0, 1.0 - rNorm * rNorm)) * smoothstep(0.0, 0.18, edge);
GrainSample gs;
gs.dome = dome;
gs.edge = edge;
gs.localDir = bestVec;
gs.id = bestId;
return gs;
}
// -----------------------------------------------------------------------------
// Aeolian Asymmetric Ripple Field
// -----------------------------------------------------------------------------
// Wind blows along windDir: gentle windward stoss slope, sharp crest, steep leeward slip face
const vec2 WIND_DIR = vec2(0.92388, 0.38268); // ~22.5 deg
const vec2 CROSS_DIR = vec2(-0.38268, 0.92388);
struct RippleInfo {
float height; // world-space ripple height contribution
float normH; // 0..1 normalized ripple height (0=trough, 1=crest)
float crestMask; // 0..1 coarse dark-mineral armor concentration
float leeMask; // 0..1 steep leeward slip-face indicator
float phase; // ripple phase for cross-bedding laminae
};
float asymWaveProfile(float x, float skew, out float dProfile) {
// Phase-warped C-infinity asymmetric wave:
// Rising limb spans ~70% of wavelength (gentle stoss), falling ~30% (steep leeward slip face)
float s1 = skew;
float s2 = 0.22 * skew * skew;
float w = x - s1 * sin(x) - s2 * sin(2.0 * x);
float dw = 1.0 - s1 * cos(x) - 2.0 * s2 * cos(2.0 * x);
float s = sin(w);
float c = cos(w);
float h0 = 0.5 + 0.5 * s;
// Sharpen crest slightly and broaden trough like true wind ripples
float h = mix(h0, pow(max(h0, 0.0), 1.48), 0.62);
h += 0.055 * pow(max(h0, 0.0), 3.6);
h = clamp(h / 1.055, 0.0, 1.0);
dProfile = c * dw; // positive on windward stoss, negative on steep leeward slip face
return h;
}
RippleInfo evalRippleField(vec2 xz, float openAmt) {
float u = dot(xz, WIND_DIR);
float v = dot(xz, CROSS_DIR);
// Organic crest sinuosity & gentle aeolian modulation
float warp = 0.36 * sin(v * 2.15 + 0.65)
+ 0.16 * sin(v * 4.8 - 1.1)
+ 0.42 * (fbm2(vec2(u * 0.65, v * 1.15) + vec2(3.1, 7.4)) - 0.5);
// Primary ripple train (~4.5 crests across the swatch)
float freq = 13.6;
float phA = u * freq + warp * 2.35;
// Subtle secondary phase for natural tuning-fork ripple bifurcation on one side
float bifurMask = smoothstep(-0.35, 0.55, v + 0.25 * sin(u * 2.5));
float phB = u * (freq * 1.08) + warp * 2.15 + 1.15;
float skew = mix(0.56, 0.68, openAmt);
float dA, dB;
float hA = asymWaveProfile(phA, skew, dA);
float hB = asymWaveProfile(phB, skew, dB);
// Softly blend trains in a localized defect zone to keep crests crisp everywhere else
float defectZone = exp(-pow((u - 0.18) * 1.9, 2.0)) * smoothstep(0.05, 0.65, bifurMask);
float normH = mix(hA, hB, defectZone * 0.42);
float dProf = mix(dA, dB, defectZone * 0.42);
// Slight natural crest height undulation along the ridge
float crestMod = 0.86 + 0.26 * fbm2(vec2(u * 1.1, v * 2.4) + vec2(11.3, 4.7));
float amp = mix(0.044, 0.102, openAmt) * crestMod;
// Gentle large-scale dune curvature across the specimen
float macroRoll = 0.014 * sin(u * 1.7 + 0.4) + 0.008 * cos(v * 1.9);
// Leeward slip-face mask (steep negative slope just downwind of crest)
float leeMask = smoothstep(-0.10, -0.85, dProf) * smoothstep(0.12, 0.55, normH);
// Wind-sorted grain-size armor mask: coarse dark heavy minerals armor the crests
// and spill slightly down the top of the leeward slip face
float sortNoise = fbm2(vec2(u * 6.5, v * 5.2) + vec2(2.7, 9.1)) - 0.5;
float armorLow = mix(0.64, 0.48, openAmt);
float armorHigh = mix(0.95, 0.84, openAmt);
float crestMask = smoothstep(armorLow, armorHigh, normH + sortNoise * 0.14 + leeMask * 0.07);
// Boost contrast with uOpen
crestMask = clamp(crestMask * mix(0.76, 1.12, openAmt), 0.0, 1.0);
RippleInfo ri;
ri.height = normH * amp + macroRoll;
ri.normH = normH;
ri.crestMask = crestMask;
ri.leeMask = leeMask;
ri.phase = mix(phA, phB, defectZone * 0.42);
return ri;
}
// -----------------------------------------------------------------------------
// Swatch SDF (Bevel-cut geological sand specimen grounded on y = 0)
// -----------------------------------------------------------------------------
float sdRoundBox2D(vec2 p, vec2 b, float r) {
vec2 q = abs(p) - b + r;
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
const vec2 SWATCH_HALF = vec2(1.16, 0.94);
const float SWATCH_BASE_H = 0.235;
float mapHero(vec3 p) {
float openAmt = clamp(uOpen, 0.0, 1.0);
// Fast bounding-box rejection for distant rays
vec3 bp = abs(p - vec3(0.0, 0.22, 0.0)) - vec3(1.26, 0.30, 1.04);
float dBound = length(max(bp, 0.0)) + min(max(bp.x, max(bp.y, bp.z)), 0.0);
if (dBound > 0.16) {
return dBound;
}
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
// Evaluate ripple surface height
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Subtle real geometric micro-displacement for coarse armored crest grains
float coarseDisp = 0.0;
if (abs(p.y - (SWATCH_BASE_H + ri.height)) < 0.06 && dXZ < 0.04) {
GrainSample cg = sampleGrains(p.xz, 38.0);
coarseDisp = cg.dome * (0.0028 + 0.0072 * ri.crestMask * mix(0.75, 1.25, openAmt));
// Subtle fine-grain ripple micro-roughness in troughs
float fineMicro = valueNoise(p.xz * 95.0) * 0.0014 * (1.0 - ri.crestMask);
coarseDisp += fineMicro;
}
float topH = SWATCH_BASE_H + ri.height + coarseDisp;
// 45-degree clean bevel cut around top perimeter of the specimen
float bevelWidth = 0.048;
float dTop = p.y - topH;
float dSide = dXZ;
float dBot = -p.y;
float dBevel = (dXZ + (p.y - (topH - bevelWidth))) * 0.70710678;
// Tiny bottom chamfer so specimen seats crisply on the bench
float dBotBevel = (dXZ + (-p.y + 0.012)) * 0.70710678;
float d = smax(dSide, dBot, 0.006);
d = smax(d, dTop, 0.007);
d = smax(d, dBevel, 0.007);
d = smax(d, dBotBevel, 0.004);
// Conservative Lipschitz factor for steep leeward slip faces & coarse crest domes
return d * 0.62;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadow, Ambient Occlusion (Strict Zero-Bug Rules)
// -----------------------------------------------------------------------------
vec3 calcNormal(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.0014;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
float softShadowHero(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 64; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
if (res < 0.001 || t > tMax) break;
t += max(h, 0.015);
}
return clamp(res, 0.0, 1.0);
}
float calcAO(vec3 p, vec3 n) {
float occ = 0.0;
float sca = 1.0;
for (int i = 0; i < 5; i++) {
float h = 0.018 + 0.042 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.18, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Softboxes + Negative-Fill Flags
// -----------------------------------------------------------------------------
float softboxRect(vec3 rd, vec3 centerDir, vec3 upHint, vec2 halfSize, float softness) {
vec3 f = normalize(centerDir);
vec3 r = normalize(cross(upHint, f));
vec3 u = cross(f, r);
float d = dot(rd, f);
if (d <= 0.05) return 0.0;
vec2 uv = vec2(dot(rd, r), dot(rd, u)) / d;
vec2 q = abs(uv) - halfSize;
float dist = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
return smoothstep(softness, -softness * 0.25, dist);
}
vec3 studioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient (neutral dark-to-mid studio room)
vec3 zenith = (lightMode == 1) ? vec3(0.26) : vec3(0.25, 0.27, 0.30);
vec3 horizon = (lightMode == 1) ? vec3(0.16) : vec3(0.16, 0.165, 0.175);
vec3 floorBounce = (lightMode == 1) ? vec3(0.11) : vec3(0.12, 0.115, 0.11);
vec3 col = mix(horizon, zenith, smoothstep(0.0, 0.85, max(y, 0.0)));
col = mix(col, floorBounce, smoothstep(0.0, -0.55, y));
// Light-mode specific softboxes
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.85);
rimCol = vec3(1.25);
} else if (lightMode == 2) {
// Grazing raking light across ripples
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(2.75, 2.38, 1.95);
fillCol = vec3(0.62, 0.70, 0.84);
rimCol = vec3(1.15, 1.08, 0.98);
} else if (lightMode == 3) {
// Backlit high-contrast rim studio
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.80, 2.55, 2.25);
fillCol = vec3(0.75, 0.80, 0.90);
rimCol = vec3(1.90, 1.75, 1.55);
} else {
// Mode 0: Hero museum studio lighting
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.45, 2.32, 2.12);
fillCol = vec3(0.74, 0.82, 0.95);
rimCol = vec3(1.35, 1.26, 1.15);
}
float sbKey = softboxRect(rd, keyDir, vec3(0.0, 1.0, 0.0), vec2(0.42, 0.28), 0.14);
float sbFill = softboxRect(rd, fillDir, vec3(0.0, 1.0, 0.0), vec2(0.50, 0.36), 0.20);
float sbRim = softboxRect(rd, rimDir, vec3(0.0, 1.0, 0.0), vec2(0.65, 0.16), 0.12);
float sbTop = softboxRect(rd, vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.55, 0.45), 0.25);
col += keyCol * sbKey * 0.95;
col += fillCol * sbFill * 0.60;
col += rimCol * sbRim * 0.75;
col += ((lightMode == 1) ? vec3(0.48) : vec3(0.46, 0.48, 0.52)) * sbTop;
// Negative-fill black flags to carve crisp specular contrast on hematite/quartz grains
float negFlagL = softboxRect(rd, normalize(vec3(-0.90, 0.12, -0.42)), vec3(0.0, 1.0, 0.0), vec2(0.32, 0.42), 0.10);
float negFlagR = softboxRect(rd, normalize(vec3( 0.88, 0.10, -0.46)), vec3(0.0, 1.0, 0.0), vec2(0.30, 0.40), 0.10);
col *= (1.0 - 0.72 * max(negFlagL, negFlagR));
return col;
}
// -----------------------------------------------------------------------------
// PBR Helpers (GGX + Schlick Fresnel + ACES Tone Mapping)
// -----------------------------------------------------------------------------
float D_GGX(float NoH, float rough) {
float a = rough * rough;
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / max(PI * d * d, 1e-5);
}
float G_Smith(float NoV, float NoL, float rough) {
float r = rough + 1.0;
float k = (r * r) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k);
float gl = NoL / (NoL * (1.0 - k) + k);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalLight(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, vec3 F0, float sssAmt) {
float NoL = max(dot(N, L), 0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(L + V);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F = F_Schlick(VoH, F0);
float D = D_GGX(NoH, rough);
float G = G_Smith(NoV, NoL, rough);
vec3 spec = (D * G * F) / max(4.0 * NoV * NoL, 0.001);
vec3 kD = (1.0 - F);
// Subtle Oren-Nayar / sand multiple-scattering wrap diffuse
float wrapNoL = max((dot(N, L) + 0.18) / 1.18, 0.0);
vec3 diff = (kD * albedo / PI) * mix(NoL, wrapNoL, sssAmt);
return (diff + spec * NoL) * lightCol;
}
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// -----------------------------------------------------------------------------
// Material Evaluation: Wind-Sorted Desert Sand Specimen & Cut Cross-Section
// -----------------------------------------------------------------------------
struct MatParams {
vec3 albedo;
vec3 normal;
float rough;
vec3 F0;
float sss;
float glint;
};
MatParams evalHeroMaterial(vec3 p, vec3 geomN, vec3 V, float openAmt) {
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Detect whether we are on the top wind-sculpted ripple surface vs. bevel-cut side walls
float topMask = smoothstep(0.42, 0.78, geomN.y);
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float bevelMask = smoothstep(-0.065, -0.015, dXZ) * (1.0 - smoothstep(0.75, 0.94, geomN.y));
// --- 1. Coarse Armored Dark-Mineral / Hematite Grains (Crests) ---
GrainSample coarseG = sampleGrains(p.xz, 38.0);
vec3 cid = hash32(coarseG.id + vec2(4.2, 8.9));
// Authentic heavy-mineral crest assemblage: hematite, magnetite, dark basalt, iron-stained quartz
vec3 hematiteCol = vec3(0.17, 0.085, 0.065);
vec3 magnetiteCol = vec3(0.085, 0.085, 0.095);
vec3 jasperCol = vec3(0.32, 0.145, 0.085);
vec3 smokyQCol = vec3(0.28, 0.195, 0.135);
vec3 coarseCol;
float coarseRough;
vec3 coarseF0;
if (cid.x < 0.42) {
coarseCol = mix(hematiteCol, jasperCol, cid.y * 0.45);
coarseRough = mix(0.28, 0.42, cid.z);
coarseF0 = vec3(0.10, 0.08, 0.075);
} else if (cid.x < 0.78) {
coarseCol = mix(magnetiteCol, vec3(0.13, 0.125, 0.13), cid.y);
coarseRough = mix(0.24, 0.38, cid.z);
coarseF0 = vec3(0.12, 0.12, 0.13);
} else {
coarseCol = mix(smokyQCol, jasperCol, cid.y * 0.5);
coarseRough = mix(0.36, 0.50, cid.z);
coarseF0 = vec3(0.06);
}
// Darken micro-crevices between coarse grains
coarseCol *= mix(0.55, 1.08, smoothstep(0.0, 0.35, coarseG.edge));
// --- 2. Fine Pale Golden-Tan Quartz Sand (Troughs & Lower Stoss) ---
GrainSample fineG = sampleGrains(p.xz, 118.0);
vec3 fid = hash32(fineG.id + vec2(19.7, 3.3));
vec3 quartzPale = vec3(0.84, 0.71, 0.52);
vec3 quartzGold = vec3(0.73, 0.57, 0.38);
vec3 quartzWarm = vec3(0.64, 0.47, 0.31);
vec3 fineCol = mix(quartzGold, quartzPale, fid.x);
fineCol = mix(fineCol, quartzWarm, fid.y * 0.45);
// Subtle 5% scattered fine dark mineral specks in the trough sand
float fineSpeck = step(0.95, fid.z);
fineCol = mix(fineCol, vec3(0.16, 0.12, 0.10), fineSpeck * 0.72);
fineCol *= mix(0.76, 1.04, smoothstep(0.0, 0.25, fineG.edge));
// Slightly warmer/brighter sunlit crest-to-trough sorting tint
float uWind = dot(p.xz, WIND_DIR);
float vCross = dot(p.xz, CROSS_DIR);
float streakNoise = fbm2(vec2(uWind * 14.0, vCross * 3.8));
fineCol *= (0.94 + 0.12 * streakNoise);
// Subtle leeward slip-face avalanche darkening & scattered rolled dark grains
float rolledDark = smoothstep(0.74, 0.92, hash12(coarseG.id + 31.4)) * ri.leeMask * mix(0.35, 0.75, openAmt);
float armorBlend = clamp(max(ri.crestMask, rolledDark * 0.85), 0.0, 1.0);
// Crisp grain-scale transition: coarse grains sit physically on top of fine sand
float grainOverlay = smoothstep(0.18, 0.55, armorBlend * (0.62 + 0.72 * coarseG.dome));
vec3 topAlbedo = mix(fineCol, coarseCol, grainOverlay);
float topRough = mix(mix(0.62, 0.76, fid.y), coarseRough, grainOverlay);
vec3 topF0 = mix(vec3(0.042), coarseF0, grainOverlay);
// Micro-normal perturbation from coarse armored grains & fine quartz grains
vec2 coarseGrad = coarseG.localDir * (1.85 * coarseG.dome) * grainOverlay;
vec2 fineGrad = fineG.localDir * (0.92 * fineG.dome) * (1.0 - grainOverlay * 0.7);
// Subtle wind-streak micro-ripple on leeward slip face
float slipMicro = (valueNoise(vec2(uWind * 42.0, vCross * 12.0)) - 0.5) * 0.14 * ri.leeMask;
vec3 topN = normalize(geomN + vec3(coarseGrad.x + fineGrad.x + slipMicro, 0.0, coarseGrad.y + fineGrad.y));
// --- 3. Bevel-Cut Side Wall Cross-Bedding Laminae ---
// Dipping aeolian cross-strata visible on the precision-cut geological specimen walls
float strataCoord = p.y * 44.0 + uWind * 14.5 + 1.4 * fbm2(vec2(uWind * 3.5, vCross * 3.5));
float strataBand = 0.5 + 0.5 * sin(strataCoord);
float heavySeam = smoothstep(0.80, 0.96, sin(strataCoord * 0.5 + 0.9));
vec3 cutQuartz = mix(vec3(0.64, 0.50, 0.34), vec3(0.78, 0.65, 0.47), strataBand);
vec3 cutAlbedo = mix(cutQuartz, vec3(0.22, 0.15, 0.12), heavySeam * mix(0.45, 0.78, openAmt));
// Fine cut-sand grain texture on vertical walls
float cutGrain = valueNoise(vec2(p.x + p.z, p.y) * 140.0);
cutAlbedo *= (0.90 + 0.18 * cutGrain);
// Darken slightly near bottom seating edge for grounded realism
cutAlbedo *= mix(0.84, 1.0, smoothstep(0.0, 0.06, p.y));
// Subtle micro-bump on cut side walls
vec3 cutN = normalize(geomN + (vec3(cutGrain, valueNoise(p.xz * 130.0), cutGrain) - 0.5) * 0.06);
// Combine top surface, bevel, and cut sides
vec3 finalAlbedo = mix(cutAlbedo, topAlbedo, topMask);
// Slightly crisper lighter dust along the clean bevel cut
finalAlbedo = mix(finalAlbedo, mix(cutAlbedo, topAlbedo, 0.65) * 1.03, bevelMask * 0.35);
vec3 finalN = normalize(mix(cutN, topN, topMask));
float finalRough = mix(0.72, topRough, topMask);
vec3 finalF0 = mix(vec3(0.038), topF0, topMask);
// Micro-facet crystalline quartz & hematite specular glints
vec3 glintSeed = hash32((grainOverlay > 0.5 ? coarseG.id : fineG.id) + vec2(71.3, 12.9));
vec3 randFacet = normalize(finalN + (glintSeed - 0.5) * 0.95);
float glint = pow(max(dot(reflect(-V, randFacet), normalize(vec3(-0.6, 0.7, 0.4))), 0.0), 48.0);
glint *= topMask * mix(0.18, 0.48, grainOverlay);
MatParams m;
m.albedo = finalAlbedo;
m.normal = finalN;
m.rough = finalRough;
m.F0 = finalF0;
m.sss = mix(0.10, mix(0.32, 0.08, grainOverlay), topMask);
m.glint = glint;
return m;
}
// -----------------------------------------------------------------------------
// Scene Shading
// -----------------------------------------------------------------------------
vec3 shadeScene(vec3 ro, vec3 rd) {
// Raymarch scene
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
if (!hit) {
return studioEnv(rd, uLightMode) * 0.85;
}
vec3 p = ro + rd * t;
vec3 V = -rd;
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
// Lighting rig matching studioEnv
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
float keyShadowK;
if (uLightMode == 1) {
// Pure neutral grayscale studio light
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.72);
rimCol = vec3(0.95);
keyShadowK = 10.0;
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(3.15, 2.72, 2.22);
fillCol = vec3(0.55, 0.62, 0.74);
rimCol = vec3(0.95, 0.90, 0.82);
keyShadowK = 14.0;
} else if (uLightMode == 3) {
// Backlit studio light
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.90, 2.65, 2.35);
fillCol = vec3(0.88, 0.94, 1.05);
rimCol = vec3(1.45, 1.32, 1.18);
keyShadowK = 11.0;
} else {
// Mode 0: Hero studio light
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.55, 2.40, 2.18);
fillCol = vec3(0.68, 0.76, 0.88);
rimCol = vec3(1.05, 0.98, 0.90);
keyShadowK = 10.5;
}
vec3 col = vec3(0.0);
if (isBench) {
vec3 N = vec3(0.0, 1.0, 0.0);
// Subtle architectural studio slate/mineral bench grain
float bNoise = fbm2(p.xz * 2.2);
float bFine = valueNoise(p.xz * 48.0);
vec3 benchBase = (uLightMode == 1) ? vec3(0.22) : vec3(0.22, 0.215, 0.208);
vec3 albedo = benchBase * (0.93 + 0.12 * bNoise) * (0.97 + 0.06 * bFine);
float rough = mix(0.66, 0.78, bNoise);
vec3 F0 = vec3(0.035);
float shKey = softShadowHero(p + N * 0.014, keyDir, 5.5, keyShadowK);
float shFill = softShadowHero(p + N * 0.014, fillDir, 5.5, 5.0);
float ao = calcAO(p, N);
// Tight contact shadow right beneath the specimen perimeter
float dEdge = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float contactAO = smoothstep(-0.02, 0.14, dEdge);
ao *= mix(0.38, 1.0, contactAO);
col += evalLight(N, V, keyDir, keyCol * shKey, albedo, rough, F0, 0.0);
col += evalLight(N, V, fillDir, fillCol * shFill, albedo, rough, F0, 0.0);
col += evalLight(N, V, rimDir, rimCol * 0.65, albedo, rough, F0, 0.0);
// Studio IBL ambient + subtle reflection on matte bench
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(reflect(-V, N), uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), F0);
col += (albedo * envDiff * 0.42 + envSpec * F * (1.0 - rough) * 0.45) * ao;
// Smooth studio horizon fade into cyclorama backdrop
float distFade = smoothstep(3.2, 11.5, length(p.xz));
col = mix(col, studioEnv(rd, uLightMode) * 0.85, distFade);
} else {
vec3 geomN = calcNormal(p);
float openAmt = clamp(uOpen, 0.0, 1.0);
MatParams mat = evalHeroMaterial(p, geomN, V, openAmt);
vec3 N = mat.normal;
float shKey = softShadowHero(p + geomN * 0.014, keyDir, 4.5, keyShadowK);
float shFill = softShadowHero(p + geomN * 0.014, fillDir, 4.5, 5.5);
float ao = calcAO(p, geomN);
col += evalLight(N, V, keyDir, keyCol * shKey, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, fillDir, fillCol * shFill, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, rimDir, rimCol, mat.albedo, mat.rough, mat.F0, mat.sss);
// Micro-crystal specular glints from key & rim lights
col += keyCol * mat.glint * shKey * 0.22;
// Studio IBL diffuse + specular reflection
vec3 R = reflect(-V, N);
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(R, uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), mat.F0);
col += mat.albedo * envDiff * 0.46 * ao;
col += envSpec * F * mix(0.65, 0.16, mat.rough) * ao;
// Warm inter-grain sand multiple-scattering bounce in shadowed troughs
vec3 sandBounce = (uLightMode == 1) ? vec3(0.055) : vec3(0.075, 0.055, 0.036);
col += mat.albedo * sandBounce * (0.45 + 0.55 * ao);
}
return col;
}
// -----------------------------------------------------------------------------
// Camera Setup & Main
// -----------------------------------------------------------------------------
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / max(uResolution.y, 1.0);
// Subtle gentle breathing motion when uTime advances; exact hero framing at uTime = 0
float yaw = 0.08 * sin(uTime * 0.22);
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x Macro camera inspecting 2-3 ripple crests & coarse-vs-fine grain sorting
ro = vec3(0.42 + 0.12 * sin(yaw), 0.78, 0.92 + 0.08 * cos(yaw));
ta = vec3(-0.04, 0.27, 0.02);
focal = 2.35;
} else {
// Hero 3/4 museum specimen framing (full bevel-cut swatch + shadow cleanly inside frame)
float r = 3.42;
float baseAng = 0.54 + yaw;
ro = vec3(sin(baseAng) * r, 1.92, cos(baseAng) * r);
ta = vec3(0.0, 0.14, 0.0);
focal = 1.58;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);
vec3 col = shadeScene(ro, rd);
// Exposure calibration for ACES (keeps hero luminance comfortably in 15..235)
col *= 0.78;
col = acesToneMap(col);
// Gentle studio vignette
float vig = 1.0 - 0.12 * dot(uv, uv);
col *= vig;
// Linear to sRGB
col = pow(max(col, 0.0), vec3(1.0 / 2.2));
// Pure neutral grayscale output for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
fragColor = vec4(col, 1.0);
}
void fragment() {
vec2 tiled_uv = fract(UV * u_tiling);
vec2 frag_xy = vec2(tiled_uv.x, 1.0 - tiled_uv.y) * uResolution;
vec4 study_col = vec4(0.0, 0.0, 0.0, 1.0);
pixelfx_study_eval(frag_xy, study_col);
vec3 tinted = clamp(mix(study_col.rgb, study_col.rgb * u_tint.rgb * 1.35, u_tint_mix), 0.0, 1.0);
float luma = dot(tinted, vec3(0.2126, 0.7152, 0.0722));
ALBEDO = tinted;
ROUGHNESS = clamp(0.85 - luma * 0.45, 0.08, 0.96);
METALLIC = 0.04;
EMISSION = tinted * 0.28;
}
// Wind-Sorted Sand Ripples — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/granular/lib/09_wind_sorted_ripple_crests.glsl (MIT License)
import * as THREE from 'three';
const vertexShader = /* glsl */ `
out vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = /* glsl */ `
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;
out vec4 fragColor;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// -----------------------------------------------------------------------------
// Hash & Procedural Noise Utilities
// -----------------------------------------------------------------------------
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 rot = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * valueNoise(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// -----------------------------------------------------------------------------
// Cellular Sand Grain Field (Voronoi with dome profile, offset, and cell ID)
// -----------------------------------------------------------------------------
struct GrainSample {
float dome; // 0..1 rounded grain elevation
float edge; // distance to grain boundary
vec2 localDir; // vector from grain center to sample point
vec2 id; // stable cell ID for mineral coloration
};
GrainSample sampleGrains(vec2 p, float scale) {
vec2 sp = p * scale;
vec2 n = floor(sp);
vec2 f = fract(sp);
float minDist = 8.0;
float secondDist = 8.0;
vec2 bestVec = vec2(0.0);
vec2 bestId = vec2(0.0);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 g = vec2(float(i), float(j));
vec2 o = hash22(n + g);
// Keep grain centers jittered inside cell for natural close-packing
vec2 r = g + (0.14 + 0.72 * o) - f;
float d2 = dot(r, r);
if (d2 < minDist) {
secondDist = minDist;
minDist = d2;
bestVec = -r;
bestId = n + g;
} else if (d2 < secondDist) {
secondDist = d2;
}
}
}
float d1 = sqrt(minDist);
float d2 = sqrt(secondDist);
float edge = clamp((d2 - d1) * 1.4, 0.0, 1.0);
// Smooth rounded quartz/hematite pebble dome
float rNorm = clamp(d1 / 0.58, 0.0, 1.0);
float dome = sqrt(max(0.0, 1.0 - rNorm * rNorm)) * smoothstep(0.0, 0.18, edge);
GrainSample gs;
gs.dome = dome;
gs.edge = edge;
gs.localDir = bestVec;
gs.id = bestId;
return gs;
}
// -----------------------------------------------------------------------------
// Aeolian Asymmetric Ripple Field
// -----------------------------------------------------------------------------
// Wind blows along windDir: gentle windward stoss slope, sharp crest, steep leeward slip face
const vec2 WIND_DIR = vec2(0.92388, 0.38268); // ~22.5 deg
const vec2 CROSS_DIR = vec2(-0.38268, 0.92388);
struct RippleInfo {
float height; // world-space ripple height contribution
float normH; // 0..1 normalized ripple height (0=trough, 1=crest)
float crestMask; // 0..1 coarse dark-mineral armor concentration
float leeMask; // 0..1 steep leeward slip-face indicator
float phase; // ripple phase for cross-bedding laminae
};
float asymWaveProfile(float x, float skew, out float dProfile) {
// Phase-warped C-infinity asymmetric wave:
// Rising limb spans ~70% of wavelength (gentle stoss), falling ~30% (steep leeward slip face)
float s1 = skew;
float s2 = 0.22 * skew * skew;
float w = x - s1 * sin(x) - s2 * sin(2.0 * x);
float dw = 1.0 - s1 * cos(x) - 2.0 * s2 * cos(2.0 * x);
float s = sin(w);
float c = cos(w);
float h0 = 0.5 + 0.5 * s;
// Sharpen crest slightly and broaden trough like true wind ripples
float h = mix(h0, pow(max(h0, 0.0), 1.48), 0.62);
h += 0.055 * pow(max(h0, 0.0), 3.6);
h = clamp(h / 1.055, 0.0, 1.0);
dProfile = c * dw; // positive on windward stoss, negative on steep leeward slip face
return h;
}
RippleInfo evalRippleField(vec2 xz, float openAmt) {
float u = dot(xz, WIND_DIR);
float v = dot(xz, CROSS_DIR);
// Organic crest sinuosity & gentle aeolian modulation
float warp = 0.36 * sin(v * 2.15 + 0.65)
+ 0.16 * sin(v * 4.8 - 1.1)
+ 0.42 * (fbm2(vec2(u * 0.65, v * 1.15) + vec2(3.1, 7.4)) - 0.5);
// Primary ripple train (~4.5 crests across the swatch)
float freq = 13.6;
float phA = u * freq + warp * 2.35;
// Subtle secondary phase for natural tuning-fork ripple bifurcation on one side
float bifurMask = smoothstep(-0.35, 0.55, v + 0.25 * sin(u * 2.5));
float phB = u * (freq * 1.08) + warp * 2.15 + 1.15;
float skew = mix(0.56, 0.68, openAmt);
float dA, dB;
float hA = asymWaveProfile(phA, skew, dA);
float hB = asymWaveProfile(phB, skew, dB);
// Softly blend trains in a localized defect zone to keep crests crisp everywhere else
float defectZone = exp(-pow((u - 0.18) * 1.9, 2.0)) * smoothstep(0.05, 0.65, bifurMask);
float normH = mix(hA, hB, defectZone * 0.42);
float dProf = mix(dA, dB, defectZone * 0.42);
// Slight natural crest height undulation along the ridge
float crestMod = 0.86 + 0.26 * fbm2(vec2(u * 1.1, v * 2.4) + vec2(11.3, 4.7));
float amp = mix(0.044, 0.102, openAmt) * crestMod;
// Gentle large-scale dune curvature across the specimen
float macroRoll = 0.014 * sin(u * 1.7 + 0.4) + 0.008 * cos(v * 1.9);
// Leeward slip-face mask (steep negative slope just downwind of crest)
float leeMask = smoothstep(-0.10, -0.85, dProf) * smoothstep(0.12, 0.55, normH);
// Wind-sorted grain-size armor mask: coarse dark heavy minerals armor the crests
// and spill slightly down the top of the leeward slip face
float sortNoise = fbm2(vec2(u * 6.5, v * 5.2) + vec2(2.7, 9.1)) - 0.5;
float armorLow = mix(0.64, 0.48, openAmt);
float armorHigh = mix(0.95, 0.84, openAmt);
float crestMask = smoothstep(armorLow, armorHigh, normH + sortNoise * 0.14 + leeMask * 0.07);
// Boost contrast with uOpen
crestMask = clamp(crestMask * mix(0.76, 1.12, openAmt), 0.0, 1.0);
RippleInfo ri;
ri.height = normH * amp + macroRoll;
ri.normH = normH;
ri.crestMask = crestMask;
ri.leeMask = leeMask;
ri.phase = mix(phA, phB, defectZone * 0.42);
return ri;
}
// -----------------------------------------------------------------------------
// Swatch SDF (Bevel-cut geological sand specimen grounded on y = 0)
// -----------------------------------------------------------------------------
float sdRoundBox2D(vec2 p, vec2 b, float r) {
vec2 q = abs(p) - b + r;
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
const vec2 SWATCH_HALF = vec2(1.16, 0.94);
const float SWATCH_BASE_H = 0.235;
float mapHero(vec3 p) {
float openAmt = clamp(uOpen, 0.0, 1.0);
// Fast bounding-box rejection for distant rays
vec3 bp = abs(p - vec3(0.0, 0.22, 0.0)) - vec3(1.26, 0.30, 1.04);
float dBound = length(max(bp, 0.0)) + min(max(bp.x, max(bp.y, bp.z)), 0.0);
if (dBound > 0.16) {
return dBound;
}
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
// Evaluate ripple surface height
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Subtle real geometric micro-displacement for coarse armored crest grains
float coarseDisp = 0.0;
if (abs(p.y - (SWATCH_BASE_H + ri.height)) < 0.06 && dXZ < 0.04) {
GrainSample cg = sampleGrains(p.xz, 38.0);
coarseDisp = cg.dome * (0.0028 + 0.0072 * ri.crestMask * mix(0.75, 1.25, openAmt));
// Subtle fine-grain ripple micro-roughness in troughs
float fineMicro = valueNoise(p.xz * 95.0) * 0.0014 * (1.0 - ri.crestMask);
coarseDisp += fineMicro;
}
float topH = SWATCH_BASE_H + ri.height + coarseDisp;
// 45-degree clean bevel cut around top perimeter of the specimen
float bevelWidth = 0.048;
float dTop = p.y - topH;
float dSide = dXZ;
float dBot = -p.y;
float dBevel = (dXZ + (p.y - (topH - bevelWidth))) * 0.70710678;
// Tiny bottom chamfer so specimen seats crisply on the bench
float dBotBevel = (dXZ + (-p.y + 0.012)) * 0.70710678;
float d = smax(dSide, dBot, 0.006);
d = smax(d, dTop, 0.007);
d = smax(d, dBevel, 0.007);
d = smax(d, dBotBevel, 0.004);
// Conservative Lipschitz factor for steep leeward slip faces & coarse crest domes
return d * 0.62;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadow, Ambient Occlusion (Strict Zero-Bug Rules)
// -----------------------------------------------------------------------------
vec3 calcNormal(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.0014;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
float softShadowHero(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 64; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
if (res < 0.001 || t > tMax) break;
t += max(h, 0.015);
}
return clamp(res, 0.0, 1.0);
}
float calcAO(vec3 p, vec3 n) {
float occ = 0.0;
float sca = 1.0;
for (int i = 0; i < 5; i++) {
float h = 0.018 + 0.042 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.18, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Softboxes + Negative-Fill Flags
// -----------------------------------------------------------------------------
float softboxRect(vec3 rd, vec3 centerDir, vec3 upHint, vec2 halfSize, float softness) {
vec3 f = normalize(centerDir);
vec3 r = normalize(cross(upHint, f));
vec3 u = cross(f, r);
float d = dot(rd, f);
if (d <= 0.05) return 0.0;
vec2 uv = vec2(dot(rd, r), dot(rd, u)) / d;
vec2 q = abs(uv) - halfSize;
float dist = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
return smoothstep(softness, -softness * 0.25, dist);
}
vec3 studioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient (neutral dark-to-mid studio room)
vec3 zenith = (lightMode == 1) ? vec3(0.26) : vec3(0.25, 0.27, 0.30);
vec3 horizon = (lightMode == 1) ? vec3(0.16) : vec3(0.16, 0.165, 0.175);
vec3 floorBounce = (lightMode == 1) ? vec3(0.11) : vec3(0.12, 0.115, 0.11);
vec3 col = mix(horizon, zenith, smoothstep(0.0, 0.85, max(y, 0.0)));
col = mix(col, floorBounce, smoothstep(0.0, -0.55, y));
// Light-mode specific softboxes
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.85);
rimCol = vec3(1.25);
} else if (lightMode == 2) {
// Grazing raking light across ripples
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(2.75, 2.38, 1.95);
fillCol = vec3(0.62, 0.70, 0.84);
rimCol = vec3(1.15, 1.08, 0.98);
} else if (lightMode == 3) {
// Backlit high-contrast rim studio
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.80, 2.55, 2.25);
fillCol = vec3(0.75, 0.80, 0.90);
rimCol = vec3(1.90, 1.75, 1.55);
} else {
// Mode 0: Hero museum studio lighting
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.45, 2.32, 2.12);
fillCol = vec3(0.74, 0.82, 0.95);
rimCol = vec3(1.35, 1.26, 1.15);
}
float sbKey = softboxRect(rd, keyDir, vec3(0.0, 1.0, 0.0), vec2(0.42, 0.28), 0.14);
float sbFill = softboxRect(rd, fillDir, vec3(0.0, 1.0, 0.0), vec2(0.50, 0.36), 0.20);
float sbRim = softboxRect(rd, rimDir, vec3(0.0, 1.0, 0.0), vec2(0.65, 0.16), 0.12);
float sbTop = softboxRect(rd, vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.55, 0.45), 0.25);
col += keyCol * sbKey * 0.95;
col += fillCol * sbFill * 0.60;
col += rimCol * sbRim * 0.75;
col += ((lightMode == 1) ? vec3(0.48) : vec3(0.46, 0.48, 0.52)) * sbTop;
// Negative-fill black flags to carve crisp specular contrast on hematite/quartz grains
float negFlagL = softboxRect(rd, normalize(vec3(-0.90, 0.12, -0.42)), vec3(0.0, 1.0, 0.0), vec2(0.32, 0.42), 0.10);
float negFlagR = softboxRect(rd, normalize(vec3( 0.88, 0.10, -0.46)), vec3(0.0, 1.0, 0.0), vec2(0.30, 0.40), 0.10);
col *= (1.0 - 0.72 * max(negFlagL, negFlagR));
return col;
}
// -----------------------------------------------------------------------------
// PBR Helpers (GGX + Schlick Fresnel + ACES Tone Mapping)
// -----------------------------------------------------------------------------
float D_GGX(float NoH, float rough) {
float a = rough * rough;
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / max(PI * d * d, 1e-5);
}
float G_Smith(float NoV, float NoL, float rough) {
float r = rough + 1.0;
float k = (r * r) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k);
float gl = NoL / (NoL * (1.0 - k) + k);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalLight(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, vec3 F0, float sssAmt) {
float NoL = max(dot(N, L), 0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(L + V);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F = F_Schlick(VoH, F0);
float D = D_GGX(NoH, rough);
float G = G_Smith(NoV, NoL, rough);
vec3 spec = (D * G * F) / max(4.0 * NoV * NoL, 0.001);
vec3 kD = (1.0 - F);
// Subtle Oren-Nayar / sand multiple-scattering wrap diffuse
float wrapNoL = max((dot(N, L) + 0.18) / 1.18, 0.0);
vec3 diff = (kD * albedo / PI) * mix(NoL, wrapNoL, sssAmt);
return (diff + spec * NoL) * lightCol;
}
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// -----------------------------------------------------------------------------
// Material Evaluation: Wind-Sorted Desert Sand Specimen & Cut Cross-Section
// -----------------------------------------------------------------------------
struct MatParams {
vec3 albedo;
vec3 normal;
float rough;
vec3 F0;
float sss;
float glint;
};
MatParams evalHeroMaterial(vec3 p, vec3 geomN, vec3 V, float openAmt) {
RippleInfo ri = evalRippleField(p.xz, openAmt);
// Detect whether we are on the top wind-sculpted ripple surface vs. bevel-cut side walls
float topMask = smoothstep(0.42, 0.78, geomN.y);
float dXZ = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float bevelMask = smoothstep(-0.065, -0.015, dXZ) * (1.0 - smoothstep(0.75, 0.94, geomN.y));
// --- 1. Coarse Armored Dark-Mineral / Hematite Grains (Crests) ---
GrainSample coarseG = sampleGrains(p.xz, 38.0);
vec3 cid = hash32(coarseG.id + vec2(4.2, 8.9));
// Authentic heavy-mineral crest assemblage: hematite, magnetite, dark basalt, iron-stained quartz
vec3 hematiteCol = vec3(0.17, 0.085, 0.065);
vec3 magnetiteCol = vec3(0.085, 0.085, 0.095);
vec3 jasperCol = vec3(0.32, 0.145, 0.085);
vec3 smokyQCol = vec3(0.28, 0.195, 0.135);
vec3 coarseCol;
float coarseRough;
vec3 coarseF0;
if (cid.x < 0.42) {
coarseCol = mix(hematiteCol, jasperCol, cid.y * 0.45);
coarseRough = mix(0.28, 0.42, cid.z);
coarseF0 = vec3(0.10, 0.08, 0.075);
} else if (cid.x < 0.78) {
coarseCol = mix(magnetiteCol, vec3(0.13, 0.125, 0.13), cid.y);
coarseRough = mix(0.24, 0.38, cid.z);
coarseF0 = vec3(0.12, 0.12, 0.13);
} else {
coarseCol = mix(smokyQCol, jasperCol, cid.y * 0.5);
coarseRough = mix(0.36, 0.50, cid.z);
coarseF0 = vec3(0.06);
}
// Darken micro-crevices between coarse grains
coarseCol *= mix(0.55, 1.08, smoothstep(0.0, 0.35, coarseG.edge));
// --- 2. Fine Pale Golden-Tan Quartz Sand (Troughs & Lower Stoss) ---
GrainSample fineG = sampleGrains(p.xz, 118.0);
vec3 fid = hash32(fineG.id + vec2(19.7, 3.3));
vec3 quartzPale = vec3(0.84, 0.71, 0.52);
vec3 quartzGold = vec3(0.73, 0.57, 0.38);
vec3 quartzWarm = vec3(0.64, 0.47, 0.31);
vec3 fineCol = mix(quartzGold, quartzPale, fid.x);
fineCol = mix(fineCol, quartzWarm, fid.y * 0.45);
// Subtle 5% scattered fine dark mineral specks in the trough sand
float fineSpeck = step(0.95, fid.z);
fineCol = mix(fineCol, vec3(0.16, 0.12, 0.10), fineSpeck * 0.72);
fineCol *= mix(0.76, 1.04, smoothstep(0.0, 0.25, fineG.edge));
// Slightly warmer/brighter sunlit crest-to-trough sorting tint
float uWind = dot(p.xz, WIND_DIR);
float vCross = dot(p.xz, CROSS_DIR);
float streakNoise = fbm2(vec2(uWind * 14.0, vCross * 3.8));
fineCol *= (0.94 + 0.12 * streakNoise);
// Subtle leeward slip-face avalanche darkening & scattered rolled dark grains
float rolledDark = smoothstep(0.74, 0.92, hash12(coarseG.id + 31.4)) * ri.leeMask * mix(0.35, 0.75, openAmt);
float armorBlend = clamp(max(ri.crestMask, rolledDark * 0.85), 0.0, 1.0);
// Crisp grain-scale transition: coarse grains sit physically on top of fine sand
float grainOverlay = smoothstep(0.18, 0.55, armorBlend * (0.62 + 0.72 * coarseG.dome));
vec3 topAlbedo = mix(fineCol, coarseCol, grainOverlay);
float topRough = mix(mix(0.62, 0.76, fid.y), coarseRough, grainOverlay);
vec3 topF0 = mix(vec3(0.042), coarseF0, grainOverlay);
// Micro-normal perturbation from coarse armored grains & fine quartz grains
vec2 coarseGrad = coarseG.localDir * (1.85 * coarseG.dome) * grainOverlay;
vec2 fineGrad = fineG.localDir * (0.92 * fineG.dome) * (1.0 - grainOverlay * 0.7);
// Subtle wind-streak micro-ripple on leeward slip face
float slipMicro = (valueNoise(vec2(uWind * 42.0, vCross * 12.0)) - 0.5) * 0.14 * ri.leeMask;
vec3 topN = normalize(geomN + vec3(coarseGrad.x + fineGrad.x + slipMicro, 0.0, coarseGrad.y + fineGrad.y));
// --- 3. Bevel-Cut Side Wall Cross-Bedding Laminae ---
// Dipping aeolian cross-strata visible on the precision-cut geological specimen walls
float strataCoord = p.y * 44.0 + uWind * 14.5 + 1.4 * fbm2(vec2(uWind * 3.5, vCross * 3.5));
float strataBand = 0.5 + 0.5 * sin(strataCoord);
float heavySeam = smoothstep(0.80, 0.96, sin(strataCoord * 0.5 + 0.9));
vec3 cutQuartz = mix(vec3(0.64, 0.50, 0.34), vec3(0.78, 0.65, 0.47), strataBand);
vec3 cutAlbedo = mix(cutQuartz, vec3(0.22, 0.15, 0.12), heavySeam * mix(0.45, 0.78, openAmt));
// Fine cut-sand grain texture on vertical walls
float cutGrain = valueNoise(vec2(p.x + p.z, p.y) * 140.0);
cutAlbedo *= (0.90 + 0.18 * cutGrain);
// Darken slightly near bottom seating edge for grounded realism
cutAlbedo *= mix(0.84, 1.0, smoothstep(0.0, 0.06, p.y));
// Subtle micro-bump on cut side walls
vec3 cutN = normalize(geomN + (vec3(cutGrain, valueNoise(p.xz * 130.0), cutGrain) - 0.5) * 0.06);
// Combine top surface, bevel, and cut sides
vec3 finalAlbedo = mix(cutAlbedo, topAlbedo, topMask);
// Slightly crisper lighter dust along the clean bevel cut
finalAlbedo = mix(finalAlbedo, mix(cutAlbedo, topAlbedo, 0.65) * 1.03, bevelMask * 0.35);
vec3 finalN = normalize(mix(cutN, topN, topMask));
float finalRough = mix(0.72, topRough, topMask);
vec3 finalF0 = mix(vec3(0.038), topF0, topMask);
// Micro-facet crystalline quartz & hematite specular glints
vec3 glintSeed = hash32((grainOverlay > 0.5 ? coarseG.id : fineG.id) + vec2(71.3, 12.9));
vec3 randFacet = normalize(finalN + (glintSeed - 0.5) * 0.95);
float glint = pow(max(dot(reflect(-V, randFacet), normalize(vec3(-0.6, 0.7, 0.4))), 0.0), 48.0);
glint *= topMask * mix(0.18, 0.48, grainOverlay);
MatParams m;
m.albedo = finalAlbedo;
m.normal = finalN;
m.rough = finalRough;
m.F0 = finalF0;
m.sss = mix(0.10, mix(0.32, 0.08, grainOverlay), topMask);
m.glint = glint;
return m;
}
// -----------------------------------------------------------------------------
// Scene Shading
// -----------------------------------------------------------------------------
vec3 shadeScene(vec3 ro, vec3 rd) {
// Raymarch scene
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
if (!hit) {
return studioEnv(rd, uLightMode) * 0.85;
}
vec3 p = ro + rd * t;
vec3 V = -rd;
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
// Lighting rig matching studioEnv
vec3 keyDir, fillDir, rimDir;
vec3 keyCol, fillCol, rimCol;
float keyShadowK;
if (uLightMode == 1) {
// Pure neutral grayscale studio light
keyDir = normalize(vec3(-0.72, 0.62, 0.42));
fillDir = normalize(vec3( 0.78, 0.44, 0.45));
rimDir = normalize(vec3( 0.25, 0.52, -0.82));
keyCol = vec3(2.35);
fillCol = vec3(0.72);
rimCol = vec3(0.95);
keyShadowK = 10.0;
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(-0.92, 0.23, 0.30));
fillDir = normalize(vec3( 0.72, 0.55, 0.42));
rimDir = normalize(vec3( 0.55, 0.32, -0.77));
keyCol = vec3(3.15, 2.72, 2.22);
fillCol = vec3(0.55, 0.62, 0.74);
rimCol = vec3(0.95, 0.90, 0.82);
keyShadowK = 14.0;
} else if (uLightMode == 3) {
// Backlit studio light
keyDir = normalize(vec3(-0.42, 0.44, -0.80));
fillDir = normalize(vec3( 0.58, 0.50, 0.64));
rimDir = normalize(vec3( 0.52, 0.36, -0.78));
keyCol = vec3(2.90, 2.65, 2.35);
fillCol = vec3(0.88, 0.94, 1.05);
rimCol = vec3(1.45, 1.32, 1.18);
keyShadowK = 11.0;
} else {
// Mode 0: Hero studio light
keyDir = normalize(vec3(-0.76, 0.56, 0.34));
fillDir = normalize(vec3( 0.80, 0.48, 0.36));
rimDir = normalize(vec3( 0.30, 0.50, -0.81));
keyCol = vec3(2.55, 2.40, 2.18);
fillCol = vec3(0.68, 0.76, 0.88);
rimCol = vec3(1.05, 0.98, 0.90);
keyShadowK = 10.5;
}
vec3 col = vec3(0.0);
if (isBench) {
vec3 N = vec3(0.0, 1.0, 0.0);
// Subtle architectural studio slate/mineral bench grain
float bNoise = fbm2(p.xz * 2.2);
float bFine = valueNoise(p.xz * 48.0);
vec3 benchBase = (uLightMode == 1) ? vec3(0.22) : vec3(0.22, 0.215, 0.208);
vec3 albedo = benchBase * (0.93 + 0.12 * bNoise) * (0.97 + 0.06 * bFine);
float rough = mix(0.66, 0.78, bNoise);
vec3 F0 = vec3(0.035);
float shKey = softShadowHero(p + N * 0.014, keyDir, 5.5, keyShadowK);
float shFill = softShadowHero(p + N * 0.014, fillDir, 5.5, 5.0);
float ao = calcAO(p, N);
// Tight contact shadow right beneath the specimen perimeter
float dEdge = sdRoundBox2D(p.xz, SWATCH_HALF, 0.055);
float contactAO = smoothstep(-0.02, 0.14, dEdge);
ao *= mix(0.38, 1.0, contactAO);
col += evalLight(N, V, keyDir, keyCol * shKey, albedo, rough, F0, 0.0);
col += evalLight(N, V, fillDir, fillCol * shFill, albedo, rough, F0, 0.0);
col += evalLight(N, V, rimDir, rimCol * 0.65, albedo, rough, F0, 0.0);
// Studio IBL ambient + subtle reflection on matte bench
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(reflect(-V, N), uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), F0);
col += (albedo * envDiff * 0.42 + envSpec * F * (1.0 - rough) * 0.45) * ao;
// Smooth studio horizon fade into cyclorama backdrop
float distFade = smoothstep(3.2, 11.5, length(p.xz));
col = mix(col, studioEnv(rd, uLightMode) * 0.85, distFade);
} else {
vec3 geomN = calcNormal(p);
float openAmt = clamp(uOpen, 0.0, 1.0);
MatParams mat = evalHeroMaterial(p, geomN, V, openAmt);
vec3 N = mat.normal;
float shKey = softShadowHero(p + geomN * 0.014, keyDir, 4.5, keyShadowK);
float shFill = softShadowHero(p + geomN * 0.014, fillDir, 4.5, 5.5);
float ao = calcAO(p, geomN);
col += evalLight(N, V, keyDir, keyCol * shKey, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, fillDir, fillCol * shFill, mat.albedo, mat.rough, mat.F0, mat.sss);
col += evalLight(N, V, rimDir, rimCol, mat.albedo, mat.rough, mat.F0, mat.sss);
// Micro-crystal specular glints from key & rim lights
col += keyCol * mat.glint * shKey * 0.22;
// Studio IBL diffuse + specular reflection
vec3 R = reflect(-V, N);
vec3 envDiff = studioEnv(N, uLightMode);
vec3 envSpec = studioEnv(R, uLightMode);
vec3 F = F_Schlick(max(dot(N, V), 0.0), mat.F0);
col += mat.albedo * envDiff * 0.46 * ao;
col += envSpec * F * mix(0.65, 0.16, mat.rough) * ao;
// Warm inter-grain sand multiple-scattering bounce in shadowed troughs
vec3 sandBounce = (uLightMode == 1) ? vec3(0.055) : vec3(0.075, 0.055, 0.036);
col += mat.albedo * sandBounce * (0.45 + 0.55 * ao);
}
return col;
}
// -----------------------------------------------------------------------------
// Camera Setup & Main
// -----------------------------------------------------------------------------
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / max(uResolution.y, 1.0);
// Subtle gentle breathing motion when uTime advances; exact hero framing at uTime = 0
float yaw = 0.08 * sin(uTime * 0.22);
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x Macro camera inspecting 2-3 ripple crests & coarse-vs-fine grain sorting
ro = vec3(0.42 + 0.12 * sin(yaw), 0.78, 0.92 + 0.08 * cos(yaw));
ta = vec3(-0.04, 0.27, 0.02);
focal = 2.35;
} else {
// Hero 3/4 museum specimen framing (full bevel-cut swatch + shadow cleanly inside frame)
float r = 3.42;
float baseAng = 0.54 + yaw;
ro = vec3(sin(baseAng) * r, 1.92, cos(baseAng) * r);
ta = vec3(0.0, 0.14, 0.0);
focal = 1.58;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);
vec3 col = shadeScene(ro, rd);
// Exposure calibration for ACES (keeps hero luminance comfortably in 15..235)
col *= 0.78;
col = acesToneMap(col);
// Gentle studio vignette
float vig = 1.0 - 0.12 * dot(uv, uv);
col *= vig;
// Linear to sRGB
col = pow(max(col, 0.0), vec3(1.0 / 2.2));
// Pure neutral grayscale output for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
fragColor = vec4(col, 1.0);
}
`;
export function createWindSortedSandRipplesMaterial(width = 1024, height = 1024) {
const material = new THREE.ShaderMaterial({
glslVersion: THREE.GLSL3,
vertexShader,
fragmentShader,
uniforms: {
uResolution: { value: new THREE.Vector2(width, height) },
uTime: { value: 1.2 },
uOpen: { value: 0.0 },
uLightMode: { value: 0 },
uCamMode: { value: 1 },
},
});
return {
material,
update(elapsedSeconds) {
material.uniforms.uTime.value = elapsedSeconds * 1.0;
},
resize(nextW, nextH) {
material.uniforms.uResolution.value.set(nextW, nextH);
},
};
}
Mathematical models & shader specifications
| Identifier | wind-sorted-sand-ripples |
|---|---|
| Category | Sci-Fi, Plasma & Noise Shaders · Cellular & Ripple Fields |
| Upstream study | granular/lib/09_wind_sorted_ripple_crests.glsl (commit 1c02d7a) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime, uOpen, uLightMode, uCamMode |
| Source size | 761 lines · 28.5 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT weather sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Physical & shading models implemented
Voronoi/Worley cellular fields (Voronoi 1908; Worley 1996); GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Oren–Nayar rough diffuse reflectance (1994); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation
Adapted upstream mathematical helpers
David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal
More sci-fi & noise shaders
All sci-fi & noise shaders
AnimatedTrinitron vertical RGB phosphor triads with aperture wires and rolling electron scanline beam.
AnimatedEmerald LPI solder mask over routed copper buses, gold ENIG pads, and plated through-hole vias.
Photolithography microchip reticle grid throwing rainbow grating diffraction across mirror silicon.
AnimatedAnti-reflective nitride-blue solar panel tiles with silver busbars and fine finger electrodes.
AnimatedRopy basaltic lava skin folding over incandescent orange-red magma seams.
