Lava lamp v1 · Antigravity CLI19.11.2026 03:49
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"><title>Lava Lamp</title><style> *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; } html, body { width: 100%; height: 100%; overflow: hidden; background: #0d0a10; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; touch-action: manipulation; user-select: none; -webkit-user-select: none; } #stage { position: relative; width: 100vw; height: 100dvh; height: 100vh; overflow: hidden; } @supports (height: 100dvh) { #stage { height: 100dvh; } } #lamp-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; } /* Tactile inline desk switch positioned dynamically on the desk */ #power-switch { position: absolute; transform: translate(-50%, -50%); appearance: none; border: none; background: none; padding: 10px; cursor: pointer; outline: none; z-index: 10; display: flex; align-items: center; justify-content: center; touch-action: manipulation; } #power-switch:focus-visible .switch-housing { box-shadow: 0 0 0 2px #ffb866, 0 10px 22px rgba(0, 0, 0, 0.7), inset 0 1px 1px rgba(255, 255, 255, 0.16); } .switch-housing { position: relative; width: 68px; height: 36px; border-radius: 18px; background: linear-gradient(180deg, #322c30 0%, #1c181b 55%, #120f12 100%); box-shadow: 0 10px 20px rgba(0, 0, 0, 0.72), 0 3px 6px rgba(0, 0, 0, 0.55), inset 0 1px 1px rgba(255, 232, 208, 0.18), inset 0 -2px 3px rgba(0, 0, 0, 0.7); border: 1px solid rgba(255, 220, 190, 0.1); display: flex; align-items: center; justify-content: space-between; padding: 0 7px 0 10px; transition: box-shadow 0.4s ease, border-color 0.4s ease, transform 0.1s ease; } #power-switch:active .switch-housing { transform: translateY(1px) scale(0.98); } /* Warm ambient reflection on switch housing when lamp is ON */ #power-switch[aria-checked="true"] .switch-housing { border-color: rgba(255, 168, 92, 0.24); box-shadow: 0 10px 22px rgba(0, 0, 0, 0.68), 0 2px 10px rgba(255, 110, 40, 0.14), inset 0 1px 1px rgba(255, 210, 165, 0.28), inset 0 -2px 3px rgba(0, 0, 0, 0.65); } .switch-led { width: 6px; height: 6px; border-radius: 50%; background: #2b2224; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.8); transition: background 0.35s ease, box-shadow 0.35s ease; flex-shrink: 0; } #power-switch[aria-checked="true"] .switch-led { background: #ffaa44; box-shadow: 0 0 6px #ff8822, 0 0 12px rgba(255, 120, 30, 0.7), inset 0 0 2px #fff2cc; } .switch-well { width: 38px; height: 22px; border-radius: 11px; background: #0c0a0c; box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.95), 0 1px 0 rgba(255, 255, 255, 0.08); padding: 2px; display: flex; align-items: center; } .switch-rocker { width: 100%; height: 100%; border-radius: 9px; position: relative; overflow: hidden; background: linear-gradient(90deg, #272226 0%, #3a3236 48%, #1d191c 52%, #2c262a 100%); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.7), inset 0 1px 1px rgba(255, 255, 255, 0.14); transition: background 0.18s ease; } /* Visual tilt of rocker switch */ .switch-rocker::before, .switch-rocker::after { position: absolute; top: 50%; transform: translateY(-50%); font-size: 7px; font-weight: 700; letter-spacing: 0.04em; pointer-events: none; transition: opacity 0.2s ease, color 0.25s ease; } .switch-rocker::before { content: "I"; left: 6px; color: rgba(255, 210, 165, 0.28); } .switch-rocker::after { content: "O"; right: 5px; color: rgba(255, 255, 255, 0.55); } #power-switch[aria-checked="true"] .switch-rocker { background: linear-gradient(90deg, #1b1619 0%, #2a2224 46%, #4a3a37 52%, #332726 100%); box-shadow: inset 2px 1px 3px rgba(0, 0, 0, 0.65), inset -1px 1px 1px rgba(255, 210, 165, 0.24); } #power-switch[aria-checked="true"] .switch-rocker::before { color: #ffbe73; text-shadow: 0 0 4px rgba(255, 140, 50, 0.65); } #power-switch[aria-checked="true"] .switch-rocker::after { color: rgba(255, 255, 255, 0.22); } #power-switch[aria-checked="false"] .switch-rocker { background: linear-gradient(90deg, #3c363a 0%, #4b4348 48%, #221e21 54%, #171417 100%); box-shadow: inset -2px 1px 3px rgba(0, 0, 0, 0.7), inset 1px 1px 1px rgba(255, 255, 255, 0.18); } .switch-label { position: absolute; top: calc(100% + 2px); left: 50%; transform: translateX(-50%); font-size: 10px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; color: rgba(255, 224, 194, 0.45); pointer-events: none; white-space: nowrap; transition: color 0.35s ease; } #power-switch[aria-checked="true"] .switch-label { color: rgba(255, 206, 158, 0.68); } @media (max-width: 420px) { .switch-housing { width: 62px; height: 34px; padding: 0 6px 0 9px; } .switch-well { width: 34px; height: 20px; } .switch-label { font-size: 9px; } }</style></head><body><div id="stage"> <canvas id="lamp-canvas"></canvas> <button id="power-switch" type="button" role="switch" aria-checked="true" aria-label="Lamp power switch"> <span class="switch-housing"> <span class="switch-led" aria-hidden="true"></span> <span class="switch-well" aria-hidden="true"> <span class="switch-rocker"></span> </span> </span> <span class="switch-label" aria-hidden="true">ON</span> </button></div><script>(() => { 'use strict'; const canvas = document.getElementById('lamp-canvas'); const switchBtn = document.getElementById('power-switch'); const switchLabel = switchBtn.querySelector('.switch-label'); // --- Lamp geometry constants (in lamp-height normalized units, v in [0, 1]) --- const V_BASE_TOP = 0.24; const V_GLASS_BOT = 0.24; const V_GLASS_TOP = 0.845; const V_LIQUID_TOP = 0.825; const V_CAP_TOP = 1.00; const GLASS_SPAN = V_LIQUID_TOP - V_GLASS_BOT; // 0.585 // Inner glass half-width as a function of normalized glass height ny in [0, 1] // Expressed in aspect-preserving glass units (1 unit = GLASS_SPAN * lampHeight) function glassHalfWidthLamp(v) { // Smooth classic Astro lava lamp glass profile // Belly peaks around v = 0.365 if (v < 0.365) { const t = Math.max(0, Math.min(1, (v - V_GLASS_BOT) / (0.365 - V_GLASS_BOT))); const s = t * t * (3 - 2 * t); return 0.088 + (0.144 - 0.088) * s; } else { const t = Math.max(0, Math.min(1, (v - 0.365) / (V_GLASS_TOP - 0.365))); // Slight graceful concave taper toward the cap const s = Math.pow(t, 0.92); return 0.144 + (0.072 - 0.144) * s; } } function glassInnerHalfWidth(ny) { const v = V_GLASS_BOT + ny * GLASS_SPAN; const outer = glassHalfWidthLamp(v) / GLASS_SPAN; return Math.max(0.06, outer - 0.015); } // --- Layout state (updated on resize) --- const layout = { w: 1, h: 1, dpr: 1, lampCx: 0.5, // normalized 0..1 of viewport width lampBotY: 0.85, // normalized 0..1 from top of viewport lampH: 0.72, // normalized 0..1 of viewport height deskY: 0.77, // desk horizon line from top (0..1) switchX: 0.75, // switch center x in 0..1 switchY: 0.875 // switch center y in 0..1 }; function updateLayout() { const rect = canvas.parentElement.getBoundingClientRect(); const w = Math.max(1, Math.round(rect.width)); const h = Math.max(1, Math.round(rect.height)); const dpr = Math.min(window.devicePixelRatio || 1, 2); layout.w = w; layout.h = h; layout.dpr = dpr; canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr); // Responsive lamp sizing that fits phone portrait & desktop cleanly const aspect = w / h; const maxLampHByWidth = (w * 0.56) / (0.288 * h); // keep lamp <= 56% of narrow screen width const targetLampH = aspect < 0.75 ? 0.72 : 0.77; layout.lampH = Math.min(targetLampH, maxLampHByWidth); layout.lampBotY = aspect < 0.75 ? 0.845 : 0.865; layout.deskY = layout.lampBotY - layout.lampH * 0.095; // Slight left shift on very narrow phones so lamp + switch balance nicely const lampPxH = layout.lampH * h; const baseHalfPx = 0.140 * lampPxH; const switchHalfW = w <= 420 ? 36 : 40; const gapPx = Math.max(18, Math.min(48, w * 0.06)); let lampCxPx = w * 0.5; let switchCxPx = lampCxPx + baseHalfPx + gapPx + switchHalfW; // Ensure switch never clips past right edge on narrow screens const rightMargin = 16; if (switchCxPx + switchHalfW > w - rightMargin) { const overflow = (switchCxPx + switchHalfW) - (w - rightMargin); lampCxPx -= overflow * 0.58; switchCxPx = lampCxPx + baseHalfPx + gapPx * 0.75 + switchHalfW; switchCxPx = Math.min(switchCxPx, w - rightMargin - switchHalfW); } layout.lampCx = lampCxPx / w; layout.switchX = switchCxPx / w; layout.switchY = Math.min(0.925, layout.lampBotY + Math.max(12, lampPxH * 0.025) / h); switchBtn.style.left = `${(layout.switchX * 100).toFixed(3)}%`; switchBtn.style.top = `${(layout.switchY * 100).toFixed(3)}%`; } // --- Subtle mechanical click sound (zero external assets) --- let audioCtx = null; function playSwitchClick(turningOn) { try { const AC = window.AudioContext || window.webkitAudioContext; if (!AC) return; if (!audioCtx) audioCtx = new AC(); if (audioCtx.state === 'suspended') audioCtx.resume(); const now = audioCtx.currentTime; const dur = 0.024; const sampleRate = audioCtx.sampleRate; const buf = audioCtx.createBuffer(1, Math.floor(sampleRate * dur), sampleRate); const data = buf.getChannelData(0); for (let i = 0; i < data.length; i++) { const env = Math.exp(-i / (sampleRate * 0.0045)); data[i] = (Math.random() * 2 - 1) * env * 0.28; } const src = audioCtx.createBufferSource(); src.buffer = buf; const filter = audioCtx.createBiquadFilter(); filter.type = 'bandpass'; filter.frequency.value = turningOn ? 1850 : 1320; filter.Q.value = 1.6; const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.32, now); gain.gain.exponentialRampToValueAtTime(0.001, now + dur); src.connect(filter); filter.connect(gain); gain.connect(audioCtx.destination); src.start(now); } catch (_) { // Audio is optional; ignore errors silently } } // --- Power state --- let isOn = true; let heaterPower = 1.0; // smooth 0..1 for bulb & heat let glowLevel = 1.0; // smooth 0..1 for visual illumination function togglePower() { isOn = !isOn; switchBtn.setAttribute('aria-checked', String(isOn)); switchLabel.textContent = isOn ? 'ON' : 'OFF'; playSwitchClick(isOn); } switchBtn.addEventListener('click', togglePower); // --- Thermodynamic Wax Blob Simulation --- const MAX_BLOBS = 18; const MIN_RADIUS = 0.040; const MAX_RADIUS = 0.112; const SPLIT_MASS_THRESHOLD = 0.0056; // ~r = 0.075 const MAX_MERGE_MASS = 0.0126; // ~r = 0.112 const NEUTRAL_TEMP = 0.49; const blobs = []; let simTime = 0; let nextBlobId = 1; function createBlob(x, y, r, temp) { return { id: nextBlobId++, x, y, vx: 0, vy: 0, r, mass: r * r, temp, cooldown: 0, phase: Math.random() * Math.PI * 2, contactTime: 0 }; } function initBlobs() { blobs.length = 0; // Start in a lively, warm, organically distributed lava state const seeds = [ // Bottom warming pool { x: -0.055, y: 0.065, r: 0.076, t: 0.56 }, { x: 0.048, y: 0.072, r: 0.078, t: 0.64 }, { x: 0.000, y: 0.125, r: 0.068, t: 0.78 }, // Rising warm blobs { x: -0.025, y: 0.275, r: 0.074, t: 0.80 }, { x: 0.052, y: 0.420, r: 0.064, t: 0.72 }, { x: -0.048, y: 0.545, r: 0.068, t: 0.62 }, // Mid-upper transitioning blobs { x: 0.022, y: 0.680, r: 0.062, t: 0.52 }, { x: -0.028, y: 0.795, r: 0.066, t: 0.36 }, // Cooling descending blobs { x: 0.044, y: 0.865, r: 0.054, t: 0.28 }, { x: 0.065, y: 0.335, r: 0.052, t: 0.35 }, { x: -0.078, y: 0.205, r: 0.056, t: 0.40 } ]; for (const s of seeds) { const b = createBlob(s.x, s.y, s.r, s.t); b.vy = (s.t - NEUTRAL_TEMP) * 0.08; blobs.push(b); } } initBlobs(); function stepPhysics(dt) { simTime += dt; // Smooth heater and glow transitions const targetPower = isOn ? 1.0 : 0.0; const rampRate = isOn ? 1.5 : 1.15; heaterPower += (targetPower - heaterPower) * Math.min(1, dt * rampRate); glowLevel += (targetPower - glowLevel) * Math.min(1, dt * (isOn ? 2.4 : 1.8)); const n = blobs.length; // 1. Heat transfer, buoyancy, and convective currents for (let i = 0; i < n; i++) { const b = blobs[i]; if (b.cooldown > 0) b.cooldown = Math.max(0, b.cooldown - dt); const hw = glassInnerHalfWidth(b.y); const radialFrac = Math.min(1, Math.abs(b.x) / Math.max(0.05, hw)); // Larger blobs have greater thermal inertia const thermalInertia = 0.068 / Math.max(0.04, b.r); // Bottom heating coil (strongest near bottom center) if (heaterPower > 0.001) { const bottomProx = Math.exp(-Math.pow(b.y / 0.20, 1.65)); const centerFocus = 1.0 - 0.38 * radialFrac * radialFrac; const slowWave = 0.84 + 0.16 * Math.sin(simTime * 0.43 + b.phase + b.x * 8.0); b.temp += heaterPower * bottomProx * centerFocus * slowWave * 0.34 * thermalInertia * dt; } // Top cap cooling zone const topProx = Math.exp(-Math.pow((1.0 - b.y) / 0.24, 1.6)); b.temp -= topProx * 0.28 * thermalInertia * dt; // Subtle side-wall cooling (drives classic convection: rise center, sink sides) const wallCool = radialFrac * radialFrac * 0.038; b.temp -= wallCool * thermalInertia * dt; // Ambient cooling (faster when lamp is off so blobs settle smoothly) const ambientCool = isOn ? 0.016 : 0.21; b.temp -= (b.temp - 0.02) * ambientCool * dt; b.temp = Math.max(0.0, Math.min(1.0, b.temp)); // Buoyancy force: warm rises, cool sinks const buoyancy = (b.temp - NEUTRAL_TEMP) * 0.42; b.vy += buoyancy * dt; // Subtle Rayleigh-Benard convective drift: // Warm blobs drift gently toward center; cool blobs drift gently toward walls if (heaterPower > 0.1) { const convDir = (b.temp - NEUTRAL_TEMP); if (convDir > 0.04) { b.vx += (-b.x * 0.28 * convDir) * dt; } else if (convDir < -0.04 && b.y > 0.25 && b.y < 0.88) { const sideSign = b.x >= 0 ? 1 : -1; b.vx += (sideSign * 0.035 * (-convDir)) * dt; } // Gentle organic meander b.vx += Math.sin(simTime * 0.55 + b.phase + b.y * 4.2) * 0.014 * heaterPower * dt; } else { // When off, gently settle toward bottom center b.vx += (-b.x * 0.12) * dt; } } // 2. Pairwise interactions: surface tension, thermal conduction, soft repulsion, and merging let mergeA = -1; let mergeB = -1; for (let i = 0; i < blobs.length; i++) { const a = blobs[i]; for (let j = i + 1; j < blobs.length; j++) { const b = blobs[j]; const dx = b.x - a.x; const dy = b.y - a.y; const dist2 = dx * dx + dy * dy; const sumR = a.r + b.r; const touchRange = sumR * 1.32; if (dist2 < touchRange * touchRange) { const dist = Math.sqrt(dist2) || 0.0001; const nx = dx / dist; const ny = dy / dist; // Heat conduction when touching or close if (dist < sumR * 1.1) { const cond = (1.0 - dist / (sumR * 1.1)) * 0.22 * dt; const dT = (b.temp - a.temp) * cond; a.temp += dT * (b.mass / (a.mass + b.mass)); b.temp -= dT * (a.mass / (a.mass + b.mass)); } const canMerge = mergeA === -1 && a.cooldown <= 0 && b.cooldown <= 0 && (a.mass + b.mass) <= MAX_MERGE_MASS && blobs.length > (isOn ? 7 : 4); const tempDiff = Math.abs(a.temp - b.temp); if (dist < sumR * 0.48 && canMerge && (tempDiff < 0.34 || !isOn)) { // Deep overlap -> coalesce seamlessly (nearly circular in metaball field) mergeA = i; mergeB = j; } else if (dist < sumR * 0.72) { // Soft elastic pressure to prevent infinite stacking when not merging const push = (sumR * 0.72 - dist) * (canMerge && tempDiff < 0.24 ? 0.25 : 1.45); const totalM = a.mass + b.mass; const fa = (b.mass / totalM) * push * dt; const fb = (a.mass / totalM) * push * dt; a.vx -= nx * fa; a.vy -= ny * fa; b.vx += nx * fb; b.vy += ny * fb; } else if (dist < touchRange && canMerge && tempDiff < 0.28) { // Gentle surface-tension attraction when two compatible blobs approach const pull = (1.0 - dist / touchRange) * 0.16 * dt; a.vx += nx * pull; a.vy += ny * pull; b.vx -= nx * pull; b.vy -= ny * pull; } } } } // Perform merge if scheduled if (mergeA !== -1 && mergeB !== -1) { const a = blobs[mergeA]; const b = blobs[mergeB]; const totalMass = a.mass + b.mass; a.x = (a.x * a.mass + b.x * b.mass) / totalMass; a.y = (a.y * a.mass + b.y * b.mass) / totalMass; a.vx = (a.vx * a.mass + b.vx * b.mass) / totalMass; a.vy = (a.vy * a.mass + b.vy * b.mass) / totalMass; a.temp = (a.temp * a.mass + b.temp * b.mass) / totalMass; a.mass = totalMass; a.r = Math.sqrt(totalMass); a.cooldown = 2.6; blobs.splice(mergeB, 1); } // 3. Viscous drag, position integration, and tapered glass wall containment const drag = Math.exp(-3.1 * dt); for (let i = 0; i < blobs.length; i++) { const b = blobs[i]; b.vx *= drag; b.vy *= drag; // Clamp max speed for languid lava motion const maxSpeed = 0.22; const spd = Math.hypot(b.vx, b.vy); if (spd > maxSpeed) { b.vx = (b.vx / spd) * maxSpeed; b.vy = (b.vy / spd) * maxSpeed; } b.x += b.vx * dt; b.y += b.vy * dt; // Vertical bounds (bottom coil & top meniscus) const minY = b.r * 0.62; const maxY = 0.985 - b.r * 0.68; if (b.y < minY) { b.y = minY; if (b.vy < 0) b.vy *= -0.15; } else if (b.y > maxY) { b.y = maxY; if (b.vy > 0) b.vy *= -0.15; } // Tapered horizontal glass walls const hw = glassInnerHalfWidth(b.y); const maxX = Math.max(0.01, hw - b.r * 0.72); if (b.x > maxX) { b.x = maxX; if (b.vx > 0) b.vx *= -0.25; } else if (b.x < -maxX) { b.x = -maxX; if (b.vx < 0) b.vx *= -0.25; } } // 4. Organic splitting of large blobs under thermal/buoyancy shear if (isOn && heaterPower > 0.5 && blobs.length < MAX_BLOBS - 1) { for (let i = 0; i < blobs.length; i++) { const b = blobs[i]; if (b.cooldown > 0 || b.mass < SPLIT_MASS_THRESHOLD) continue; // Blobs split when hot near the bottom (pinching off a rising plume), // or when large and transitioning in mid/upper column const hotBottomSplit = b.y < 0.28 && b.temp > 0.66; const coolTopSplit = b.y > 0.68 && b.temp < 0.42; const midShearSplit = b.mass > 0.0086 && Math.abs(b.vy) > 0.055; const splitChance = (hotBottomSplit ? 0.45 : coolTopSplit ? 0.35 : midShearSplit ? 0.25 : 0.0) * dt; if (Math.random() < splitChance) { const frac = 0.40 + Math.random() * 0.18; const m1 = b.mass * frac; const m2 = b.mass * (1.0 - frac); const r1 = Math.sqrt(m1); const r2 = Math.sqrt(m2); if (r1 < MIN_RADIUS || r2 < MIN_RADIUS) continue; // Spawn overlapping closely along a slightly tilted vertical axis so metaball stretches smoothly const angle = Math.PI * 0.5 + (Math.random() - 0.5) * 0.42; const sep = (r1 + r2) * 0.38; const sx = Math.cos(angle) * sep; const sy = Math.sin(angle) * sep; // Upper daughter slightly warmer, lower daughter slightly cooler (conserving average temp) const dT = 0.11; const tUpper = Math.min(0.96, b.temp + dT * (1.0 - frac)); const tLower = Math.max(0.08, b.temp - dT * frac); const cx = b.x; const cy = b.y; const cvx = b.vx; const cvy = b.vy; // Reuse b as lower daughter b.x = cx - sx * 0.5; b.y = Math.max(r2 * 0.62, cy - sy * 0.5); b.r = r2; b.mass = m2; b.temp = tLower; b.vx = cvx - sx * 0.22; b.vy = cvy - 0.025; b.cooldown = 3.2; // Create upper daughter const upper = createBlob( cx + sx * 0.5, Math.min(0.95, cy + sy * 0.5), r1, tUpper ); upper.vx = cvx + sx * 0.22; upper.vy = cvy + 0.038; upper.cooldown = 3.2; blobs.push(upper); break; // at most one split per frame } } } } // --- WebGL Renderer --- const VERT_SRC = ` attribute vec2 aPos; varying vec2 vUv; void main() { vUv = aPos * 0.5 + 0.5; gl_Position = vec4(aPos, 0.0, 1.0); } `; const FRAG_SRC = ` precision highp float; varying vec2 vUv; uniform vec2 uRes; uniform float uDpr; uniform float uTime; uniform float uGlow; uniform float uHeater; uniform vec4 uLayout; // lampCx, lampBotY (from top), lampH, deskY (from top) uniform vec2 uSwitchPos; // switchX, switchY (from top) uniform vec4 uBlobs[${MAX_BLOBS}]; uniform int uBlobCount; const float V_BASE_TOP = 0.24; const float V_GLASS_BOT = 0.24; const float V_GLASS_TOP = 0.845; const float V_LIQUID_TOP = 0.825; const float V_CAP_TOP = 1.00; const float GLASS_SPAN = 0.585; float sq(float x) { return x * x; } 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); } float noise2(vec2 p) { vec2 i = floor(p); vec2 f = fract(p); f = 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, f.x), mix(c, d, f.x), f.y); } // Half-width of lamp silhouette in lamp-height units for v in [0, 1] float baseHalfWidth(float v) { float t = clamp(v / V_BASE_TOP, 0.0, 1.0); // Slight foot flare at bottom, smooth conical taper to waist float foot = 0.008 * exp(-t * 28.0); float waist = mix(0.138, 0.088, pow(max(t, 1e-5), 0.95)); return waist + foot; } float glassHalfWidth(float v) { if (v < 0.365) { float t = clamp((v - V_GLASS_BOT) / (0.365 - V_GLASS_BOT), 0.0, 1.0); float s = t * t * (3.0 - 2.0 * t); return mix(0.088, 0.144, s); } else { float t = clamp((v - 0.365) / (V_GLASS_TOP - 0.365), 0.0, 1.0); float s = pow(max(t, 1e-5), 0.92); return mix(0.144, 0.072, s); } } float capHalfWidth(float v) { float t = clamp((v - V_GLASS_TOP) / (V_CAP_TOP - V_GLASS_TOP), 0.0, 1.0); return mix(0.0725, 0.055, t); } // Distance to quadratic Bezier curve (for power cord on desk) float sdBezier(vec2 pos, vec2 A, vec2 B, vec2 C) { vec2 a = B - A; vec2 b = A - 2.0 * B + C; vec2 c = a * 2.0; vec2 d = A - pos; float kk = 1.0 / max(dot(b, b), 1e-6); float kx = kk * dot(a, b); float ky = kk * (2.0 * dot(a, a) + dot(d, b)) / 3.0; float kz = kk * dot(d, a); float res = 0.0; float p = ky - kx * kx; float p3 = p * p * p; float q = kx * (2.0 * kx * kx - 3.0 * ky) + kz; float h = q * q + 4.0 * p3; if (h >= 0.0) { h = sqrt(h); vec2 x = (vec2(h, -h) - q) * 0.5; vec2 uv = sign(x) * pow(max(abs(x), vec2(1e-6)), vec2(1.0 / 3.0)); float t = clamp(uv.x + uv.y - kx, 0.0, 1.0); vec2 qv = d + (c + b * t) * t; res = dot(qv, qv); } else { float z = sqrt(-p); float v = acos(clamp(q / (p * z * 2.0), -1.0, 1.0)) / 3.0; float m = cos(v); float n = sin(v) * 1.732050808; vec3 t = clamp(vec3(m + m, -n - m, n - m) * z - kx, 0.0, 1.0); vec2 q1 = d + (c + b * t.x) * t.x; vec2 q2 = d + (c + b * t.y) * t.y; res = min(dot(q1, q1), dot(q2, q2)); } return sqrt(res); } void main() { // Pixel coordinates with y = 0 at top, y = uRes.y at bottom vec2 fragPx = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y); vec2 uv = fragPx / uRes; float lampCxPx = uLayout.x * uRes.x; float lampBotPx = uLayout.y * uRes.y; float lampHPx = uLayout.z * uRes.y; float deskYPx = uLayout.w * uRes.y; float pxToLamp = 1.0 / lampHPx; // Lamp-local coordinates: u = horizontal from lamp center, v = 0 at base bottom, 1 at cap top float u = (fragPx.x - lampCxPx) * pxToLamp; float v = (lampBotPx - fragPx.y) * pxToLamp; // Subtle breathing flicker of warm bulb light float bulbPulse = 1.0 + 0.025 * sin(uTime * 1.7) + 0.015 * sin(uTime * 3.1 + 1.2); float glow = uGlow * bulbPulse; // ===================================================== // 1. BACKGROUND ROOM WALL & DESK SURFACE // ===================================================== vec3 col; vec2 glowCenter = vec2(0.0, 0.44); float distToLamp = length(vec2(u * 0.85, v - glowCenter.y)); // Wall base color (cool dim room when OFF, warm cozy glow when ON) vec3 wallOff = mix(vec3(0.055, 0.062, 0.088), vec3(0.11, 0.12, 0.155), smoothstep(1.3, 0.0, distToLamp)); vec3 wallGlow = vec3(0.38, 0.11, 0.16) * exp(-distToLamp * 2.2) + vec3(0.42, 0.17, 0.07) * exp(-length(vec2(u * 1.1, v - 0.30)) * 3.2); vec3 wallCol = wallOff + wallGlow * glow; // Subtle vignette on wall float vig = 1.0 - 0.32 * dot(uv - 0.5, uv - 0.5); wallCol *= vig; // Desk surface (fragPx.y >= deskYPx) float deskEdge = smoothstep(deskYPx - 1.5, deskYPx + 1.5, fragPx.y); if (deskEdge > 0.001) { float deskDepth = max(0.0, (fragPx.y - deskYPx) / max(1.0, uRes.y - deskYPx)); // Perspective-warped wood grain float persp = 1.0 / (0.35 + deskDepth * 0.95); vec2 woodUv = vec2(u * 2.4 * persp, deskDepth * 14.0); float grain = noise2(vec2(woodUv.x * 1.6, woodUv.y * 5.2 + noise2(woodUv * 2.0) * 1.8)); float fineGrain = noise2(vec2(woodUv.x * 12.0, woodUv.y * 28.0)); vec3 woodDark = vec3(0.085, 0.062, 0.055); vec3 woodLight = vec3(0.155, 0.105, 0.082); vec3 deskCol = mix(woodDark, woodLight, grain * 0.72 + fineGrain * 0.28); // Ambient room light across desk deskCol *= (0.72 + 0.36 * smoothstep(1.1, 0.0, abs(u))); // Subtle desk back-edge bevel highlight float bevel = exp(-abs(fragPx.y - deskYPx) * 0.55); deskCol += (vec3(0.06, 0.065, 0.08) + vec3(0.18, 0.08, 0.05) * glow) * bevel; // Warm glossy reflection of the glowing lava lamp onto the desk surface float reflDist = length(vec2(u * 1.35, (v + 0.05) * 2.8)); float deskGlow = exp(-reflDist * 2.6) * glow; float broadGlow = exp(-length(vec2(u * 0.85, (v + 0.02) * 1.7)) * 2.1) * glow; deskCol += vec3(0.55, 0.19, 0.11) * broadGlow * 0.65; deskCol += vec3(0.85, 0.42, 0.16) * deskGlow * (0.45 + 0.25 * grain); // Soft cast shadow under the lamp base (oval on desk) float shadowDist = length(vec2(u / 0.165, (v - 0.005) / 0.036)); float baseShadow = smoothstep(0.65, 1.35, shadowDist); float contactShadow = smoothstep(0.75, 1.08, length(vec2(u / 0.145, (v - 0.002) / 0.018))); deskCol *= mix(0.28, 1.0, baseShadow) * mix(0.35, 1.0, contactShadow); // Subtle shadow under switch housing vec2 swPx = uSwitchPos * uRes; vec2 dSw = (fragPx - swPx) * pxToLamp; float swShadow = smoothstep(0.04, 0.11, length(vec2(dSw.x * 0.65, (dSw.y - 0.025) * 1.7))); deskCol *= mix(0.45, 1.0, swShadow); col = mix(wallCol, deskCol, deskEdge); } else { col = wallCol; } // ===================================================== // 2. POWER CORD ON DESK (connecting base to switch) // ===================================================== { vec2 swPx = uSwitchPos * uRes; vec2 cordStart = vec2(lampCxPx + 0.125 * lampHPx, lampBotPx - 0.012 * lampHPx); vec2 cordEnd = vec2(swPx.x - 28.0 * uDpr, swPx.y + 1.0 * uDpr); vec2 cordMid = vec2(mix(cordStart.x, cordEnd.x, 0.48), max(cordStart.y, cordEnd.y) + 0.028 * lampHPx); float dCord = sdBezier(fragPx, cordStart, cordMid, cordEnd); float cordRadius = max(2.0, 0.0062 * lampHPx); // Cord shadow on desk float dCordSh = sdBezier(fragPx - vec2(0.0, cordRadius * 1.25), cordStart, cordMid, cordEnd); float cordShadow = smoothstep(cordRadius * 0.7, cordRadius * 2.4, dCordSh); col *= mix(0.55, 1.0, cordShadow); float cordMask = smoothstep(cordRadius + 1.0, cordRadius - 0.8, dCord); if (cordMask > 0.001) { float cordHighlight = smoothstep(cordRadius, 0.0, abs(dCord - cordRadius * 0.35)); vec3 cordCol = vec3(0.08, 0.075, 0.085) + vec3(0.09, 0.07, 0.06) * cordHighlight; cordCol += vec3(0.14, 0.06, 0.03) * glow * cordHighlight; col = mix(col, cordCol, cordMask); } } // ===================================================== // 3. GLASS VESSEL & METABALL WAX (v in [0.24, 0.845]) // ===================================================== if (v >= V_GLASS_BOT - 0.006 && v <= V_GLASS_TOP + 0.006 && abs(u) < 0.165) { float gw = glassHalfWidth(clamp(v, V_GLASS_BOT, V_GLASS_TOP)); float edgeDistPx = (gw - abs(u)) * lampHPx; float glassMask = smoothstep(-1.0, 1.0, edgeDistPx); // Clip cleanly at base collar and cap collar float botClip = smoothstep(-1.0, 1.0, (v - V_GLASS_BOT) * lampHPx); float topClip = smoothstep(-1.0, 1.0, (V_GLASS_TOP - v) * lampHPx); glassMask *= botClip * topClip; if (glassMask > 0.001) { float xNorm = clamp(u / max(gw, 0.001), -1.0, 1.0); // -1..1 across glass width float ny = (v - V_GLASS_BOT) / GLASS_SPAN; // 0..1 inside liquid float nx = u / GLASS_SPAN; // aspect-preserving x // Meniscus curve near the top of the liquid float meniscusY = 1.0 + 0.012 * (xNorm * xNorm - 0.5); float inLiquid = smoothstep(meniscusY + 0.006, meniscusY - 0.004, ny); // --- Liquid Interior Color --- // When ON: golden-amber near bulb -> ruby magenta mid -> deep plum top vec3 liqBotOn = vec3(1.00, 0.54, 0.16); vec3 liqMidOn = vec3(0.64, 0.10, 0.28); vec3 liqTopOn = vec3(0.19, 0.04, 0.22); vec3 liqOn = mix(liqBotOn, liqMidOn, smoothstep(0.0, 0.48, ny)); liqOn = mix(liqOn, liqTopOn, smoothstep(0.42, 1.0, ny)); // Bright incandescent bulb cone right at bottom center float bulbCone = exp(-length(vec2(nx * 3.8, ny * 2.6)) * 2.2); liqOn += vec3(1.0, 0.72, 0.28) * bulbCone * 0.65; // Cylindrical depth shading in liquid (slightly brighter center, richer edges) float cylDepth = sqrt(max(0.0, 1.0 - xNorm * xNorm * 0.78)); liqOn *= (0.68 + 0.38 * cylDepth); // When OFF: translucent cool smoky plum-slate glass fluid vec3 liqOff = mix(vec3(0.11, 0.09, 0.13), vec3(0.065, 0.06, 0.095), ny); liqOff *= (0.72 + 0.35 * cylDepth); vec3 fluidCol = mix(liqOff, liqOn, glow); // Air gap above liquid meniscus vec3 airCol = mix(vec3(0.08, 0.085, 0.11), vec3(0.24, 0.08, 0.16), glow * 0.65); vec3 interiorCol = mix(airCol, fluidCol, inLiquid); // Subtle meniscus rim highlight float meniscusLine = exp(-abs(ny - meniscusY) * 190.0); interiorCol += mix(vec3(0.22, 0.24, 0.30), vec3(0.85, 0.38, 0.55), glow) * meniscusLine * 0.65; // --- Metaball Wax Field Evaluation --- if (inLiquid > 0.01) { vec2 p = vec2(nx, ny); float field = 0.0; vec2 grad = vec2(0.0); float wTemp = 0.0; for (int i = 0; i < ${MAX_BLOBS}; i++) { if (i >= uBlobCount) break; vec4 b = uBlobs[i]; vec2 d = p - b.xy; float r2 = b.z * b.z; float dist2 = dot(d, d) + 0.00022; float contrib = r2 / dist2; field += contrib; grad += (-2.0 * r2 / (dist2 * dist2)) * d; wTemp += contrib * b.w; } // Analytical Euclidean distance to iso-surface (field = 1.0) float gradLen = max(length(grad), 0.001); float isoDist = (field - 1.0) / gradLen; float pxInGlass = 1.0 / (GLASS_SPAN * lampHPx); float waxMask = smoothstep(-pxInGlass * 1.25, pxInGlass * 1.25, isoDist) * inLiquid; // Subtle warm halo around glowing blobs in the surrounding fluid float waxHalo = smoothstep(0.42, 0.98, field) * (1.0 - waxMask) * glow * inLiquid; interiorCol += vec3(0.42, 0.14, 0.06) * waxHalo * 0.42; if (waxMask > 0.001) { float temp = clamp(wTemp / max(field, 0.001), 0.0, 1.0); // Approximate 3D thickness and surface normal from metaball potential float thickness = clamp((field - 1.0) / (field + 0.55), 0.0, 1.0); vec2 outDir = -grad / gradLen; vec3 N = normalize(vec3(outDir * (1.0 - thickness * 0.72), 0.32 + thickness * 1.25)); // Bottom bulb light vector vec3 lightPos = vec3(0.0, -0.08, 0.30); vec3 frag3 = vec3(nx, ny, thickness * 0.16); vec3 L = normalize(lightPos - frag3); float NdotL = max(0.0, dot(N, L)); // Room ambient light from upper-left vec3 Lroom = normalize(vec3(-0.55, 0.65, 0.75)); float roomDiff = max(0.0, dot(N, Lroom)); // Wax base palette when ON (molten coral-crimson -> incandescent tangerine-gold) vec3 waxCoolOn = vec3(0.82, 0.11, 0.25); vec3 waxMidOn = vec3(0.98, 0.34, 0.12); vec3 waxHotOn = vec3(1.00, 0.76, 0.22); vec3 waxOn = mix(waxCoolOn, waxMidOn, smoothstep(0.15, 0.62, temp)); waxOn = mix(waxOn, waxHotOn, smoothstep(0.55, 0.96, temp)); // Extra golden illumination near the bottom bulb float botHeatGlow = exp(-ny * 2.5); waxOn = mix(waxOn, vec3(1.0, 0.88, 0.42), botHeatGlow * 0.42 * uHeater); // Subsurface scattering: thinner edges & stretched necks transmit warm glow float sss = pow(max(1.0 - thickness, 1e-5), 1.5) * (0.45 + 0.55 * botHeatGlow); vec3 waxLitOn = waxOn * (0.58 + 0.55 * NdotL + 0.14 * roomDiff); waxLitOn += vec3(1.0, 0.62, 0.22) * sss * (0.45 + 0.45 * temp); // Soft glossy specular highlight on wet wax vec3 V = vec3(0.0, 0.0, 1.0); vec3 H = normalize(Lroom + V); float spec = pow(max(1e-5, dot(N, H)), 24.0); waxLitOn += vec3(1.0, 0.85, 0.75) * spec * 0.24; // Wax appearance when OFF (denser, cooler, sculptural burgundy-coral wax) vec3 waxOffBase = mix(vec3(0.20, 0.065, 0.10), vec3(0.32, 0.10, 0.13), temp); vec3 waxLitOff = waxOffBase * (0.52 + 0.58 * roomDiff) + vec3(0.24, 0.22, 0.26) * spec * 0.28; vec3 waxCol = mix(waxLitOff, waxLitOn, glow); // Slight glass-wall depth darkening at far left/right edges waxCol *= (0.76 + 0.24 * cylDepth); interiorCol = mix(interiorCol, waxCol, waxMask); } } // --- Glass Wall Optical Effects (refraction rim + curved specular reflections) --- // Subtle shadow right under metal cap and right above base collar float topCapShadow = smoothstep(V_GLASS_TOP, V_GLASS_TOP - 0.032, v); float botCollarGlow = exp(-max(0.0, v - V_GLASS_BOT) * 42.0); interiorCol *= mix(0.58, 1.0, topCapShadow); interiorCol += vec3(0.95, 0.52, 0.18) * botCollarGlow * glow * 0.35; // Glass edge Fresnel rim (gives crisp borosilicate wall thickness) float absX = abs(xNorm); float glassWall = smoothstep(0.84, 0.99, absX); vec3 rimTint = mix(vec3(0.22, 0.25, 0.32), vec3(0.95, 0.55, 0.42), glow * 0.6); interiorCol = mix(interiorCol, interiorCol * 0.65 + rimTint * 0.32, glassWall * 0.72); // Primary curved studio reflection ribbon on left side of glass float leftSpec = exp(-sq((xNorm + 0.68) * 10.5)); float leftSpecFade = smoothstep(V_GLASS_BOT, V_GLASS_BOT + 0.05, v) * smoothstep(V_GLASS_TOP, V_GLASS_TOP - 0.04, v); interiorCol += vec3(0.92, 0.94, 1.0) * leftSpec * leftSpecFade * mix(0.16, 0.24, glow); // Secondary thinner rim reflection on right side of glass float rightSpec = exp(-sq((xNorm - 0.80) * 18.0)); interiorCol += vec3(1.0, 0.88, 0.80) * rightSpec * leftSpecFade * mix(0.10, 0.18, glow); col = mix(col, interiorCol, glassMask); } } // ===================================================== // 4. BRUSHED METAL BASE (v in [0.0, 0.24]) // ===================================================== if (v >= -0.008 && v <= V_BASE_TOP + 0.006 && abs(u) < 0.165) { float bw = baseHalfWidth(clamp(v, 0.0, V_BASE_TOP)); float edgeDistPx = (bw - abs(u)) * lampHPx; float baseMask = smoothstep(-1.0, 1.0, edgeDistPx); // Slight elliptical curve at bottom foot so it sits in perspective on desk float xNorm = clamp(u / max(bw, 0.001), -1.0, 1.0); float footCurve = -0.008 * (1.0 - xNorm * xNorm); float botClip = smoothstep(-1.0, 1.0, (v - footCurve) * lampHPx); float topClip = smoothstep(-1.0, 1.0, (V_BASE_TOP - v) * lampHPx); baseMask *= botClip * topClip; if (baseMask > 0.001) { // Brushed aluminum cylindrical shading float brushed = (hash12(vec2(floor(v * 260.0), floor(xNorm * 24.0))) - 0.5) * 0.035; float cylLight = 0.22 + 0.48 * exp(-sq((xNorm + 0.42) * 2.4)) + 0.22 * exp(-sq((xNorm - 0.62) * 4.5)) + 0.14 * (1.0 - abs(xNorm)); vec3 metalCool = vec3(0.48, 0.50, 0.55) * (cylLight + brushed); // Warm reflected bounce from desk & glowing glass above float collarBounce = exp(-(V_BASE_TOP - v) * 18.0); float deskBounce = exp(-max(0.0, v) * 9.0); vec3 metalWarm = vec3(0.62, 0.42, 0.32) * (cylLight + brushed) + vec3(0.85, 0.36, 0.12) * collarBounce * 0.48 * (0.4 + 0.6 * cylLight) + vec3(0.35, 0.14, 0.06) * deskBounce * 0.35; vec3 baseCol = mix(metalCool, metalWarm, glow); // Crisp metallic collar seam at top of base float seam = exp(-abs(v - V_BASE_TOP) * 220.0); baseCol *= (1.0 - 0.35 * seam); // Subtle dark rim at very bottom foot float footRim = exp(-abs(v - footCurve) * 180.0); baseCol *= (1.0 - 0.38 * footRim); col = mix(col, baseCol, baseMask); } } // ===================================================== // 5. BRUSHED METAL CAP (v in [0.845, 1.0]) // ===================================================== if (v >= V_GLASS_TOP - 0.006 && v <= V_CAP_TOP + 0.006 && abs(u) < 0.095) { float cw = capHalfWidth(clamp(v, V_GLASS_TOP, V_CAP_TOP)); float edgeDistPx = (cw - abs(u)) * lampHPx; float capMask = smoothstep(-1.0, 1.0, edgeDistPx); float xNorm = clamp(u / max(cw, 0.001), -1.0, 1.0); // Subtle domed top edge float topCurve = V_CAP_TOP + 0.004 * (1.0 - xNorm * xNorm); float botClip = smoothstep(-1.0, 1.0, (v - V_GLASS_TOP) * lampHPx); float topClip = smoothstep(-1.0, 1.0, (topCurve - v) * lampHPx); capMask *= botClip * topClip; if (capMask > 0.001) { float brushed = (hash12(vec2(floor(v * 260.0), floor(xNorm * 20.0))) - 0.5) * 0.035; float cylLight = 0.20 + 0.46 * exp(-sq((xNorm + 0.40) * 2.5)) + 0.20 * exp(-sq((xNorm - 0.60) * 4.5)) + 0.12 * (1.0 - abs(xNorm)); vec3 capCool = vec3(0.46, 0.48, 0.54) * (cylLight + brushed); float lowerBounce = exp(-(v - V_GLASS_TOP) * 16.0); vec3 capWarm = vec3(0.56, 0.38, 0.38) * (cylLight + brushed) + vec3(0.65, 0.16, 0.30) * lowerBounce * 0.45; vec3 capCol = mix(capCool, capWarm, glow); // Crisp highlight on top bevel of cap float topBevel = exp(-abs(v - topCurve) * 210.0); capCol += vec3(0.24, 0.24, 0.28) * topBevel; col = mix(col, capCol, capMask); } } // Soft warm atmospheric bloom around the glowing glass float lampHaloDist = length(vec2(u * 1.45, (v - 0.48) * 0.85)); col += vec3(0.22, 0.06, 0.07) * exp(-lampHaloDist * 4.2) * glow * 0.38; // Subtle dithering to eliminate gradient banding on dark walls float dither = (hash12(gl_FragCoord.xy + fract(uTime)) - 0.5) / 255.0; col += dither; // Gentle filmic tone-curve & gamma col = col / (1.0 + col * 0.14); col = pow(max(col, vec3(1e-5)), vec3(0.95)); gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0); } `; function initWebGL() { const gl = canvas.getContext('webgl', { alpha: false, antialias: false, depth: false, stencil: false, preserveDrawingBuffer: false, powerPreference: 'high-performance' }); if (!gl) return null; function compile(type, src) { const sh = gl.createShader(type); gl.shaderSource(sh, src); gl.compileShader(sh); if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { console.warn('Shader compile error:', gl.getShaderInfoLog(sh)); gl.deleteShader(sh); return null; } return sh; } const vs = compile(gl.VERTEX_SHADER, VERT_SRC); const fs = compile(gl.FRAGMENT_SHADER, FRAG_SRC); if (!vs || !fs) return null; const prog = gl.createProgram(); gl.attachShader(prog, vs); gl.attachShader(prog, fs); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { console.warn('Program link error:', gl.getProgramInfoLog(prog)); return null; } const buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1 ]), gl.STATIC_DRAW); const aPos = gl.getAttribLocation(prog, 'aPos'); gl.enableVertexAttribArray(aPos); gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0); gl.useProgram(prog); const uniforms = { uRes: gl.getUniformLocation(prog, 'uRes'), uDpr: gl.getUniformLocation(prog, 'uDpr'), uTime: gl.getUniformLocation(prog, 'uTime'), uGlow: gl.getUniformLocation(prog, 'uGlow'), uHeater: gl.getUniformLocation(prog, 'uHeater'), uLayout: gl.getUniformLocation(prog, 'uLayout'), uSwitchPos: gl.getUniformLocation(prog, 'uSwitchPos'), uBlobs: gl.getUniformLocation(prog, 'uBlobs[0]'), uBlobCount: gl.getUniformLocation(prog, 'uBlobCount') }; const blobData = new Float32Array(MAX_BLOBS * 4); return { render() { gl.viewport(0, 0, canvas.width, canvas.height); gl.uniform2f(uniforms.uRes, canvas.width, canvas.height); gl.uniform1f(uniforms.uDpr, layout.dpr); gl.uniform1f(uniforms.uTime, simTime); gl.uniform1f(uniforms.uGlow, glowLevel); gl.uniform1f(uniforms.uHeater, heaterPower); gl.uniform4f(uniforms.uLayout, layout.lampCx, layout.lampBotY, layout.lampH, layout.deskY); gl.uniform2f(uniforms.uSwitchPos, layout.switchX, layout.switchY); const count = Math.min(blobs.length, MAX_BLOBS); for (let i = 0; i < count; i++) { const b = blobs[i]; const off = i * 4; blobData[off] = b.x; blobData[off + 1] = b.y; blobData[off + 2] = b.r; blobData[off + 3] = b.temp; } gl.uniform4fv(uniforms.uBlobs, blobData); gl.uniform1i(uniforms.uBlobCount, count); gl.drawArrays(gl.TRIANGLES, 0, 6); } }; } // --- Graceful 2D Canvas Fallback (if WebGL is unavailable) --- function initCanvas2D() { const ctx = canvas.getContext('2d'); if (!ctx) return null; return { render() { const w = canvas.width; const h = canvas.height; const cx = layout.lampCx * w; const botY = layout.lampBotY * h; const lh = layout.lampH * h; const deskY = layout.deskY * h; // Background wall const bgGrad = ctx.createRadialGradient(cx, botY - lh * 0.45, lh * 0.05, cx, botY - lh * 0.45, Math.max(w, h) * 0.75); bgGrad.addColorStop(0, glowLevel > 0.05 ? `rgba(95, 28, 42, ${0.4 + 0.6 * glowLevel})` : '#1a1d26'); bgGrad.addColorStop(1, '#0c0a10'); ctx.fillStyle = bgGrad; ctx.fillRect(0, 0, w, h); // Desk const deskGrad = ctx.createLinearGradient(0, deskY, 0, h); deskGrad.addColorStop(0, glowLevel > 0.1 ? '#2b1b16' : '#1b1716'); deskGrad.addColorStop(1, '#120d0c'); ctx.fillStyle = deskGrad; ctx.fillRect(0, deskY, w, h - deskY); // Power cord const swX = layout.switchX * w; const swY = layout.switchY * h; ctx.strokeStyle = '#19161a'; ctx.lineWidth = Math.max(2, lh * 0.011); ctx.beginPath(); ctx.moveTo(cx + lh * 0.125, botY - lh * 0.012); ctx.quadraticCurveTo((cx + swX) * 0.5, Math.max(botY, swY) + lh * 0.03, swX - 24 * layout.dpr, swY); ctx.stroke(); // Glass clipping path ctx.save(); ctx.beginPath(); const steps = 24; for (let i = 0; i <= steps; i++) { const v = V_GLASS_BOT + (V_GLASS_TOP - V_GLASS_BOT) * (i / steps); const x = cx + glassHalfWidthLamp(v) * lh; const y = botY - v * lh; if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } for (let i = steps; i >= 0; i--) { const v = V_GLASS_BOT + (V_GLASS_TOP - V_GLASS_BOT) * (i / steps); const x = cx - glassHalfWidthLamp(v) * lh; const y = botY - v * lh; ctx.lineTo(x, y); } ctx.closePath(); ctx.clip(); // Liquid gradient const liqGrad = ctx.createLinearGradient(0, botY - V_GLASS_BOT * lh, 0, botY - V_GLASS_TOP * lh); if (glowLevel > 0.05) { liqGrad.addColorStop(0, `rgba(255, 138, 40, ${0.4 + 0.6 * glowLevel})`); liqGrad.addColorStop(0.5, `rgba(155, 25, 68, ${0.4 + 0.6 * glowLevel})`); liqGrad.addColorStop(1, `rgba(48, 12, 58, ${0.5 + 0.5 * glowLevel})`); } else { liqGrad.addColorStop(0, '#1c1822'); liqGrad.addColorStop(1, '#100e17'); } ctx.fillStyle = liqGrad; ctx.fillRect(cx - lh * 0.2, botY - V_GLASS_TOP * lh, lh * 0.4, (V_GLASS_TOP - V_GLASS_BOT) * lh); // Wax blobs const glassH = GLASS_SPAN * lh; for (const b of blobs) { const bx = cx + b.x * glassH; const by = botY - (V_GLASS_BOT + b.y * GLASS_SPAN) * lh; const br = b.r * glassH * 1.15; const g = ctx.createRadialGradient(bx - br * 0.2, by + br * 0.2, br * 0.1, bx, by, br); if (glowLevel > 0.05) { const r = Math.round(215 + 40 * b.temp); const gr = Math.round(35 + 150 * b.temp); const bl = Math.round(60 - 20 * b.temp); g.addColorStop(0, `rgba(${r}, ${Math.min(255, gr + 55)}, ${bl + 20}, 0.96)`); g.addColorStop(1, `rgba(${r}, ${gr}, ${bl}, 0.85)`); } else { g.addColorStop(0, 'rgba(95, 32, 46, 0.95)'); g.addColorStop(1, 'rgba(58, 18, 28, 0.9)'); } ctx.fillStyle = g; ctx.beginPath(); ctx.arc(bx, by, br, 0, Math.PI * 2); ctx.fill(); } ctx.restore(); // Metal base const baseGrad = ctx.createLinearGradient(cx - lh * 0.14, 0, cx + lh * 0.14, 0); baseGrad.addColorStop(0, '#302c32'); baseGrad.addColorStop(0.32, '#8b7b76'); baseGrad.addColorStop(0.65, '#42393b'); baseGrad.addColorStop(1, '#262226'); ctx.fillStyle = baseGrad; ctx.beginPath(); ctx.moveTo(cx - lh * 0.14, botY); ctx.lineTo(cx + lh * 0.14, botY); ctx.lineTo(cx + lh * 0.088, botY - V_BASE_TOP * lh); ctx.lineTo(cx - lh * 0.088, botY - V_BASE_TOP * lh); ctx.closePath(); ctx.fill(); // Metal cap ctx.beginPath(); ctx.moveTo(cx - lh * 0.0725, botY - V_GLASS_TOP * lh); ctx.lineTo(cx + lh * 0.0725, botY - V_GLASS_TOP * lh); ctx.lineTo(cx + lh * 0.055, botY - V_CAP_TOP * lh); ctx.lineTo(cx - lh * 0.055, botY - V_CAP_TOP * lh); ctx.closePath(); ctx.fill(); } }; } updateLayout(); window.addEventListener('resize', updateLayout, { passive: true }); window.addEventListener('orientationchange', () => setTimeout(updateLayout, 100), { passive: true }); const renderer = initWebGL() || initCanvas2D(); // --- Main 60 FPS Animation Loop --- let lastTime = performance.now(); function frame(now) { const dt = Math.min((now - lastTime) / 1000, 0.05); lastTime = now; stepPhysics(dt); if (renderer) renderer.render(); requestAnimationFrame(frame); } requestAnimationFrame(frame);})();</script></body></html>