/* ((Triforce, 11-29) The Character page: Link in 3D (char3d.js) from the player's own game, young or adult (Kokiri, Goron or Zora tunic), and his colors (launcher/App/Backend.Character.cs, launcher/Logic/Character/LinkColors.cs). The launcher sends state.char: game, tool {have, busy, frac, size}, parts [{id, label, group, ages, tunic, swatch}], colors {child, adult} (the picks), applied {child, adult} | null (what the game has), appliedOn, same, busy, line, calBusy, others; and the messages charLook {look, n, error?} (the look's files are ready), charCal {maps [{tex, file, parts, yref}]} (where each part lies on each colour map) and charExact {age, n, colors, tex} (OOTRR's own recoloured maps for those colours). A pick shows at once as a tint over the calibration mask, then OOTRR's exact map takes its place. Uses app.js's $, $$, S, ACTIONS, cmd, sfx, esc, toast and its colour helpers. */ 'use strict'; const CH = { age: 'child', tunic: 'kokiri', open: null, h: 0, s: 0, v: 0, viewer: null, look: null, want: null, n: 0, have: new Set(), sceneTex: new Set(), loading: false, error: '', cal: null, masks: new Set(), exact: { child: null, adult: null }, cmKey: new Map(), colors: { child: {}, adult: {}, 'linkle:child': {}, 'linkle:adult': {} }, touchedAt: 0, sendT: 0, face: false, hero: 'link', }; /* swatches in the picker: the game's tunics first, then a spread */ const CH_SWATCH = [['#2f8f3a', 'Kokiri green'], ['#b3262c', 'Goron red'], ['#2a4fb0', 'Zora blue'], ['#1f5e2e', 'Deep forest'], ['#7d1622', 'Crimson'], ['#1c2a52', 'Midnight'], ['#4b2a84', 'Royal purple'], ['#2a8f8a', 'Lake teal'], ['#d4a634', 'Triforce gold'], ['#c8641e', 'Ember'], ['#d86a9a', 'Fairy pink'], ['#ebe7de', 'Snow'], ['#a7abb3', 'Silver'], ['#6b4a2b', 'Leather'], ['#2b2b2e', 'Charcoal'], ['#121214', 'Shadow']]; /* whole looks: each names what it changes; Original takes every part back to the game's colour */ const CH_LOOKS = [ { id: 'original', name: 'Original', c: {} }, { id: 'midnight', name: 'Midnight', c: { kokiri: '#1f2b4d', kokiri_pants: '#2b3352', kokiri_shirt: '#cfc8b8', kokiri_belt: '#3b2a1f', leather_trim: '#8a1f2b', boots: '#2a1d15', gloves: '#2a1d15', feathers: '#e9e5dc', goron: '#33204a', goron_trim: '#c9a24a', zora: '#1c2448', zora_shirt: '#cfd3da' } }, { id: 'crimson', name: 'Crimson', c: { kokiri: '#8e1b22', kokiri_pants: '#5a1a1a', kokiri_shirt: '#e6dcc8', kokiri_belt: '#3b2416', leather_trim: '#d4a634', boots: '#3b2416', gloves: '#3b2416', feathers: '#f2ead8', zora: '#7a1a2a' } }, { id: 'royal', name: 'Royal', c: { kokiri: '#3b2a7a', kokiri_pants: '#2e2458', kokiri_shirt: '#ece3cf', kokiri_belt: '#4a2f1d', leather_trim: '#d4a634', feathers: '#f5e6b0', goron: '#4b2a84', goron_trim: '#e0b84a', zora: '#3b2a7a' } }, { id: 'snow', name: 'Snow', c: { kokiri: '#e8e6df', kokiri_pants: '#cfcac0', kokiri_shirt: '#f4f1ea', kokiri_belt: '#8a6a4a', leather_trim: '#b8963e', boots: '#e0dbd0', gloves: '#e0dbd0', feathers: '#ffffff', goron: '#e8e6df', zora: '#e8eef4' } }, { id: 'shadow', name: 'Shadow', c: { kokiri: '#1b1b1e', kokiri_pants: '#222226', kokiri_shirt: '#3a3a3e', kokiri_belt: '#141414', leather_trim: '#6a0f18', boots: '#111113', gloves: '#111113', feathers: '#2a2a2e', hair: '#e8e8ea', goron: '#1b1b1e', zora: '#1b1b1e' } }, { id: 'random', name: 'Random', c: null }, ]; function chS() { return S.char || {}; } function chLook() { return CH.age === 'child' ? 'child' : CH.tunic === 'kokiri' ? 'adult' : 'adult-' + CH.tunic; } /* a colour set's key: the age, "linkle:" in front for her */ function chKey(age) { return (CH.hero === 'linkle' ? 'linkle:' : '') + (age || CH.age); } function chCur() { return CH.colors[chKey()] || (CH.colors[chKey()] = {}); } function chLinkle() { return chS().linkle || {}; } function chHeroName() { return CH.hero === 'linkle' ? 'Linkle' : 'Link'; } function chParts() { return (chS().parts || []).filter(p => p.ages.split(',').includes(CH.age) && (CH.age === 'child' || !p.tunic || p.tunic === CH.tunic)); } function chLin(hex) { return hexRgb(hex).map(x => { x /= 255; return x <= .04045 ? x / 12.92 : ((x + .055) / 1.055) ** 2.4; }); } /* ---------------- the stage ---------------- */ function chViewer() { if (CH.viewer) return CH.viewer; const c = $('#chCanvas'); try { CH.viewer = window.Char3D && Char3D.create(c); } catch (e) { CH.viewer = null; console.error(e); } if (!CH.viewer) { CH.error = 'This PC\'s browser engine has no WebGL 2, so Link cannot be drawn here. Colors still apply.'; drawChar(); } new ResizeObserver(() => CH.viewer && CH.viewer.resize()).observe(c); return CH.viewer; } function chLoad() { const look = CH.hero + '/' + chLook(); if (CH.look === look || CH.want === look || !chS().game || !chViewer()) return; if (CH.hero === 'linkle' && !chLinkle().have) return; CH.want = look; CH.loading = true; CH.error = ''; drawChar(); cmd('char.look', { look: chLook(), have: [...CH.have] }); } async function chFetchTex(name) { const r = await fetch('/char/tex/' + encodeURIComponent(name) + '.bin'); if (!r.ok) return null; const b = await r.arrayBuffer(), d = new DataView(b); CH.have.add(name); return { w: d.getUint32(0, true), h: d.getUint32(4, true), data: new Uint8Array(b, 8) }; } async function chFetchBin(url) { const r = await fetch(url); if (!r.ok) return null; const b = await r.arrayBuffer(), d = new DataView(b); return { w: d.getUint32(0, true), h: d.getUint32(4, true), data: new Uint8Array(b, 8) }; } async function chOnLook(m) { const want = (m.hero || 'link') + '/' + m.look; if (want !== CH.want) return; if (m.error) { CH.want = null; CH.loading = false; CH.error = m.error; drawChar(); return; } try { const info = await (await fetch('/char/scene/' + m.n + '.json')).json(); const mesh = await (await fetch('/char/scene/' + m.n + '.bin')).arrayBuffer(); if (want !== CH.want) return; await CH.viewer.load(info, mesh, chFetchTex); cmd('char.loaded', { n: m.n }); CH.look = want; CH.want = null; CH.loading = false; CH.sceneTex = new Set(info.parts.map(p => p.alb).filter(Boolean)); CH.cmKey.clear(); CH.viewer.clearColorMaps(); chTint(); } catch (e) { CH.want = null; CH.loading = false; CH.error = 'Link could not be drawn: ' + e.message; } drawChar(); } /* ---------------- colours on the model: OOTRR's exact map when it matches the picks, else the live tint ---------------- */ function chTint() { const v = CH.viewer; if (!v || !CH.look) return; const cur = chCur(), ex = CH.exact[chKey()]; v.setNaviColor(cur.navi || (CH.hero === 'linkle' ? '#e8f070' : null)); const same = (parts, colors) => parts.every(p => (cur[p] || null) === ((colors || {})[p] || null)); const done = new Set(); for (const [tex, c] of (CH.hero === 'link' && CH.cal || new Map())) { if (!CH.sceneTex.has(tex)) continue; done.add(tex); if (ex && same(c.parts, ex.colors)) { v.setTint(tex, null); chUseExact(tex, ex); continue; } chUseExact(tex, null); const slots = c.parts.map((p, i) => cur[p] ? chTintSlot(cur[p], c.ym[i], c.qm[i], c.ty[i], c.g ? c.g[i] : 1) : null); if (slots.some(Boolean) && chMask(tex, c.file)) v.setTint(tex, slots); else v.setTint(tex, null); } /* (no calibration yet: the exact maps alone, when they match every pick of this age) */ if (ex) for (const tex of ex.tex) if (!done.has(tex) && CH.sceneTex.has(tex)) chUseExact(tex, Object.keys(cur).length === Object.keys(ex.colors).length && Object.keys(cur).every(k => cur[k] === ex.colors[k]) ? ex : null); } /* a part's live tint, as OOTRR's dye comes out: [r, g, b] the colour at the part's median texel (linear), ym that texel's luminance, and the contrast g. OOTRR compresses brightness, so the median texel comes out near qm * (Y / ty)^0.62, mixes the colour toward its own grey (measured 11-30 at the median texel, linear: 26% for #FF0000, 36% for #2050C0 on young Link's belt; 30% here), and scales the shading by (y / ym)^g, flattened at the top (char3d.js). 11-30: the tint was brighter than OOTRR's map, and painted the buckle and ring, so a pick seemed to switch when the exact map came */ function chTintSlot(hex, ym, qm, ty, g) { const c = chLin(hex), y = Math.max(1e-4, .2126 * c[0] + .7152 * c[1] + .0722 * c[2]); const out = qm * Math.pow(y / ty, .62), k = out / y; return c.map(x => (x * .7 + y * .3) * k).concat(Math.max(1e-4, ym), g || 1); } function chUseExact(tex, ex) { const key = ex && ex.tex.includes(tex) ? chKey() + '/' + ex.n : ''; if ((CH.cmKey.get(tex) || '') === key) return; CH.cmKey.set(tex, key); if (!key) { CH.viewer.setColorMap(tex, null); return; } chFetchBin('/char/exact/' + chKey() + '/' + ex.n + '/' + encodeURIComponent(tex) + '.bin').then(t => { if (CH.cmKey.get(tex) === key && t) CH.viewer.setColorMap(tex, t); }); } function chMask(tex, file) { if (CH.masks.has(tex)) return true; CH.masks.add(tex); chFetchBin('/char/mask/' + encodeURIComponent(file) + '.bin').then(t => { if (t) { CH.viewer.setMask(tex, t); chTint(); } else CH.masks.delete(tex); }); return false; } /* ---------------- picks ---------------- */ function chNaviBoth() { const h = chCur().navi, o = CH.colors[chKey(CH.age === 'child' ? 'adult' : 'child')] || (CH.colors[chKey(CH.age === 'child' ? 'adult' : 'child')] = {}); if (h) o.navi = h; else delete o.navi; } function chSet(part, hex, final) { const c = chCur(); if (hex) c[part] = hex; else delete c[part]; if (part === 'navi') chNaviBoth(); CH.touchedAt = performance.now(); chTint(); if (final) drawChar(); else chPatchRow(part); chDyeing(); clearTimeout(CH.sendT); const go = () => cmd('char.set', { age: CH.age, part, hex: hex || null }); if (final) go(); else CH.sendT = setTimeout(go, 220); } function chMany(colors) { const c = chCur(); for (const k in colors) { if (colors[k]) c[k] = colors[k]; else delete c[k]; } if ('navi' in colors) chNaviBoth(); CH.touchedAt = performance.now(); chTint(); drawChar(); cmd('char.many', { age: CH.age, colors }); } function chRandom() { const out = {}, base = Math.random() * 360; chParts().forEach((p, i) => { const h = (base + (p.group === 'Tunic' ? 0 : p.group === 'Leather' ? 25 + Math.random() * 30 : Math.random() * 360) + (i % 3) * 8) % 360; const s = p.group === 'Leather' ? .35 + Math.random() * .3 : .45 + Math.random() * .45; const v = p.group === 'Leather' ? .22 + Math.random() * .25 : .35 + Math.random() * .5; out[p.id] = rgbHex(hsvRgb(h, s, v)); }); return out; } /* ---------------- drawing the page ---------------- */ function drawChar() { if (!$('#s-char')) return; const st = chS(); // (the launcher's picks are taken unless one was made here a moment ago and is still on its way) if (st.colors && performance.now() - CH.touchedAt > 1500) { CH.colors = {}; for (const k of ['child', 'adult', 'linkle:child', 'linkle:adult']) CH.colors[k] = Object.assign({}, st.colors[k] || {}); } if (st.hero && st.hero !== CH.hero) chHeroSwitch(st.hero); $$('#chHeroes span').forEach(s => s.classList.toggle('on', s.dataset.act === 'chhero:' + CH.hero)); $$('#chAges span').forEach(s => { s.textContent = (s.dataset.act === 'chage:child' ? 'Young ' : 'Adult ') + chHeroName(); }); $$('#chAges span').forEach(s => s.classList.toggle('on', s.dataset.act === 'chage:' + CH.age)); $('#chTunics').style.visibility = CH.age === 'adult' ? 'visible' : 'hidden'; $$('#chTunics span').forEach(s => s.classList.toggle('on', s.dataset.act === 'chtunic:' + CH.tunic)); $('#chTitle').textContent = (CH.age === 'child' ? 'Young ' : 'Adult ') + chHeroName() + '’s colors'; $('#chFace').textContent = CH.face ? 'Full body' : 'Face'; const wait = $('#chWait'); const lk = chLinkle(); const noLinkle = CH.hero === 'linkle' && st.game && !lk.have; const waitText = !st.game ? 'Build the game first: Link is read from it' : noLinkle ? '' : CH.error || (CH.loading ? 'Reading ' + chHeroName() + ' from your files' : ''); wait.classList.toggle('on', !!waitText); wait.classList.toggle('err', !!(CH.error || !st.game)); $('#chWaitT').textContent = waitText; const get = $('#chGet'); get.classList.toggle('on', !!noLinkle); if (noLinkle) get.innerHTML = `
${esc(lk.name || 'Playable Linkle')} by ${esc(lk.author || '')}: Linkle from Hyrule Warriors, young and adult, with her own voice, gear and fairy, Liri. A mod from GameBanana, about 320 MB.
` + (lk.busy ? `${esc(lk.line || 'Downloading')}
` : `Liri is one fairy for both ages. Linkle is ${esc(chLinkle().name || 'a mod')} by ${esc(chLinkle().author || '')}.
` : 'Navi is one fairy for both ages: her color goes with young and adult Link.
'; // (the list is drawn again only when what it shows changed: a pick while dragging moves the marks, not the whole list) if (list.dataset.html !== html) { const top = list.scrollTop; list.innerHTML = html; list.dataset.html = html; list.scrollTop = top; if (keep && $('#' + keep)) { focused = null; setFocus($('#' + keep)); } } chDrawPicker(); } function chPicker(p, hex, set) { return `Build the game first: the colors go into its files.
'; else if (!t.have) html = t.busy ? `Getting the color tool… ${Math.round((t.frac || 0) * 100)} %
` : `Picks show here now. To put them in your game, Triforce uses the randomizer’s color tool (OOTRR, ${esc(t.size || '5 MB')}, once).
${esc(st.line || 'Working')}… about half a minute
Over ${esc(st.others.join(', '))}, which changes Link too.
` : ''; html = `${msg}
${others}