PartLock-CMU / live /live.html
Yakimask's picture
Upload 31 files
c7ae99a verified
Raw History Blame Contribute Delete
20.2 kB
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>リアルタイム生成</title>
<meta name="claude:viewport-fit-sides" content="cover">
<style>
:root {
--bg: #f6f7f5; --panel: #ffffff; --ink: #161a18; --ink-2: #4c5551; --muted: #84908a;
--line: #dde2df; --fig: #0f8a74; --trail: #b9c1bd; --floor: #e3e8e5; --front: #e8742c;
--f-ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
--f-head: "Space Grotesk", "Inter", system-ui, sans-serif;
--f-num: "JetBrains Mono", ui-monospace, Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #121514; --panel: #1a1e1c; --ink: #edf1ee; --ink-2: #b9c2bd; --muted: #7f8a85;
--line: #2b312e; --fig: #2fc4a6; --trail: #4d5551; --floor: #262c29; --front: #ff9a4d; color-scheme: dark;
}
}
:root[data-theme="dark"] {
--bg: #121514; --panel: #1a1e1c; --ink: #edf1ee; --ink-2: #b9c2bd; --muted: #7f8a85;
--line: #2b312e; --fig: #2fc4a6; --trail: #4d5551; --floor: #262c29; --front: #ff9a4d; color-scheme: dark;
}
body { background: var(--bg); color: var(--ink); font: 14px/1.6 var(--f-ui); }
.wrap { max-width: 1080px; margin: 0 auto; display: grid; gap: 14px;
padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); padding-block: 24px 56px; }
h1 { font: 600 22px/1.25 var(--f-head); margin: 0; text-wrap: balance; }
.sub { color: var(--ink-2); margin: 4px 0 0; max-width: 70ch; }
.row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.row button, .ctl button { font: 500 13px var(--f-ui); color: var(--ink); background: var(--panel);
border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; cursor: pointer; }
.row button[aria-pressed="true"] { border-color: var(--ink); background: var(--ink); color: var(--bg); }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--fig); outline-offset: 2px; }
.caption { font-size: 15px; color: var(--ink); }
.stage { position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
overflow: hidden; height: min(60vh, 520px); min-height: 320px; }
.stage canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
.hint { position: absolute; right: 12px; bottom: 10px; font-size: 12px; color: var(--muted); pointer-events: none; }
.ctl { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; }
.ctl label { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); font-size: 13px; }
.ctl input[type=range] { width: min(320px, 60vw); accent-color: var(--fig); }
.fr { font-family: var(--f-num); font-size: 12.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.tbl { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: right; font-family: var(--f-num);
font-variant-numeric: tabular-nums; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; font-family: var(--f-ui); }
tr:last-child td { border-bottom: none; }
thead th { color: var(--ink-2); font-weight: 600; font-family: var(--f-ui); }
.notes { color: var(--ink-2); font-size: 13px; max-width: 72ch; margin: 0; padding-left: 20px; }
.notes li { margin: 3px 0; }
.bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.bar input[type=text] { flex: 1 1 260px; min-width: 0; font: 15px var(--f-ui); color: var(--ink); background: var(--panel);
border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; }
.go { font: 600 15px var(--f-ui); color: var(--bg); background: var(--fig); border: none; border-radius: 10px; padding: 9px 20px; cursor: pointer; }
.ghost { font: 500 13px var(--f-ui); color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; cursor: pointer; }
.now { font-size: 16px; } .now b { font-weight: 600; }
.pend { color: var(--front); font-size: 13px; }
.stat { font-family: var(--f-num); font-size: 12.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.warn { color: var(--front); }
.log { font-size: 12.5px; color: var(--ink-2); margin: 0; padding-left: 18px; }
</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Space+Grotesk:wght@600&family=JetBrains+Mono:wght@400&display=swap">
<div class="wrap">
<header>
<h1>リアルタイム生成(CMU モデル)</h1>
<p class="sub">止めずにずっと動きを作り続けて、その場で再生します。英語の文章を入れて「生成」を押すと、次のステップ(4コマ=0.13秒)から新しい文章に切り替わります。前の動きの流れは消さずにつなぎます。</p>
</header>
<form class="bar" id="f" autocomplete="off">
<input type="text" id="txt" placeholder="例: walk / run/jog / jump / Wave / kick" aria-label="文章(英語)">
<button class="go" type="submit">生成</button>
</form>
<div class="row" id="chips" role="group" aria-label="学習データにある文章"></div>
<div class="now" id="now"></div>
<div class="pend" id="pend"></div>
<div class="stage" id="stage">
<canvas id="cv" aria-label="生成中の動きの人形"></canvas>
<div class="hint">ドラッグで回転・ホイールで拡大</div>
</div>
<div class="ctl">
<button class="ghost" id="pause" type="button">一時停止</button>
<button class="ghost" id="reset" type="button">最初から(流れを消す)</button>
<label for="s">文章の強さ s <select id="s"><option>1</option><option>2</option><option>3</option><option selected>4</option><option>6</option><option>8</option></select></label>
</div>
<div class="stat" id="stat"></div>
<ol class="log" id="log" reversed></ol>
</div>
<script>
"use strict";
const RAD = { 1: .07, 2: .07, 3: .1, 6: .1, 9: .1, 4: .065, 5: .065, 7: .05, 8: .05, 10: .04, 11: .04, 12: .05,
13: .055, 14: .055, 15: .05, 16: .05, 17: .05, 18: .045, 19: .045, 20: .038, 21: .038 };
const css = n => getComputedStyle(document.documentElement).getPropertyValue(n).trim();
const stage = document.getElementById("stage"), cv = document.getElementById("cv"), ctx = cv.getContext("2d");
let W = 300, H = 300, DPR = 1;
const cam = { yaw: 0.5, pitch: 0.3, dist: 5.0, tx: 0, ty: 0.9, tz: 0 };
const st = { clip: 0, t: 0, playing: true };
function project(x, y, z) {
const dx = x - cam.tx, dy = y - cam.ty, dz = z - cam.tz;
const cy = Math.cos(cam.yaw), sy = Math.sin(cam.yaw);
const x1 = dx * cy - dz * sy, z1 = dx * sy + dz * cy;
const cp = Math.cos(cam.pitch), sp = Math.sin(cam.pitch);
const y2 = dy * cp - z1 * sp, z2 = dy * sp + z1 * cp;
const depth = cam.dist - z2;
if (!(depth > 0.1)) return null;
const f = Math.min(W, H) * 1.35 / depth;
return [W / 2 + x1 * f, H * 0.56 - y2 * f, depth];
}
function drawFloor() {
ctx.strokeStyle = css("--floor"); ctx.lineWidth = 1;
const cx = Math.round(cam.tx * 2) / 2, cz = Math.round(cam.tz * 2) / 2;
for (let i = -5; i <= 5.0001; i += 0.5)
for (const [a, b] of [[[cx + i, 0, cz - 5], [cx + i, 0, cz + 5]], [[cx - 5, 0, cz + i], [cx + 5, 0, cz + i]]]) {
const p = project(...a), q = project(...b); if (!p || !q) continue;
ctx.beginPath(); ctx.moveTo(p[0], p[1]); ctx.lineTo(q[0], q[1]); ctx.stroke();
}
}
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
const mul = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
const 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]];
const norm = a => { const n = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / n, a[1] / n, a[2] / n]; };
function shade(hex, k) { const n = parseInt(hex.slice(1), 16), r = n >> 16 & 255, g = n >> 8 & 255, b = n & 255;
const f = v => Math.max(0, Math.min(255, Math.round(v * k))); return "rgb(" + f(r) + "," + f(g) + "," + f(b) + ")"; }
// SMPL joints: 0 pelvis 1 L_hip 2 R_hip 3 spine1 4 L_knee 5 R_knee 6 spine2 7 L_ankle 8 R_ankle 9 spine3
// 10 L_foot 11 R_foot 12 neck 13 L_collar 14 R_collar 15 head 16 L_shoulder 17 R_shoulder 18 L_elbow 19 R_elbow 20 L_wrist 21 R_wrist
// [a, b, radius at a, radius at b, side] side: 0 center, 1 left, 2 right (right side drawn darker)
// torso and head are boxes that follow the joint's rotation; front face (+z in SMPL) is orange, back face dark
// [joint for rotation, center = mix of joints a,b (t), offset in local axes, half sizes x,y,z]
const BOX = [[0, 0, 0, 0, [0, 0.01, 0], [.16, .09, .105]], // hips
[3, 3, 6, 0.5, [0, 0, 0], [.135, .085, .095]], // belly
[9, 9, 12, 0.35, [0, 0, 0], [.175, .13, .1]], // chest
[15, 15, 15, 0, [0, 0.085, 0.02], [.08, .11, .1]]]; // head
const SEG = [
[12, 15, .045, .045, 0],
[13, 16, .06, .055, 1], [14, 17, .06, .055, 2],
[16, 18, .052, .042, 1], [18, 20, .04, .031, 1], [17, 19, .052, .042, 2], [19, 21, .04, .031, 2],
[1, 4, .085, .057, 1], [4, 7, .055, .04, 1], [2, 5, .085, .057, 2], [5, 8, .055, .04, 2],
[7, 10, .042, .034, 1], [8, 11, .042, .034, 2]];
function figurePieces(c, k, color, out) {
const P = c.p, R = c.r, po = k * 66, ro = k * 198, ox = c.tr[2 * k], oz = c.tr[2 * k + 1];
const pos = j => [P[po + j * 3] + ox, P[po + j * 3 + 1], P[po + j * 3 + 2] + oz];
const col = (j, q) => [R[ro + j * 9 + q], R[ro + j * 9 + 3 + q], R[ro + j * 9 + 6 + q]];
const tone = s => s === 2 ? 0.78 : s === 1 ? 1.08 : 1;
const cap = (a, b, ra, rb, s, dz) => { const pa = project(...a), pb = project(...b); if (!pa || !pb) return;
out.push({ depth: (pa[2] + pb[2]) / 2 + (dz || 0), cap: [pa, pb, ra, rb], fill: color, k: tone(s) }); };
for (const [a, b, ra, rb, s] of SEG) cap(pos(a), pos(b), ra, rb, s, 0);
// hands follow the WRIST rotation (SMPL T-pose: hand along +x left / -x right, palm faces -y, thumb +z)
for (const [w, s, sx] of [[20, 1, 1], [21, 2, -1]]) {
const pw = pos(w), hx = mul(col(w, 0), sx), pn = mul(col(w, 1), -1), tz = col(w, 2);
const C = (l, t) => add(add(pw, mul(hx, l)), mul(tz, t));
const quad = [C(0.01, 0.035), C(0.17, 0.035), C(0.17, -0.035), C(0.01, -0.035)].map(p => project(...p));
if (quad.every(Boolean)) {
const dep = quad.reduce((a, q) => a + q[2], 0) / 4 - 0.003;
out.push({ depth: dep, palm: quad, fill: color, k: tone(s) * (0.75 + 0.45 * Math.abs(dot(pn, norm([-0.4, 0.8, 0.5])))) });
}
}
const front = css("--front");
for (const [jr, ja, jb, t, off, hs] of BOX) {
const ax = [col(jr, 0), col(jr, 1), col(jr, 2)];
let c0 = add(mul(pos(ja), 1 - t), mul(pos(jb), t));
for (let i = 0; i < 3; i++) c0 = add(c0, mul(ax[i], off[i]));
for (let a = 0; a < 3; a++) for (const sgn of [1, -1]) {
const b = (a + 1) % 3, cc = (a + 2) % 3;
const corner = (ub, uc) => add(add(add(c0, mul(ax[a], sgn * hs[a])), mul(ax[b], ub * hs[b])), mul(ax[cc], uc * hs[cc]));
let q = [corner(-1, -1), corner(1, -1), corner(1, 1), corner(-1, 1)];
if (sgn < 0) q.reverse();
q = q.map(p => project(...p)); if (!q.every(Boolean)) continue;
let area = 0; for (let i = 0; i < 4; i++) { const p = q[i], r = q[(i + 1) % 4]; area += p[0] * r[1] - r[0] * p[1]; }
if (area > 0) continue; // facing away from the camera
const nrm = mul(ax[a], sgn), lit = 0.7 + 0.45 * Math.max(0, dot(nrm, norm([-0.4, 0.8, 0.5])));
const isFront = a === 2 && sgn > 0, isBack = a === 2 && sgn < 0;
out.push({ depth: q.reduce((s, p) => s + p[2], 0) / 4, palm: q, fill: isFront ? front : color,
k: isFront ? 0.85 + 0.25 * lit / 1.15 : isBack ? 0.45 : lit * 0.85 });
}
}
const g = project(pos(0)[0], 0.002, pos(0)[2]); // floor shadow
if (g) out.push({ depth: 1e9, shadow: g });
}
function paintPiece(s, F) {
if (s.shadow) { const r = 0.28 * F / s.shadow[2]; ctx.fillStyle = "rgba(0,0,0,0.12)";
ctx.beginPath(); ctx.ellipse(s.shadow[0], s.shadow[1], r, r * 0.35, 0, 0, 7); ctx.fill(); return; }
if (s.palm) { const q = s.palm; ctx.fillStyle = shade(s.fill, s.k); ctx.strokeStyle = shade(s.fill, s.k * 0.8);
ctx.lineWidth = Math.max(2, 0.018 * F / q[0][2]); ctx.lineJoin = "round";
ctx.beginPath(); ctx.moveTo(q[0][0], q[0][1]); for (let i = 1; i < 4; i++) ctx.lineTo(q[i][0], q[i][1]);
ctx.closePath(); ctx.fill(); ctx.stroke(); return; }
if (s.ball) { const r = s.r * F / s.ball[2], [x, y] = s.ball;
const gr = ctx.createRadialGradient(x - r * 0.35, y - r * 0.4, r * 0.1, x, y, r);
const kb = s.k || 1; gr.addColorStop(0, shade(s.fill, 1.35 * kb)); gr.addColorStop(1, shade(s.fill, 0.7 * kb));
ctx.fillStyle = gr; ctx.beginPath(); ctx.arc(x, y, r, 0, 7); ctx.fill(); return; }
const [a, b, ra0, rb0] = s.cap, ra = ra0 * F / a[2], rb = rb0 * F / b[2];
let dx = b[0] - a[0], dy = b[1] - a[1]; const L = Math.hypot(dx, dy) || 1e-6; dx /= L; dy /= L;
const nx = -dy, ny = dx, mx = (a[0] + b[0]) / 2, my = (a[1] + b[1]) / 2, rm = Math.max(ra, rb);
const sg = nx * -0.6 + ny * -0.8 > 0 ? 1 : -1; // lit side faces the upper left
const gr = ctx.createLinearGradient(mx - nx * rm * sg, my - ny * rm * sg, mx + nx * rm * sg, my + ny * rm * sg);
const k = s.k || 1;
gr.addColorStop(0, shade(s.fill, 0.62 * k)); gr.addColorStop(0.62, shade(s.fill, 1.22 * k)); gr.addColorStop(1, shade(s.fill, 0.95 * k));
ctx.fillStyle = gr;
ctx.beginPath(); ctx.arc(a[0], a[1], ra, 0, 7); ctx.fill();
ctx.beginPath(); ctx.arc(b[0], b[1], rb, 0, 7); ctx.fill();
ctx.beginPath(); ctx.moveTo(a[0] + nx * ra, a[1] + ny * ra); ctx.lineTo(b[0] + nx * rb, b[1] + ny * rb);
ctx.lineTo(b[0] - nx * rb, b[1] - ny * rb); ctx.lineTo(a[0] - nx * ra, a[1] - ny * ra); ctx.closePath(); ctx.fill();
}
const CHIPS = ["Standing still", "walk", "run/jog", "jump", "Wave", "kick", "salsa dance", "boxing", "sit on high stool, stand up", "walk backwards and turn", "cartwheel", "Mickey sneaking walk"];
const PAR = [-1, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 12, 13, 14, 16, 17, 18, 19];
const FPS = 30;
let buf = [], bufStart = 0, have = -1, playF = 0, paused = false, trail = [], lastInfo = null, stalled = 0;
const $ = id => document.getElementById(id);
async function post(path, obj) { try { await fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(obj || {}) }); } catch (e) {} }
CHIPS.forEach(t => { const b = document.createElement("button"); b.type = "button"; b.textContent = t;
b.addEventListener("click", () => { $("txt").value = t; send(t); }); $("chips").appendChild(b); });
function send(t) { t = (t || "").trim(); if (!t) return; post("/prompt", { text: t }); $("pend").textContent = "切り替え待ち: “" + t + "”"; }
$("f").addEventListener("submit", e => { e.preventDefault(); send($("txt").value); });
$("pause").addEventListener("click", e => { paused = !paused; e.currentTarget.textContent = paused ? "再生" : "一時停止"; });
$("reset").addEventListener("click", () => { post("/reset"); buf = []; bufStart = 0; have = 0; playF = 0; trail = []; });
$("s").addEventListener("change", e => post("/s", { s: +e.target.value }));
async function poll() {
try {
const r = await fetch("/frames?from=" + have + "&play=" + Math.floor(playF), { cache: "no-store" });
const j = await r.json(); lastInfo = j;
if (have < 0) { buf = []; bufStart = j.start; have = j.start; playF = j.start; }
if (j.total < have) { buf = []; bufStart = j.start; have = j.start; playF = j.start; trail = []; }
if (j.start > have) { buf = []; bufStart = j.start; have = j.start; playF = Math.max(playF, j.start); }
for (const f of j.frames) { buf.push(f); have++; }
$("pend").textContent = j.pending ? "切り替え待ち: “" + j.pending + "”" : "";
const ok = j.step_ms <= j.realtime_ms;
$("stat").innerHTML = "1ステップの計算 " + j.step_ms.toFixed(0) + " ms(リアルタイムは " + j.realtime_ms.toFixed(0) + " ms 以下)" +
(ok ? "" : " <span class='warn'>← 間に合っていない</span>") + " 先読み " + ((have - playF) / FPS).toFixed(1) + " 秒" +
" 経過 " + (playF / FPS).toFixed(0) + " 秒" + (stalled > 0.2 ? " <span class='warn'>生成待ちで止まった合計 " + stalled.toFixed(1) + " 秒</span>" : "");
$("log").textContent = "";
j.log.slice().reverse().forEach(([f, t]) => { const li = document.createElement("li"); li.textContent = (f / FPS).toFixed(1) + " 秒から “" + t + "”"; $("log").appendChild(li); });
} catch (e) { $("stat").innerHTML = "<span class='warn'>サーバーにつながらない(live_server.py が動いているか確認)</span>"; }
setTimeout(poll, 120);
}
function frameAt(i) { const k = i - bufStart; return k >= 0 && k < buf.length ? buf[k] : null; }
function drawLive() {
const f = frameAt(Math.floor(playF));
ctx.setTransform(DPR, 0, 0, DPR, 0, 0); ctx.clearRect(0, 0, W, H);
if (!f) { drawFloor(); return; }
if (Math.hypot(f.tr[0] - cam.tx, f.tr[1] - cam.tz) > 3) { cam.tx = f.tr[0]; cam.tz = f.tr[1]; }
cam.tx += (f.tr[0] - cam.tx) * 0.15; cam.tz += (f.tr[1] - cam.tz) * 0.15;
drawFloor();
ctx.strokeStyle = css("--trail"); ctx.lineWidth = 2; ctx.beginPath(); let started = false;
for (const t of trail) { const p = project(t[0], 0.001, t[1]); if (!p) continue;
if (!started) { ctx.moveTo(p[0], p[1]); started = true; } else ctx.lineTo(p[0], p[1]); }
ctx.stroke();
const c = { p: f.p, r: f.r, tr: f.tr }, pieces = [];
figurePieces(c, 0, css("--fig"), pieces);
pieces.sort((a, b) => b.depth - a.depth);
const F = Math.min(W, H) * 1.35;
for (const s of pieces) paintPiece(s, F);
const txt = "いまの文章: “" + f.text + "”";
if ($("now").textContent !== txt) $("now").textContent = txt;
}
const ptrs = new Map();
cv.addEventListener("pointerdown", e => { cv.setPointerCapture(e.pointerId); ptrs.set(e.pointerId, [e.clientX, e.clientY]); });
cv.addEventListener("pointermove", e => { if (!ptrs.has(e.pointerId)) return; const prev = ptrs.get(e.pointerId); ptrs.set(e.pointerId, [e.clientX, e.clientY]);
cam.yaw += (e.clientX - prev[0]) * 0.01; cam.pitch = Math.max(-0.2, Math.min(1.3, cam.pitch + (e.clientY - prev[1]) * 0.01)); });
const up = e => ptrs.delete(e.pointerId); cv.addEventListener("pointerup", up); cv.addEventListener("pointercancel", up);
cv.addEventListener("wheel", e => { e.preventDefault(); cam.dist = Math.max(1.5, Math.min(16, cam.dist * Math.exp(e.deltaY * 0.001))); }, { passive: false });
function resize() { DPR = Math.min(2, window.devicePixelRatio || 1); W = stage.clientWidth || 300; H = stage.clientHeight || 300;
cv.width = Math.round(W * DPR); cv.height = Math.round(H * DPR); }
new ResizeObserver(resize).observe(stage); resize();
let last = performance.now(), lastTrail = -1;
function loop(now) { const dt = Math.min(0.1, (now - last) / 1000); last = now;
if (!paused) {
const next = playF + dt * FPS;
if (next < have - 1) playF = next; else if (have > 0) stalled += dt;
const fi = Math.floor(playF);
if (fi !== lastTrail && fi % 2 === 0) { const f = frameAt(fi); if (f) { trail.push(f.tr); if (trail.length > 900) trail.shift(); } lastTrail = fi; }
const drop = Math.floor(playF) - bufStart - 60; if (drop > 300) { buf.splice(0, drop); bufStart += drop; }
}
drawLive(); requestAnimationFrame(loop); }
poll(); requestAnimationFrame(loop);
</script>