// 3D preview (viewer-core.js), its toolbar and the side column (light / expressions / tune / info). import { BONE_COLORS, Viewer } from '/viewer/viewer-core.js'; import { $, S, api, errText, esc, t, toast } from './core.js'; import { renderFiles } from './files.js'; import { flushOpts } from './options.js'; import { T, leaveTune, loadTune, springParams } from './tune.js'; export const viewer = new Viewer($('pv')); export let modelUrl = null; viewer.onLoad = (info) => { fillPreviewPanels(info); fillLegend(); // reload after saving tune edits keeps the camera if (S.keepView) { viewer.camera.position.copy(S.keepView.pos); viewer.controls.target.copy(S.keepView.tgt); viewer.controls.update(); S.keepView = null; } else viewer.frame(document.querySelector('[data-frame].on').dataset.frame); if (T.fid === S.current && T.info && T.info.ready && T.dirty) viewer.setSpringParams(springParams()); }; export function showBusy(text) { $('pvBusy').hidden = !text; $('pvBusyText').textContent = text || ''; } export async function previewFile(id, output) { if (!leaveTune(id)) return; S.current = id; renderFiles(); // the tune panel opens with the preview once there is something to tune (a converted / preview VRM), // unless another panel (light / expressions / info) is open S.autoTune = !openPanel(); loadTune(); await flushOpts(); const r = await api('/api/preview', output === undefined ? { id } : { id, output }); if (r.url) loadModel(r.url, r.name); else if (r.pending) showBusy(t('pvMaking')); else if (r.error) toast(errText(r.error, r.error_en)); } export async function loadModel(url, name, keepView) { showBusy(t('pvLoading')); S.keepView = keepView && viewer.loaded ? { pos: viewer.camera.position.clone(), tgt: viewer.controls.target.clone() } : null; try { await viewer.loadUrl(url, name); modelUrl = url; $('pvEmpty').hidden = true; $('pvHint').hidden = false; $('pvTitle').textContent = name; if (!T.dirty) loadTune(); // the Tune panel shows the model on screen (an archive can hold several) } catch (e) { toast(t('pvFailed', { err: e.message })); } showBusy(null); } // ---- side column: one panel at a time; the preview narrows instead of being covered const PANELS = { lightPanel: 'lightBtn', exprPanel: 'exprBtn', tunePanel: 'tuneBtn', infoPanel: 'infoBtn' }; export const openPanel = () => Object.keys(PANELS).find((p) => !$(p).hidden) || null; export function showPanel(id) { for (const [p, btn] of Object.entries(PANELS)) { $(p).hidden = p !== id; $(btn).classList.toggle('on', p === id); $(btn).setAttribute('aria-pressed', p === id); } $('side').hidden = !id; if (id !== 'tunePanel') viewer.highlightChain(null); $('splitSide').hidden = !id; $('side').classList.toggle('wide', id === 'tunePanel'); } for (const [p, btn] of Object.entries(PANELS)) { $(btn).onclick = () => { S.autoTune = false; const open = $(p).hidden; showPanel(open ? p : null); if (open && p === 'tunePanel') loadTune(); }; } // ---- info + expression panels export function fillPreviewPanels(info) { if (!info) return; const rows = info.spec === 'PMX' ? [[t('iFormat'), 'PMX'], [t('iHeight'), info.height.toFixed(1) + t('iMmdUnit')], [t('iTris'), info.triangles.toLocaleString()], [t('iMats'), info.materials], [t('iTex'), info.textures], [t('iMorphs'), info.expressions.length], [t('iPhysics'), info.rigidBodies ? t('iRigid', { r: info.rigidBodies, j: info.constraints }) + (info.physics ? '' : t('iOff')) : '—']] : [['VRM', info.spec], [t('iHeight'), info.height.toFixed(2) + ' m'], [t('iHumanoid'), info.humanBones], [t('iTris'), info.triangles.toLocaleString()], [t('iMats'), info.materials], [t('iTex'), info.textures], [t('iExpr'), info.expressions.length], ['SpringBone', info.springs ? t('iChains', { n: info.springs, j: info.joints }) : '—'], [t('iColliders'), info.colliders]]; if (info.bones) rows.splice(info.spec === 'PMX' ? 1 : 2, 0, [t('iBones'), t('iBonesV', { n: info.bones.total, u: info.bones.unused })]); $('infoList').innerHTML = rows.map(([k, v]) => `