The showcase prompt, round 3, Gemini 4 Carbon
agent (cont.)
+42:54read_file{"EndLine":2250,"Path":"~/run/index.html","StartLine":1330}
input
{ "EndLine": 2250, "Path": "~/run/index.html", "StartLine": 1330 }result, 801 lines
Showing lines 1330-2129 of 3137 1330: a.click(); 1331: a.remove(); 1332: setTimeout(() => URL.revokeObjectURL(url), 3000); 1333: } 1334: 1335: // ============================================================================ 1336: // 5. MATRIX UTILITIES (Minimal Zero-Dependency 4x4 Camera Math) 1337: // ============================================================================ 1338: function mat4Perspective(fovRad, aspect, near, far) { 1339: const f = 1.0 / Math.tan(fovRad * 0.5), nf = 1.0 / (near - far); 1340: return new Float32Array([ 1341: f / aspect, 0, 0, 0, 1342: 0, f, 0, 0, 1343: 0, 0, (far + near) * nf, -1, 1344: 0, 0, (2 * far * near) * nf, 0 1345: ]); 1346: } 1347: 1348: function mat4LookAt(eye, center, up) { 1349: let zx = eye[0] - center[0], zy = eye[1] - center[1], zz = eye[2] - center[2]; 1350: let len = 1 / (Math.hypot(zx, zy, zz) || 1); 1351: zx *= len; zy *= len; zz *= len; 1352: let xx = up[1] * zz - up[2] * zy, xy = up[2] * zx - up[0] * zz, xz = up[0] * zy - up[1] * zx; 1353: len = Math.hypot(xx, xy, xz); 1354: if (len < 1e-5) { xx = 1; xy = 0; xz = 0; } else { xx /= len; xy /= len; xz /= len; } 1355: const yx = zy * xz - zz * xy, yy = zz * xx - zx * xz, yz = zx * xy - zy * xx; 1356: return new Float32Array([ 1357: xx, yx, zx, 0, 1358: xy, yy, zy, 0, 1359: xz, yz, zz, 0, 1360: -(xx * eye[0] + xy * eye[1] + xz * eye[2]), 1361: -(yx * eye[0] + yy * eye[1] + yz * eye[2]), 1362: -(zx * eye[0] + zy * eye[1] + zz * eye[2]), 1363: 1 1364: ]); 1365: } 1366: 1367: function mat4Mul(a, b) { 1368: const out = new Float32Array(16); 1369: for (let c = 0; c < 4; c++) { 1370: for (let r = 0; r < 4; r++) { 1371: out[c * 4 + r] = 1372: a[0 * 4 + r] * b[c * 4 + 0] + 1373: a[1 * 4 + r] * b[c * 4 + 1] + 1374: a[2 * 4 + r] * b[c * 4 + 2] + 1375: a[3 * 4 + r] * b[c * 4 + 3]; 1376: } 1377: } 1378: return out; 1379: } 1380: 1381: function projectToScreen(vp, wx, wy, wz, width, height) { 1382: const cx = vp[0] * wx + vp[4] * wy + vp[8] * wz + vp[12]; 1383: const cy = vp[1] * wx + vp[5] * wy + vp[9] * wz + vp[13]; 1384: const cw = vp[3] * wx + vp[7] * wy + vp[11] * wz + vp[15]; 1385: if (cw <= 0.01) return null; 1386: return [ 1387: ((cx / cw) * 0.5 + 0.5) * width, 1388: (0.5 - (cy / cw) * 0.5) * height 1389: ]; 1390: } 1391: 1392: // ============================================================================ 1393: // 6. WEBGL2 MULTI-PASS OPTICAL ENGINE 1394: // ============================================================================ 1395: const canvas = document.getElementById("glCanvas"); 1396: const gl = canvas.getContext("webgl2", { antialias: true, alpha: false, powerPreference: "high-performance" }); 1397: if (!gl) { 1398: document.body.innerHTML = "<div style='padding:32px;color:#ece8df;font-family:sans-serif'>WebGL2 is required to run this real-time optical simulation.</div>"; 1399: return; 1400: } 1401: 1402: const extColorFloat = gl.getExtension("EXT_color_buffer_float"); 1403: const extFloatBlend = gl.getExtension("EXT_float_blend"); 1404: const extLinearFloat = gl.getExtension("OES_texture_float_linear"); 1405: const useFloatCausticFBO = !!(extColorFloat && extFloatBlend); 1406: 1407: function compileShader(type, src) { 1408: const sh = gl.createShader(type); 1409: gl.shaderSource(sh, src); 1410: gl.compileShader(sh); 1411: if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { 1412: console.error(gl.getShaderInfoLog(sh), src); 1413: throw new Error(gl.getShaderInfoLog(sh)); 1414: } 1415: return sh; 1416: } 1417: 1418: function createProgram(vsSrc, fsSrc) { 1419: const p = gl.createProgram(); 1420: gl.attachShader(p, compileShader(gl.VERTEX_SHADER, vsSrc)); 1421: gl.attachShader(p, compileShader(gl.FRAGMENT_SHADER, fsSrc)); 1422: gl.linkProgram(p); 1423: if (!gl.getProgramParameter(p, gl.LINK_STATUS)) { 1424: throw new Error(gl.getProgramInfoLog(p)); 1425: } 1426: return p; 1427: } 1428: 1429: // Shared GLSL Uniform Cubic B-Spline (C^2 smooth!) Evaluator on uHeightTex (256x256 R32F) 1430: // Guarantees continuous 2nd derivatives across texels -> zero discrete grid artifacts in caustics! 1431: const GLSL_BSPLINE_HEIGHT = ` 1432: uniform sampler2D uHeightTex; 1433: const float H_RES = 256.0; 1434: 1435: // Returns vec3(h, dh/dpx, dh/dps) where px, ps in [-1, 1] 1436: vec3 sampleGlassSpline(vec2 p) { 1437: vec2 uv = clamp(p * 0.5 + 0.5, 0.0, 1.0); 1438: vec2 coord = uv * H_RES - 0.5; 1439: vec2 base = floor(coord); 1440: vec2 f = coord - base; 1441: 1442: // Cubic B-spline weights w(f) and derivatives dw/df 1443: vec2 omf = 1.0 - f; 1444: vec2 w0 = (omf * omf * omf) / 6.0; 1445: vec2 w1 = (3.0 * f * f * f - 6.0 * f * f + 4.0) / 6.0; 1446: vec2 w2 = (-3.0 * f * f * f + 3.0 * f * f + 3.0 * f + 1.0) / 6.0; 1447: vec2 w3 = (f * f * f) / 6.0; 1448: 1449: vec2 dw0 = -0.5 * omf * omf; 1450: vec2 dw1 = 1.5 * f * f - 2.0 * f; 1451: vec2 dw2 = -1.5 * f * f + f + 0.5; 1452: vec2 dw3 = 0.5 * f * f; 1453: 1454: float wx[4] = float[4](w0.x, w1.x, w2.x, w3.x); 1455: float wy[4] = float[4](w0.y, w1.y, w2.y, w3.y); 1456: float dx[4] = float[4](dw0.x, dw1.x, dw2.x, dw3.x); 1457: float dy[4] = float[4](dw0.y, dw1.y, dw2.y, dw3.y); 1458: 1459: float h = 0.0, dhx = 0.0, dhy = 0.0; 1460: for (int j = 0; j < 4; j++) { 1461: int iy = clamp(int(base.y) + j - 1, 0, 255); 1462: float rowH = 0.0, rowDx = 0.0; 1463: for (int i = 0; i < 4; i++) { 1464: int ix = clamp(int(base.x) + i - 1, 0, 255); 1465: float v = texelFetch(uHeightTex, ivec2(ix, iy), 0).r; 1466: rowH += v * wx[i]; 1467: rowDx += v * dx[i]; 1468: } 1469: h += rowH * wy[j]; 1470: dhx += rowDx * wy[j]; 1471: dhy += rowH * dy[j]; 1472: } 1473: // Convert derivative from texel units to physical [-1, 1] units: d(coord)/d(p) = 0.5 * H_RES 1474: float scaleD = 0.5 * H_RES; 1475: return vec3(h, dhx * scaleD, dhy * scaleD); 1476: } 1477: 1478: // Exact 3D Snell refraction of ray from point lamp uLightPos (in optical coords: px, ps, zHeight) 1479: // through flat top (z = uFocalZ + uTGlass) and sculpted bottom (z = uFocalZ + h(px,ps)) onto stage (z = 0). 1480: // Returns vec4(landPx, landPs, transmittedFluxFactor, validFlag) 1481: vec4 traceRefractedRay(vec2 p, vec3 lightOpt, float focalZ, float tGlass, float nIOR) { 1482: vec3 hGrad = sampleGlassSpline(p); 1483: float zTop = focalZ + tGlass; 1484: // Approximate entrance point on top face so ray hits bottom at p 1485: vec3 dirAirApprox = normalize(vec3(p, focalZ) - lightOpt); 1486: vec2 pTop = p - dirAirApprox.xy * (tGlass / max(0.25, -dirAirApprox.z)) * (1.0 / nIOR); 1487: vec3 I_air = normalize(vec3(pTop, zTop) - lightOpt); 1488: float cosI1 = max(0.001, -I_air.z); 1489: float eta1 = 1.0 / nIOR; 1490: float k1 = 1.0 - eta1 * eta1 * (1.0 - cosI1 * cosI1); 1491: vec3 I_glass = vec3(eta1 * I_air.xy, -sqrt(max(1e-6, k1))); 1492: 1493: // Schlick Fresnel transmission at top face 1494: float r0 = (nIOR - 1.0) / (nIOR + 1.0); 1495: r0 = r0 * r0; 1496: float T1 = 1.0 - (r0 + (1.0 - r0) * pow(1.0 - cosI1, 5.0)); 1497: 1498: // Bottom sculpted surface normal pointing UP into glass: N = (-hx, -hy, 1) 1499: vec3 N_up = normalize(vec3(-hGrad.y, -hGrad.z, 1.0)); 1500: float cosI2 = -dot(I_glass, N_up); 1501: if (cosI2 <= 0.01) return vec4(p, 0.0, 0.0); 1502: float k2 = 1.0 - nIOR * nIOR * (1.0 - cosI2 * cosI2); 1503: if (k2 <= 0.0002) { 1504: // Real Total Internal Reflection! 1505: return vec4(p, 0.0, 0.0); 1506: } 1507: float cosT2 = sqrt(k2); 1508: vec3 T_out = nIOR * I_glass + (nIOR * cosI2 - cosT2) * N_up; 1509: if (T_out.z >= -0.02) return vec4(p, 0.0, 0.0); 1510: 1511: float T2 = 1.0 - (r0 + (1.0 - r0) * pow(1.0 - cosT2, 5.0)); 1512: float pz = focalZ + hGrad.x; 1513: float tHit = -pz / T_out.z; 1514: vec2 hit = p + T_out.xy * tHit; 1515: float flux = pow(cosI1, 3.0) * T1 * T2; 1516: return vec4(hit, flux, 1.0); 1517: } 1518: `; 1519: 1520: // ---------------------------------------------------------------------------- 1521: // PASS 1 SHADER: CONTINUOUS GPU SPECTRAL CAUSTIC MICRO-MESH (5 WAVELENGTHS!) 1522: // ---------------------------------------------------------------------------- 1523: const CAUSTIC_VS = `#version 300 es 1524: precision highp float; 1525: layout(location = 0) in vec2 aGridUV; // [0, 1]^2 across lens aperture 1526: 1527: uniform vec3 uLightOpt; // (lx, ls, ly_height) 1528: uniform float uFocalZ; 1529: uniform float uTGlass; 1530: uniform float uBaseIOR; 1531: uniform float uDispersion; 1532: uniform float uExposureScale; 1533: uniform int uNumBands; 1534: 1535: ${GLSL_BSPLINE_HEIGHT} 1536: 1537: out vec3 vColor; 1538: out float vValid; 1539: 1540: void main() { 1541: // Spectral dispersion offset across 5 bands (655nm Red -> 440nm Violet) or 3 RGB bands 1542: float bandOffset = 0.0; 1543: vec3 specWeight = vec3(1.0); 1544: if (uNumBands == 5) { 1545: if (gl_InstanceID == 0) { bandOffset = -1.00; specWeight = vec3(0.46, 0.04, 0.00); } // 655nm Deep Red 1546: else if (gl_InstanceID == 1) { bandOffset = -0.45; specWeight = vec3(0.38, 0.28, 0.00); } // 595nm Warm Amber 1547: else if (gl_InstanceID == 2) { bandOffset = 0.00; specWeight = vec3(0.14, 0.44, 0.06); } // 535nm Emerald 1548: else if (gl_InstanceID == 3) { bandOffset = 0.52; specWeight = vec3(0.02, 0.18, 0.36); } // 485nm Cyan 1549: else { bandOffset = 1.15; specWeight = vec3(0.06, 0.02, 0.42); } // 440nm Violet 1550: } else { 1551: if (gl_InstanceID == 0) { bandOffset = -0.85; specWeight = vec3(1.00, 0.12, 0.00); } 1552: else if (gl_InstanceID == 1) { bandOffset = 0.00; specWeight = vec3(0.06, 0.80, 0.10); } 1553: else { bandOffset = 0.95; specWeight = vec3(0.02, 0.08, 0.76); } 1554: } 1555: 1556: float nLambda = uBaseIOR + bandOffset * uDispersion; 1557: vec2 p = aGridUV * 2.0 - 1.0; 1558: 1559: // Evaluate exact ray landing & local Jacobian area compression via finite differential 1560: float eps = 2.0 / 320.0; 1561: vec4 r0 = traceRefractedRay(p, uLightOpt, uFocalZ, uTGlass, nLambda); 1562: vec4 rx = traceRefractedRay(p + vec2(eps, 0.0), uLightOpt, uFocalZ, uTGlass, nLambda); 1563: vec4 ry = traceRefractedRay(p + vec2(0.0, eps), uLightOpt, uFocalZ, uTGlass, nLambda); 1564: 1565: vec2 dX = (rx.xy - r0.xy) / eps; 1566: vec2 dY = (ry.xy - r0.xy) / eps; 1567: float jacobian = abs(dX.x * dY.y - dX.y * dY.x); 1568: 1569: // Conserved photon irradiance = incident_flux / jacobian (clamped only at sub-pixel caustic singularity) 1570: float irradiance = r0.z / max(jacobian, 0.0045); 1571: 1572: // Soft aperture edge feather so outer glass bevel doesn't cast hard square clipping line 1573: vec2 edgeDist = abs(p); 1574: float edgeFeather = smoothstep(1.0, 0.965, max(edgeDist.x, edgeDist.y)); 1575: 1576: vValid = r0.w * rx.w * ry.w; 1577: vColor = specWeight * irradiance * edgeFeather * uExposureScale; 1578: 1579: // Map stage [-1.45, +1.45]^2 into Caustic FBO NDC [-1, +1]^2 (allows wide off-axis caustic sweeps!) 1580: const float STAGE_EXTENT = 1.45; 1581: gl_Position = vec4(r0.xy / STAGE_EXTENT, 0.0, 1.0); 1582: } 1583: `; 1584: 1585: const CAUSTIC_FS = `#version 300 es 1586: precision highp float; 1587: in vec3 vColor; 1588: in float vValid; 1589: out vec4 fragColor; 1590: void main() { 1591: if (vValid < 0.5) discard; 1592: fragColor = vec4(vColor, 1.0); 1593: } 1594: `; 1595: 1596: // ---------------------------------------------------------------------------- 1597: // PASS 2 SHADER: DUAL-KAWASE PHYSICALLY SOFT HDR CAUSTIC HALATION / BLOOM 1598: // ---------------------------------------------------------------------------- 1599: const QUAD_VS = `#version 300 es 1600: precision highp float; 1601: out vec2 vUV; 1602: void main() { 1603: vec2 pos = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2); 1604: vUV = pos; 1605: gl_Position = vec4(pos * 2.0 - 1.0, 0.0, 1.0); 1606: } 1607: `; 1608: 1609: const BLOOM_FS = `#version 300 es 1610: precision highp float; 1611: in vec2 vUV; 1612: uniform sampler2D uTex; 1613: uniform vec2 uTexel; 1614: uniform float uRadius; 1615: out vec4 fragColor; 1616: void main() { 1617: vec2 d = uTexel * uRadius; 1618: vec3 s = texture(uTex, vUV).rgb * 0.28; 1619: s += texture(uTex, vUV + vec2( d.x, d.y)).rgb * 0.18; 1620: s += texture(uTex, vUV + vec2(-d.x, d.y)).rgb * 0.18; 1621: s += texture(uTex, vUV + vec2( d.x, -d.y)).rgb * 0.18; 1622: s += texture(uTex, vUV + vec2(-d.x, -d.y)).rgb * 0.18; 1623: fragColor = vec4(s, 1.0); 1624: } 1625: `; 1626: 1627: // ---------------------------------------------------------------------------- 1628: // PASS 3A: STUDIO BACKGROUND + OBSIDIAN STAGE FLOOR + HDR CAUSTIC PROJECTION 1629: // ---------------------------------------------------------------------------- 1630: const STAGE_VS = `#version 300 es 1631: precision highp float; 1632: layout(location = 0) in vec3 aPos; 1633: uniform mat4 uVP; 1634: out vec3 vWorldPos; 1635: void main() { 1636: vWorldPos = aPos; 1637: gl_Position = uVP * vec4(aPos, 1.0); 1638: } 1639: `; 1640: 1641: const STAGE_FS = `#version 300 es 1642: precision highp float; 1643: in vec3 vWorldPos; 1644: uniform sampler2D uCausticTex; 1645: uniform sampler2D uBloomTex; 1646: uniform sampler2D uTargetTex; 1647: uniform vec3 uCamPos; 1648: uniform vec3 uLightWorld; 1649: uniform float uFocalZ; 1650: uniform float uCausticScale; 1651: uniform int uCompareMask; 1652: 1653: out vec4 fragColor; 1654: 1655: // Subtle procedural slate micro-grain 1656: float hash21(vec2 p) { 1657: p = fract(p * vec2(123.34, 456.21)); 1658: p += dot(p, p + 45.32); 1659: return fract(p.x * p.y); 1660: } 1661: 1662: void main() { 1663: // Optical stage coords: px = x, ps = -z 1664: vec2 optP = vec2(vWorldPos.x, -vWorldPos.z); 1665: const float STAGE_EXTENT = 1.45; 1666: vec2 causticUV = optP / (2.0 * STAGE_EXTENT) + 0.5; 1667: 1668: // Base dark anodized obsidian tabletop 1669: float rStage = length(vWorldPos.xz); 1670: float grain = (hash21(floor(vWorldPos.xz * 260.0)) - 0.5) * 0.012; 1671: vec3 baseSlate = vec3(0.026, 0.028, 0.036) + grain; 1672: 1673: // Subtle soft specular sheen from point lamp on matte stone 1674: vec3 L = normalize(uLightWorld - vWorldPos); 1675: vec3 V = normalize(uCamPos - vWorldPos); 1676: vec3 H = normalize(L + V); 1677: float spec = pow(max(0.0, H.y), 42.0) * 0.035; 1678: // Soft shadow cast by glass mount frame outside [-1,1]^2 vs subtle ambient occlusion 1679: vec2 boxD = abs(optP) - vec2(1.0); 1680: float frameBorder = abs(max(boxD.x, boxD.y)); 1681: baseSlate += vec3(1.0, 0.88, 0.72) * spec * smoothstep(0.0, 0.4, max(boxD.x, boxD.y) + 0.2); 1682: 1683: // Calibrated hairline optical bench reticle & 100mm aperture frame ticks 1684: float borderLine = exp(-pow(frameBorder / 0.0045, 2.0)) * step(max(abs(optP.x), abs(optP.y)), 1.03); 1685: float tickMarks = 0.0; 1686: if (abs(frameBorder - 0.022) < 0.018 && max(abs(optP.x), abs(optP.y)) < 1.03) { 1687: float c = (abs(optP.x) > abs(optP.y)) ? optP.y : optP.x; 1688: tickMarks = exp(-pow(sin(c * 3.14159265 * 10.0) / 0.12, 2.0)) * 0.35; 1689: } 1690: vec3 reticleCol = vec3(0.32, 0.35, 0.44) * (borderLine * 0.42 + tickMarks * 0.22); 1691: 1692: // Sample live raytraced HDR Caustics + physically soft Dual-Kawase halation 1693: vec3 causticRaw = vec3(0.0); 1694: vec3 bloomRaw = vec3(0.0); 1695: if (causticUV.x > 0.002 && causticUV.x < 0.998 && causticUV.y > 0.002 && causticUV.y < 0.998) { 1696: causticRaw = texture(uCausticTex, causticUV).rgb * uCausticScale; 1697: bloomRaw = texture(uBloomTex, causticUV).rgb * uCausticScale; 1698: } 1699: 1700: vec3 lightDeposit = causticRaw + bloomRaw * 0.36; 1701: 1702: // Optional "Mask vs. Lens" split comparison (left half: absorptive stencil mask; right half: refractive lens!) 1703: if (uCompareMask == 1 && abs(optP.x) < 1.0 && abs(optP.y) < 1.0) { 1704: if (optP.x < 0.0) { 1705: vec2 tUV = optP * 0.5 + 0.5; 1706: float targVal = texture(uTargetTex, tUV).r; 1707: // Absorptive mask simply blocks light where targVal < max, peaking at ONLY 1.0x uniform lamp irradiance! 1708: // Notice how dim 1.0x un-concentrated sunlight is compared to 20x-30x optimal-transport photon focusing! 1709: float maskTrans = clamp((targVal - 0.032) / 1.15, 0.0, 1.0); 1710: float directSun = 0.14; // exact same 1.0x mean incident photon budget! 1711: lightDeposit = vec3(1.0, 0.88, 0.68) * (directSun * maskTrans); 1712: } 1713: // Hairline split divider at x = 0 1714: float splitLine = exp(-pow(optP.x / 0.004, 2.0)); 1715: reticleCol += vec3(0.95, 0.74, 0.45) * splitLine * 0.65; 1716: } 1717: 1718: vec3 col = baseSlate + reticleCol + lightDeposit; 1719: 1720: // Smooth radial fade into dark studio floor 1721: float vig = smoothstep(3.3, 1.35, rStage); 1722: col *= vig; 1723: 1724: // Filmic ACES-inspired highlight roll-off so 30x caustic cusps glow incandescent white-gold! 1725: col = (col * (2.51 * col + 0.03)) / (col * (2.43 * col + 0.59) + 0.14); 1726: col = pow(max(col, 0.0), vec3(1.0 / 2.2)); 1727: fragColor = vec4(col, 1.0); 1728: }401 more lines not shown.