FARMER SKOT

Sleep Little Kitchen

A game you can hide in, and the director's-cut player: the shots, the house, the foley, all in one page

Sleep Little Kitchen on YouTube Watch on YouTube ↗
Play the game → Editor's station YouTube ↗ Download the code zip

The player

A WebGL2 renderer and a shot list, packed as one page, with foley the house learns as it runs

player.js — the tiny WebGL2 engine (opening)
(() => {
"use strict";
// ============================================================
// engine.js — tiny WebGL2 renderer: math, GL, geometry, painted
// textures, scene / paint / device / copy programs.
// ============================================================
const TAU = Math.PI * 2;
const clamp = (x, a, b) => (x < a ? a : x > b ? b : x);
const lerp = (a, b, t) => a + (b - a) * t;
const smooth = (a, b, x) => { const t = clamp((x - a) / (b - a), 0, 1); return t * t * (3 - 2 * t); };
const ease = (x) => { x = clamp(x, 0, 1); return x * x * (3 - 2 * x); };
function hash1(n) { const s = Math.sin(n * 127.1 + 311.7) * 43758.5453; return s - Math.floor(s); }
function hash2(a, b) { return hash1(a * 57.31 + b * 113.97); }
function vnoise(x) { const i = Math.floor(x), f = x - i, u = f * f * (3 - 2 * f); return lerp(hash1(i), hash1(i + 1), u); }
function fbm1(x) { return vnoise(x) * 0.5 + vnoise(x * 2.03 + 7.1) * 0.3 + vnoise(x * 4.1 + 3.3) * 0.2; }
function rng(seed) { let s = (seed * 2654435761) >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }

// ---------- vectors ----------
const v3 = {
  add: (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]],
  sub: (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
  mul: (a, s) => [a[0] * s, a[1] * s, a[2] * s],
  dot: (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
  cross: (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]],
  len: (a) => Math.hypot(a[0], a[1], a[2]),
  norm: (a) => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; },
  lerp: (a, b, t) => [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)],
};

// ---------- 4x4 matrices (column-major, GL convention) ----------
const M = {
  id() { const m = new Float32Array(16); m[0] = m[5] = m[10] = m[15] = 1; return m; },
  mul(a, b) {
    const o = new Float32Array(16);
    for (let c = 0; c < 4; c++) for (let r = 0; r < 4; r++)
      o[c * 4 + r] = a[r] * b[c * 4] + a[4 + r] * b[c * 4 + 1] + a[8 + r] * b[c * 4 + 2] + a[12 + r] * b[c * 4 + 3];
    return o;
  },
  T(x, y, z) { const m = M.id(); m[12] = x; m[13] = y; m[14] = z; return m; },
  S(x, y, z) { const m = M.id(); m[0] = x; m[5] = y === undefined ? x : y; m[10] = z === undefined ? x : z; return m; },
  Rx(a) { const c = Math.cos(a), s = Math.sin(a), m = M.id(); m[5] = c; m[6] = s; m[9] = -s; m[10] = c; return m; },
  Ry(a) { const c = Math.cos(a), s = Math.sin(a), m = M.id(); m[0] = c; m[2] = -s; m[8] = s; m[10] = c; return m; },
  Rz(a) { const c = Math.cos(a), s = Math.sin(a), m = M.id(); m[0] = c; m[1] = s; m[4] = -s; m[5] = c; return m; },
  persp(fovy, asp, n, f) {
    const t = 1 / Math.tan(fovy / 2), m = new Float32Array(16);
    m[0] = t / asp; m[5] = t; m[10] = (f + n) / (n - f); m[11] = -1; m[14] = (2 * f * n) / (n - f); return m;
  },
  ortho(l, r, b, t, n, f) {
    const m = M.id(); m[0] = 2 / (r - l); m[5] = 2 / (t - b); m[10] = -2 / (f - n);
    m[12] = -(r + l) / (r - l); m[13] = -(t + b) / (t - b); m[14] = -(f + n) / (f - n); return m;
  },
  lookAt(e, t, u) {
    const z = v3.norm(v3.sub(e, t)), x = v3.norm(v3.cross(u, z)), y = v3.cross(z, x), m = M.id();
    m[0] = x[0]; m[4] = x[1]; m[8] = x[2];
    m[1] = y[0]; m[5] = y[1]; m[9] = y[2];
    m[2] = z[0]; m[6] = z[1]; m[10] = z[2];
    m[12] = -v3.dot(x, e); m[13] = -v3.dot(y, e); m[14] = -v3.dot(z, e); return m;
  },
  nrm3(m) {
    const c0 = [m[0], m[1], m[2]], c1 = [m[4], m[5], m[6]], c2 = [m[8], m[9], m[10]];
    const a = v3.cross(c1, c2), b = v3.cross(c2, c0), c = v3.cross(c0, c1);
    const det = v3.dot(c0, a) || 1e-9, o = new Float32Array(9);
    o[0] = a[0] / det; o[1] = a[1] / det; o[2] = a[2] / det;
    o[3] = b[0] / det; o[4] = b[1] / det; o[5] = b[2] / det;
    o[6] = c[0] / det; o[7] = c[1] / det; o[8] = c[2] / det; return o;
  },
  xf(m, p) { // transform point, returns [x,y,z,w]
    const x = p[0], y = p[1], z = p[2];
    return [m[0] * x + m[4] * y + m[8] * z + m[12], m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]];
  },
};
function mm(...ms) { let r = ms[0]; for (let i = 1; i < ms.length; i++) r = M.mul(r, ms[i]); return r; }

// ---------- GL context ----------
const glc = document.createElement('canvas');
glc.width = 1536; glc.height = 1536; // tall enough for the 9:16 doorbell (1280 px)
const gl = glc.getContext('webgl2', { antialias: false, preserveDrawingBuffer: true, alpha: false, premultipliedAlpha: false, powerPreference: 'high-performance' });
if (!gl) throw new Error('WebGL2 is not available in this browser.');
player.js — the director's cut
Director').addEventListener('click', () => setSolo(null));
// layer switch: director's cut / app / technician / model
const LAYER_HINT = { auto: 'Whatever view the cut chose. Press L to switch.', app: 'What the owner sees in the app.', tech: 'What a technician would pull: registers, waveforms, packets.', model: 'What each model takes in and puts out. Devices with no model say so.' };
function setLayer(m) {
  LAYER.mode = m; state.dirty = true;
  document.querySelectorAll('[data-layer]').forEach((b) => b.setAttribute('aria-pressed', String(b.dataset.layer === m)));
  $('layerHint').textContent = LAYER_HINT[m];
}
document.querySelectorAll('[data-layer]').forEach((b) => b.addEventListener('click', () => setLayer(b.dataset.layer)));
const CUT_HINT = { beat: 'Every cut on the beat, 125 BPM. Press C to switch.', story: 'The first edit: long holds, cut on the lyrics. Press C to switch.' };
function setCut(m) {
  setCutMode(m); state.dirty = true; state.lastShot = null; state.lastSolo = undefined;
  document.querySelectorAll('[data-cut]').forEach((b) => b.setAttribute('aria-pressed', String(b.dataset.cut === m)));
  $('cutHint').textContent = CUT_HINT[m]; buildShotTable(); drawScrub();
}
document.querySelectorAll('[data-cut]').forEach((b) => b.addEventListener('click', () => setCut(b.dataset.cut)));
// format: 16:9 for the video, 9:16 for Shorts, Reels and vertical ads
function setFormat(v) {
  FORMAT.v = v; scr.width = v ? 1080 : 1920; scr.height = v ? 1920 : 1080; state.dirty = true;
  $('stage').classList.toggle('vertical', v);
  document.querySelectorAll('[data-fmt]').forEach((b) => b.setAttribute('aria-pressed', String((b.dataset.fmt === 'v') === v)));
  $('fmtHint').textContent = v ? '1080×1920 for Shorts, Reels and vertical ads. A screen on its own is shown above its mod