Photovoltaic Solar Cell Grid shader

photovoltaic-solar-cell-grid.glsl · 821 lines · GLSL ES 3.00 · MIT

Photovoltaic Solar Cell GridSci-Fi & Noise
Photovoltaic Solar Cell Grid WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 821 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Sci-Fi & Micro-Optics
photovoltaic-solar-cell-grid.glsl (821 lines)
#version 300 es
// Photovoltaic Solar Cell Grid (photovoltaic-solar-cell-grid.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/electronics/lib/07_monocrystalline_solar_cell_grid.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;

// -----------------------------------------------------------------------------
// Constants & Material IDs
// -----------------------------------------------------------------------------
const float PI = 3.14159265359;
const float TILE_TILT = 0.44; // ~25.2 deg resting angle on studio bench
const float COS_TILT = 0.90475166;
const float SIN_TILT = 0.42593947;

const vec3 FRAME_HALF = vec3(0.82, 0.045, 0.82);
const float FRAME_RAD = 0.010;
// Exact vertical center so the tilted frame's bottom-front bevel kisses y = 0.0
const float TILE_Y_CENTER =
    (FRAME_HALF.y - FRAME_RAD) * COS_TILT +
    (FRAME_HALF.z - FRAME_RAD) * SIN_TILT +
    FRAME_RAD;

const int MAT_BENCH     = 0;
const int MAT_FRAME     = 1;
const int MAT_BACKSHEET = 2;
const int MAT_SILICON   = 3;
const int MAT_BUSBAR    = 4;
const int MAT_STAND     = 5;

// -----------------------------------------------------------------------------
// Hash & Procedural Noise / Voronoi
// -----------------------------------------------------------------------------
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);
}

float valueNoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = hash12(i);
    float b = hash12(i + vec2(1.0, 0.0));
    float c = hash12(i + vec2(0.0, 1.0));
    float d = hash12(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}

float fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
    for (int i = 0; i < 4; i++) {
        v += a * valueNoise(p);
        p = m * p * 2.03 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

// Two-pass Voronoi returning:
// xy = winning cell ID, z = distance to nearest grain boundary, w = cell hash
vec4 voronoiCrystal(vec2 x) {
    vec2 n = floor(x);
    vec2 f = fract(x);

    vec2 mg, mr;
    vec2 bestCell = vec2(0.0);
    float md = 8.0;

    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 g = vec2(float(i), float(j));
            vec2 o = hash22(n + g);
            // Slight irregularity for natural polycrystalline silicon ingots
            vec2 r = g + (0.12 + 0.76 * o) - f;
            float d = dot(r, r);
            if (d < md) {
                md = d;
                mr = r;
                mg = g;
                bestCell = n + g;
            }
        }
    }

    // Distance to nearest border edge
    md = 8.0;
    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 g = mg + vec2(float(i), float(j));
            vec2 o = hash22(n + g);
            vec2 r = g + (0.12 + 0.76 * o) - f;
            if (dot(mr - r, mr - r) > 0.00001) {
                md = min(md, dot(0.5 * (mr + r), normalize(r - mr)));
            }
        }
    }

    return vec4(bestCell, md, hash12(bestCell));
}

// -----------------------------------------------------------------------------
// SDF Primitives & Coordinate Transforms
// -----------------------------------------------------------------------------
vec3 worldToTile(vec3 p) {
    vec3 w = p - vec3(0.0, TILE_Y_CENTER, 0.0);
    return vec3(
        w.x,
        w.y * COS_TILT + w.z * SIN_TILT,
       -w.y * SIN_TILT + w.z * COS_TILT
    );
}

vec3 tileDirToWorld(vec3 d) {
    return vec3(
        d.x,
        d.y * COS_TILT - d.z * SIN_TILT,
        d.y * SIN_TILT + d.z * COS_TILT
    );
}

float sdRoundBox(vec3 p, vec3 b, float r) {
    vec3 q = abs(p) - (b - r);
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}

float sdBox(vec3 p, vec3 b) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

// 2D pseudo-square octagon SDF (Czochralski monocrystalline wafer profile)
float sdPseudoSquare2D(vec2 p, float halfSize, float chamferSum) {
    vec2 a = abs(p);
    float dSquare = max(a.x, a.y) - halfSize;
    float dChamfer = (a.x + a.y - chamferSum) * 0.70710678;
    return max(dSquare, dChamfer);
}

float sdExtrude2D(float d2d, float y, float halfH, float r) {
    vec2 w = vec2(d2d + r, abs(y) - (halfH - r));
    return min(max(w.x, w.y), 0.0) + length(max(w, 0.0)) - r;
}

// -----------------------------------------------------------------------------
// Hero Scene SDF (Tile + Frame + Wafer + 3 Raised Busbars + Rear Support)
// -----------------------------------------------------------------------------
float mapHeroMat(vec3 p, out int matId) {
    vec3 q = worldToTile(p);

    // 1. Anodized Aluminum Outer Frame with recessed bezel & inner chamfer
    float dOuter = sdRoundBox(q, FRAME_HALF, FRAME_RAD);
    // Inner recessed opening (wider at top to create a crisp 45-deg bezel chamfer)
    float bevelExpand = clamp((q.y - 0.028) * 0.85, 0.0, 0.016);
    float dWell = sdBox(q - vec3(0.0, 0.055, 0.0), vec3(0.760 + bevelExpand, 0.044, 0.760 + bevelExpand));
    float dFrame = max(dOuter, -dWell);

    // Subtle machined side groove around the frame perimeter
    float sideGroove = max(abs(q.y) - 0.006, 0.816 - max(abs(q.x), abs(q.z)));
    dFrame = max(dFrame, -sideGroove * 0.45);

    float dMin = dFrame;
    matId = MAT_FRAME;

    // 2. White Tedlar Backsheet (visible in the 4 chamfered wafer corners)
    float dBack = sdRoundBox(q - vec3(0.0, 0.012, 0.0), vec3(0.768, 0.005, 0.768), 0.002);
    if (dBack < dMin) {
        dMin = dBack;
        matId = MAT_BACKSHEET;
    }

    // 3. Beveled Pseudo-Square Monocrystalline Silicon Wafer
    // Half-size 0.742, corner chamfer sum 1.245 -> crisp 45-deg Czochralski corners
    float dWafer2D = sdPseudoSquare2D(q.xz, 0.742, 1.245);
    float dWafer = sdExtrude2D(dWafer2D, q.y - 0.021, 0.007, 0.003);
    if (dWafer < dMin) {
        dMin = dWafer;
        matId = MAT_SILICON;
    }

    // 4. Three Raised Soldered Tinned-Copper Busbar Ribbons (x = -0.45, 0.0, +0.45)
    // Fold X to evaluate all 3 busbars cleanly
    float bx = min(abs(q.x), abs(abs(q.x) - 0.45));
    // Slight solder ribbon profile with crisp rounded bevels sitting atop wafer (y = 0.028..0.0345)
    float dBus = sdRoundBox(
        vec3(bx, q.y - 0.0308, q.z),
        vec3(0.0235, 0.0038, 0.728),
        0.0022
    );
    if (dBus < dMin) {
        dMin = dBus;
        matId = MAT_BUSBAR;
    }

    // 5. Minimalist Anodized Rear Support Foot (holds tile cleanly at resting angle on y = 0)
    // Positioned behind the tile center, flat bottom kissing y = 0.0
    vec3 sp = p - vec3(0.0, 0.145, -0.34);
    float dFoot = sdRoundBox(sp, vec3(0.46, 0.145, 0.08), 0.012);
    // Trim foot cleanly so it never pokes through the front plane of the tile
    dFoot = max(dFoot, q.y + 0.025);
    if (dFoot < dMin) {
        dMin = dFoot;
        matId = MAT_STAND;
    }

    return dMin;
}

float mapHero(vec3 p) {
    int dummy;
    return mapHeroMat(p, dummy);
}

float mapScene(vec3 p) {
    return min(p.y, mapHero(p));
}

// -----------------------------------------------------------------------------
// Normals, Soft Shadows, AO (Strict QA Contract)
// -----------------------------------------------------------------------------
vec3 calcNormal(vec3 p) {
    const vec2 k = vec2(1.0, -1.0);
    const float e = 0.0005;
    return normalize(
        k.xyy * mapHero(p + k.xyy * e) +
        k.yyx * mapHero(p + k.yyx * e) +
        k.yxy * mapHero(p + k.yxy * e) +
        k.xxx * mapHero(p + k.xxx * e)
    );
}

float softShadow(vec3 ro, vec3 rd, float tMax, float k) {
    float res = 1.0;
    float t = 0.025;
    for (int i = 0; i < 64; i++) {
        float h = mapHero(ro + rd * t);
        if (h < 0.0005) 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 pos, vec3 nor) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 0; i < 5; i++) {
        float h = 0.015 + 0.045 * float(i);
        float d = mapScene(pos + h * nor);
        occ += (h - d) * sca;
        sca *= 0.72;
    }
    return clamp(1.0 - 2.6 * occ, 0.12, 1.0);
}

// -----------------------------------------------------------------------------
// Studio Lighting & Environment (Softboxes + Negative-Fill Flags)
// -----------------------------------------------------------------------------
struct LightRig {
    vec3 keyDir;
    vec3 keyCol;
    vec3 fillDir;
    vec3 fillCol;
    vec3 rimDir;
    vec3 rimCol;
    float envScale;
    float neutral;
};

LightRig getLightRig(int mode) {
    LightRig r;
    r.neutral = (mode == 1) ? 1.0 : 0.0;

    if (mode == 1) {
        // Pure neutral grayscale studio lighting
        r.keyDir   = normalize(vec3(-0.52, 0.78, 0.35));
        r.keyCol   = vec3(2.55);
        r.fillDir  = normalize(vec3(0.75, 0.42, 0.51));
        r.fillCol  = vec3(0.82);
        r.rimDir   = normalize(vec3(0.18, 0.62, -0.76));
        r.rimCol   = vec3(1.15);
        r.envScale = 1.05;
    } else if (mode == 2) {
        // Grazing raking light across micro-pyramids, collector fingers & busbars
        r.keyDir   = normalize(vec3(-0.92, 0.24, 0.31));
        r.keyCol   = vec3(3.15, 2.98, 2.78);
        r.fillDir  = normalize(vec3(0.68, 0.52, 0.52));
        r.fillCol  = vec3(0.50, 0.56, 0.65);
        r.rimDir   = normalize(vec3(0.35, 0.42, -0.84));
        r.rimCol   = vec3(1.25, 1.32, 1.45);
        r.envScale = 0.78;
    } else if (mode == 3) {
        // Backlit rim-dominant studio setup
        r.keyDir   = normalize(vec3(-0.18, 0.44, -0.88));
        r.keyCol   = vec3(3.10, 3.22, 3.40);
        r.fillDir  = normalize(vec3(-0.45, 0.72, 0.53));
        r.fillCol  = vec3(0.85, 0.88, 0.94);
        r.rimDir   = normalize(vec3(0.72, 0.38, -0.58));
        r.rimCol   = vec3(1.65, 1.60, 1.55);
        r.envScale = 0.92;
    } else {
        // Mode 0: Hero museum studio lighting
        r.keyDir   = normalize(vec3(-0.48, 0.79, 0.38));
        r.keyCol   = vec3(2.65, 2.58, 2.48);
        r.fillDir  = normalize(vec3(0.76, 0.44, 0.48));
        r.fillCol  = vec3(0.78, 0.85, 0.96);
        r.rimDir   = normalize(vec3(0.16, 0.58, -0.80));
        r.rimCol   = vec3(1.25, 1.32, 1.45);
        r.envScale = 1.05;
    }
    return r;
}

float softboxMask(vec3 d, vec3 center, vec3 axisU, vec3 axisV, vec2 halfExt, float edgeSoft) {
    float proj = dot(d, center);
    if (proj <= 0.05) return 0.0;
    vec3 p = d / proj - center;
    vec2 uv = vec2(dot(p, axisU), dot(p, axisV));
    vec2 dist = abs(uv) - halfExt;
    float sd = max(dist.x, dist.y);
    return 1.0 - smoothstep(-edgeSoft, edgeSoft, sd);
}

vec3 sampleStudioEnv(vec3 dir, float rough, int lightMode) {
    vec3 d = normalize(dir);
    float blur = mix(0.06, 0.65, rough * rough);
    bool neutral = (lightMode == 1);

    // Base studio cyclorama gradient
    vec3 floorCol = neutral ? vec3(0.095) : vec3(0.088, 0.092, 0.100);
    vec3 wallCol  = neutral ? vec3(0.185) : vec3(0.175, 0.185, 0.205);
    vec3 ceilCol  = neutral ? vec3(0.240) : vec3(0.220, 0.235, 0.260);

    vec3 col = mix(floorCol, wallCol, smoothstep(-0.20, 0.18, d.y));
    col = mix(col, ceilCol, smoothstep(0.18, 0.85, d.y));

    // Overhead main studio softbox
    vec3 sb1Center = normalize(vec3(-0.30, 0.86, 0.40));
    if (lightMode == 2) sb1Center = normalize(vec3(-0.82, 0.34, 0.45));
    if (lightMode == 3) sb1Center = normalize(vec3(-0.12, 0.55, -0.82));
    vec3 sb1U = normalize(cross(vec3(0.0, 0.0, 1.0), sb1Center));
    if (length(cross(vec3(0.0, 0.0, 1.0), sb1Center)) < 0.01) sb1U = vec3(1.0, 0.0, 0.0);
    vec3 sb1V = normalize(cross(sb1Center, sb1U));
    float sb1 = softboxMask(d, sb1Center, sb1U, sb1V, vec2(0.55, 0.38) + blur * 0.4, 0.08 + blur);
    vec3 sb1Tint = neutral ? vec3(2.35) : vec3(2.45, 2.38, 2.28);
    col += sb1 * sb1Tint * (1.0 - 0.55 * rough);

    // Right-side vertical strip softbox (gives crisp linear sheen on busbars & crystal grains)
    vec3 sb2Center = normalize(vec3(0.84, 0.38, 0.38));
    vec3 sb2U = normalize(cross(vec3(0.0, 1.0, 0.0), sb2Center));
    vec3 sb2V = normalize(cross(sb2Center, sb2U));
    float sb2 = softboxMask(d, sb2Center, sb2U, sb2V, vec2(0.18, 0.62) + blur * 0.3, 0.07 + blur);
    vec3 sb2Tint = neutral ? vec3(1.55) : vec3(1.42, 1.56, 1.78);
    col += sb2 * sb2Tint * (1.0 - 0.50 * rough);

    // Rear rim strip softbox
    vec3 sb3Center = normalize(vec3(0.08, 0.48, -0.87));
    vec3 sb3U = normalize(cross(vec3(0.0, 1.0, 0.0), sb3Center));
    vec3 sb3V = normalize(cross(sb3Center, sb3U));
    float sb3 = softboxMask(d, sb3Center, sb3U, sb3V, vec2(0.68, 0.20) + blur * 0.3, 0.08 + blur);
    vec3 sb3Tint = neutral ? vec3(1.65) : vec3(1.60, 1.68, 1.82);
    float rimBoost = (lightMode == 3) ? 1.85 : 1.0;
    col += sb3 * sb3Tint * rimBoost * (1.0 - 0.45 * rough);

    // Negative-fill flags (dark matte studio baffles for crisp metallic reflections)
    vec3 nf1Center = normalize(vec3(-0.85, 0.25, -0.45));
    vec3 nf1U = normalize(cross(vec3(0.0, 1.0, 0.0), nf1Center));
    vec3 nf1V = normalize(cross(nf1Center, nf1U));
    float nf1 = softboxMask(d, nf1Center, nf1U, nf1V, vec2(0.45, 0.55), 0.12 + blur);

    vec3 nf2Center = normalize(vec3(0.52, 0.72, -0.45));
    vec3 nf2U = normalize(cross(vec3(0.0, 0.0, 1.0), nf2Center));
    vec3 nf2V = normalize(cross(nf2Center, nf2U));
    float nf2 = softboxMask(d, nf2Center, nf2U, nf2V, vec2(0.32, 0.35), 0.12 + blur);

    float negFill = clamp(max(nf1, nf2 * 0.85), 0.0, 1.0) * (1.0 - 0.55 * rough);
    col = mix(col, neutral ? vec3(0.032) : vec3(0.028, 0.031, 0.038), negFill * 0.82);

    return max(col, vec3(0.025));
}

// -----------------------------------------------------------------------------
// PBR Helpers (GGX + Schlick)
// -----------------------------------------------------------------------------
float D_GGX(float NdotH, float rough) {
    float a = max(rough * rough, 0.002);
    float a2 = a * a;
    float d = (NdotH * NdotH) * (a2 - 1.0) + 1.0;
    return a2 / (PI * d * d + 1e-6);
}

float G_Smith(float NdotV, float NdotL, float rough) {
    float k = (rough + 1.0);
    k = (k * k) * 0.125;
    float gv = NdotV / (NdotV * (1.0 - k) + k);
    float gl = NdotL / (NdotL * (1.0 - k) + k);
    return gv * gl;
}

vec3 F_Schlick(float cosTheta, vec3 F0) {
    return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}

vec3 evalDirectLight(
    vec3 N, vec3 V, vec3 L, vec3 lightCol,
    vec3 albedo, float metallic, float rough, vec3 F0
) {
    float NdotL = max(dot(N, L), 0.0);
    if (NdotL <= 0.0) return vec3(0.0);

    vec3 H = normalize(V + L);
    float NdotV = max(dot(N, V), 0.001);
    float NdotH = max(dot(N, H), 0.0);
    float HdotV = max(dot(H, V), 0.0);

    float D = D_GGX(NdotH, rough);
    float G = G_Smith(NdotV, NdotL, rough);
    vec3  F = F_Schlick(HdotV, F0);

    vec3 spec = (D * G * F) / (4.0 * NdotV * NdotL + 0.001);
    vec3 kD = (1.0 - F) * (1.0 - metallic);
    return (kD * albedo / PI + spec) * lightCol * NdotL;
}

// -----------------------------------------------------------------------------
// Silicon Wafer Material: Micro-Pyramids, SiNx AR Film, Silver Fingers, Poly Grains
// -----------------------------------------------------------------------------
void shadeSiliconWafer(
    vec3 q, vec3 Nworld, vec3 V, float polyBlend,
    out vec3 albedo, out float metallic, out float rough, out vec3 F0, out vec3 Nout
) {
    // Local tangent basis on the wafer top face
    vec3 T = vec3(1.0, 0.0, 0.0);
    vec3 B = tileDirToWorld(vec3(0.0, 0.0, 1.0));
    vec2 uv = q.xz;

    // 1. Polycrystalline Voronoi Crystal Domains (driven by uOpen = polyBlend)
    vec4 vor = voronoiCrystal(uv * 4.35 + vec2(1.7, -0.9));
    vec2 cellId = vor.xy;
    float edgeDist = vor.z;
    float cellHash = vor.w;
    vec2 cellRand = hash22(cellId + vec2(11.3, 27.9));

    // Secondary twin-crystal sub-bands inside select grains (authentic multicrystalline silicon)
    float twinAngle = cellRand.x * PI;
    vec2 twinDir = vec2(cos(twinAngle), sin(twinAngle));
    float twinCoord = dot(uv, twinDir) * (9.0 + 8.0 * cellRand.y);
    float twinBand = smoothstep(0.42, 0.58, sin(twinCoord + cellHash * 6.2831) * 0.5 + 0.5);
    float hasTwins = step(0.52, hash12(cellId + vec2(43.1, 7.3)));
    float twinState = twinBand * hasTwins;

    // Per-grain crystal lattice orientation tilt (creates sharp faceted blue specular domains!)
    vec2 grainTilt2D = (cellRand * 2.0 - 1.0) * 0.24;
    grainTilt2D += (twinState - 0.5) * 0.11 * vec2(-twinDir.y, twinDir.x);

    // Grain boundary line (antialiased with fwidth)
    float fwEdge = max(fwidth(edgeDist), 0.002);
    float grainBoundary = 1.0 - smoothstep(0.006, 0.006 + fwEdge * 1.6, edgeDist);

    // 2. Micro-Pyramid Alkaline-Etched Silicon Texture (MIP-filtered via fwidth)
    float pyrAngle = mix(0.0, cellRand.y * 1.5707, polyBlend);
    float ca = cos(pyrAngle), sa = sin(pyrAngle);
    vec2 puv = mat2(ca, -sa, sa, ca) * uv;
    float pyrFreq = 210.0;
    vec2 pf = fract(puv * pyrFreq) - 0.5;
    float fwPyr = length(fwidth(puv * pyrFreq));
    float pyrFade = 1.0 - smoothstep(0.18, 0.85, fwPyr);
    // Upright square pyramid facet slopes
    vec2 pyrGrad = (abs(pf.x) > abs(pf.y))
        ? vec2(sign(pf.x), 0.0)
        : vec2(0.0, sign(pf.y));
    vec2 pyrTilt = mat2(ca, sa, -sa, ca) * (pyrGrad * 0.14 * pyrFade);

    // Subtle monocrystalline Czochralski concentric growth ring micro-variation
    float rWafer = length(uv);
    float czRing = sin(rWafer * 38.0) * 0.5 + 0.5;

    // Combine silicon surface normal perturbations
    vec2 siTilt = pyrTilt + grainTilt2D * polyBlend;
    vec3 Nsi = normalize(Nworld + T * siTilt.x + B * siTilt.y);

    // 3. Anti-Reflective SiNx Thin-Film Sapphire-Indigo Color & Interference
    float NdotV_si = max(dot(Nsi, V), 0.001);
    float grazing = pow(1.0 - NdotV_si, 2.4);

    // Monocrystalline deep sapphire-indigo base
    vec3 monoDeep   = vec3(0.006, 0.020, 0.095);
    vec3 monoMid    = vec3(0.011, 0.038, 0.155);
    vec3 monoGraze  = vec3(0.022, 0.068, 0.225);
    vec3 siAlbedo   = mix(monoDeep, monoMid, 0.35 + 0.14 * czRing);
    siAlbedo        = mix(siAlbedo, monoGraze, grazing * 0.50);

    // Polycrystalline grain-dependent deep sapphire / navy / cobalt-indigo palette
    vec3 polyColA = vec3(0.005, 0.016, 0.075); // deep midnight navy
    vec3 polyColB = vec3(0.012, 0.048, 0.195); // rich royal sapphire
    vec3 polyColC = vec3(0.024, 0.088, 0.260); // bright cerulean-cobalt
    vec3 polyColD = vec3(0.018, 0.028, 0.145); // deep indigo-violet
    vec3 grainCol = mix(
        mix(polyColA, polyColB, smoothstep(0.0, 0.55, cellHash)),
        mix(polyColC, polyColD, smoothstep(0.45, 1.0, cellHash)),
        cellRand.x
    );
    grainCol = mix(grainCol, grainCol * 1.42 + vec3(0.005, 0.015, 0.042), twinState * 0.55);
    // Subtle dark etched grain boundary seam
    grainCol *= (1.0 - 0.42 * grainBoundary);

    siAlbedo = mix(siAlbedo, grainCol, polyBlend);

    // SiNx thin-film tinted specular F0 (suppresses green/yellow, reflects deep sapphire-blue)
    vec3 siF0 = mix(
        vec3(0.030, 0.058, 0.160),
        vec3(0.048, 0.112, 0.260),
        polyBlend * (0.35 + 0.65 * cellRand.y)
    );
    // Micro-pyramid velvet roughness increases slightly when pyramids are sub-pixel
    float siRough = mix(0.18, 0.27, 1.0 - pyrFade);
    siRough = mix(siRough, mix(0.14, 0.29, cellRand.x), polyBlend * 0.65);

    // 4. Fine Parallel Silver Screen-Printed Collector Fingers (fwidth-antialiased!)
    // Fingers run horizontally along X (constant Z spacing), clipped inside wafer margin
    float dWaferInner = sdPseudoSquare2D(uv, 0.722, 1.215);
    float insideGrid = 1.0 - smoothstep(-0.004, 0.004, dWaferInner);

    float fingerPitch = 0.0245; // ~59 crisp fingers across the wafer
    float fCoord = uv.y / fingerPitch;
    float fFrac = abs(fract(fCoord + 0.5) - 0.5);
    float fwF = max(fwidth(fCoord), 0.002);
    float fingerHalfW = 0.068; // ~1.67 mm visual silver line proportion
    // Antialiased coverage (smoothly widens & softens at distance to prevent moire)
    float aaWidth = max(fwF * 0.85, 0.015);
    float fingerMask = (1.0 - smoothstep(fingerHalfW - aaWidth, fingerHalfW + aaWidth, fFrac)) * insideGrid;
    // Energy-conserving thin-line attenuation when pitch approaches pixel size
    float subpixelFade = clamp(0.85 / max(fwF * 2.2, 0.85), 0.22, 1.0);
    fingerMask *= subpixelFade;

    // Fine perimeter collector edge trace around the pseudo-square wafer border
    float edgeTraceDist = abs(dWaferInner + 0.005);
    float fwTrace = max(fwidth(dWaferInner), 0.0006);
    float edgeTrace = 1.0 - smoothstep(0.0022, 0.0022 + fwTrace * 1.4, edgeTraceDist);
    float silverMask = clamp(max(fingerMask, edgeTrace * 0.92), 0.0, 1.0);

    // Micro-bevel normal bump for the raised silver paste fingers
    float fSigned = fract(fCoord + 0.5) - 0.5;
    float fingerSlope = clamp(fSigned / max(fingerHalfW, 0.02), -1.0, 1.0);
    float slopeMask = smoothstep(fingerHalfW + aaWidth * 1.5, fingerHalfW * 0.35, abs(fSigned)) * insideGrid * subpixelFade;
    vec3 Nsilver = normalize(Nworld + B * (fingerSlope * 0.30 * slopeMask));

    // Screen-printed silver paste material (bright metallic silver with fine sintered texture)
    float agGrain = valueNoise(uv * 160.0);
    vec3 agAlbedo = vec3(0.86, 0.87, 0.89) * (0.94 + 0.10 * agGrain);
    float agRough = 0.24 + 0.07 * agGrain;
    vec3 agF0 = agAlbedo;

    // Blend silicon wafer with silver collector fingers
    albedo   = mix(siAlbedo, agAlbedo, silverMask);
    metallic = mix(0.08, 0.96, silverMask);
    rough    = mix(siRough, agRough, silverMask);
    F0       = mix(siF0, agF0, silverMask);
    Nout     = normalize(mix(Nsi, Nsilver, silverMask));
}

// -----------------------------------------------------------------------------
// Studio Bench Shading
// -----------------------------------------------------------------------------
vec3 shadeBench(vec3 p, vec3 V, LightRig rig, int lightMode) {
    vec3 N = vec3(0.0, 1.0, 0.0);
    bool neutral = (lightMode == 1);

    // Subtle matte-satin architectural studio tabletop texture
    float micro = fbm2(p.xz * 7.5);
    float fine  = valueNoise(p.xz * 42.0);
    float radial = smoothstep(0.2, 4.8, length(p.xz));

    vec3 baseCol = neutral
        ? mix(vec3(0.225), vec3(0.145), radial)
        : mix(vec3(0.215, 0.220, 0.232), vec3(0.130, 0.136, 0.150), radial);
    baseCol *= (0.94 + 0.08 * micro + 0.03 * fine);

    float rough = clamp(0.46 + 0.10 * micro, 0.32, 0.72);
    vec3 F0 = vec3(0.04);

    // Soft shadows from hero solar cell tile onto the bench
    float shKey  = softShadow(p + N * 0.014, rig.keyDir, 5.5, 11.0);
    float shFill = softShadow(p + N * 0.014, rig.fillDir, 5.5, 6.0);
    float shRim  = softShadow(p + N * 0.014, rig.rimDir, 5.5, 9.0);
    float ao     = calcAO(p, N);

    vec3 col = vec3(0.0);
    col += evalDirectLight(N, V, rig.keyDir,  rig.keyCol,  baseCol, 0.0, rough, F0) * shKey;
    col += evalDirectLight(N, V, rig.fillDir, rig.fillCol, baseCol, 0.0, rough, F0) * mix(0.35, 1.0, shFill);
    col += evalDirectLight(N, V, rig.rimDir,  rig.rimCol,  baseCol, 0.0, rough, F0) * mix(0.25, 1.0, shRim);

    // Studio IBL ambient + subtle reflection on bench
    vec3 R = reflect(-V, N);
    vec3 envDiff = sampleStudioEnv(N, 0.92, lightMode) * rig.envScale;
    vec3 envSpec = sampleStudioEnv(R, rough, lightMode) * rig.envScale;
    vec3 F = F_Schlick(max(dot(N, V), 0.0), F0);

    col += baseCol * envDiff * 0.65 * ao;
    col += envSpec * F * (0.45 * ao * mix(0.30, 1.0, shKey));

    // Gentle distance falloff into studio cyclorama backdrop
    float fog = smoothstep(3.8, 12.5, length(p.xz));
    vec3 bgCol = sampleStudioEnv(normalize(vec3(-V.x, max(-V.y, 0.06), -V.z)), 0.75, lightMode) * rig.envScale;
    return mix(col, bgCol, fog);
}

// -----------------------------------------------------------------------------
// Hero Material Shading
// -----------------------------------------------------------------------------
vec3 shadeHero(vec3 p, vec3 N, vec3 V, int matId, LightRig rig, int lightMode) {
    vec3 q = worldToTile(p);
    vec3 T = vec3(1.0, 0.0, 0.0);
    vec3 B = tileDirToWorld(vec3(0.0, 0.0, 1.0));

    vec3 albedo = vec3(0.5);
    float metallic = 0.0;
    float rough = 0.35;
    vec3 F0 = vec3(0.04);
    vec3 Nshade = N;

    float polyBlend = clamp(uOpen, 0.0, 1.0);

    if (matId == MAT_SILICON) {
        // Only apply wafer face pattern on the top face (not the thin beveled underside)
        float topMask = smoothstep(0.45, 0.85, dot(N, tileDirToWorld(vec3(0.0, 1.0, 0.0))));
        vec3 wAlb, wF0, wN;
        float wMet, wRough;
        shadeSiliconWafer(q, N, V, polyBlend, wAlb, wMet, wRough, wF0, wN);

        albedo   = mix(vec3(0.025, 0.045, 0.115), wAlb, topMask);
        metallic = mix(0.12, wMet, topMask);
        rough    = mix(0.28, wRough, topMask);
        F0       = mix(vec3(0.08, 0.12, 0.22), wF0, topMask);
        Nshade   = normalize(mix(N, wN, topMask));
    } else if (matId == MAT_BUSBAR) {
        // Soldered tinned-copper busbar ribbon with subtle solder-pad undulations & micro-grain
        float padRipple = sin(q.z / 0.0245 * 2.0 * PI) * 0.5 + 0.5;
        float fluxNoise = fbm2(vec2(q.x * 90.0, q.z * 28.0));
        float brushed   = valueNoise(vec2(q.x * 240.0, q.z * 14.0));

        // Slight molten solder meniscus bump along the ribbon length
        float zGrad = cos(q.z / 0.0245 * 2.0 * PI) * 0.035;
        float xGrad = (fluxNoise - 0.5) * 0.045;
        Nshade = normalize(N + T * xGrad + B * zGrad);

        // Warm-silver tinned-copper solder sheen
        albedo   = vec3(0.84, 0.83, 0.81) * (0.93 + 0.09 * brushed);
        metallic = 0.98;
        rough    = clamp(0.13 + 0.08 * fluxNoise + 0.03 * padRipple, 0.09, 0.28);
        F0       = albedo;
    } else if (matId == MAT_FRAME) {
        // Bead-blasted brushed anodized aluminum frame
        float brushAxis = (abs(q.x) > abs(q.z)) ? q.z : q.x;
        float brushPerp = (abs(q.x) > abs(q.z)) ? q.x : q.z;
        float brush = valueNoise(vec2(brushAxis * 18.0, brushPerp * 190.0));
        float bead  = fbm2(q.xz * 55.0 + q.y * 55.0);

        // Subtle miter corner seam lines at 45 degrees on the frame bezel
        float miterDist = abs(abs(q.x) - abs(q.z));
        float onBezel = step(0.755, max(abs(q.x), abs(q.z)));
        float fwMiter = max(fwidth(miterDist), 0.001);
        float miterSeam = (1.0 - smoothstep(0.002, 0.002 + fwMiter * 1.4, miterDist)) * onBezel;

        albedo   = vec3(0.56, 0.58, 0.61) * (0.92 + 0.10 * brush) * (1.0 - 0.45 * miterSeam);
        metallic = 0.92;
        rough    = clamp(0.24 + 0.09 * bead - 0.04 * brush, 0.16, 0.42);
        F0       = albedo;
    } else if (matId == MAT_BACKSHEET) {
        // Crisp matte-satin white Tedlar backsheet visible in the 4 chamfered corners
        float weave = fbm2(q.xz * 48.0);
        albedo   = vec3(0.82, 0.83, 0.84) * (0.96 + 0.06 * weave);
        metallic = 0.0;
        rough    = 0.48 + 0.08 * weave;
        F0       = vec3(0.04);
    } else {
        // MAT_STAND: dark bead-blasted anodized aluminum rear support foot
        float bead = fbm2(p.xz * 40.0 + p.y * 40.0);
        albedo   = vec3(0.18, 0.19, 0.21) * (0.92 + 0.10 * bead);
        metallic = 0.85;
        rough    = 0.36 + 0.08 * bead;
        F0       = albedo;
    }

    // Soft shadows & ambient occlusion
    float shKey  = softShadow(p + N * 0.014, rig.keyDir, 4.5, 14.0);
    float shFill = softShadow(p + N * 0.014, rig.fillDir, 4.5, 8.0);
    float shRim  = softShadow(p + N * 0.014, rig.rimDir, 4.5, 10.0);
    float ao     = calcAO(p, N);

    // Direct studio lights
    vec3 col = vec3(0.0);
    col += evalDirectLight(Nshade, V, rig.keyDir,  rig.keyCol,  albedo, metallic, rough, F0) * shKey;
    col += evalDirectLight(Nshade, V, rig.fillDir, rig.fillCol, albedo, metallic, rough, F0) * mix(0.35, 1.0, shFill);
    col += evalDirectLight(Nshade, V, rig.rimDir,  rig.rimCol,  albedo, metallic, rough, F0) * mix(0.25, 1.0, shRim);

    // Studio environment IBL (softboxes + negative-fill flags)
    vec3 R = reflect(-V, Nshade);
    float NdotV = max(dot(Nshade, V), 0.001);
    vec3 F_env = F_Schlick(NdotV, F0);
    vec3 kD_env = (1.0 - F_env) * (1.0 - metallic);

    vec3 envDiff = sampleStudioEnv(Nshade, 0.92, lightMode) * rig.envScale;
    vec3 envSpec = sampleStudioEnv(R, rough, lightMode) * rig.envScale;

    col += kD_env * albedo * envDiff * 0.52 * ao;
    col += envSpec * F_env * mix(0.55, 0.96, metallic) * ao;

    // Subtle bench bounce light onto underside / edges
    float downMask = clamp(-Nshade.y * 0.5 + 0.5, 0.0, 1.0);
    vec3 bounceCol = (lightMode == 1) ? vec3(0.11) : vec3(0.10, 0.11, 0.13);
    col += albedo * (1.0 - metallic * 0.5) * bounceCol * downMask * 0.25 * ao;

    return col;
}

// -----------------------------------------------------------------------------
// 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);
}

// -----------------------------------------------------------------------------
// Raymarching & Camera Setup
// -----------------------------------------------------------------------------
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;

    // Camera configuration: 0 = hero full-tile 3/4 view, 1 = ~2.5x macro surface/busbar/corner detail
    vec3 ro, ta;
    float focal;
    if (uCamMode == 1) {
        // ~2.5x macro focused on right busbar, silver fingers, micro-pyramids, poly grains & chamfered corner
        ro    = vec3(0.76, 1.35, 0.96);
        ta    = vec3(0.36, 0.48, -0.20);
        focal = 1.90;
    } else {
        // Hero 3/4 view framing entire beveled solar cell tile resting on studio bench with soft shadow
        ro    = vec3(0.85, 2.20, 3.45);
        ta    = vec3(0.00, 0.30, 0.06);
        focal = 1.80;
    }

    vec3 cw = normalize(ta - ro);
    vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
    vec3 cv = cross(cu, cw);
    vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);

    LightRig rig = getLightRig(uLightMode);

    // Raymarch scene
    float t = 0.0;
    float tMax = 18.0;
    bool hit = false;
    for (int i = 0; i < 128; i++) {
        vec3 p = ro + rd * t;
        float d = mapScene(p);
        if (d < 0.0005) {
            hit = true;
            break;
        }
        t += d * 0.92;
        if (t > tMax) break;
    }

    vec3 col;
    if (hit) {
        vec3 p = ro + rd * t;
        int matId;
        float dHero = mapHeroMat(p, matId);
        bool isBench = (p.y <= dHero + 0.001);

        if (isBench) {
            col = shadeBench(p, -rd, rig, uLightMode);
        } else {
            vec3 N = calcNormal(p);
            col = shadeHero(p, N, -rd, matId, rig, uLightMode);
        }
    } else {
        col = sampleStudioEnv(rd, 0.65, uLightMode) * rig.envScale;
    }

    // Mode 1: pure neutral grayscale output (zero chroma)
    if (uLightMode == 1) {
        col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
    }

    // Calibrated studio exposure + ACES tone mapping + sRGB gamma (hero lum cleanly in 10..244)
    col *= 0.92;
    col = acesToneMap(col);
    // Subtle studio vignette
    float vig = 1.0 - 0.14 * dot(uv, uv);
    col *= clamp(vig, 0.80, 1.0);
    // Lift floor safely above 0 black & cap ceiling cleanly below 245 (0.88^(1/2.2)*255 = 240.6)
    col = mix(vec3(0.005), vec3(0.880), col);
    col = pow(max(col, 0.0), vec3(1.0 / 2.2));

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierphotovoltaic-solar-cell-grid
CategorySci-Fi, Plasma & Noise Shaders · Sci-Fi & Micro-Optics
Upstream studyelectronics/lib/07_monocrystalline_solar_cell_grid.glsl (commit 14ad58d)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size821 lines · 31.4 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT scifi sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Voronoi/Worley cellular fields (Voronoi 1908; Worley 1996); GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Schlick Fresnel reflectance (1994)

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, tetrahedral SDF normal

All sci-fi & noise shaders