AnimatedBranching cyan-green fungal rhizomorph veins pulsing across damp cavern bark.
sphagnum-forest-moss.glsl · 766 lines · GLSL ES 3.00 · MIT
#version 300 es
// Sphagnum Forest Moss (sphagnum-forest-moss.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/fungi_marine/lib/04_sphagnum_moss_star_carpet.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;
// -----------------------------------------------------------------------------
// Noise & Hash 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 noise2(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.8, 0.6, -0.6, 0.8);
for (int i = 0; i < 4; i++) {
v += a * noise2(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
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);
}
// -----------------------------------------------------------------------------
// Peat-Humus Hummock Geometry
// -----------------------------------------------------------------------------
// Organic swatch footprint radius in XZ (pure analytical for fast SDF eval)
float swatchRadius(vec2 xz) {
float ang = atan(xz.y, xz.x);
float r = 1.04;
r += 0.08 * sin(ang * 2.0 + 0.55);
r += 0.05 * cos(ang * 3.0 - 1.10);
r += 0.03 * sin(ang * 5.0 + 2.20);
return r;
}
// Smooth dome profile of the underlying peat-humus cushion (analytical, zero fbm)
float hummockHeight(vec2 xz) {
float rad = swatchRadius(xz);
float u = length(xz) / rad;
float dome = max(0.0, 1.0 - u * u);
// Pillowy sub-mounds characteristic of natural Sphagnum hummocks
vec2 d1 = xz - vec2(-0.28, 0.18);
vec2 d2 = xz - vec2( 0.30, 0.14);
vec2 d3 = xz - vec2( 0.02, -0.32);
float lobes = 0.058 * exp(-dot(d1, d1) * 3.8)
+ 0.052 * exp(-dot(d2, d2) * 4.2)
+ 0.048 * exp(-dot(d3, d3) * 4.0);
float ripple = 0.018 * sin(xz.x * 5.4 + 0.6) * cos(xz.y * 4.9 - 0.4);
float h = 0.44 * pow(dome, 0.72) + (lobes + ripple) * smoothstep(1.0, 0.35, u);
return max(0.0, h);
}
// Signed distance to the fibrous peat-humus base swatch
float sdPeatBase(vec3 p) {
float rad = swatchRadius(p.xz);
float r = length(p.xz);
float hTop = hummockHeight(p.xz);
// Slight natural undercut/bevel at the base rim where the swatch rests on the bench
float rimBevel = 0.045 * (1.0 - smoothstep(0.0, 0.13, p.y));
float dSide = r - (rad - rimBevel);
float dTop = (p.y - hTop) * 0.78;
float dBot = -p.y;
float dBody = smax(dSide, dTop, 0.065);
dBody = smax(dBody, dBot, 0.014);
// Fast 1-octave fibrous peat micro-relief only near the peat surface
if (dBody < 0.06) {
float peatMask = smoothstep(0.18, 0.02, p.y) + 0.30;
float fiber = (noise2(vec2(atan(p.z, p.x) * 6.0, p.y * 26.0) + p.xz * 4.5) - 0.5) * 0.014 * peatMask;
dBody += fiber;
}
return dBody;
}
// -----------------------------------------------------------------------------
// 3D Star-Headed Sphagnum Moss Capitula Carpet
// -----------------------------------------------------------------------------
struct MossEval {
float dist; // Combined hero SDF
float matId; // 0 = peat humus, 1 = sphagnum moss, 2 = capillary dew bead
float tipFactor; // 0 = deep stem/hollow, 1 = upper star rosette tip
float starVar; // per-capitulum color/species variation (0..1)
float axilWet; // capillary water film intensity (0..1)
};
// Single 3D star-headed Sphagnum capitulum in local tangent space (y up)
// Returns vec3(sdf, tipFactor, axilWet)
vec3 evalCapitulum(vec3 lp, float sc, float nArms, float rot, float seed) {
// Gentle organic breeze/hydration micro-motion when uTime > 0
float sway = sin(uTime * 0.8 + seed * TAU) * 0.0025 * smoothstep(0.0, 0.05, lp.y + 0.03);
lp.x += sway;
float rad = length(lp.xz);
float ang = atan(lp.z, lp.x) + rot;
// --- Outer primary star whorl (5 or 6 radiating lanceolate branches) ---
float sector = TAU / nArms;
float a1 = mod(ang + 0.5 * sector, sector) - 0.5 * sector;
vec2 q1 = vec2(cos(a1), sin(a1)) * rad;
float L1 = 0.076 * sc;
float t1 = clamp(q1.x / L1, 0.0, 1.0);
// Arching upward from the bud then gently recurving outward and down at the tip
float arch1 = sc * (0.015 * sin(t1 * 2.55) - 0.024 * t1 * t1);
// Pointed lanceolate leaf-branch cross-section with subtle imbricate leaf fluting
float flute1 = 1.0 + 0.11 * sin(t1 * 18.0 + seed * 10.0);
float w1 = sc * 0.0165 * (1.0 - 0.78 * t1) * (0.55 + 0.45 * sin(t1 * 2.8 + 0.35)) * flute1;
// Slightly flattened vertically like clustered tiny moss leaves
vec3 bp1 = vec3(q1.x - t1 * L1, (lp.y - arch1) * 1.18, q1.y);
float dOuter = length(bp1) - w1;
// --- Inner raised secondary whorl (rotated half-sector, compact central rosette) ---
float a2 = mod(ang + sector, sector) - 0.5 * sector;
vec2 q2 = vec2(cos(a2), sin(a2)) * rad;
float L2 = 0.044 * sc;
float t2 = clamp(q2.x / L2, 0.0, 1.0);
float arch2 = sc * (0.019 + 0.012 * sin(t2 * 2.4) - 0.014 * t2 * t2);
float w2 = sc * 0.0135 * (1.0 - 0.72 * t2) * (0.65 + 0.35 * sin(t2 * 3.0 + 0.4));
vec3 bp2 = vec3(q2.x - t2 * L2, (lp.y - arch2) * 1.15, q2.y);
float dInner = length(bp2) - w2;
// --- Central apical bud (plump rosette heart) ---
float dBud = length(lp - vec3(0.0, 0.017 * sc, 0.0)) - 0.019 * sc;
// --- Lower stem descending into the boggy inter-stem cavity ---
float stemTop = 0.008 * sc;
float stemBot = -0.105 * sc;
float sy = clamp(lp.y, stemBot, stemTop);
float dStem = length(vec2(rad, lp.y - sy)) - 0.0135 * sc;
// Combine rosette components smoothly so each star reads as one organic gametophyte
float dStar = smin(dOuter, dInner, 0.0075 * sc);
dStar = smin(dStar, dBud, 0.0065 * sc);
float dFull = smin(dStar, dStem, 0.009 * sc);
// Tip factor: highest on the central bud, inner whorl, and outer star tips
float outerTip = smoothstep(0.15, 0.95, t1) * smoothstep(-0.025 * sc, 0.022 * sc, lp.y);
float crownBoost = smoothstep(0.055 * sc, 0.0, rad) * smoothstep(0.0, 0.028 * sc, lp.y);
float tipFactor = clamp(max(outerTip, crownBoost * 0.92), 0.0, 1.0);
// Suppress tip factor down on the stem
tipFactor *= smoothstep(-0.045 * sc, 0.005 * sc, lp.y);
// Capillary water meniscus collects in the concave axils between branches and center
float axil = smoothstep(0.055 * sc, 0.010 * sc, rad) * smoothstep(0.026 * sc, -0.006 * sc, lp.y);
float groove = smoothstep(0.016 * sc, 0.001 * sc, abs(q1.y)) * (1.0 - t1 * 0.5);
float axilWet = clamp(axil * 0.85 + groove * 0.35, 0.0, 1.0);
return vec3(dFull, tipFactor, axilWet);
}
MossEval evalHero(vec3 p) {
MossEval res;
res.matId = 0.0;
res.tipFactor = 0.0;
res.starVar = 0.5;
res.axilWet = 0.25;
// Cheap bounding sphere early-out so far rays skip all hummock/moss work
float dB = length(p - vec3(0.0, 0.25, 0.0)) - 1.32;
if (dB > 0.12) {
res.dist = dB;
return res;
}
float dPeat = sdPeatBase(p);
res.dist = dPeat;
// Cheap bounding envelope around the hummock canopy so mid-range rays march fast
if (dPeat > 0.17) {
res.dist = dPeat - 0.095;
return res;
}
// Peat base slightly recessed beneath the moss carpet on top, exposed at the lower skirt
float rad = swatchRadius(p.xz);
float uRad = length(p.xz) / rad;
float canopyMask = smoothstep(0.99, 0.84, uRad) * smoothstep(0.035, 0.115, p.y);
float dPeatRecessed = dPeat + 0.038 * canopyMask;
res.dist = dPeatRecessed;
res.matId = 0.0;
// Only evaluate star rosettes within the living canopy footprint
if (uRad > 1.08 || p.y < 0.015) {
return res;
}
// Tightly packed jittered 2D grid of interlocking 3D Sphagnum capitula (3x3 neighborhood)
const float cellSize = 0.094;
vec2 gPos = p.xz / cellSize;
vec2 gId = floor(gPos);
float dMoss = 1e5;
float bestTip = 0.0;
float bestVar = 0.5;
float bestWet = 0.0;
float dDew = 1e5;
float dewScale = mix(1.0, 0.52, uOpen);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 id = gId + vec2(float(i), float(j));
vec3 rnd = hash32(id);
// Jittered center in XZ
vec2 cXZ = (id + 0.18 + 0.64 * rnd.xy) * cellSize;
// Fast XZ proximity reject matched to ~1.3x lush capitula radius
vec2 dxz = p.xz - cXZ;
if (dot(dxz, dxz) > 0.028) continue;
float cRad = swatchRadius(cXZ);
float cU = length(cXZ) / cRad;
if (cU > 0.965) continue;
// Anchor height on the hummock dome + plush organic height variation
float baseH = hummockHeight(cXZ);
float edgeTaper = smoothstep(0.97, 0.80, cU);
float plushOff = (rnd.z - 0.32) * 0.060 * edgeTaper + 0.034 * edgeTaper;
vec3 cPos = vec3(cXZ.x, baseH + plushOff, cXZ.y);
// Quick 3D sphere cull per capitulum
vec3 dp = p - cPos;
if (dot(dp, dp) > 0.034) continue;
vec2 rnd2 = hash22(id + vec2(19.3, 47.1));
// Analytical dome normal (zero finite-difference calls)
vec3 hN = normalize(vec3(cXZ.x * 0.85, 1.0, cXZ.y * 0.85));
vec3 up = normalize(mix(vec3(0.0, 1.0, 0.0), hN, 0.58)
+ vec3(rnd2.x - 0.5, 0.0, rnd2.y - 0.5) * 0.24);
vec3 side = normalize(cross(up, vec3(0.0, 0.0, 1.0)));
vec3 fwd = cross(side, up);
vec3 lp = vec3(dot(dp, side), dot(dp, up), dot(dp, fwd));
float sc = (1.06 + 0.44 * rnd2.x) * (0.80 + 0.20 * edgeTaper);
float nArms = (rnd2.y > 0.42) ? 5.0 : 6.0;
float rot = rnd.z * TAU;
vec3 cap = evalCapitulum(lp, sc, nArms, rot, rnd.x);
if (cap.x < dMoss) {
dMoss = cap.x;
bestTip = cap.y;
bestVar = rnd2.y;
bestWet = cap.z;
}
// Tiny glistening capillary dew beads perched on ~38% of star rosettes
if (rnd.x > 0.62 && dewScale > 0.2) {
float beadAng = rot + floor(rnd.y * nArms) * (TAU / nArms);
float beadR = (0.026 + 0.028 * rnd2.x) * sc;
vec3 beadPos = vec3(cos(beadAng) * beadR, 0.017 * sc, sin(beadAng) * beadR);
float beadRad = (0.0082 + 0.0045 * rnd.z) * sc * dewScale;
float db = length(lp - beadPos) - beadRad;
dDew = min(dDew, db);
}
}
}
// Select closest hero surface: peat humus, Sphagnum moss, or dew droplet
if (dMoss < res.dist) {
res.dist = dMoss;
res.matId = 1.0;
res.tipFactor = bestTip;
res.starVar = bestVar;
res.axilWet = bestWet;
}
if (dDew < res.dist) {
res.dist = dDew;
res.matId = 2.0;
res.tipFactor = 1.0;
res.starVar = 1.0;
res.axilWet = 1.0;
}
return res;
}
float mapHero(vec3 p) {
return evalHero(p).dist;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadows & Ambient Occlusion (Strict QA Contract)
// -----------------------------------------------------------------------------
// Unit-weight tetrahedral normal on hero SDF
vec3 calcHeroNormal(vec3 p) {
const float h = 0.0014;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Clean hero-only soft shadow (no ph, bias p+N*0.014, tMin=0.025, step max(h,0.015))
float softShadow(vec3 p, vec3 N, vec3 rd, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 30; i++) {
vec3 q = ro + rd * t;
float h = (t > 0.15) ? (sdPeatBase(q) - 0.035) : mapHero(q);
if (h < 0.0008) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
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 < 4; i++) {
float h = 0.018 + 0.038 * float(i);
vec3 q = p + N * h;
float d = (i < 2) ? mapScene(q) : min(sdPeatBase(q) - 0.035, q.y);
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.35 * occ, 0.08, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Cyclorama, Softboxes & Negative-Fill Flags
// -----------------------------------------------------------------------------
vec3 sampleStudioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.11, 0.115, 0.11);
vec3 midCol = vec3(0.24, 0.25, 0.245);
vec3 topCol = vec3(0.38, 0.40, 0.42);
if (lightMode == 1) {
botCol = vec3(0.13);
midCol = vec3(0.26);
topCol = vec3(0.40);
} else if (lightMode == 2) {
botCol = vec3(0.08, 0.085, 0.09);
midCol = vec3(0.16, 0.175, 0.19);
topCol = vec3(0.24, 0.27, 0.31);
} else if (lightMode == 3) {
botCol = vec3(0.09, 0.095, 0.09);
midCol = vec3(0.19, 0.20, 0.19);
topCol = vec3(0.28, 0.30, 0.32);
}
vec3 env = mix(botCol, midCol, smoothstep(-0.25, 0.18, y));
env = mix(env, topCol, smoothstep(0.18, 0.85, y));
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.25, 0.90, 0.35));
float sb1 = smoothstep(0.76, 0.95, dot(rd, sb1Dir));
vec3 sb1Col = (lightMode == 1) ? vec3(1.45) : vec3(1.45, 1.42, 1.35);
env += sb1 * sb1Col * (lightMode == 2 ? 0.45 : 0.95);
// Side fill softbox (cool-neutral)
vec3 sb2Dir = normalize(vec3(-0.82, 0.42, 0.38));
float sb2 = smoothstep(0.78, 0.96, dot(rd, sb2Dir));
vec3 sb2Col = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.02);
env += sb2 * sb2Col * 0.75;
// Back/rim strip softbox (boosted in backlit mode)
vec3 sb3Dir = normalize(vec3(-0.20, 0.42, -0.88));
float sb3 = smoothstep(0.80, 0.97, dot(rd, sb3Dir));
vec3 sb3Col = (lightMode == 1) ? vec3(1.25) : vec3(1.35, 1.28, 1.12);
env += sb3 * sb3Col * (lightMode == 3 ? 1.85 : 0.70);
// Grazing softbox when uLightMode == 2
if (lightMode == 2) {
vec3 sbGDir = normalize(vec3(0.92, 0.24, 0.28));
float sbG = smoothstep(0.84, 0.97, dot(rd, sbGDir));
env += sbG * vec3(1.75, 1.62, 1.40);
}
// Negative-fill flags on lower-right and far-left to carve glossy dew contrast
float negFlag1 = smoothstep(0.70, 0.90, dot(rd, normalize(vec3(0.88, 0.05, -0.45))));
float negFlag2 = smoothstep(0.74, 0.92, dot(rd, normalize(vec3(-0.90, 0.02, -0.40))));
env *= (1.0 - 0.62 * negFlag1) * (1.0 - 0.55 * negFlag2);
return env;
}
// ACES Filmic Tone Mapping
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);
}
// -----------------------------------------------------------------------------
// Shading
// -----------------------------------------------------------------------------
void getLightRig(int lightMode, out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol) {
if (lightMode == 1) {
// Pure neutral grayscale studio rig
keyDir = normalize(vec3(0.55, 0.78, 0.42));
keyCol = vec3(2.05);
fillDir = normalize(vec3(-0.75, 0.45, 0.48));
fillCol = vec3(0.65);
rimDir = normalize(vec3(-0.25, 0.52, -0.82));
rimCol = vec3(0.85);
} else if (lightMode == 2) {
// Grazing raking light across the star carpet
keyDir = normalize(vec3(0.92, 0.25, 0.30));
keyCol = vec3(2.45, 2.28, 1.98);
fillDir = normalize(vec3(-0.72, 0.55, 0.42));
fillCol = vec3(0.52, 0.60, 0.72);
rimDir = normalize(vec3(-0.45, 0.40, -0.80));
rimCol = vec3(0.72, 0.76, 0.82);
} else if (lightMode == 3) {
// Backlit botanical translucency rig
keyDir = normalize(vec3(-0.18, 0.46, -0.87));
keyCol = vec3(2.65, 2.42, 2.02);
fillDir = normalize(vec3(0.42, 0.68, 0.60));
fillCol = vec3(0.68, 0.74, 0.80);
rimDir = normalize(vec3(0.65, 0.40, -0.64));
rimCol = vec3(1.15, 1.12, 0.98);
} else {
// Hero studio lighting
keyDir = normalize(vec3(0.54, 0.78, 0.44));
keyCol = vec3(2.15, 2.06, 1.90);
fillDir = normalize(vec3(-0.78, 0.46, 0.42));
fillCol = vec3(0.58, 0.67, 0.80);
rimDir = normalize(vec3(-0.28, 0.52, -0.81));
rimCol = vec3(0.95, 0.92, 0.82);
}
}
vec3 shadeBench(vec3 p, vec3 V, int lightMode) {
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Fine matte mineral-stone specimen bench texture
float grain = fbm2(p.xz * 7.5) * 0.5 + noise2(p.xz * 34.0) * 0.5;
vec3 baseCol = (lightMode == 1)
? vec3(0.36) * (0.94 + 0.12 * grain)
: vec3(0.37, 0.365, 0.35) * (0.94 + 0.12 * grain);
// Subtle damp bog-moisture halo right around the peat-humus swatch contact edge
float r = length(p.xz) / swatchRadius(p.xz);
float dampRing = smoothstep(1.14, 0.96, r) * mix(0.28, 0.12, uOpen);
baseCol *= (1.0 - dampRing);
float shKey = softShadow(p, N, keyDir, 3.5, 9.0);
float ao = calcAO(p, N);
float ndlKey = max(dot(N, keyDir), 0.0);
float ndlFill = max(dot(N, fillDir), 0.0);
float ndlRim = max(dot(N, rimDir), 0.0);
vec3 diff = baseCol / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.45 + 0.55 * ao) +
rimCol * ndlRim * 0.35 * ao
);
// Soft studio ambient + slight sheen on damp contact ring
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 col = diff + baseCol * envDiff * 0.52 * ao;
vec3 R = reflect(-V, N);
float fres = pow(1.0 - max(dot(N, V), 0.0), 4.0);
float benchSpec = mix(0.06, 0.24, dampRing);
col += sampleStudioEnv(R, lightMode) * fres * benchSpec * ao;
// Gentle radial studio backdrop vignette fade on distant bench
float distFade = smoothstep(2.4, 8.5, length(p.xz));
vec3 horizonCol = sampleStudioEnv(normalize(vec3(p.x, 0.12, p.z)), lightMode) * 0.72;
col = mix(col, horizonCol, distFade);
return col;
}
vec3 shadeHero(vec3 p, vec3 V, MossEval m, int lightMode) {
vec3 N = calcHeroNormal(p);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
float ao = calcAO(p, N);
float shKey = softShadow(p, N, keyDir, 2.5, 11.0);
// Local micro-texture for botanical leaf cellularity & peat fibers
float micro = fbm2(p.xz * 26.0 + p.y * 14.0);
float fineCell = noise2(p.xz * 78.0 + p.y * 55.0);
// Determine albedo, roughness, F0, and SSS parameters by material ID
vec3 albedo;
vec3 sssTint;
float sssStrength = 0.0;
float roughness = 0.55;
float specBoost = 1.0;
float wetness = 0.0;
if (m.matId < 0.5) {
// Fibrous dark bog peat-humus substrate (deep rich boggy humus under canopy)
vec3 peatDark = vec3(0.032, 0.024, 0.016);
vec3 peatWarm = vec3(0.092, 0.065, 0.040);
albedo = mix(peatDark, peatWarm, micro * 0.78 + fineCell * 0.22);
// Deepen under-canopy inter-stem humus pockets into dark boggy olive-peat shadow
float underCanopy = smoothstep(0.06, 0.22, p.y);
albedo = mix(albedo, vec3(0.030, 0.046, 0.018), underCanopy * 0.72);
roughness = mix(0.68, 0.42, mix(0.75, 0.35, uOpen) * (1.0 - micro * 0.4));
sssTint = vec3(0.08, 0.05, 0.03);
sssStrength = 0.04;
wetness = mix(0.45, 0.18, uOpen);
} else if (m.matId < 1.5) {
// Living 3D star-headed Sphagnum gametophyte
float openT = clamp(uOpen, 0.0, 1.0);
// Hydrated lush emerald palette (uOpen = 0)
vec3 stemEmerald = vec3(0.048, 0.125, 0.028);
vec3 midEmerald = vec3(0.135, 0.355, 0.062);
vec3 limeTip = vec3(0.395, 0.575, 0.115);
vec3 coolMintVar = vec3(0.115, 0.315, 0.105);
vec3 emeraldCol = mix(stemEmerald, midEmerald, smoothstep(0.08, 0.58, m.tipFactor));
emeraldCol = mix(emeraldCol, limeTip, smoothstep(0.55, 0.98, m.tipFactor));
emeraldCol = mix(emeraldCol, coolMintVar, m.starVar * 0.22 * (1.0 - m.tipFactor * 0.5));
// Sun-blushed bog-crimson autumn palette (uOpen = 1)
vec3 bogOlive = vec3(0.085, 0.132, 0.032);
vec3 amberOchre = vec3(0.315, 0.235, 0.055);
vec3 bogCrimson = vec3(0.510, 0.058, 0.064);
vec3 wineRoseTip = vec3(0.675, 0.108, 0.098);
// Sun exposure favors upper hummock crown + rosette star tips
float sunBlush = smoothstep(0.18, 0.86, m.tipFactor) * (0.62 + 0.38 * smoothstep(0.08, 0.42, p.y));
sunBlush = clamp(sunBlush + (m.starVar - 0.45) * 0.26, 0.0, 1.0);
vec3 autumnCol = mix(bogOlive, amberOchre, smoothstep(0.08, 0.48, m.tipFactor));
autumnCol = mix(autumnCol, bogCrimson, smoothstep(0.28, 0.72, sunBlush));
autumnCol = mix(autumnCol, wineRoseTip, smoothstep(0.66, 0.98, sunBlush));
albedo = mix(emeraldCol, autumnCol, openT);
// Fine hyaline cell speckle
albedo *= (0.88 + 0.24 * fineCell);
// Capillary dew film glistens in axils and across hydrated leaves
float hydration = mix(1.0, 0.32, openT);
wetness = clamp((m.axilWet * 0.85 + 0.28) * hydration, 0.08, 0.95);
roughness = mix(0.46, 0.14, wetness);
// Translucent leaf SSS tint
sssTint = mix(vec3(0.58, 0.82, 0.18), vec3(0.88, 0.17, 0.10), openT * smoothstep(0.20, 0.82, m.tipFactor));
sssStrength = (0.32 + 0.48 * m.tipFactor) * (0.75 + 0.25 * fineCell);
} else {
// Glistening trapped capillary dew bead
albedo = vec3(0.06, 0.09, 0.06);
roughness = 0.035;
specBoost = 1.65;
wetness = 1.0;
sssTint = mix(vec3(0.65, 0.88, 0.35), vec3(0.85, 0.42, 0.28), uOpen);
sssStrength = 0.65;
}
// Pure neutral grayscale studio mode (uLightMode == 1)
if (lightMode == 1) {
float luma = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
// Keep clear tonal separation between peat, moss stems, and star tips in neutral gray
luma = clamp(luma * 1.18 + 0.03, 0.04, 0.72);
albedo = vec3(luma);
sssTint = vec3(0.55);
}
// Wrap diffuse for velvety botanical scattering
float wrap = (m.matId > 0.5 && m.matId < 1.5) ? 0.34 : 0.10;
float ndlKey = max(0.0, (dot(N, keyDir) + wrap) / (1.0 + wrap));
float ndlFill = max(0.0, (dot(N, fillDir) + wrap) / (1.0 + wrap));
float ndlRim = max(0.0, (dot(N, rimDir) + wrap) / (1.0 + wrap));
// Deep boggy inter-stem shadow contrast
float bogAO = mix(ao * ao, ao, 0.45);
vec3 diff = albedo / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.35 + 0.65 * bogAO) +
rimCol * ndlRim * 0.45 * bogAO
);
// Subsurface scattering (thickness probe along light + backlit forward scatter)
float thickKey = clamp(mapHero(p + keyDir * 0.045) / 0.045, 0.0, 1.0);
float thickRim = clamp(mapHero(p + rimDir * 0.045) / 0.045, 0.0, 1.0);
float backKey = pow(clamp(dot(V, -keyDir), 0.0, 1.0), 3.0);
float backRim = pow(clamp(dot(V, -rimDir), 0.0, 1.0), 2.5);
float sssMult = (lightMode == 3) ? 1.55 : 0.85;
vec3 sss = sssTint * sssStrength * sssMult * (
keyCol * (0.28 * thickKey + 0.72 * backKey * (0.4 + 0.6 * thickKey)) * (0.25 + 0.75 * shKey) +
rimCol * (0.25 * thickRim + 0.65 * backRim)
) * (0.35 + 0.65 * ao);
// Specular (GGX-inspired Blinn-Phong lobe + studio reflection for capillary dew films)
vec3 Hkey = normalize(keyDir + V);
vec3 Hrim = normalize(rimDir + V);
float ndv = max(dot(N, V), 0.001);
float fres = mix(0.025, 0.045, wetness) + (1.0 - mix(0.025, 0.045, wetness)) * pow(1.0 - ndv, 5.0);
float shininess = mix(14.0, 180.0, (1.0 - roughness) * (1.0 - roughness));
float specNorm = (shininess + 2.0) / (8.0 * PI);
float sKey = pow(max(dot(N, Hkey), 0.0), shininess) * specNorm * shKey;
float sRim = pow(max(dot(N, Hrim), 0.0), shininess * 0.7) * specNorm * 0.65;
vec3 spec = (keyCol * sKey + rimCol * sRim) * fres * specBoost * (0.45 + 0.55 * wetness);
// Studio environment IBL (diffuse + glossy capillary dew reflection)
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 R = reflect(-V, N);
vec3 envSpec = sampleStudioEnv(R, lightMode);
vec3 col = diff + sss + spec;
col += albedo * envDiff * 0.46 * bogAO;
col += envSpec * fres * mix(0.18, 1.05, wetness) * (1.0 - roughness * 0.65) * (0.35 + 0.65 * ao);
// Dew droplet refractive caustics/bright core glint
if (m.matId > 1.5) {
float coreGlint = pow(ndv, 2.2) * (0.35 + 0.65 * shKey);
vec3 dewCore = (lightMode == 1) ? vec3(0.45) : mix(vec3(0.45, 0.72, 0.20), vec3(0.65, 0.25, 0.16), uOpen);
col += dewCore * coreGlint * 0.55;
}
return col;
}
// -----------------------------------------------------------------------------
// Camera & Raymarch
// -----------------------------------------------------------------------------
void getCamera(int camMode, out vec3 ro, out vec3 ta, out float fov) {
if (camMode == 1) {
// ~2.5x macro view inspecting clustered 3D star-headed Sphagnum capitula & dew
ro = vec3(0.58, 0.78, 0.88);
ta = vec3(0.03, 0.34, 0.04);
fov = 0.82;
} else {
// Hero botanical specimen view grounded on studio bench (~15% closer framing)
ro = vec3(1.32, 1.18, 1.82);
ta = vec3(0.0, 0.25, 0.0);
fov = 1.10;
}
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;
vec3 ro, ta;
float fov;
getCamera(uCamMode, ro, ta, fov);
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
float tMax = 12.0;
bool hit = false;
for (int i = 0; i < 88; i++) {
vec3 p = ro + rd * t;
if (p.y > 1.7 && rd.y > 0.0) break;
float d = mapScene(p);
if (d < 0.0010) {
hit = true;
break;
}
// Conservative step inside the detailed moss canopy for crisp star tips
t += (d < 0.03) ? d * 0.82 : d * 0.95;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
MossEval m = evalHero(p);
float dHero = m.dist;
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
col = shadeBench(p, -rd, uLightMode);
} else {
col = shadeHero(p, -rd, m, uLightMode);
}
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.74;
}
// Gentle studio exposure + ACES tone mapping + sRGB gamma
col *= 1.08;
col = acesToneMap(col);
// Soft studio optical vignette
vec2 q = gl_FragCoord.xy / uResolution.xy;
float vig = 0.88 + 0.12 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.18);
col *= vig;
// Enforce pure neutral grayscale in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(g);
}
col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));
fragColor = vec4(col, 1.0);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Sphagnum Forest Moss — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/fungi_marine/lib/04_sphagnum_moss_star_carpet.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;
// -----------------------------------------------------------------------------
// Noise & Hash 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 noise2(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.8, 0.6, -0.6, 0.8);
for (int i = 0; i < 4; i++) {
v += a * noise2(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
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);
}
// -----------------------------------------------------------------------------
// Peat-Humus Hummock Geometry
// -----------------------------------------------------------------------------
// Organic swatch footprint radius in XZ (pure analytical for fast SDF eval)
float swatchRadius(vec2 xz) {
float ang = atan(xz.y, xz.x);
float r = 1.04;
r += 0.08 * sin(ang * 2.0 + 0.55);
r += 0.05 * cos(ang * 3.0 - 1.10);
r += 0.03 * sin(ang * 5.0 + 2.20);
return r;
}
// Smooth dome profile of the underlying peat-humus cushion (analytical, zero fbm)
float hummockHeight(vec2 xz) {
float rad = swatchRadius(xz);
float u = length(xz) / rad;
float dome = max(0.0, 1.0 - u * u);
// Pillowy sub-mounds characteristic of natural Sphagnum hummocks
vec2 d1 = xz - vec2(-0.28, 0.18);
vec2 d2 = xz - vec2( 0.30, 0.14);
vec2 d3 = xz - vec2( 0.02, -0.32);
float lobes = 0.058 * exp(-dot(d1, d1) * 3.8)
+ 0.052 * exp(-dot(d2, d2) * 4.2)
+ 0.048 * exp(-dot(d3, d3) * 4.0);
float ripple = 0.018 * sin(xz.x * 5.4 + 0.6) * cos(xz.y * 4.9 - 0.4);
float h = 0.44 * pow(dome, 0.72) + (lobes + ripple) * smoothstep(1.0, 0.35, u);
return max(0.0, h);
}
// Signed distance to the fibrous peat-humus base swatch
float sdPeatBase(vec3 p) {
float rad = swatchRadius(p.xz);
float r = length(p.xz);
float hTop = hummockHeight(p.xz);
// Slight natural undercut/bevel at the base rim where the swatch rests on the bench
float rimBevel = 0.045 * (1.0 - smoothstep(0.0, 0.13, p.y));
float dSide = r - (rad - rimBevel);
float dTop = (p.y - hTop) * 0.78;
float dBot = -p.y;
float dBody = smax(dSide, dTop, 0.065);
dBody = smax(dBody, dBot, 0.014);
// Fast 1-octave fibrous peat micro-relief only near the peat surface
if (dBody < 0.06) {
float peatMask = smoothstep(0.18, 0.02, p.y) + 0.30;
float fiber = (noise2(vec2(atan(p.z, p.x) * 6.0, p.y * 26.0) + p.xz * 4.5) - 0.5) * 0.014 * peatMask;
dBody += fiber;
}
return dBody;
}
// -----------------------------------------------------------------------------
// 3D Star-Headed Sphagnum Moss Capitula Carpet
// -----------------------------------------------------------------------------
struct MossEval {
float dist; // Combined hero SDF
float matId; // 0 = peat humus, 1 = sphagnum moss, 2 = capillary dew bead
float tipFactor; // 0 = deep stem/hollow, 1 = upper star rosette tip
float starVar; // per-capitulum color/species variation (0..1)
float axilWet; // capillary water film intensity (0..1)
};
// Single 3D star-headed Sphagnum capitulum in local tangent space (y up)
// Returns vec3(sdf, tipFactor, axilWet)
vec3 evalCapitulum(vec3 lp, float sc, float nArms, float rot, float seed) {
// Gentle organic breeze/hydration micro-motion when uTime > 0
float sway = sin(uTime * 0.8 + seed * TAU) * 0.0025 * smoothstep(0.0, 0.05, lp.y + 0.03);
lp.x += sway;
float rad = length(lp.xz);
float ang = atan(lp.z, lp.x) + rot;
// --- Outer primary star whorl (5 or 6 radiating lanceolate branches) ---
float sector = TAU / nArms;
float a1 = mod(ang + 0.5 * sector, sector) - 0.5 * sector;
vec2 q1 = vec2(cos(a1), sin(a1)) * rad;
float L1 = 0.076 * sc;
float t1 = clamp(q1.x / L1, 0.0, 1.0);
// Arching upward from the bud then gently recurving outward and down at the tip
float arch1 = sc * (0.015 * sin(t1 * 2.55) - 0.024 * t1 * t1);
// Pointed lanceolate leaf-branch cross-section with subtle imbricate leaf fluting
float flute1 = 1.0 + 0.11 * sin(t1 * 18.0 + seed * 10.0);
float w1 = sc * 0.0165 * (1.0 - 0.78 * t1) * (0.55 + 0.45 * sin(t1 * 2.8 + 0.35)) * flute1;
// Slightly flattened vertically like clustered tiny moss leaves
vec3 bp1 = vec3(q1.x - t1 * L1, (lp.y - arch1) * 1.18, q1.y);
float dOuter = length(bp1) - w1;
// --- Inner raised secondary whorl (rotated half-sector, compact central rosette) ---
float a2 = mod(ang + sector, sector) - 0.5 * sector;
vec2 q2 = vec2(cos(a2), sin(a2)) * rad;
float L2 = 0.044 * sc;
float t2 = clamp(q2.x / L2, 0.0, 1.0);
float arch2 = sc * (0.019 + 0.012 * sin(t2 * 2.4) - 0.014 * t2 * t2);
float w2 = sc * 0.0135 * (1.0 - 0.72 * t2) * (0.65 + 0.35 * sin(t2 * 3.0 + 0.4));
vec3 bp2 = vec3(q2.x - t2 * L2, (lp.y - arch2) * 1.15, q2.y);
float dInner = length(bp2) - w2;
// --- Central apical bud (plump rosette heart) ---
float dBud = length(lp - vec3(0.0, 0.017 * sc, 0.0)) - 0.019 * sc;
// --- Lower stem descending into the boggy inter-stem cavity ---
float stemTop = 0.008 * sc;
float stemBot = -0.105 * sc;
float sy = clamp(lp.y, stemBot, stemTop);
float dStem = length(vec2(rad, lp.y - sy)) - 0.0135 * sc;
// Combine rosette components smoothly so each star reads as one organic gametophyte
float dStar = smin(dOuter, dInner, 0.0075 * sc);
dStar = smin(dStar, dBud, 0.0065 * sc);
float dFull = smin(dStar, dStem, 0.009 * sc);
// Tip factor: highest on the central bud, inner whorl, and outer star tips
float outerTip = smoothstep(0.15, 0.95, t1) * smoothstep(-0.025 * sc, 0.022 * sc, lp.y);
float crownBoost = smoothstep(0.055 * sc, 0.0, rad) * smoothstep(0.0, 0.028 * sc, lp.y);
float tipFactor = clamp(max(outerTip, crownBoost * 0.92), 0.0, 1.0);
// Suppress tip factor down on the stem
tipFactor *= smoothstep(-0.045 * sc, 0.005 * sc, lp.y);
// Capillary water meniscus collects in the concave axils between branches and center
float axil = smoothstep(0.055 * sc, 0.010 * sc, rad) * smoothstep(0.026 * sc, -0.006 * sc, lp.y);
float groove = smoothstep(0.016 * sc, 0.001 * sc, abs(q1.y)) * (1.0 - t1 * 0.5);
float axilWet = clamp(axil * 0.85 + groove * 0.35, 0.0, 1.0);
return vec3(dFull, tipFactor, axilWet);
}
MossEval evalHero(vec3 p) {
MossEval res;
res.matId = 0.0;
res.tipFactor = 0.0;
res.starVar = 0.5;
res.axilWet = 0.25;
// Cheap bounding sphere early-out so far rays skip all hummock/moss work
float dB = length(p - vec3(0.0, 0.25, 0.0)) - 1.32;
if (dB > 0.12) {
res.dist = dB;
return res;
}
float dPeat = sdPeatBase(p);
res.dist = dPeat;
// Cheap bounding envelope around the hummock canopy so mid-range rays march fast
if (dPeat > 0.17) {
res.dist = dPeat - 0.095;
return res;
}
// Peat base slightly recessed beneath the moss carpet on top, exposed at the lower skirt
float rad = swatchRadius(p.xz);
float uRad = length(p.xz) / rad;
float canopyMask = smoothstep(0.99, 0.84, uRad) * smoothstep(0.035, 0.115, p.y);
float dPeatRecessed = dPeat + 0.038 * canopyMask;
res.dist = dPeatRecessed;
res.matId = 0.0;
// Only evaluate star rosettes within the living canopy footprint
if (uRad > 1.08 || p.y < 0.015) {
return res;
}
// Tightly packed jittered 2D grid of interlocking 3D Sphagnum capitula (3x3 neighborhood)
const float cellSize = 0.094;
vec2 gPos = p.xz / cellSize;
vec2 gId = floor(gPos);
float dMoss = 1e5;
float bestTip = 0.0;
float bestVar = 0.5;
float bestWet = 0.0;
float dDew = 1e5;
float dewScale = mix(1.0, 0.52, uOpen);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 id = gId + vec2(float(i), float(j));
vec3 rnd = hash32(id);
// Jittered center in XZ
vec2 cXZ = (id + 0.18 + 0.64 * rnd.xy) * cellSize;
// Fast XZ proximity reject matched to ~1.3x lush capitula radius
vec2 dxz = p.xz - cXZ;
if (dot(dxz, dxz) > 0.028) continue;
float cRad = swatchRadius(cXZ);
float cU = length(cXZ) / cRad;
if (cU > 0.965) continue;
// Anchor height on the hummock dome + plush organic height variation
float baseH = hummockHeight(cXZ);
float edgeTaper = smoothstep(0.97, 0.80, cU);
float plushOff = (rnd.z - 0.32) * 0.060 * edgeTaper + 0.034 * edgeTaper;
vec3 cPos = vec3(cXZ.x, baseH + plushOff, cXZ.y);
// Quick 3D sphere cull per capitulum
vec3 dp = p - cPos;
if (dot(dp, dp) > 0.034) continue;
vec2 rnd2 = hash22(id + vec2(19.3, 47.1));
// Analytical dome normal (zero finite-difference calls)
vec3 hN = normalize(vec3(cXZ.x * 0.85, 1.0, cXZ.y * 0.85));
vec3 up = normalize(mix(vec3(0.0, 1.0, 0.0), hN, 0.58)
+ vec3(rnd2.x - 0.5, 0.0, rnd2.y - 0.5) * 0.24);
vec3 side = normalize(cross(up, vec3(0.0, 0.0, 1.0)));
vec3 fwd = cross(side, up);
vec3 lp = vec3(dot(dp, side), dot(dp, up), dot(dp, fwd));
float sc = (1.06 + 0.44 * rnd2.x) * (0.80 + 0.20 * edgeTaper);
float nArms = (rnd2.y > 0.42) ? 5.0 : 6.0;
float rot = rnd.z * TAU;
vec3 cap = evalCapitulum(lp, sc, nArms, rot, rnd.x);
if (cap.x < dMoss) {
dMoss = cap.x;
bestTip = cap.y;
bestVar = rnd2.y;
bestWet = cap.z;
}
// Tiny glistening capillary dew beads perched on ~38% of star rosettes
if (rnd.x > 0.62 && dewScale > 0.2) {
float beadAng = rot + floor(rnd.y * nArms) * (TAU / nArms);
float beadR = (0.026 + 0.028 * rnd2.x) * sc;
vec3 beadPos = vec3(cos(beadAng) * beadR, 0.017 * sc, sin(beadAng) * beadR);
float beadRad = (0.0082 + 0.0045 * rnd.z) * sc * dewScale;
float db = length(lp - beadPos) - beadRad;
dDew = min(dDew, db);
}
}
}
// Select closest hero surface: peat humus, Sphagnum moss, or dew droplet
if (dMoss < res.dist) {
res.dist = dMoss;
res.matId = 1.0;
res.tipFactor = bestTip;
res.starVar = bestVar;
res.axilWet = bestWet;
}
if (dDew < res.dist) {
res.dist = dDew;
res.matId = 2.0;
res.tipFactor = 1.0;
res.starVar = 1.0;
res.axilWet = 1.0;
}
return res;
}
float mapHero(vec3 p) {
return evalHero(p).dist;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadows & Ambient Occlusion (Strict QA Contract)
// -----------------------------------------------------------------------------
// Unit-weight tetrahedral normal on hero SDF
vec3 calcHeroNormal(vec3 p) {
const float h = 0.0014;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Clean hero-only soft shadow (no ph, bias p+N*0.014, tMin=0.025, step max(h,0.015))
float softShadow(vec3 p, vec3 N, vec3 rd, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 30; i++) {
vec3 q = ro + rd * t;
float h = (t > 0.15) ? (sdPeatBase(q) - 0.035) : mapHero(q);
if (h < 0.0008) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
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 < 4; i++) {
float h = 0.018 + 0.038 * float(i);
vec3 q = p + N * h;
float d = (i < 2) ? mapScene(q) : min(sdPeatBase(q) - 0.035, q.y);
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.35 * occ, 0.08, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Cyclorama, Softboxes & Negative-Fill Flags
// -----------------------------------------------------------------------------
vec3 sampleStudioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.11, 0.115, 0.11);
vec3 midCol = vec3(0.24, 0.25, 0.245);
vec3 topCol = vec3(0.38, 0.40, 0.42);
if (lightMode == 1) {
botCol = vec3(0.13);
midCol = vec3(0.26);
topCol = vec3(0.40);
} else if (lightMode == 2) {
botCol = vec3(0.08, 0.085, 0.09);
midCol = vec3(0.16, 0.175, 0.19);
topCol = vec3(0.24, 0.27, 0.31);
} else if (lightMode == 3) {
botCol = vec3(0.09, 0.095, 0.09);
midCol = vec3(0.19, 0.20, 0.19);
topCol = vec3(0.28, 0.30, 0.32);
}
vec3 env = mix(botCol, midCol, smoothstep(-0.25, 0.18, y));
env = mix(env, topCol, smoothstep(0.18, 0.85, y));
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.25, 0.90, 0.35));
float sb1 = smoothstep(0.76, 0.95, dot(rd, sb1Dir));
vec3 sb1Col = (lightMode == 1) ? vec3(1.45) : vec3(1.45, 1.42, 1.35);
env += sb1 * sb1Col * (lightMode == 2 ? 0.45 : 0.95);
// Side fill softbox (cool-neutral)
vec3 sb2Dir = normalize(vec3(-0.82, 0.42, 0.38));
float sb2 = smoothstep(0.78, 0.96, dot(rd, sb2Dir));
vec3 sb2Col = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.02);
env += sb2 * sb2Col * 0.75;
// Back/rim strip softbox (boosted in backlit mode)
vec3 sb3Dir = normalize(vec3(-0.20, 0.42, -0.88));
float sb3 = smoothstep(0.80, 0.97, dot(rd, sb3Dir));
vec3 sb3Col = (lightMode == 1) ? vec3(1.25) : vec3(1.35, 1.28, 1.12);
env += sb3 * sb3Col * (lightMode == 3 ? 1.85 : 0.70);
// Grazing softbox when uLightMode == 2
if (lightMode == 2) {
vec3 sbGDir = normalize(vec3(0.92, 0.24, 0.28));
float sbG = smoothstep(0.84, 0.97, dot(rd, sbGDir));
env += sbG * vec3(1.75, 1.62, 1.40);
}
// Negative-fill flags on lower-right and far-left to carve glossy dew contrast
float negFlag1 = smoothstep(0.70, 0.90, dot(rd, normalize(vec3(0.88, 0.05, -0.45))));
float negFlag2 = smoothstep(0.74, 0.92, dot(rd, normalize(vec3(-0.90, 0.02, -0.40))));
env *= (1.0 - 0.62 * negFlag1) * (1.0 - 0.55 * negFlag2);
return env;
}
// ACES Filmic Tone Mapping
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);
}
// -----------------------------------------------------------------------------
// Shading
// -----------------------------------------------------------------------------
void getLightRig(int lightMode, out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol) {
if (lightMode == 1) {
// Pure neutral grayscale studio rig
keyDir = normalize(vec3(0.55, 0.78, 0.42));
keyCol = vec3(2.05);
fillDir = normalize(vec3(-0.75, 0.45, 0.48));
fillCol = vec3(0.65);
rimDir = normalize(vec3(-0.25, 0.52, -0.82));
rimCol = vec3(0.85);
} else if (lightMode == 2) {
// Grazing raking light across the star carpet
keyDir = normalize(vec3(0.92, 0.25, 0.30));
keyCol = vec3(2.45, 2.28, 1.98);
fillDir = normalize(vec3(-0.72, 0.55, 0.42));
fillCol = vec3(0.52, 0.60, 0.72);
rimDir = normalize(vec3(-0.45, 0.40, -0.80));
rimCol = vec3(0.72, 0.76, 0.82);
} else if (lightMode == 3) {
// Backlit botanical translucency rig
keyDir = normalize(vec3(-0.18, 0.46, -0.87));
keyCol = vec3(2.65, 2.42, 2.02);
fillDir = normalize(vec3(0.42, 0.68, 0.60));
fillCol = vec3(0.68, 0.74, 0.80);
rimDir = normalize(vec3(0.65, 0.40, -0.64));
rimCol = vec3(1.15, 1.12, 0.98);
} else {
// Hero studio lighting
keyDir = normalize(vec3(0.54, 0.78, 0.44));
keyCol = vec3(2.15, 2.06, 1.90);
fillDir = normalize(vec3(-0.78, 0.46, 0.42));
fillCol = vec3(0.58, 0.67, 0.80);
rimDir = normalize(vec3(-0.28, 0.52, -0.81));
rimCol = vec3(0.95, 0.92, 0.82);
}
}
vec3 shadeBench(vec3 p, vec3 V, int lightMode) {
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Fine matte mineral-stone specimen bench texture
float grain = fbm2(p.xz * 7.5) * 0.5 + noise2(p.xz * 34.0) * 0.5;
vec3 baseCol = (lightMode == 1)
? vec3(0.36) * (0.94 + 0.12 * grain)
: vec3(0.37, 0.365, 0.35) * (0.94 + 0.12 * grain);
// Subtle damp bog-moisture halo right around the peat-humus swatch contact edge
float r = length(p.xz) / swatchRadius(p.xz);
float dampRing = smoothstep(1.14, 0.96, r) * mix(0.28, 0.12, uOpen);
baseCol *= (1.0 - dampRing);
float shKey = softShadow(p, N, keyDir, 3.5, 9.0);
float ao = calcAO(p, N);
float ndlKey = max(dot(N, keyDir), 0.0);
float ndlFill = max(dot(N, fillDir), 0.0);
float ndlRim = max(dot(N, rimDir), 0.0);
vec3 diff = baseCol / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.45 + 0.55 * ao) +
rimCol * ndlRim * 0.35 * ao
);
// Soft studio ambient + slight sheen on damp contact ring
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 col = diff + baseCol * envDiff * 0.52 * ao;
vec3 R = reflect(-V, N);
float fres = pow(1.0 - max(dot(N, V), 0.0), 4.0);
float benchSpec = mix(0.06, 0.24, dampRing);
col += sampleStudioEnv(R, lightMode) * fres * benchSpec * ao;
// Gentle radial studio backdrop vignette fade on distant bench
float distFade = smoothstep(2.4, 8.5, length(p.xz));
vec3 horizonCol = sampleStudioEnv(normalize(vec3(p.x, 0.12, p.z)), lightMode) * 0.72;
col = mix(col, horizonCol, distFade);
return col;
}
vec3 shadeHero(vec3 p, vec3 V, MossEval m, int lightMode) {
vec3 N = calcHeroNormal(p);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
float ao = calcAO(p, N);
float shKey = softShadow(p, N, keyDir, 2.5, 11.0);
// Local micro-texture for botanical leaf cellularity & peat fibers
float micro = fbm2(p.xz * 26.0 + p.y * 14.0);
float fineCell = noise2(p.xz * 78.0 + p.y * 55.0);
// Determine albedo, roughness, F0, and SSS parameters by material ID
vec3 albedo;
vec3 sssTint;
float sssStrength = 0.0;
float roughness = 0.55;
float specBoost = 1.0;
float wetness = 0.0;
if (m.matId < 0.5) {
// Fibrous dark bog peat-humus substrate (deep rich boggy humus under canopy)
vec3 peatDark = vec3(0.032, 0.024, 0.016);
vec3 peatWarm = vec3(0.092, 0.065, 0.040);
albedo = mix(peatDark, peatWarm, micro * 0.78 + fineCell * 0.22);
// Deepen under-canopy inter-stem humus pockets into dark boggy olive-peat shadow
float underCanopy = smoothstep(0.06, 0.22, p.y);
albedo = mix(albedo, vec3(0.030, 0.046, 0.018), underCanopy * 0.72);
roughness = mix(0.68, 0.42, mix(0.75, 0.35, uOpen) * (1.0 - micro * 0.4));
sssTint = vec3(0.08, 0.05, 0.03);
sssStrength = 0.04;
wetness = mix(0.45, 0.18, uOpen);
} else if (m.matId < 1.5) {
// Living 3D star-headed Sphagnum gametophyte
float openT = clamp(uOpen, 0.0, 1.0);
// Hydrated lush emerald palette (uOpen = 0)
vec3 stemEmerald = vec3(0.048, 0.125, 0.028);
vec3 midEmerald = vec3(0.135, 0.355, 0.062);
vec3 limeTip = vec3(0.395, 0.575, 0.115);
vec3 coolMintVar = vec3(0.115, 0.315, 0.105);
vec3 emeraldCol = mix(stemEmerald, midEmerald, smoothstep(0.08, 0.58, m.tipFactor));
emeraldCol = mix(emeraldCol, limeTip, smoothstep(0.55, 0.98, m.tipFactor));
emeraldCol = mix(emeraldCol, coolMintVar, m.starVar * 0.22 * (1.0 - m.tipFactor * 0.5));
// Sun-blushed bog-crimson autumn palette (uOpen = 1)
vec3 bogOlive = vec3(0.085, 0.132, 0.032);
vec3 amberOchre = vec3(0.315, 0.235, 0.055);
vec3 bogCrimson = vec3(0.510, 0.058, 0.064);
vec3 wineRoseTip = vec3(0.675, 0.108, 0.098);
// Sun exposure favors upper hummock crown + rosette star tips
float sunBlush = smoothstep(0.18, 0.86, m.tipFactor) * (0.62 + 0.38 * smoothstep(0.08, 0.42, p.y));
sunBlush = clamp(sunBlush + (m.starVar - 0.45) * 0.26, 0.0, 1.0);
vec3 autumnCol = mix(bogOlive, amberOchre, smoothstep(0.08, 0.48, m.tipFactor));
autumnCol = mix(autumnCol, bogCrimson, smoothstep(0.28, 0.72, sunBlush));
autumnCol = mix(autumnCol, wineRoseTip, smoothstep(0.66, 0.98, sunBlush));
albedo = mix(emeraldCol, autumnCol, openT);
// Fine hyaline cell speckle
albedo *= (0.88 + 0.24 * fineCell);
// Capillary dew film glistens in axils and across hydrated leaves
float hydration = mix(1.0, 0.32, openT);
wetness = clamp((m.axilWet * 0.85 + 0.28) * hydration, 0.08, 0.95);
roughness = mix(0.46, 0.14, wetness);
// Translucent leaf SSS tint
sssTint = mix(vec3(0.58, 0.82, 0.18), vec3(0.88, 0.17, 0.10), openT * smoothstep(0.20, 0.82, m.tipFactor));
sssStrength = (0.32 + 0.48 * m.tipFactor) * (0.75 + 0.25 * fineCell);
} else {
// Glistening trapped capillary dew bead
albedo = vec3(0.06, 0.09, 0.06);
roughness = 0.035;
specBoost = 1.65;
wetness = 1.0;
sssTint = mix(vec3(0.65, 0.88, 0.35), vec3(0.85, 0.42, 0.28), uOpen);
sssStrength = 0.65;
}
// Pure neutral grayscale studio mode (uLightMode == 1)
if (lightMode == 1) {
float luma = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
// Keep clear tonal separation between peat, moss stems, and star tips in neutral gray
luma = clamp(luma * 1.18 + 0.03, 0.04, 0.72);
albedo = vec3(luma);
sssTint = vec3(0.55);
}
// Wrap diffuse for velvety botanical scattering
float wrap = (m.matId > 0.5 && m.matId < 1.5) ? 0.34 : 0.10;
float ndlKey = max(0.0, (dot(N, keyDir) + wrap) / (1.0 + wrap));
float ndlFill = max(0.0, (dot(N, fillDir) + wrap) / (1.0 + wrap));
float ndlRim = max(0.0, (dot(N, rimDir) + wrap) / (1.0 + wrap));
// Deep boggy inter-stem shadow contrast
float bogAO = mix(ao * ao, ao, 0.45);
vec3 diff = albedo / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.35 + 0.65 * bogAO) +
rimCol * ndlRim * 0.45 * bogAO
);
// Subsurface scattering (thickness probe along light + backlit forward scatter)
float thickKey = clamp(mapHero(p + keyDir * 0.045) / 0.045, 0.0, 1.0);
float thickRim = clamp(mapHero(p + rimDir * 0.045) / 0.045, 0.0, 1.0);
float backKey = pow(clamp(dot(V, -keyDir), 0.0, 1.0), 3.0);
float backRim = pow(clamp(dot(V, -rimDir), 0.0, 1.0), 2.5);
float sssMult = (lightMode == 3) ? 1.55 : 0.85;
vec3 sss = sssTint * sssStrength * sssMult * (
keyCol * (0.28 * thickKey + 0.72 * backKey * (0.4 + 0.6 * thickKey)) * (0.25 + 0.75 * shKey) +
rimCol * (0.25 * thickRim + 0.65 * backRim)
) * (0.35 + 0.65 * ao);
// Specular (GGX-inspired Blinn-Phong lobe + studio reflection for capillary dew films)
vec3 Hkey = normalize(keyDir + V);
vec3 Hrim = normalize(rimDir + V);
float ndv = max(dot(N, V), 0.001);
float fres = mix(0.025, 0.045, wetness) + (1.0 - mix(0.025, 0.045, wetness)) * pow(1.0 - ndv, 5.0);
float shininess = mix(14.0, 180.0, (1.0 - roughness) * (1.0 - roughness));
float specNorm = (shininess + 2.0) / (8.0 * PI);
float sKey = pow(max(dot(N, Hkey), 0.0), shininess) * specNorm * shKey;
float sRim = pow(max(dot(N, Hrim), 0.0), shininess * 0.7) * specNorm * 0.65;
vec3 spec = (keyCol * sKey + rimCol * sRim) * fres * specBoost * (0.45 + 0.55 * wetness);
// Studio environment IBL (diffuse + glossy capillary dew reflection)
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 R = reflect(-V, N);
vec3 envSpec = sampleStudioEnv(R, lightMode);
vec3 col = diff + sss + spec;
col += albedo * envDiff * 0.46 * bogAO;
col += envSpec * fres * mix(0.18, 1.05, wetness) * (1.0 - roughness * 0.65) * (0.35 + 0.65 * ao);
// Dew droplet refractive caustics/bright core glint
if (m.matId > 1.5) {
float coreGlint = pow(ndv, 2.2) * (0.35 + 0.65 * shKey);
vec3 dewCore = (lightMode == 1) ? vec3(0.45) : mix(vec3(0.45, 0.72, 0.20), vec3(0.65, 0.25, 0.16), uOpen);
col += dewCore * coreGlint * 0.55;
}
return col;
}
// -----------------------------------------------------------------------------
// Camera & Raymarch
// -----------------------------------------------------------------------------
void getCamera(int camMode, out vec3 ro, out vec3 ta, out float fov) {
if (camMode == 1) {
// ~2.5x macro view inspecting clustered 3D star-headed Sphagnum capitula & dew
ro = vec3(0.58, 0.78, 0.88);
ta = vec3(0.03, 0.34, 0.04);
fov = 0.82;
} else {
// Hero botanical specimen view grounded on studio bench (~15% closer framing)
ro = vec3(1.32, 1.18, 1.82);
ta = vec3(0.0, 0.25, 0.0);
fov = 1.10;
}
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / uResolution.y;
vec3 ro, ta;
float fov;
getCamera(uCamMode, ro, ta, fov);
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
float tMax = 12.0;
bool hit = false;
for (int i = 0; i < 88; i++) {
vec3 p = ro + rd * t;
if (p.y > 1.7 && rd.y > 0.0) break;
float d = mapScene(p);
if (d < 0.0010) {
hit = true;
break;
}
// Conservative step inside the detailed moss canopy for crisp star tips
t += (d < 0.03) ? d * 0.82 : d * 0.95;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
MossEval m = evalHero(p);
float dHero = m.dist;
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
col = shadeBench(p, -rd, uLightMode);
} else {
col = shadeHero(p, -rd, m, uLightMode);
}
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.74;
}
// Gentle studio exposure + ACES tone mapping + sRGB gamma
col *= 1.08;
col = acesToneMap(col);
// Soft studio optical vignette
vec2 q = pfx_frag_xy / uResolution.xy;
float vig = 0.88 + 0.12 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.18);
col *= vig;
// Enforce pure neutral grayscale in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(g);
}
col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));
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;
// Sphagnum Forest Moss — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/fungi_marine/lib/04_sphagnum_moss_star_carpet.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;
// -----------------------------------------------------------------------------
// Noise & Hash 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 noise2(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.8, 0.6, -0.6, 0.8);
for (int i = 0; i < 4; i++) {
v += a * noise2(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
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);
}
// -----------------------------------------------------------------------------
// Peat-Humus Hummock Geometry
// -----------------------------------------------------------------------------
// Organic swatch footprint radius in XZ (pure analytical for fast SDF eval)
float swatchRadius(vec2 xz) {
float ang = atan(xz.y, xz.x);
float r = 1.04;
r += 0.08 * sin(ang * 2.0 + 0.55);
r += 0.05 * cos(ang * 3.0 - 1.10);
r += 0.03 * sin(ang * 5.0 + 2.20);
return r;
}
// Smooth dome profile of the underlying peat-humus cushion (analytical, zero fbm)
float hummockHeight(vec2 xz) {
float rad = swatchRadius(xz);
float u = length(xz) / rad;
float dome = max(0.0, 1.0 - u * u);
// Pillowy sub-mounds characteristic of natural Sphagnum hummocks
vec2 d1 = xz - vec2(-0.28, 0.18);
vec2 d2 = xz - vec2( 0.30, 0.14);
vec2 d3 = xz - vec2( 0.02, -0.32);
float lobes = 0.058 * exp(-dot(d1, d1) * 3.8)
+ 0.052 * exp(-dot(d2, d2) * 4.2)
+ 0.048 * exp(-dot(d3, d3) * 4.0);
float ripple = 0.018 * sin(xz.x * 5.4 + 0.6) * cos(xz.y * 4.9 - 0.4);
float h = 0.44 * pow(dome, 0.72) + (lobes + ripple) * smoothstep(1.0, 0.35, u);
return max(0.0, h);
}
// Signed distance to the fibrous peat-humus base swatch
float sdPeatBase(vec3 p) {
float rad = swatchRadius(p.xz);
float r = length(p.xz);
float hTop = hummockHeight(p.xz);
// Slight natural undercut/bevel at the base rim where the swatch rests on the bench
float rimBevel = 0.045 * (1.0 - smoothstep(0.0, 0.13, p.y));
float dSide = r - (rad - rimBevel);
float dTop = (p.y - hTop) * 0.78;
float dBot = -p.y;
float dBody = smax(dSide, dTop, 0.065);
dBody = smax(dBody, dBot, 0.014);
// Fast 1-octave fibrous peat micro-relief only near the peat surface
if (dBody < 0.06) {
float peatMask = smoothstep(0.18, 0.02, p.y) + 0.30;
float fiber = (noise2(vec2(atan(p.z, p.x) * 6.0, p.y * 26.0) + p.xz * 4.5) - 0.5) * 0.014 * peatMask;
dBody += fiber;
}
return dBody;
}
// -----------------------------------------------------------------------------
// 3D Star-Headed Sphagnum Moss Capitula Carpet
// -----------------------------------------------------------------------------
struct MossEval {
float dist; // Combined hero SDF
float matId; // 0 = peat humus, 1 = sphagnum moss, 2 = capillary dew bead
float tipFactor; // 0 = deep stem/hollow, 1 = upper star rosette tip
float starVar; // per-capitulum color/species variation (0..1)
float axilWet; // capillary water film intensity (0..1)
};
// Single 3D star-headed Sphagnum capitulum in local tangent space (y up)
// Returns vec3(sdf, tipFactor, axilWet)
vec3 evalCapitulum(vec3 lp, float sc, float nArms, float rot, float seed) {
// Gentle organic breeze/hydration micro-motion when uTime > 0
float sway = sin(uTime * 0.8 + seed * TAU) * 0.0025 * smoothstep(0.0, 0.05, lp.y + 0.03);
lp.x += sway;
float rad = length(lp.xz);
float ang = atan(lp.z, lp.x) + rot;
// --- Outer primary star whorl (5 or 6 radiating lanceolate branches) ---
float sector = TAU / nArms;
float a1 = mod(ang + 0.5 * sector, sector) - 0.5 * sector;
vec2 q1 = vec2(cos(a1), sin(a1)) * rad;
float L1 = 0.076 * sc;
float t1 = clamp(q1.x / L1, 0.0, 1.0);
// Arching upward from the bud then gently recurving outward and down at the tip
float arch1 = sc * (0.015 * sin(t1 * 2.55) - 0.024 * t1 * t1);
// Pointed lanceolate leaf-branch cross-section with subtle imbricate leaf fluting
float flute1 = 1.0 + 0.11 * sin(t1 * 18.0 + seed * 10.0);
float w1 = sc * 0.0165 * (1.0 - 0.78 * t1) * (0.55 + 0.45 * sin(t1 * 2.8 + 0.35)) * flute1;
// Slightly flattened vertically like clustered tiny moss leaves
vec3 bp1 = vec3(q1.x - t1 * L1, (lp.y - arch1) * 1.18, q1.y);
float dOuter = length(bp1) - w1;
// --- Inner raised secondary whorl (rotated half-sector, compact central rosette) ---
float a2 = mod(ang + sector, sector) - 0.5 * sector;
vec2 q2 = vec2(cos(a2), sin(a2)) * rad;
float L2 = 0.044 * sc;
float t2 = clamp(q2.x / L2, 0.0, 1.0);
float arch2 = sc * (0.019 + 0.012 * sin(t2 * 2.4) - 0.014 * t2 * t2);
float w2 = sc * 0.0135 * (1.0 - 0.72 * t2) * (0.65 + 0.35 * sin(t2 * 3.0 + 0.4));
vec3 bp2 = vec3(q2.x - t2 * L2, (lp.y - arch2) * 1.15, q2.y);
float dInner = length(bp2) - w2;
// --- Central apical bud (plump rosette heart) ---
float dBud = length(lp - vec3(0.0, 0.017 * sc, 0.0)) - 0.019 * sc;
// --- Lower stem descending into the boggy inter-stem cavity ---
float stemTop = 0.008 * sc;
float stemBot = -0.105 * sc;
float sy = clamp(lp.y, stemBot, stemTop);
float dStem = length(vec2(rad, lp.y - sy)) - 0.0135 * sc;
// Combine rosette components smoothly so each star reads as one organic gametophyte
float dStar = smin(dOuter, dInner, 0.0075 * sc);
dStar = smin(dStar, dBud, 0.0065 * sc);
float dFull = smin(dStar, dStem, 0.009 * sc);
// Tip factor: highest on the central bud, inner whorl, and outer star tips
float outerTip = smoothstep(0.15, 0.95, t1) * smoothstep(-0.025 * sc, 0.022 * sc, lp.y);
float crownBoost = smoothstep(0.055 * sc, 0.0, rad) * smoothstep(0.0, 0.028 * sc, lp.y);
float tipFactor = clamp(max(outerTip, crownBoost * 0.92), 0.0, 1.0);
// Suppress tip factor down on the stem
tipFactor *= smoothstep(-0.045 * sc, 0.005 * sc, lp.y);
// Capillary water meniscus collects in the concave axils between branches and center
float axil = smoothstep(0.055 * sc, 0.010 * sc, rad) * smoothstep(0.026 * sc, -0.006 * sc, lp.y);
float groove = smoothstep(0.016 * sc, 0.001 * sc, abs(q1.y)) * (1.0 - t1 * 0.5);
float axilWet = clamp(axil * 0.85 + groove * 0.35, 0.0, 1.0);
return vec3(dFull, tipFactor, axilWet);
}
MossEval evalHero(vec3 p) {
MossEval res;
res.matId = 0.0;
res.tipFactor = 0.0;
res.starVar = 0.5;
res.axilWet = 0.25;
// Cheap bounding sphere early-out so far rays skip all hummock/moss work
float dB = length(p - vec3(0.0, 0.25, 0.0)) - 1.32;
if (dB > 0.12) {
res.dist = dB;
return res;
}
float dPeat = sdPeatBase(p);
res.dist = dPeat;
// Cheap bounding envelope around the hummock canopy so mid-range rays march fast
if (dPeat > 0.17) {
res.dist = dPeat - 0.095;
return res;
}
// Peat base slightly recessed beneath the moss carpet on top, exposed at the lower skirt
float rad = swatchRadius(p.xz);
float uRad = length(p.xz) / rad;
float canopyMask = smoothstep(0.99, 0.84, uRad) * smoothstep(0.035, 0.115, p.y);
float dPeatRecessed = dPeat + 0.038 * canopyMask;
res.dist = dPeatRecessed;
res.matId = 0.0;
// Only evaluate star rosettes within the living canopy footprint
if (uRad > 1.08 || p.y < 0.015) {
return res;
}
// Tightly packed jittered 2D grid of interlocking 3D Sphagnum capitula (3x3 neighborhood)
const float cellSize = 0.094;
vec2 gPos = p.xz / cellSize;
vec2 gId = floor(gPos);
float dMoss = 1e5;
float bestTip = 0.0;
float bestVar = 0.5;
float bestWet = 0.0;
float dDew = 1e5;
float dewScale = mix(1.0, 0.52, uOpen);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 id = gId + vec2(float(i), float(j));
vec3 rnd = hash32(id);
// Jittered center in XZ
vec2 cXZ = (id + 0.18 + 0.64 * rnd.xy) * cellSize;
// Fast XZ proximity reject matched to ~1.3x lush capitula radius
vec2 dxz = p.xz - cXZ;
if (dot(dxz, dxz) > 0.028) continue;
float cRad = swatchRadius(cXZ);
float cU = length(cXZ) / cRad;
if (cU > 0.965) continue;
// Anchor height on the hummock dome + plush organic height variation
float baseH = hummockHeight(cXZ);
float edgeTaper = smoothstep(0.97, 0.80, cU);
float plushOff = (rnd.z - 0.32) * 0.060 * edgeTaper + 0.034 * edgeTaper;
vec3 cPos = vec3(cXZ.x, baseH + plushOff, cXZ.y);
// Quick 3D sphere cull per capitulum
vec3 dp = p - cPos;
if (dot(dp, dp) > 0.034) continue;
vec2 rnd2 = hash22(id + vec2(19.3, 47.1));
// Analytical dome normal (zero finite-difference calls)
vec3 hN = normalize(vec3(cXZ.x * 0.85, 1.0, cXZ.y * 0.85));
vec3 up = normalize(mix(vec3(0.0, 1.0, 0.0), hN, 0.58)
+ vec3(rnd2.x - 0.5, 0.0, rnd2.y - 0.5) * 0.24);
vec3 side = normalize(cross(up, vec3(0.0, 0.0, 1.0)));
vec3 fwd = cross(side, up);
vec3 lp = vec3(dot(dp, side), dot(dp, up), dot(dp, fwd));
float sc = (1.06 + 0.44 * rnd2.x) * (0.80 + 0.20 * edgeTaper);
float nArms = (rnd2.y > 0.42) ? 5.0 : 6.0;
float rot = rnd.z * TAU;
vec3 cap = evalCapitulum(lp, sc, nArms, rot, rnd.x);
if (cap.x < dMoss) {
dMoss = cap.x;
bestTip = cap.y;
bestVar = rnd2.y;
bestWet = cap.z;
}
// Tiny glistening capillary dew beads perched on ~38% of star rosettes
if (rnd.x > 0.62 && dewScale > 0.2) {
float beadAng = rot + floor(rnd.y * nArms) * (TAU / nArms);
float beadR = (0.026 + 0.028 * rnd2.x) * sc;
vec3 beadPos = vec3(cos(beadAng) * beadR, 0.017 * sc, sin(beadAng) * beadR);
float beadRad = (0.0082 + 0.0045 * rnd.z) * sc * dewScale;
float db = length(lp - beadPos) - beadRad;
dDew = min(dDew, db);
}
}
}
// Select closest hero surface: peat humus, Sphagnum moss, or dew droplet
if (dMoss < res.dist) {
res.dist = dMoss;
res.matId = 1.0;
res.tipFactor = bestTip;
res.starVar = bestVar;
res.axilWet = bestWet;
}
if (dDew < res.dist) {
res.dist = dDew;
res.matId = 2.0;
res.tipFactor = 1.0;
res.starVar = 1.0;
res.axilWet = 1.0;
}
return res;
}
float mapHero(vec3 p) {
return evalHero(p).dist;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadows & Ambient Occlusion (Strict QA Contract)
// -----------------------------------------------------------------------------
// Unit-weight tetrahedral normal on hero SDF
vec3 calcHeroNormal(vec3 p) {
const float h = 0.0014;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Clean hero-only soft shadow (no ph, bias p+N*0.014, tMin=0.025, step max(h,0.015))
float softShadow(vec3 p, vec3 N, vec3 rd, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 30; i++) {
vec3 q = ro + rd * t;
float h = (t > 0.15) ? (sdPeatBase(q) - 0.035) : mapHero(q);
if (h < 0.0008) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
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 < 4; i++) {
float h = 0.018 + 0.038 * float(i);
vec3 q = p + N * h;
float d = (i < 2) ? mapScene(q) : min(sdPeatBase(q) - 0.035, q.y);
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.35 * occ, 0.08, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Cyclorama, Softboxes & Negative-Fill Flags
// -----------------------------------------------------------------------------
vec3 sampleStudioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.11, 0.115, 0.11);
vec3 midCol = vec3(0.24, 0.25, 0.245);
vec3 topCol = vec3(0.38, 0.40, 0.42);
if (lightMode == 1) {
botCol = vec3(0.13);
midCol = vec3(0.26);
topCol = vec3(0.40);
} else if (lightMode == 2) {
botCol = vec3(0.08, 0.085, 0.09);
midCol = vec3(0.16, 0.175, 0.19);
topCol = vec3(0.24, 0.27, 0.31);
} else if (lightMode == 3) {
botCol = vec3(0.09, 0.095, 0.09);
midCol = vec3(0.19, 0.20, 0.19);
topCol = vec3(0.28, 0.30, 0.32);
}
vec3 env = mix(botCol, midCol, smoothstep(-0.25, 0.18, y));
env = mix(env, topCol, smoothstep(0.18, 0.85, y));
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.25, 0.90, 0.35));
float sb1 = smoothstep(0.76, 0.95, dot(rd, sb1Dir));
vec3 sb1Col = (lightMode == 1) ? vec3(1.45) : vec3(1.45, 1.42, 1.35);
env += sb1 * sb1Col * (lightMode == 2 ? 0.45 : 0.95);
// Side fill softbox (cool-neutral)
vec3 sb2Dir = normalize(vec3(-0.82, 0.42, 0.38));
float sb2 = smoothstep(0.78, 0.96, dot(rd, sb2Dir));
vec3 sb2Col = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.02);
env += sb2 * sb2Col * 0.75;
// Back/rim strip softbox (boosted in backlit mode)
vec3 sb3Dir = normalize(vec3(-0.20, 0.42, -0.88));
float sb3 = smoothstep(0.80, 0.97, dot(rd, sb3Dir));
vec3 sb3Col = (lightMode == 1) ? vec3(1.25) : vec3(1.35, 1.28, 1.12);
env += sb3 * sb3Col * (lightMode == 3 ? 1.85 : 0.70);
// Grazing softbox when uLightMode == 2
if (lightMode == 2) {
vec3 sbGDir = normalize(vec3(0.92, 0.24, 0.28));
float sbG = smoothstep(0.84, 0.97, dot(rd, sbGDir));
env += sbG * vec3(1.75, 1.62, 1.40);
}
// Negative-fill flags on lower-right and far-left to carve glossy dew contrast
float negFlag1 = smoothstep(0.70, 0.90, dot(rd, normalize(vec3(0.88, 0.05, -0.45))));
float negFlag2 = smoothstep(0.74, 0.92, dot(rd, normalize(vec3(-0.90, 0.02, -0.40))));
env *= (1.0 - 0.62 * negFlag1) * (1.0 - 0.55 * negFlag2);
return env;
}
// ACES Filmic Tone Mapping
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);
}
// -----------------------------------------------------------------------------
// Shading
// -----------------------------------------------------------------------------
void getLightRig(int lightMode, out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol) {
if (lightMode == 1) {
// Pure neutral grayscale studio rig
keyDir = normalize(vec3(0.55, 0.78, 0.42));
keyCol = vec3(2.05);
fillDir = normalize(vec3(-0.75, 0.45, 0.48));
fillCol = vec3(0.65);
rimDir = normalize(vec3(-0.25, 0.52, -0.82));
rimCol = vec3(0.85);
} else if (lightMode == 2) {
// Grazing raking light across the star carpet
keyDir = normalize(vec3(0.92, 0.25, 0.30));
keyCol = vec3(2.45, 2.28, 1.98);
fillDir = normalize(vec3(-0.72, 0.55, 0.42));
fillCol = vec3(0.52, 0.60, 0.72);
rimDir = normalize(vec3(-0.45, 0.40, -0.80));
rimCol = vec3(0.72, 0.76, 0.82);
} else if (lightMode == 3) {
// Backlit botanical translucency rig
keyDir = normalize(vec3(-0.18, 0.46, -0.87));
keyCol = vec3(2.65, 2.42, 2.02);
fillDir = normalize(vec3(0.42, 0.68, 0.60));
fillCol = vec3(0.68, 0.74, 0.80);
rimDir = normalize(vec3(0.65, 0.40, -0.64));
rimCol = vec3(1.15, 1.12, 0.98);
} else {
// Hero studio lighting
keyDir = normalize(vec3(0.54, 0.78, 0.44));
keyCol = vec3(2.15, 2.06, 1.90);
fillDir = normalize(vec3(-0.78, 0.46, 0.42));
fillCol = vec3(0.58, 0.67, 0.80);
rimDir = normalize(vec3(-0.28, 0.52, -0.81));
rimCol = vec3(0.95, 0.92, 0.82);
}
}
vec3 shadeBench(vec3 p, vec3 V, int lightMode) {
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Fine matte mineral-stone specimen bench texture
float grain = fbm2(p.xz * 7.5) * 0.5 + noise2(p.xz * 34.0) * 0.5;
vec3 baseCol = (lightMode == 1)
? vec3(0.36) * (0.94 + 0.12 * grain)
: vec3(0.37, 0.365, 0.35) * (0.94 + 0.12 * grain);
// Subtle damp bog-moisture halo right around the peat-humus swatch contact edge
float r = length(p.xz) / swatchRadius(p.xz);
float dampRing = smoothstep(1.14, 0.96, r) * mix(0.28, 0.12, uOpen);
baseCol *= (1.0 - dampRing);
float shKey = softShadow(p, N, keyDir, 3.5, 9.0);
float ao = calcAO(p, N);
float ndlKey = max(dot(N, keyDir), 0.0);
float ndlFill = max(dot(N, fillDir), 0.0);
float ndlRim = max(dot(N, rimDir), 0.0);
vec3 diff = baseCol / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.45 + 0.55 * ao) +
rimCol * ndlRim * 0.35 * ao
);
// Soft studio ambient + slight sheen on damp contact ring
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 col = diff + baseCol * envDiff * 0.52 * ao;
vec3 R = reflect(-V, N);
float fres = pow(1.0 - max(dot(N, V), 0.0), 4.0);
float benchSpec = mix(0.06, 0.24, dampRing);
col += sampleStudioEnv(R, lightMode) * fres * benchSpec * ao;
// Gentle radial studio backdrop vignette fade on distant bench
float distFade = smoothstep(2.4, 8.5, length(p.xz));
vec3 horizonCol = sampleStudioEnv(normalize(vec3(p.x, 0.12, p.z)), lightMode) * 0.72;
col = mix(col, horizonCol, distFade);
return col;
}
vec3 shadeHero(vec3 p, vec3 V, MossEval m, int lightMode) {
vec3 N = calcHeroNormal(p);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
float ao = calcAO(p, N);
float shKey = softShadow(p, N, keyDir, 2.5, 11.0);
// Local micro-texture for botanical leaf cellularity & peat fibers
float micro = fbm2(p.xz * 26.0 + p.y * 14.0);
float fineCell = noise2(p.xz * 78.0 + p.y * 55.0);
// Determine albedo, roughness, F0, and SSS parameters by material ID
vec3 albedo;
vec3 sssTint;
float sssStrength = 0.0;
float roughness = 0.55;
float specBoost = 1.0;
float wetness = 0.0;
if (m.matId < 0.5) {
// Fibrous dark bog peat-humus substrate (deep rich boggy humus under canopy)
vec3 peatDark = vec3(0.032, 0.024, 0.016);
vec3 peatWarm = vec3(0.092, 0.065, 0.040);
albedo = mix(peatDark, peatWarm, micro * 0.78 + fineCell * 0.22);
// Deepen under-canopy inter-stem humus pockets into dark boggy olive-peat shadow
float underCanopy = smoothstep(0.06, 0.22, p.y);
albedo = mix(albedo, vec3(0.030, 0.046, 0.018), underCanopy * 0.72);
roughness = mix(0.68, 0.42, mix(0.75, 0.35, uOpen) * (1.0 - micro * 0.4));
sssTint = vec3(0.08, 0.05, 0.03);
sssStrength = 0.04;
wetness = mix(0.45, 0.18, uOpen);
} else if (m.matId < 1.5) {
// Living 3D star-headed Sphagnum gametophyte
float openT = clamp(uOpen, 0.0, 1.0);
// Hydrated lush emerald palette (uOpen = 0)
vec3 stemEmerald = vec3(0.048, 0.125, 0.028);
vec3 midEmerald = vec3(0.135, 0.355, 0.062);
vec3 limeTip = vec3(0.395, 0.575, 0.115);
vec3 coolMintVar = vec3(0.115, 0.315, 0.105);
vec3 emeraldCol = mix(stemEmerald, midEmerald, smoothstep(0.08, 0.58, m.tipFactor));
emeraldCol = mix(emeraldCol, limeTip, smoothstep(0.55, 0.98, m.tipFactor));
emeraldCol = mix(emeraldCol, coolMintVar, m.starVar * 0.22 * (1.0 - m.tipFactor * 0.5));
// Sun-blushed bog-crimson autumn palette (uOpen = 1)
vec3 bogOlive = vec3(0.085, 0.132, 0.032);
vec3 amberOchre = vec3(0.315, 0.235, 0.055);
vec3 bogCrimson = vec3(0.510, 0.058, 0.064);
vec3 wineRoseTip = vec3(0.675, 0.108, 0.098);
// Sun exposure favors upper hummock crown + rosette star tips
float sunBlush = smoothstep(0.18, 0.86, m.tipFactor) * (0.62 + 0.38 * smoothstep(0.08, 0.42, p.y));
sunBlush = clamp(sunBlush + (m.starVar - 0.45) * 0.26, 0.0, 1.0);
vec3 autumnCol = mix(bogOlive, amberOchre, smoothstep(0.08, 0.48, m.tipFactor));
autumnCol = mix(autumnCol, bogCrimson, smoothstep(0.28, 0.72, sunBlush));
autumnCol = mix(autumnCol, wineRoseTip, smoothstep(0.66, 0.98, sunBlush));
albedo = mix(emeraldCol, autumnCol, openT);
// Fine hyaline cell speckle
albedo *= (0.88 + 0.24 * fineCell);
// Capillary dew film glistens in axils and across hydrated leaves
float hydration = mix(1.0, 0.32, openT);
wetness = clamp((m.axilWet * 0.85 + 0.28) * hydration, 0.08, 0.95);
roughness = mix(0.46, 0.14, wetness);
// Translucent leaf SSS tint
sssTint = mix(vec3(0.58, 0.82, 0.18), vec3(0.88, 0.17, 0.10), openT * smoothstep(0.20, 0.82, m.tipFactor));
sssStrength = (0.32 + 0.48 * m.tipFactor) * (0.75 + 0.25 * fineCell);
} else {
// Glistening trapped capillary dew bead
albedo = vec3(0.06, 0.09, 0.06);
roughness = 0.035;
specBoost = 1.65;
wetness = 1.0;
sssTint = mix(vec3(0.65, 0.88, 0.35), vec3(0.85, 0.42, 0.28), uOpen);
sssStrength = 0.65;
}
// Pure neutral grayscale studio mode (uLightMode == 1)
if (lightMode == 1) {
float luma = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
// Keep clear tonal separation between peat, moss stems, and star tips in neutral gray
luma = clamp(luma * 1.18 + 0.03, 0.04, 0.72);
albedo = vec3(luma);
sssTint = vec3(0.55);
}
// Wrap diffuse for velvety botanical scattering
float wrap = (m.matId > 0.5 && m.matId < 1.5) ? 0.34 : 0.10;
float ndlKey = max(0.0, (dot(N, keyDir) + wrap) / (1.0 + wrap));
float ndlFill = max(0.0, (dot(N, fillDir) + wrap) / (1.0 + wrap));
float ndlRim = max(0.0, (dot(N, rimDir) + wrap) / (1.0 + wrap));
// Deep boggy inter-stem shadow contrast
float bogAO = mix(ao * ao, ao, 0.45);
vec3 diff = albedo / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.35 + 0.65 * bogAO) +
rimCol * ndlRim * 0.45 * bogAO
);
// Subsurface scattering (thickness probe along light + backlit forward scatter)
float thickKey = clamp(mapHero(p + keyDir * 0.045) / 0.045, 0.0, 1.0);
float thickRim = clamp(mapHero(p + rimDir * 0.045) / 0.045, 0.0, 1.0);
float backKey = pow(clamp(dot(V, -keyDir), 0.0, 1.0), 3.0);
float backRim = pow(clamp(dot(V, -rimDir), 0.0, 1.0), 2.5);
float sssMult = (lightMode == 3) ? 1.55 : 0.85;
vec3 sss = sssTint * sssStrength * sssMult * (
keyCol * (0.28 * thickKey + 0.72 * backKey * (0.4 + 0.6 * thickKey)) * (0.25 + 0.75 * shKey) +
rimCol * (0.25 * thickRim + 0.65 * backRim)
) * (0.35 + 0.65 * ao);
// Specular (GGX-inspired Blinn-Phong lobe + studio reflection for capillary dew films)
vec3 Hkey = normalize(keyDir + V);
vec3 Hrim = normalize(rimDir + V);
float ndv = max(dot(N, V), 0.001);
float fres = mix(0.025, 0.045, wetness) + (1.0 - mix(0.025, 0.045, wetness)) * pow(1.0 - ndv, 5.0);
float shininess = mix(14.0, 180.0, (1.0 - roughness) * (1.0 - roughness));
float specNorm = (shininess + 2.0) / (8.0 * PI);
float sKey = pow(max(dot(N, Hkey), 0.0), shininess) * specNorm * shKey;
float sRim = pow(max(dot(N, Hrim), 0.0), shininess * 0.7) * specNorm * 0.65;
vec3 spec = (keyCol * sKey + rimCol * sRim) * fres * specBoost * (0.45 + 0.55 * wetness);
// Studio environment IBL (diffuse + glossy capillary dew reflection)
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 R = reflect(-V, N);
vec3 envSpec = sampleStudioEnv(R, lightMode);
vec3 col = diff + sss + spec;
col += albedo * envDiff * 0.46 * bogAO;
col += envSpec * fres * mix(0.18, 1.05, wetness) * (1.0 - roughness * 0.65) * (0.35 + 0.65 * ao);
// Dew droplet refractive caustics/bright core glint
if (m.matId > 1.5) {
float coreGlint = pow(ndv, 2.2) * (0.35 + 0.65 * shKey);
vec3 dewCore = (lightMode == 1) ? vec3(0.45) : mix(vec3(0.45, 0.72, 0.20), vec3(0.65, 0.25, 0.16), uOpen);
col += dewCore * coreGlint * 0.55;
}
return col;
}
// -----------------------------------------------------------------------------
// Camera & Raymarch
// -----------------------------------------------------------------------------
void getCamera(int camMode, out vec3 ro, out vec3 ta, out float fov) {
if (camMode == 1) {
// ~2.5x macro view inspecting clustered 3D star-headed Sphagnum capitula & dew
ro = vec3(0.58, 0.78, 0.88);
ta = vec3(0.03, 0.34, 0.04);
fov = 0.82;
} else {
// Hero botanical specimen view grounded on studio bench (~15% closer framing)
ro = vec3(1.32, 1.18, 1.82);
ta = vec3(0.0, 0.25, 0.0);
fov = 1.10;
}
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / uResolution.y;
vec3 ro, ta;
float fov;
getCamera(uCamMode, ro, ta, fov);
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
float tMax = 12.0;
bool hit = false;
for (int i = 0; i < 88; i++) {
vec3 p = ro + rd * t;
if (p.y > 1.7 && rd.y > 0.0) break;
float d = mapScene(p);
if (d < 0.0010) {
hit = true;
break;
}
// Conservative step inside the detailed moss canopy for crisp star tips
t += (d < 0.03) ? d * 0.82 : d * 0.95;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
MossEval m = evalHero(p);
float dHero = m.dist;
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
col = shadeBench(p, -rd, uLightMode);
} else {
col = shadeHero(p, -rd, m, uLightMode);
}
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.74;
}
// Gentle studio exposure + ACES tone mapping + sRGB gamma
col *= 1.08;
col = acesToneMap(col);
// Soft studio optical vignette
vec2 q = pfx_frag_xy / uResolution.xy;
float vig = 0.88 + 0.12 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.18);
col *= vig;
// Enforce pure neutral grayscale in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(g);
}
col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));
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.0;
}
// Sphagnum Forest Moss — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/fungi_marine/lib/04_sphagnum_moss_star_carpet.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;
// -----------------------------------------------------------------------------
// Noise & Hash 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 noise2(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.8, 0.6, -0.6, 0.8);
for (int i = 0; i < 4; i++) {
v += a * noise2(p);
p = rot * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
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);
}
// -----------------------------------------------------------------------------
// Peat-Humus Hummock Geometry
// -----------------------------------------------------------------------------
// Organic swatch footprint radius in XZ (pure analytical for fast SDF eval)
float swatchRadius(vec2 xz) {
float ang = atan(xz.y, xz.x);
float r = 1.04;
r += 0.08 * sin(ang * 2.0 + 0.55);
r += 0.05 * cos(ang * 3.0 - 1.10);
r += 0.03 * sin(ang * 5.0 + 2.20);
return r;
}
// Smooth dome profile of the underlying peat-humus cushion (analytical, zero fbm)
float hummockHeight(vec2 xz) {
float rad = swatchRadius(xz);
float u = length(xz) / rad;
float dome = max(0.0, 1.0 - u * u);
// Pillowy sub-mounds characteristic of natural Sphagnum hummocks
vec2 d1 = xz - vec2(-0.28, 0.18);
vec2 d2 = xz - vec2( 0.30, 0.14);
vec2 d3 = xz - vec2( 0.02, -0.32);
float lobes = 0.058 * exp(-dot(d1, d1) * 3.8)
+ 0.052 * exp(-dot(d2, d2) * 4.2)
+ 0.048 * exp(-dot(d3, d3) * 4.0);
float ripple = 0.018 * sin(xz.x * 5.4 + 0.6) * cos(xz.y * 4.9 - 0.4);
float h = 0.44 * pow(dome, 0.72) + (lobes + ripple) * smoothstep(1.0, 0.35, u);
return max(0.0, h);
}
// Signed distance to the fibrous peat-humus base swatch
float sdPeatBase(vec3 p) {
float rad = swatchRadius(p.xz);
float r = length(p.xz);
float hTop = hummockHeight(p.xz);
// Slight natural undercut/bevel at the base rim where the swatch rests on the bench
float rimBevel = 0.045 * (1.0 - smoothstep(0.0, 0.13, p.y));
float dSide = r - (rad - rimBevel);
float dTop = (p.y - hTop) * 0.78;
float dBot = -p.y;
float dBody = smax(dSide, dTop, 0.065);
dBody = smax(dBody, dBot, 0.014);
// Fast 1-octave fibrous peat micro-relief only near the peat surface
if (dBody < 0.06) {
float peatMask = smoothstep(0.18, 0.02, p.y) + 0.30;
float fiber = (noise2(vec2(atan(p.z, p.x) * 6.0, p.y * 26.0) + p.xz * 4.5) - 0.5) * 0.014 * peatMask;
dBody += fiber;
}
return dBody;
}
// -----------------------------------------------------------------------------
// 3D Star-Headed Sphagnum Moss Capitula Carpet
// -----------------------------------------------------------------------------
struct MossEval {
float dist; // Combined hero SDF
float matId; // 0 = peat humus, 1 = sphagnum moss, 2 = capillary dew bead
float tipFactor; // 0 = deep stem/hollow, 1 = upper star rosette tip
float starVar; // per-capitulum color/species variation (0..1)
float axilWet; // capillary water film intensity (0..1)
};
// Single 3D star-headed Sphagnum capitulum in local tangent space (y up)
// Returns vec3(sdf, tipFactor, axilWet)
vec3 evalCapitulum(vec3 lp, float sc, float nArms, float rot, float seed) {
// Gentle organic breeze/hydration micro-motion when uTime > 0
float sway = sin(uTime * 0.8 + seed * TAU) * 0.0025 * smoothstep(0.0, 0.05, lp.y + 0.03);
lp.x += sway;
float rad = length(lp.xz);
float ang = atan(lp.z, lp.x) + rot;
// --- Outer primary star whorl (5 or 6 radiating lanceolate branches) ---
float sector = TAU / nArms;
float a1 = mod(ang + 0.5 * sector, sector) - 0.5 * sector;
vec2 q1 = vec2(cos(a1), sin(a1)) * rad;
float L1 = 0.076 * sc;
float t1 = clamp(q1.x / L1, 0.0, 1.0);
// Arching upward from the bud then gently recurving outward and down at the tip
float arch1 = sc * (0.015 * sin(t1 * 2.55) - 0.024 * t1 * t1);
// Pointed lanceolate leaf-branch cross-section with subtle imbricate leaf fluting
float flute1 = 1.0 + 0.11 * sin(t1 * 18.0 + seed * 10.0);
float w1 = sc * 0.0165 * (1.0 - 0.78 * t1) * (0.55 + 0.45 * sin(t1 * 2.8 + 0.35)) * flute1;
// Slightly flattened vertically like clustered tiny moss leaves
vec3 bp1 = vec3(q1.x - t1 * L1, (lp.y - arch1) * 1.18, q1.y);
float dOuter = length(bp1) - w1;
// --- Inner raised secondary whorl (rotated half-sector, compact central rosette) ---
float a2 = mod(ang + sector, sector) - 0.5 * sector;
vec2 q2 = vec2(cos(a2), sin(a2)) * rad;
float L2 = 0.044 * sc;
float t2 = clamp(q2.x / L2, 0.0, 1.0);
float arch2 = sc * (0.019 + 0.012 * sin(t2 * 2.4) - 0.014 * t2 * t2);
float w2 = sc * 0.0135 * (1.0 - 0.72 * t2) * (0.65 + 0.35 * sin(t2 * 3.0 + 0.4));
vec3 bp2 = vec3(q2.x - t2 * L2, (lp.y - arch2) * 1.15, q2.y);
float dInner = length(bp2) - w2;
// --- Central apical bud (plump rosette heart) ---
float dBud = length(lp - vec3(0.0, 0.017 * sc, 0.0)) - 0.019 * sc;
// --- Lower stem descending into the boggy inter-stem cavity ---
float stemTop = 0.008 * sc;
float stemBot = -0.105 * sc;
float sy = clamp(lp.y, stemBot, stemTop);
float dStem = length(vec2(rad, lp.y - sy)) - 0.0135 * sc;
// Combine rosette components smoothly so each star reads as one organic gametophyte
float dStar = smin(dOuter, dInner, 0.0075 * sc);
dStar = smin(dStar, dBud, 0.0065 * sc);
float dFull = smin(dStar, dStem, 0.009 * sc);
// Tip factor: highest on the central bud, inner whorl, and outer star tips
float outerTip = smoothstep(0.15, 0.95, t1) * smoothstep(-0.025 * sc, 0.022 * sc, lp.y);
float crownBoost = smoothstep(0.055 * sc, 0.0, rad) * smoothstep(0.0, 0.028 * sc, lp.y);
float tipFactor = clamp(max(outerTip, crownBoost * 0.92), 0.0, 1.0);
// Suppress tip factor down on the stem
tipFactor *= smoothstep(-0.045 * sc, 0.005 * sc, lp.y);
// Capillary water meniscus collects in the concave axils between branches and center
float axil = smoothstep(0.055 * sc, 0.010 * sc, rad) * smoothstep(0.026 * sc, -0.006 * sc, lp.y);
float groove = smoothstep(0.016 * sc, 0.001 * sc, abs(q1.y)) * (1.0 - t1 * 0.5);
float axilWet = clamp(axil * 0.85 + groove * 0.35, 0.0, 1.0);
return vec3(dFull, tipFactor, axilWet);
}
MossEval evalHero(vec3 p) {
MossEval res;
res.matId = 0.0;
res.tipFactor = 0.0;
res.starVar = 0.5;
res.axilWet = 0.25;
// Cheap bounding sphere early-out so far rays skip all hummock/moss work
float dB = length(p - vec3(0.0, 0.25, 0.0)) - 1.32;
if (dB > 0.12) {
res.dist = dB;
return res;
}
float dPeat = sdPeatBase(p);
res.dist = dPeat;
// Cheap bounding envelope around the hummock canopy so mid-range rays march fast
if (dPeat > 0.17) {
res.dist = dPeat - 0.095;
return res;
}
// Peat base slightly recessed beneath the moss carpet on top, exposed at the lower skirt
float rad = swatchRadius(p.xz);
float uRad = length(p.xz) / rad;
float canopyMask = smoothstep(0.99, 0.84, uRad) * smoothstep(0.035, 0.115, p.y);
float dPeatRecessed = dPeat + 0.038 * canopyMask;
res.dist = dPeatRecessed;
res.matId = 0.0;
// Only evaluate star rosettes within the living canopy footprint
if (uRad > 1.08 || p.y < 0.015) {
return res;
}
// Tightly packed jittered 2D grid of interlocking 3D Sphagnum capitula (3x3 neighborhood)
const float cellSize = 0.094;
vec2 gPos = p.xz / cellSize;
vec2 gId = floor(gPos);
float dMoss = 1e5;
float bestTip = 0.0;
float bestVar = 0.5;
float bestWet = 0.0;
float dDew = 1e5;
float dewScale = mix(1.0, 0.52, uOpen);
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
vec2 id = gId + vec2(float(i), float(j));
vec3 rnd = hash32(id);
// Jittered center in XZ
vec2 cXZ = (id + 0.18 + 0.64 * rnd.xy) * cellSize;
// Fast XZ proximity reject matched to ~1.3x lush capitula radius
vec2 dxz = p.xz - cXZ;
if (dot(dxz, dxz) > 0.028) continue;
float cRad = swatchRadius(cXZ);
float cU = length(cXZ) / cRad;
if (cU > 0.965) continue;
// Anchor height on the hummock dome + plush organic height variation
float baseH = hummockHeight(cXZ);
float edgeTaper = smoothstep(0.97, 0.80, cU);
float plushOff = (rnd.z - 0.32) * 0.060 * edgeTaper + 0.034 * edgeTaper;
vec3 cPos = vec3(cXZ.x, baseH + plushOff, cXZ.y);
// Quick 3D sphere cull per capitulum
vec3 dp = p - cPos;
if (dot(dp, dp) > 0.034) continue;
vec2 rnd2 = hash22(id + vec2(19.3, 47.1));
// Analytical dome normal (zero finite-difference calls)
vec3 hN = normalize(vec3(cXZ.x * 0.85, 1.0, cXZ.y * 0.85));
vec3 up = normalize(mix(vec3(0.0, 1.0, 0.0), hN, 0.58)
+ vec3(rnd2.x - 0.5, 0.0, rnd2.y - 0.5) * 0.24);
vec3 side = normalize(cross(up, vec3(0.0, 0.0, 1.0)));
vec3 fwd = cross(side, up);
vec3 lp = vec3(dot(dp, side), dot(dp, up), dot(dp, fwd));
float sc = (1.06 + 0.44 * rnd2.x) * (0.80 + 0.20 * edgeTaper);
float nArms = (rnd2.y > 0.42) ? 5.0 : 6.0;
float rot = rnd.z * TAU;
vec3 cap = evalCapitulum(lp, sc, nArms, rot, rnd.x);
if (cap.x < dMoss) {
dMoss = cap.x;
bestTip = cap.y;
bestVar = rnd2.y;
bestWet = cap.z;
}
// Tiny glistening capillary dew beads perched on ~38% of star rosettes
if (rnd.x > 0.62 && dewScale > 0.2) {
float beadAng = rot + floor(rnd.y * nArms) * (TAU / nArms);
float beadR = (0.026 + 0.028 * rnd2.x) * sc;
vec3 beadPos = vec3(cos(beadAng) * beadR, 0.017 * sc, sin(beadAng) * beadR);
float beadRad = (0.0082 + 0.0045 * rnd.z) * sc * dewScale;
float db = length(lp - beadPos) - beadRad;
dDew = min(dDew, db);
}
}
}
// Select closest hero surface: peat humus, Sphagnum moss, or dew droplet
if (dMoss < res.dist) {
res.dist = dMoss;
res.matId = 1.0;
res.tipFactor = bestTip;
res.starVar = bestVar;
res.axilWet = bestWet;
}
if (dDew < res.dist) {
res.dist = dDew;
res.matId = 2.0;
res.tipFactor = 1.0;
res.starVar = 1.0;
res.axilWet = 1.0;
}
return res;
}
float mapHero(vec3 p) {
return evalHero(p).dist;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// -----------------------------------------------------------------------------
// Normals, Soft Shadows & Ambient Occlusion (Strict QA Contract)
// -----------------------------------------------------------------------------
// Unit-weight tetrahedral normal on hero SDF
vec3 calcHeroNormal(vec3 p) {
const float h = 0.0014;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Clean hero-only soft shadow (no ph, bias p+N*0.014, tMin=0.025, step max(h,0.015))
float softShadow(vec3 p, vec3 N, vec3 rd, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 30; i++) {
vec3 q = ro + rd * t;
float h = (t > 0.15) ? (sdPeatBase(q) - 0.035) : mapHero(q);
if (h < 0.0008) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
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 < 4; i++) {
float h = 0.018 + 0.038 * float(i);
vec3 q = p + N * h;
float d = (i < 2) ? mapScene(q) : min(sdPeatBase(q) - 0.035, q.y);
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.35 * occ, 0.08, 1.0);
}
// -----------------------------------------------------------------------------
// Studio Environment: Cyclorama, Softboxes & Negative-Fill Flags
// -----------------------------------------------------------------------------
vec3 sampleStudioEnv(vec3 rd, int lightMode) {
float y = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.11, 0.115, 0.11);
vec3 midCol = vec3(0.24, 0.25, 0.245);
vec3 topCol = vec3(0.38, 0.40, 0.42);
if (lightMode == 1) {
botCol = vec3(0.13);
midCol = vec3(0.26);
topCol = vec3(0.40);
} else if (lightMode == 2) {
botCol = vec3(0.08, 0.085, 0.09);
midCol = vec3(0.16, 0.175, 0.19);
topCol = vec3(0.24, 0.27, 0.31);
} else if (lightMode == 3) {
botCol = vec3(0.09, 0.095, 0.09);
midCol = vec3(0.19, 0.20, 0.19);
topCol = vec3(0.28, 0.30, 0.32);
}
vec3 env = mix(botCol, midCol, smoothstep(-0.25, 0.18, y));
env = mix(env, topCol, smoothstep(0.18, 0.85, y));
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.25, 0.90, 0.35));
float sb1 = smoothstep(0.76, 0.95, dot(rd, sb1Dir));
vec3 sb1Col = (lightMode == 1) ? vec3(1.45) : vec3(1.45, 1.42, 1.35);
env += sb1 * sb1Col * (lightMode == 2 ? 0.45 : 0.95);
// Side fill softbox (cool-neutral)
vec3 sb2Dir = normalize(vec3(-0.82, 0.42, 0.38));
float sb2 = smoothstep(0.78, 0.96, dot(rd, sb2Dir));
vec3 sb2Col = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.02);
env += sb2 * sb2Col * 0.75;
// Back/rim strip softbox (boosted in backlit mode)
vec3 sb3Dir = normalize(vec3(-0.20, 0.42, -0.88));
float sb3 = smoothstep(0.80, 0.97, dot(rd, sb3Dir));
vec3 sb3Col = (lightMode == 1) ? vec3(1.25) : vec3(1.35, 1.28, 1.12);
env += sb3 * sb3Col * (lightMode == 3 ? 1.85 : 0.70);
// Grazing softbox when uLightMode == 2
if (lightMode == 2) {
vec3 sbGDir = normalize(vec3(0.92, 0.24, 0.28));
float sbG = smoothstep(0.84, 0.97, dot(rd, sbGDir));
env += sbG * vec3(1.75, 1.62, 1.40);
}
// Negative-fill flags on lower-right and far-left to carve glossy dew contrast
float negFlag1 = smoothstep(0.70, 0.90, dot(rd, normalize(vec3(0.88, 0.05, -0.45))));
float negFlag2 = smoothstep(0.74, 0.92, dot(rd, normalize(vec3(-0.90, 0.02, -0.40))));
env *= (1.0 - 0.62 * negFlag1) * (1.0 - 0.55 * negFlag2);
return env;
}
// ACES Filmic Tone Mapping
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);
}
// -----------------------------------------------------------------------------
// Shading
// -----------------------------------------------------------------------------
void getLightRig(int lightMode, out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol) {
if (lightMode == 1) {
// Pure neutral grayscale studio rig
keyDir = normalize(vec3(0.55, 0.78, 0.42));
keyCol = vec3(2.05);
fillDir = normalize(vec3(-0.75, 0.45, 0.48));
fillCol = vec3(0.65);
rimDir = normalize(vec3(-0.25, 0.52, -0.82));
rimCol = vec3(0.85);
} else if (lightMode == 2) {
// Grazing raking light across the star carpet
keyDir = normalize(vec3(0.92, 0.25, 0.30));
keyCol = vec3(2.45, 2.28, 1.98);
fillDir = normalize(vec3(-0.72, 0.55, 0.42));
fillCol = vec3(0.52, 0.60, 0.72);
rimDir = normalize(vec3(-0.45, 0.40, -0.80));
rimCol = vec3(0.72, 0.76, 0.82);
} else if (lightMode == 3) {
// Backlit botanical translucency rig
keyDir = normalize(vec3(-0.18, 0.46, -0.87));
keyCol = vec3(2.65, 2.42, 2.02);
fillDir = normalize(vec3(0.42, 0.68, 0.60));
fillCol = vec3(0.68, 0.74, 0.80);
rimDir = normalize(vec3(0.65, 0.40, -0.64));
rimCol = vec3(1.15, 1.12, 0.98);
} else {
// Hero studio lighting
keyDir = normalize(vec3(0.54, 0.78, 0.44));
keyCol = vec3(2.15, 2.06, 1.90);
fillDir = normalize(vec3(-0.78, 0.46, 0.42));
fillCol = vec3(0.58, 0.67, 0.80);
rimDir = normalize(vec3(-0.28, 0.52, -0.81));
rimCol = vec3(0.95, 0.92, 0.82);
}
}
vec3 shadeBench(vec3 p, vec3 V, int lightMode) {
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Fine matte mineral-stone specimen bench texture
float grain = fbm2(p.xz * 7.5) * 0.5 + noise2(p.xz * 34.0) * 0.5;
vec3 baseCol = (lightMode == 1)
? vec3(0.36) * (0.94 + 0.12 * grain)
: vec3(0.37, 0.365, 0.35) * (0.94 + 0.12 * grain);
// Subtle damp bog-moisture halo right around the peat-humus swatch contact edge
float r = length(p.xz) / swatchRadius(p.xz);
float dampRing = smoothstep(1.14, 0.96, r) * mix(0.28, 0.12, uOpen);
baseCol *= (1.0 - dampRing);
float shKey = softShadow(p, N, keyDir, 3.5, 9.0);
float ao = calcAO(p, N);
float ndlKey = max(dot(N, keyDir), 0.0);
float ndlFill = max(dot(N, fillDir), 0.0);
float ndlRim = max(dot(N, rimDir), 0.0);
vec3 diff = baseCol / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.45 + 0.55 * ao) +
rimCol * ndlRim * 0.35 * ao
);
// Soft studio ambient + slight sheen on damp contact ring
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 col = diff + baseCol * envDiff * 0.52 * ao;
vec3 R = reflect(-V, N);
float fres = pow(1.0 - max(dot(N, V), 0.0), 4.0);
float benchSpec = mix(0.06, 0.24, dampRing);
col += sampleStudioEnv(R, lightMode) * fres * benchSpec * ao;
// Gentle radial studio backdrop vignette fade on distant bench
float distFade = smoothstep(2.4, 8.5, length(p.xz));
vec3 horizonCol = sampleStudioEnv(normalize(vec3(p.x, 0.12, p.z)), lightMode) * 0.72;
col = mix(col, horizonCol, distFade);
return col;
}
vec3 shadeHero(vec3 p, vec3 V, MossEval m, int lightMode) {
vec3 N = calcHeroNormal(p);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getLightRig(lightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
float ao = calcAO(p, N);
float shKey = softShadow(p, N, keyDir, 2.5, 11.0);
// Local micro-texture for botanical leaf cellularity & peat fibers
float micro = fbm2(p.xz * 26.0 + p.y * 14.0);
float fineCell = noise2(p.xz * 78.0 + p.y * 55.0);
// Determine albedo, roughness, F0, and SSS parameters by material ID
vec3 albedo;
vec3 sssTint;
float sssStrength = 0.0;
float roughness = 0.55;
float specBoost = 1.0;
float wetness = 0.0;
if (m.matId < 0.5) {
// Fibrous dark bog peat-humus substrate (deep rich boggy humus under canopy)
vec3 peatDark = vec3(0.032, 0.024, 0.016);
vec3 peatWarm = vec3(0.092, 0.065, 0.040);
albedo = mix(peatDark, peatWarm, micro * 0.78 + fineCell * 0.22);
// Deepen under-canopy inter-stem humus pockets into dark boggy olive-peat shadow
float underCanopy = smoothstep(0.06, 0.22, p.y);
albedo = mix(albedo, vec3(0.030, 0.046, 0.018), underCanopy * 0.72);
roughness = mix(0.68, 0.42, mix(0.75, 0.35, uOpen) * (1.0 - micro * 0.4));
sssTint = vec3(0.08, 0.05, 0.03);
sssStrength = 0.04;
wetness = mix(0.45, 0.18, uOpen);
} else if (m.matId < 1.5) {
// Living 3D star-headed Sphagnum gametophyte
float openT = clamp(uOpen, 0.0, 1.0);
// Hydrated lush emerald palette (uOpen = 0)
vec3 stemEmerald = vec3(0.048, 0.125, 0.028);
vec3 midEmerald = vec3(0.135, 0.355, 0.062);
vec3 limeTip = vec3(0.395, 0.575, 0.115);
vec3 coolMintVar = vec3(0.115, 0.315, 0.105);
vec3 emeraldCol = mix(stemEmerald, midEmerald, smoothstep(0.08, 0.58, m.tipFactor));
emeraldCol = mix(emeraldCol, limeTip, smoothstep(0.55, 0.98, m.tipFactor));
emeraldCol = mix(emeraldCol, coolMintVar, m.starVar * 0.22 * (1.0 - m.tipFactor * 0.5));
// Sun-blushed bog-crimson autumn palette (uOpen = 1)
vec3 bogOlive = vec3(0.085, 0.132, 0.032);
vec3 amberOchre = vec3(0.315, 0.235, 0.055);
vec3 bogCrimson = vec3(0.510, 0.058, 0.064);
vec3 wineRoseTip = vec3(0.675, 0.108, 0.098);
// Sun exposure favors upper hummock crown + rosette star tips
float sunBlush = smoothstep(0.18, 0.86, m.tipFactor) * (0.62 + 0.38 * smoothstep(0.08, 0.42, p.y));
sunBlush = clamp(sunBlush + (m.starVar - 0.45) * 0.26, 0.0, 1.0);
vec3 autumnCol = mix(bogOlive, amberOchre, smoothstep(0.08, 0.48, m.tipFactor));
autumnCol = mix(autumnCol, bogCrimson, smoothstep(0.28, 0.72, sunBlush));
autumnCol = mix(autumnCol, wineRoseTip, smoothstep(0.66, 0.98, sunBlush));
albedo = mix(emeraldCol, autumnCol, openT);
// Fine hyaline cell speckle
albedo *= (0.88 + 0.24 * fineCell);
// Capillary dew film glistens in axils and across hydrated leaves
float hydration = mix(1.0, 0.32, openT);
wetness = clamp((m.axilWet * 0.85 + 0.28) * hydration, 0.08, 0.95);
roughness = mix(0.46, 0.14, wetness);
// Translucent leaf SSS tint
sssTint = mix(vec3(0.58, 0.82, 0.18), vec3(0.88, 0.17, 0.10), openT * smoothstep(0.20, 0.82, m.tipFactor));
sssStrength = (0.32 + 0.48 * m.tipFactor) * (0.75 + 0.25 * fineCell);
} else {
// Glistening trapped capillary dew bead
albedo = vec3(0.06, 0.09, 0.06);
roughness = 0.035;
specBoost = 1.65;
wetness = 1.0;
sssTint = mix(vec3(0.65, 0.88, 0.35), vec3(0.85, 0.42, 0.28), uOpen);
sssStrength = 0.65;
}
// Pure neutral grayscale studio mode (uLightMode == 1)
if (lightMode == 1) {
float luma = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
// Keep clear tonal separation between peat, moss stems, and star tips in neutral gray
luma = clamp(luma * 1.18 + 0.03, 0.04, 0.72);
albedo = vec3(luma);
sssTint = vec3(0.55);
}
// Wrap diffuse for velvety botanical scattering
float wrap = (m.matId > 0.5 && m.matId < 1.5) ? 0.34 : 0.10;
float ndlKey = max(0.0, (dot(N, keyDir) + wrap) / (1.0 + wrap));
float ndlFill = max(0.0, (dot(N, fillDir) + wrap) / (1.0 + wrap));
float ndlRim = max(0.0, (dot(N, rimDir) + wrap) / (1.0 + wrap));
// Deep boggy inter-stem shadow contrast
float bogAO = mix(ao * ao, ao, 0.45);
vec3 diff = albedo / PI * (
keyCol * ndlKey * shKey +
fillCol * ndlFill * (0.35 + 0.65 * bogAO) +
rimCol * ndlRim * 0.45 * bogAO
);
// Subsurface scattering (thickness probe along light + backlit forward scatter)
float thickKey = clamp(mapHero(p + keyDir * 0.045) / 0.045, 0.0, 1.0);
float thickRim = clamp(mapHero(p + rimDir * 0.045) / 0.045, 0.0, 1.0);
float backKey = pow(clamp(dot(V, -keyDir), 0.0, 1.0), 3.0);
float backRim = pow(clamp(dot(V, -rimDir), 0.0, 1.0), 2.5);
float sssMult = (lightMode == 3) ? 1.55 : 0.85;
vec3 sss = sssTint * sssStrength * sssMult * (
keyCol * (0.28 * thickKey + 0.72 * backKey * (0.4 + 0.6 * thickKey)) * (0.25 + 0.75 * shKey) +
rimCol * (0.25 * thickRim + 0.65 * backRim)
) * (0.35 + 0.65 * ao);
// Specular (GGX-inspired Blinn-Phong lobe + studio reflection for capillary dew films)
vec3 Hkey = normalize(keyDir + V);
vec3 Hrim = normalize(rimDir + V);
float ndv = max(dot(N, V), 0.001);
float fres = mix(0.025, 0.045, wetness) + (1.0 - mix(0.025, 0.045, wetness)) * pow(1.0 - ndv, 5.0);
float shininess = mix(14.0, 180.0, (1.0 - roughness) * (1.0 - roughness));
float specNorm = (shininess + 2.0) / (8.0 * PI);
float sKey = pow(max(dot(N, Hkey), 0.0), shininess) * specNorm * shKey;
float sRim = pow(max(dot(N, Hrim), 0.0), shininess * 0.7) * specNorm * 0.65;
vec3 spec = (keyCol * sKey + rimCol * sRim) * fres * specBoost * (0.45 + 0.55 * wetness);
// Studio environment IBL (diffuse + glossy capillary dew reflection)
vec3 envDiff = sampleStudioEnv(N, lightMode);
vec3 R = reflect(-V, N);
vec3 envSpec = sampleStudioEnv(R, lightMode);
vec3 col = diff + sss + spec;
col += albedo * envDiff * 0.46 * bogAO;
col += envSpec * fres * mix(0.18, 1.05, wetness) * (1.0 - roughness * 0.65) * (0.35 + 0.65 * ao);
// Dew droplet refractive caustics/bright core glint
if (m.matId > 1.5) {
float coreGlint = pow(ndv, 2.2) * (0.35 + 0.65 * shKey);
vec3 dewCore = (lightMode == 1) ? vec3(0.45) : mix(vec3(0.45, 0.72, 0.20), vec3(0.65, 0.25, 0.16), uOpen);
col += dewCore * coreGlint * 0.55;
}
return col;
}
// -----------------------------------------------------------------------------
// Camera & Raymarch
// -----------------------------------------------------------------------------
void getCamera(int camMode, out vec3 ro, out vec3 ta, out float fov) {
if (camMode == 1) {
// ~2.5x macro view inspecting clustered 3D star-headed Sphagnum capitula & dew
ro = vec3(0.58, 0.78, 0.88);
ta = vec3(0.03, 0.34, 0.04);
fov = 0.82;
} else {
// Hero botanical specimen view grounded on studio bench (~15% closer framing)
ro = vec3(1.32, 1.18, 1.82);
ta = vec3(0.0, 0.25, 0.0);
fov = 1.10;
}
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;
vec3 ro, ta;
float fov;
getCamera(uCamMode, ro, ta, fov);
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
float tMax = 12.0;
bool hit = false;
for (int i = 0; i < 88; i++) {
vec3 p = ro + rd * t;
if (p.y > 1.7 && rd.y > 0.0) break;
float d = mapScene(p);
if (d < 0.0010) {
hit = true;
break;
}
// Conservative step inside the detailed moss canopy for crisp star tips
t += (d < 0.03) ? d * 0.82 : d * 0.95;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
MossEval m = evalHero(p);
float dHero = m.dist;
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
col = shadeBench(p, -rd, uLightMode);
} else {
col = shadeHero(p, -rd, m, uLightMode);
}
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.74;
}
// Gentle studio exposure + ACES tone mapping + sRGB gamma
col *= 1.08;
col = acesToneMap(col);
// Soft studio optical vignette
vec2 q = gl_FragCoord.xy / uResolution.xy;
float vig = 0.88 + 0.12 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.18);
col *= vig;
// Enforce pure neutral grayscale in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(g);
}
col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));
fragColor = vec4(col, 1.0);
}
`;
export function createSphagnumForestMossMaterial(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);
},
};
}
| Identifier | sphagnum-forest-moss |
|---|---|
| Category | Fabrics & Organic Shaders · Bark, Scales & Flora |
| Upstream study | fungi_marine/lib/04_sphagnum_moss_star_carpet.glsl (commit c24eae6) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime, uOpen, uLightMode, uCamMode |
| Source size | 766 lines · 28.3 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT nature sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation
David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal
AnimatedBranching cyan-green fungal rhizomorph veins pulsing across damp cavern bark.
PBR StudyPlush uncut cotton yarn loops casting soft micro-occlusion across woven backing.
PBR StudyDiamond-stitched gambeson / flight-suit padding with pillows puffing between taut thread seams.
PBR StudyCrisp warp-and-weft twill weave with continuous self-finished red-line selvedge edge.
PBR StudyDeep pile velvet fabric with strong grazing retro-reflective rim sheen and brushed nap swirls.
PBR StudyParallel tufted velveteen wales separated by crisp base-weave channels with fibrous sheen.