const GENERIC_OBJECTS = [ ['Genericky_Sedacka2Mistna', 'Sedačka 2-místná'], ['Genericky_Sedacka3Mistna', 'Sedačka 3-místná'], ['Genericky_SedackaLLeva', 'Sedačka L levá'], ['Genericky_SedackaLPrava', 'Sedačka L pravá'], ['Genericky_KresloKompaktni', 'Křeslo kompaktní'], ['Genericky_KresloStredni', 'Křeslo střední'], ['Genericky_KresloUsak', 'Křeslo ušák'], ['Genericky_KonferencniStolekKulaty', 'Konferenční stolek kulatý'], ['Genericky_KonferencniStolekObdelnikovy', 'Konferenční stolek obdélníkový'], ['Genericky_TvSkrinka', 'TV skříňka'], ['Genericky_Skrin', 'Skříň'], ['Genericky_Regal', 'Regál'], ['Genericky_KomodaZakladni', 'Komoda'], ['Genericky_KomodaStylova', 'Komoda stylová'], ['Genericky_JidelniStulKulaty', 'Jídelní stůl kulatý'], ['Genericky_JidelniStulObdelnikovy', 'Jídelní stůl obdélníkový'], ['Genericky_PracovniStul', 'Pracovní stůl'], ['Genericky_Pricka', 'Příčka'], ['Genericky_Ostrov', 'Kuchyňský ostrůvek'] ]; const TINY_TEST_SCENE = {"_name":"Default","_rooms":[{"_size":{"x":6,"y":3,"z":6},"_position":{"x":0,"y":0,"z":0},"_rotation":0,"_objects":[{"_id":0,"_parentId":-2,"_name":"CeilingLight","_position":{"x":-1.25,"y":3.003000020980835,"z":1.250003695487976},"_rotation":{"x":0,"y":0,"z":0,"w":1.0000001192092896}},{"_id":1,"_parentId":-2,"_name":"CeilingLight","_position":{"x":1.25,"y":3.003000020980835,"z":-1.250003695487976},"_rotation":{"x":0,"y":0,"z":0,"w":1.0000001192092896}},{"_id":2,"_parentId":-2,"_name":"CeilingLight","_position":{"x":-1.25,"y":3.003000020980835,"z":-1.250003695487976},"_rotation":{"x":0,"y":0,"z":0,"w":1.0000001192092896}},{"_id":3,"_parentId":-2,"_name":"CeilingLight","_position":{"x":1.25,"y":3.003000020980835,"z":1.250003695487976},"_rotation":{"x":0,"y":0,"z":0,"w":1.0000001192092896}}],"_floor":"Parquet","_walls":{"_leftWall":{"r":255,"g":255,"b":255,"a":1},"_rightWall":{"r":255,"g":255,"b":255,"a":1},"_frontWall":{"r":255,"g":255,"b":255,"a":1},"_backWall":{"r":255,"g":255,"b":255,"a":1},"_ceiling":{"r":255,"g":255,"b":255,"a":1}}}]}; const MAT_TEST_LOAD_WAIT_MS = 2500; const DEV_SCENE_REFRESH_MS = 2500; var _matTestSnapshot = null; function initDevtools() { const panel = $('#devtools'); $('#devtools-toggle').addEventListener('click', () => setHidden(panel, !panel.classList.contains('hidden'))); $('#devtools-close').addEventListener('click', () => setHidden(panel, true)); const on = (id, handler) => $(id).addEventListener('click', handler); on('#dev-local-save', () => { _localSaveRequested = true; sendDev('GetSceneJson'); }); on('#dev-local-load', () => { let sceneJson = null; try { sceneJson = localStorage.getItem(LOCAL_SCENE_KEY); } catch (e) { sceneJson = null; } if (!sceneJson) { toast('No local save found.', 'error'); return; } devLoadJson(sceneJson); }); on('#dev-last-scene', () => { let sceneJson = null; try { sceneJson = currentRoom ? localStorage.getItem(LAST_SCENE_KEY_PREFIX + currentRoom.id) : null; } catch (e) { sceneJson = null; } if (!sceneJson) { toast('No locally kept scene for this room.', 'error'); return; } devLoadJson(sceneJson); }); on('#dev-tiny-scene', () => devLoadJson(JSON.stringify(TINY_TEST_SCENE))); on('#dev-room-popup', openLegacyRoomSizePopup); $('#dev-cap-60').addEventListener('change', e => sendDev('SetMaxFrameRate', e.target.checked ? 60 : 0)); on('#dev-resize-room-send', () => sendDev('ResizeRoom', $('#dev-resize-room').value.trim())); on('#dev-add-json', () => { const json = parseDevJson(); if (json !== null) { devAddJson(JSON.stringify(json)); } }); on('#dev-load-json', () => { const json = parseDevJson(); if (json !== null) { devLoadJson(JSON.stringify(json)); } }); on('#dev-last-json', () => { $('#dev-json').value = lastSceneJson || ''; }); on('#dev-mat-capture', matTestCapture); on('#dev-mat-roundtrip', matTestRoundTrip); on('#dev-mat-legacy', matTestLoadAsLegacy); on('#dev-mat-show', matTestShowJson); const genericList = $('#dev-generic-list'); GENERIC_OBJECTS.forEach(([addressableName, label]) => { genericList.appendChild(el('button', { type: 'button', class: 'dev-item', text: label + ' (' + addressableName + ')', onclick: () => addGenericObject(addressableName) })); }); on('#dev-create', () => { const createParams = { x: 0, y: 0, z: 0, addressableName: $('#dev-create-addressable').value, accesoryId: $('#dev-create-accesory').value, url: $('#dev-create-url').value, snapType: parseInt($('#dev-create-snaptype').value, 10) }; devCreate(createParams); }); on('#dev-object-keys', () => { if (offlineMode) { sendDev('GetObjectKeysByFilter', 'Ikea;Bonami;Lindby;TestManufactorer2'); } else { sendDev('GetObjectKeys'); } }); on('#dev-window-keys', () => sendDev('GetObjectKeysByFilter', 'window;door')); on('#dev-pick-glb', () => $('#glbInput').click()); $('#glbInput').addEventListener('change', onGlbPicked); on('#dev-load-model', () => sendDev('LoadModel', $('#dev-model-url').value.trim())); on('#dev-load-texture', () => sendDev('LoadTexture', $('#dev-texture-url').value.trim())); on('#dev-change-texture-send', changeTexture); on('#dev-mirror', () => sendDev('Mirror')); on('#dev-panning', () => sendDev('Panning')); on('#dev-deselect', () => sendDev('Deselect')); on('#dev-colliders', () => sendDev('SwitchColliders')); on('#dev-group-individual', () => sendDev('ToggleGroupIndividualMode')); on('#dev-enable-input', () => sendDev('EnableInput')); on('#dev-transform-mode', () => sendDev('TransformMode')); on('#dev-fullscreen', () => { if (applicationInstance) { applicationInstance.SetFullscreen(1); } }); document.querySelectorAll('[data-change-rotation]').forEach((button) => { button.addEventListener('click', () => sendDev('ChangeRotation', parseInt(button.dataset.changeRotation, 10))); }); $('#dev-wheel-zoom').addEventListener('change', (event) => { wheelZoomEnabled = event.target.checked; }); on('#dev-set-highlights', () => sendDev('SetHighlights', $('#dev-highlight').value)); on('#dev-clear-highlights', () => sendDev('ClearHighlights')); on('#dev-unity-login', () => { const user = $('#dev-unity-user').value; const pass = $('#dev-unity-pass').value; if (!user || !pass) { setStatus($('#dev-unity-login-status'), 'Username and password required.', 'error'); return; } if (!applicationInstance) { setStatus($('#dev-unity-login-status'), 'Unity not ready.', 'error'); return; } setStatus($('#dev-unity-login-status'), 'Logging Unity in…'); console.log('[DEV] -> Login (credentials hidden)'); applicationInstance.SendMessage('ApplicationAPI', 'Login', user + ';' + pass); $('#dev-unity-pass').value = ''; }); } function devLoadJson(json) { sendDev('LoadJson', json); refreshSceneAfterDevChange(); } function devAddJson(json) { sendDev('AddJson', json); refreshSceneAfterDevChange(); } function refreshSceneAfterDevChange() { setTimeout(() => sendDev('GetSceneJson'), DEV_SCENE_REFRESH_MS); } function parseDevJson() { const text = $('#dev-json').value; try { if (text.includes('\\"')) { return JSON.parse(JSON.parse('"' + text + '"')); } return JSON.parse(text); } catch (e) { toast('Invalid JSON: ' + e.message, 'error'); return null; } } function addGenericObject(addressableName) { devCreate({ x: 0, y: 0, z: 0, addressableName, accesoryId: addressableName, url: '', snapType: 1 }); } function openLegacyRoomSizePopup() { sendDev('DisableInput'); const dialog = openDialog('Enter room dimensions (dev, width;height;depth)'); const input = (placeholder) => el('input', { type: 'number', step: '0.01', placeholder }); const width = input('Width [m]'); const height = input('Height [m]'); const depth = input('Depth [m]'); dialog.body.appendChild(el('div', { class: 'stack-form' }, [ el('label', null, ['Width', width]), el('label', null, ['Height', height]), el('label', null, ['Depth', depth]), el('div', { class: 'form-actions' }, [el('button', { type: 'button', class: 'btn btn-primary', text: 'Confirm', onclick: () => { if (!width.value || !height.value || !depth.value) { toast('Please set all dimensions', 'error'); return; } sendDev('ResizeRoom', `${width.value};${height.value};${depth.value}`); dialog.close(); } })]) ])); } async function onGlbPicked(event) { const file = event.target.files[0]; if (!file || !applicationInstance) { return; } const byteArray = new Uint8Array(await file.arrayBuffer()); const chunkSize = 64 * 1024; console.log('[DEV] -> GLBReceiver ' + file.name + ' (' + byteArray.length + ' bytes)'); for (let i = 0; i < byteArray.length; i += chunkSize) { applicationInstance.SendMessage('GLBReceiver', 'OnReceiveChunk', Array.from(byteArray.slice(i, i + chunkSize)).join(',')); } applicationInstance.SendMessage('GLBReceiver', 'OnReceiveFinished', byteArray.length.toString()); event.target.value = ''; } function changeTexture() { const input = $('#dev-change-texture').value.trim(); const textureId = parseInt(input, 10); if (!isNaN(textureId) && input === textureId.toString()) { sendDev('ChangeTextureById', textureId); return; } try { new URL(input); sendDev('ChangeTexture', input); } catch (e) { toast('Not a valid texture id or URL', 'error'); } } function matTestLog(message) { const log = $('#mat-test-log'); log.textContent = '[' + new Date().toLocaleTimeString() + '] ' + message + '\n' + log.textContent; } function forEachObject(scene, callback) { (scene._rooms || []).forEach((room) => (room._objects || []).forEach(callback)); } function matTestSceneMaterialCount(sceneJson) { const materials = JSON.parse(sceneJson)._materials; return Array.isArray(materials) ? materials.length : 0; } function matTestObjectMaterialCount(sceneJson) { let total = 0; forEachObject(JSON.parse(sceneJson), (object) => total += (object._materials || []).length); return total; } function matTestCollectChoices(sceneJson) { const byId = {}; forEachObject(JSON.parse(sceneJson), (object) => { byId[object._id] = (object._materials || []) .map((choice) => choice._variant + '=' + choice._index) .sort() .join(','); }); return byId; } async function matTestCapture() { _matTestSnapshot = await requestSceneJson(); matTestLog('Captured. scene-level _materials=' + matTestSceneMaterialCount(_matTestSnapshot) + ', per-object choices=' + matTestObjectMaterialCount(_matTestSnapshot) + '. Full JSON in console.'); console.log(_matTestSnapshot); } async function matTestRoundTrip() { if (!_matTestSnapshot) { matTestLog('Capture a scene first (step 1).'); return; } const before = matTestCollectChoices(_matTestSnapshot); sendDev('LoadJson', _matTestSnapshot); matTestLog('Reloaded captured snapshot, waiting ' + MAT_TEST_LOAD_WAIT_MS + 'ms for instantiation...'); await new Promise((resolve) => setTimeout(resolve, MAT_TEST_LOAD_WAIT_MS)); const after = matTestCollectChoices(await requestSceneJson()); const ids = new Set([...Object.keys(before), ...Object.keys(after)]); const mismatches = []; ids.forEach((id) => { if (before[id] !== after[id]) { mismatches.push(' id ' + id + ': "' + (before[id] ?? '') + '" -> "' + (after[id] ?? '') + '"'); } }); if (mismatches.length === 0) { matTestLog('ROUND-TRIP PASS: per-object materials identical across save/load (' + ids.size + ' objects).'); } else { matTestLog('ROUND-TRIP FAIL:\n' + mismatches.join('\n')); } } function matTestStripPerObject(sceneJson) { const scene = JSON.parse(sceneJson); forEachObject(scene, (object) => object._materials = []); return JSON.stringify(scene); } function matTestLoadAsLegacy() { if (!_matTestSnapshot) { matTestLog('Capture a scene first (step 1).'); return; } if (matTestSceneMaterialCount(_matTestSnapshot) === 0) { matTestLog('WARNING: snapshot has no scene-level _materials; the legacy fallback has nothing to apply.'); } devLoadJson(matTestStripPerObject(_matTestSnapshot)); matTestLog('Loaded snapshot with per-object _materials stripped (simulates an old save). ' + 'Scene must render via the global _materials fallback; per-instance differences collapse to one pick per variant. Verify visually.'); } function matTestShowJson() { if (!_matTestSnapshot) { matTestLog('Capture a scene first (step 1).'); return; } $('#dev-json').value = _matTestSnapshot; matTestLog('Captured JSON written to the json textarea and console.'); console.log(_matTestSnapshot); }