// Scene images are checked against independent renders of the resolved scene and its passes. // Godot and Tiled then read the actual resource files alongside the textures they reference. import assert from 'node:assert/strict'; import { buildAtlas } from '../src/core.js'; import { sceneHaze, scenePlanes, renderScene, wholeDraws, sceneImage } from '../src/scene.js'; import { encodePNG } from '../src/png.js'; import { createSceneBundle } from '../src/scene-export.js'; import { compileAction } from '../src/actions.js'; import { normalsYUp } from '../src/godot.js'; import { engineInputs } from './read-delivery-engines.mjs'; import { still } from './example-delivery.mjs'; import { checkEdit } from './round-trip-example.mjs'; import { sceneMovie, numberedClip } from './example-playback.mjs'; function planeImages(scene, alignment, unlit) { const labels = scene.instances.map(p => p.name ?? p.asset); const sprites = i => { const p = scene.instances[i], project = scene.assets[p.asset].recipe; const frames = project.animations[p.animation]?.frames ?? []; if (frames.length < 2 || p.hidden || p.tilemap || p.shade || p.sequence?.length || p.trajectory || p.attach || Array.isArray(p.lift)) return false; return p.anchor !== 'frame' || new Set(frames.map(f => String(project.frames[f].anchor ?? [0, 0]))).size === 1; }; const { draws } = wholeDraws(scene, unlit.draws, sprites); const planes = []; let background = []; function finishRun() { planes.push(...scenePlanes(scene, background).planes); background = []; } for (const draw of draws) { if (sprites(draw.instance)) finishRun(); else background.push(draw); } finishRun(); assert.equal(planes.length, alignment.planes.length); return Object.fromEntries(planes.map((p, i) => { const record = alignment.planes[i], file = 'planes/' + i + '.png'; assert.equal(record.image, file); assert.deepEqual(record.at, p.at); assert.deepEqual(record.placements, p.instances.map(j => labels[j])); return [file, still(p)]; })); } export async function sceneDelivery(delivery, document, original, engines, id) { const { scene, files } = await createSceneBundle(document); const unlit = renderScene(scene, { lit: false, draws: true }), lit = renderScene(scene); const alignment = JSON.parse(files.get('alignment.json')); assert.deepEqual(JSON.parse(files.get('scene.json')), document); const pictures = { 'scene.png': still(lit), 'unlit.png': still(unlit), ...planeImages(scene, alignment, unlit) }; const required = ['scene.tmx', 'scene.tscn', 'alignment.json', 'review.json', 'scene.json', 'preview.html', 'player/scene-player.js', 'player/scene.css']; const haze = sceneHaze(scene); for (const [i, [key, asset]] of Object.entries(scene.assets).entries()) { const folder = 'assets/' + i, info = alignment.assets[key]; assert.equal(info.directory, folder); required.push(folder + '/tileset.tsx', folder + '/sprite_frames.tres', folder + '/atlas.json'); for (const [pass, project] of Object.entries(asset)) { const atlas = buildAtlas(project, { trim: false }); const name = pass === 'recipe' ? 'color' : pass; assert.equal(info.passes[name], folder + '/' + name + '.png'); pictures[info.passes[name]] = still(atlas); if (pass === 'normal') { required.push(folder + '/canvas_texture.tres'); pictures[folder + '/normal_y_up.png'] = still({ ...atlas, data: normalsYUp(atlas.data) }); } if (pass === 'recipe') { const amounts = [...new Set(scene.instances.filter(p => p.asset === key && p.haze > 0).map(p => p.haze))].sort((a, b) => a - b); assert.deepEqual((info.haze ?? []).map(h => h.amount), amounts); for (const amount of amounts) { const dir = folder + '/haze-' + amount; const data = Uint8Array.from(atlas.data); for (let j = 0; j < data.length; j += 4) if (data[j + 3]) data.set(haze(amount, data.subarray(j, j + 3)), j); pictures[dir + '/color.png'] = still({ ...atlas, data }); required.push(dir + '/tileset.tsx', dir + '/sprite_frames.tres'); } } } } for (const name of required) assert.ok(files.has(name), name); const expected = new Set([...required, ...Object.keys(pictures)]); if (alignment.tileset) expected.add('tileset.tres'); if (alignment.isoTileset) expected.add('tileset_iso.tres'); for (const name of files.keys()) { // A standalone scene ships the browser modules imported by its player. if (/^player\/[\w-]+\.js$/.test(name)) expected.add(name); } assert.deepEqual([...files.keys()].sort(), [...expected].sort(), 'scene bundle members'); const resources = await engineInputs(engines, id, files); await delivery.bundle(files, pictures); await delivery.interchange(original, document, files.get('scene.png'), pictures['scene.png']); for (const mode of ['grayscale', 'depth']) { const view = sceneImage(renderScene(scene, { depth: mode === 'depth' }), { mode }); await delivery.image('views/' + mode + '.png', encodePNG(view.data, view.width, view.height), still(view)); } await sceneMovie(delivery, scene); await numberedClip(delivery, document, { scene }); const [key, first] = Object.entries(document.assets)[0]; const asset = first.recipe ?? first; const editable = asset.format ? compileAction(asset).recipe : asset; checkEdit(editable, scene.assets[key].recipe, delivery.restrictions); return resources; }