A lightweight 2.5D character player for the web, with lip sync and webcam face tracking.
Examples: a dropped download no longer leaves the page empty
LivLiv223c52921291
diff --git a/example/demo.html b/example/demo.htmlindex da7d3f9..e327c52 100644--- a/example/demo.html+++ b/example/demo.html@@ -17,11 +17,10 @@<div class="bar" id="bar"></div><p id="status" role="status"></p>-<script>if (!/[?&]model=/.test(location.search)) document.head.append(Object.assign(document.createElement('link'), { rel: 'preload', as: 'fetch', crossOrigin: 'anonymous', href: '../characters/d03ft.livliv' }))</script><script type="module">import { Player } from '../dist/index.js'import { cameraButton } from './camera.js'- import { loading, needWebGL } from './loading.js'+ import { loadWithFallback, loading, needWebGL } from './loading.js'needWebGL(Player)const player = new Player(document.getElementById('stage'))@@ -38,7 +37,7 @@// a featured character's small picture lies in its folder, beside its packageconst done = loading(document.body, typeof source === 'string' && /(\/|\.livliv)$/.test(source) ? `${source.replace(/\.livliv$/, '/')}avatar.webp` : null)try {- const m = await player.load(source)+ const m = await loadWithFallback(player, source)status.textContent = `${m.name} · ${m.format} ${m.version} · mark: ${m.watermark ?? 'none'}${m.meta?.license ? ` · ${m.meta.license.id}` : ''}`controls(m)} catch (e) {diff --git a/example/gallery.html b/example/gallery.htmlindex 221b2c5..1c41845 100644--- a/example/gallery.html+++ b/example/gallery.html@@ -28,15 +28,9 @@</div><ul class="cast" id="cast"></ul></main>-<script>- // the first character's package starts downloading before the script below- const first = new URLSearchParams(location.search).get('id')- document.head.append(Object.assign(document.createElement('link'), { rel: 'preload', as: 'fetch', crossOrigin: 'anonymous',- href: `../characters/${first && /^[\w-]+$/.test(first) ? first : 'd03ft'}.livliv` }))-</script><script type="module">import { Player } from '../dist/index.js'- import { loading, needWebGL } from './loading.js'+ import { loadWithFallback, loading, needWebGL } from './loading.js'// All the featured characters: one plays on the stage (one player, one WebGL context), the rest wait as their// pictures; a click on one puts it on the stage. ?id=<id> starts with that one (else Refined Nana); ?demo=1 plays expressions and@@ -72,7 +66,7 @@const done = loading(document.getElementById('view'), `../characters/${id}/avatar.webp`)let mtry {- m = await player.load(`../characters/${id}.livliv`).finally(done)+ m = await loadWithFallback(player, `../characters/${id}.livliv`).finally(done)} catch (e) { // the one on the stage stays; a click can try this one againif (id === current) { current = before; press(before); name.textContent = e.message }returndiff --git a/example/index.html b/example/index.htmlindex 09e5b5e..90b92e7 100644--- a/example/index.html+++ b/example/index.html@@ -6,15 +6,15 @@<style>html, body { margin: 0; height: 100%; background: #20222b } canvas { display: block; width: 100%; height: 100% }</style><canvas id="stage"></canvas><p id="wait" style="position:fixed;inset:45% 0 auto;margin:0;color:#eef;font:14px system-ui,sans-serif;text-align:center">Loading…</p>-<link rel="preload" as="fetch" crossorigin href="../characters/d03ft.livliv"><script type="module">import { Player } from '../dist/index.js'+ import { loadWithFallback } from './loading.js'// a featured character; your own export works the sameconst wait = document.getElementById('wait')if (!Player.isSupported()) { wait.textContent = 'This browser has no WebGL2'; throw new Error('no WebGL2') }const player = new Player(document.getElementById('stage'))- const model = await player.load('../characters/d03ft.livliv').catch((e) => { wait.textContent = e.message; throw e })+ const model = await loadWithFallback(player, '../characters/d03ft.livliv').catch((e) => { wait.textContent = e.message; throw e })wait.remove()// each click shows the next expression, then neutral againdiff --git a/example/loading.js b/example/loading.jsindex 0ff7104..65e1634 100644--- a/example/loading.js+++ b/example/loading.js@@ -25,3 +25,13 @@ export function needWebGL(Player) {document.body.append(note)throw new Error('no WebGL2')}++/** Loads a model, with a featured character's folder as the second chance when its package (one big file) does not come. */+export async function loadWithFallback(player, source) {+ try {+ return await player.load(source)+ } catch (e) {+ if (typeof source !== 'string' || !source.endsWith('.livliv')) throw e+ return player.load(source.replace(/\.livliv$/, '/')).catch(() => { throw e })+ }+}diff --git a/src/index.ts b/src/index.tsindex baa5204..16eff57 100644--- a/src/index.ts+++ b/src/index.ts@@ -352,11 +352,21 @@ function explain(e: unknown, where: string): Error {return new Error(msg.startsWith('LivLiv') ? msg : `LivLiv: ${msg}`)}-/** a .livliv package from a URL, as a Blob */+/** a .livliv package from a URL, as a Blob. One file is one connection to lose where a folder has dozens, and a dropped one+ * breaks in the body as often as at the start: tried once more, unless the server said no (4xx). What the page gets is+ * our words, never the browser's ("Failed to fetch", "Load failed") */async function fetchPackage(url: string): Promise<Blob> {- const r = await fetch(url).catch((e) => { throw explain(e, url) })- if (!r.ok) throw explain(Object.assign(new Error(`HTTP ${r.status}`), { status: r.status }), url)- return r.blob()+ for (let again = true; ; again = false) {+ try {+ const r = await fetch(url)+ if (!r.ok) throw Object.assign(new Error(`HTTP ${r.status}`), { status: r.status })+ return await r.blob()+ } catch (e) {+ const status = (e as { status?: number }).status+ if (status && status < 500) throw explain(e, url)+ if (!again) throw status ? explain(e, url) : new Error(`LivLiv: could not load ${url}: unreachable or the connection dropped, or on another origin that sends no CORS header`)+ }+ }}/** a model from files: model.json (the shallowest one) and its images by their paths relative to it, and its keyformsdiff --git a/test/smoke.html b/test/smoke.htmlindex 1a4c75a..db94e7e 100644--- a/test/smoke.html+++ b/test/smoke.html@@ -158,10 +158,10 @@const { createTexture, deleteTexture } = glgl.createTexture = function () { made++; return createTexture.call(gl) }gl.deleteTexture = function (t) { if (t) freed++; return deleteTexture.call(gl, t) }- const load = p.load(`../characters/${id}/`).then(() => 'resolved', (e) => e.message)- await new Promise((r) => setTimeout(r, 5))- p.dispose()- out.load = await load+ // disposed just as the images start coming in (a timer could lose the race to a quick load)+ const loadModel = p.puppet.loadModel.bind(p.puppet)+ p.puppet.loadModel = (...args) => { p.dispose(); return loadModel(...args) }+ out.load = await p.load(`../characters/${id}/`).then(() => 'resolved', (e) => e.message)p.resize()await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))const px = new Uint8Array(4)@@ -192,6 +192,16 @@return out}+ /** a package's connection dropping (the flaky routes of smoke.mjs): once, it is tried again and plays; always, the page+ * gets our words, not the browser's; a 404 is not tried again */+ window.runDropped = async (id) => {+ const p = new Player(canvas, { autoplay: false, followPointer: false })+ const tell = (route) => p.load(route).then((m) => `played ${m.name}`, (e) => e.message)+ const out = { once: await tell(`/__dropped/once/${id}.livliv`), always: await tell(`/__dropped/always/${id}.livliv`), missing: await tell('/__dropped/missing/x.livliv') }+ p.dispose()+ return out+ }+/** hostile packages: each must be refused (or, for broken keyforms, played on the rig alone), the model before kept */window.runHostile = async (id, kfId) => {const player = new Player(canvas, { autoplay: false, followPointer: false })diff --git a/test/smoke.mjs b/test/smoke.mjsindex 8243346..a563b41 100644--- a/test/smoke.mjs+++ b/test/smoke.mjs@@ -36,7 +36,22 @@ function bombPng(side = 9000) {chunk('IDAT', deflateSync(Buffer.alloc(side * (side + 1)))), chunk('IEND', Buffer.alloc(0))])}const bomb = bombPng()+/** requests per route of runDropped */+const dropped = {}const server = createServer((req, res) => {+ const drop = req.url.match(/^\/__dropped\/(once|always|missing)\/(.+)\.livliv$/)+ if (drop) {+ const [, how, id] = drop, n = (dropped[how] = (dropped[how] || 0) + 1)+ if (how === 'missing') { res.writeHead(404).end(); return }+ const b = readFileSync(join(chars, `${id}.livliv`))+ if (how === 'always' || n === 1) { // half of the file, then the connection goes+ res.writeHead(200, { 'content-type': 'application/octet-stream', 'content-length': b.length })+ res.write(b.subarray(0, b.length >> 1), () => res.destroy())+ return+ }+ res.writeHead(200, { 'content-type': 'application/octet-stream', 'content-length': b.length }).end(b)+ return+ }if (req.url === '/__tiny.vrm') { res.writeHead(200, { 'content-type': 'model/gltf-binary' }).end(vrm); return }if (req.url === '/__bomb.png') { res.writeHead(200, { 'content-type': 'image/png' }).end(bomb); return }const p = normalize(decodeURIComponent(new URL(req.url, 'http://x').pathname)).replace(/^([/\\])+/, '')@@ -90,6 +105,13 @@ try {if (!kfOk) failed++console.log(`${kfOk ? 'ok ' : 'FAIL'} broken keyforms: ${hostile.keyforms.refused ?? `${hostile.keyforms.name} plays on the rig alone`}`)+ // a package whose connection drops (see runDropped)+ const drops = await page.evaluate((id) => window.runDropped(id), ids[0])+ const dropsOk = /^played/.test(drops.once) && dropped.once === 2 && /could not load/.test(drops.always) && !/Failed to fetch/.test(drops.always) && dropped.always === 2+ && /HTTP 404/.test(drops.missing) && dropped.missing === 1+ if (!dropsOk) failed+++ console.log(`${dropsOk ? 'ok ' : 'FAIL'} a package's connection drops: once "${drops.once}" (${dropped.once} requests), always "${drops.always.slice(0, 60)}…" (${dropped.always}), a 404 "${drops.missing.slice(0, 50)}…" (${dropped.missing})`)+// disposing while something runs (see runLifecycle)const life = await page.evaluate((id) => window.runLifecycle(id), ids[0])const lifeOk = /disposed/.test(life.load) && life.model === null && life.drawn === 0 && life.freed >= life.made && life.glError === 0