isle.pizza/src/core/assetLoader.js
Christian Semmler 9872902e4d
Some checks are pending
Build / build (push) Waiting to run
Actor editor (#24)
* Improve Lighthouse LCP and accessibility scores

Preload the LCP image (install.webp) from the HTML and add
fetchpriority="high" so the browser discovers it before JS executes.
Use a <main> landmark for the primary content container to satisfy
the "document has a main landmark" accessibility audit.

* Add actor editor with animated 3D character preview

Browse and customize all 66 game actors with a Three.js rendered preview
featuring skeletal walk cycle animations. Click interaction matches the
game's character-dependent behavior (Pepper=hat, Nick=color, etc.).

- Parse WDB global parts and global textures for character assembly
- Parse and serialize character data (66 entries x 16 bytes) in save files
- AnimationParser for .ani binary format with hierarchical keyframe evaluation
- Full g_cycles animation table (11 types x 17 animations) driven by move/sound
- Per-mesh texture support for hats, torso, and face textures

* Extract BaseRenderer to deduplicate actor and vehicle renderers

- Extract shared Three.js setup, lighting, texture, geometry, and
  animation loop code into BaseRenderer base class (~170 lines)
- Deduplicate WdbParser.parseGlobalParts via parsePartData delegation
- Consolidate lego brown/lt grey into shared LegoColors constant
- Remove dead code: updatePartColor, SUFFIX_NAMES, CharacterType,
  getCharacterType, partToLODIndex, unused imports and re-exports
- Simplify updateCharacter and resolve methods by removing unnecessary
  defensive checks on frozen data and bounded UI inputs
- Extract actorKey helper in ActorEditor to deduplicate key computation
- Delete unused animations/manifest.json

* Add reset to default button for actor editor

Compare each actor's character state against ActorInfoInit defaults
and show a reset button when any field differs. Resets all 10 fields
(sound, move, mood, hat, colors) in a single save round-trip by
extending updateSaveSlot to accept batch character updates.

* Show full character names in actor editor

Add ActorDisplayNames lookup with names from savegame.ksy doc comments
(e.g. "Pepper Roni", "Mama Brickolini") instead of internal IDs.
Widen nav label min-width to 150px to prevent button shifting.

* Stabilize actor position when hat changes

Override centerAndScaleModel in ActorRenderer to exclude the hat
part from the bounding box calculation, so switching between hats
of different sizes no longer shifts the body/head position.

* Fetch assets from SI files via HTTP Range requests

Replace static animation, texture, and globe bitmap files with a
manifest-driven approach that extracts them directly from the game's
SI files at runtime using HTTP Range requests.

A new generate-manifest script scans SI files by MxCh objectId to
locate each asset's byte offset(s), verifies integrity via MD5, and
writes an asset-ranges.json manifest. The app consumes this manifest
to fetch assets on demand, including support for files split across
MxCh interleave boundaries.

Also removes unused constants (ActorLODIndex, animation keyframe
flag constants).

* Fetch character icons from SI files via HTTP Range requests

Replace static webp character icons with runtime extraction from
INFOMAIN.SI, extending the bitmap manifest to support multiple SI files.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>

* Fix actor editor animation and interaction bugs

Use mood (not sound+4*move) to select walking animation, matching
FUN_10063b90. Load secondary animation tier (speed 4.0 threshold)
which NPCs typically use in-game, producing the independent head/hat
movement. Fix switchSound wrap to 9 values, add switchColor click
remapping for claws/head/body, fix g_cycles case mismatches, add
morph key visibility support, and preserve root Y-translation for
vertical bounce while stripping horizontal movement.

* Add click animations to actor editor

Play a one-shot gesture animation when clicking an actor, matching the
in-game LegoEntity::ClickAnimation behavior (objectId = m_move + 10).
After the click animation finishes, the walking loop resumes. Adds the
4 click animations from SNDANIM.SI to the asset manifest and extends
ActorRenderer with queue-based click animation playback. Also fixes
treadmill XZ stripping for click animations where actor_01 is nested
under wrapper nodes.

* Add vehicle rendering to actor editor

Actors with personal vehicles (skateboard, motorcycles, bicycles) can
now be toggled between walking and vehicle mode via a button in the
actor navigation bar. Vehicle geometries are loaded from WDB world
models and rendered alongside the character with matching animations.

* Add click sound playback to actor editor

Plays the character's click sound (m_sound + 50) and, for Laura's
SwitchMood, an additional mood sound (m_mood + 66) from SNDANIM.SI,
matching the original game behavior. Sounds are fetched on demand,
decoded as WAV, and cached as AudioBuffers.

* Replace Range request asset loading with packed binary bundle

Extract save editor assets (animations, sounds, textures, bitmaps) into
a single save-editor.bin file at build time instead of fetching byte
ranges from ~550MB SI files at runtime. The bundle packs an embedded
JSON index and all fragment data into one file (~756KB), eliminating
Range request complexity and enabling proper Workbox precaching.

* Clean up actor editor branch: DRY, dead code, CSS

- Extract buildNodeToPartGroupMap() in ActorRenderer to deduplicate
  map-building logic in loadAnimationForActor and playClickAnimation
- Refactor updateMissionScore() to use getMissionScoreOffset() instead
  of duplicating offset calculation
- Remove unused ActorPartLabels export from actorConstants
- Make fetchBitmap module-private (only used by fetchBitmapAsURL)
- Merge duplicate .globe-btn CSS blocks in LightPositionEditor

* Add drag-to-orbit controls to vehicle and actor editors

Use Three.js OrbitControls in BaseRenderer for rotation-only orbiting
with damping. Vehicle editor auto-rotates and resets on part switch.
Actor editor uses orbit without auto-rotate (has skeletal animations).
Drag vs click detection uses pointermove threshold to avoid false
positives from autoRotate damping.

* Rebase WdbModelRenderer on BaseRenderer

Remove duplicated scene/camera/renderer/lighting setup, geometry
unpacking, animation loop, and dispose logic. Score cube gets orbit
controls and drag-vs-click detection for free.

* Add zoom, pan, and camera reset to 3D editors

Enable zoom (scroll/pinch) and pan (right-click/two-finger drag) on
all OrbitControls. Add resetView() to BaseRenderer that restores
initial camera state and auto-rotate via OrbitControls.saveState/reset.
Add reset camera button to EditorTooltip with mobile-friendly touch
targets and hover-only highlight to avoid sticky state on touch.

* Update changelog and fix sticky hover on touch devices

Add actor editor features, 3D orbit/zoom/pan controls, and camera
reset button to the February 2026 changelog. Wrap hover styles in
@media (hover: hover) for vehicle toggle and texture buttons.

* Update README with save editor setup, project structure, and Three.js
2026-02-14 02:29:55 +01:00

140 lines
4.1 KiB
JavaScript

// Loads assets from a packed binary bundle generated by scripts/generate-save-editor-assets.js
// Format: [U32LE indexLen][JSON index][fragment data]
let bundleIndex = null;
let dataOffset = 0;
let bundleBuffer = null;
let bundlePromise = null;
async function loadBundle() {
if (!bundlePromise) {
bundlePromise = fetch('/save-editor.bin').then(async (resp) => {
bundleBuffer = await resp.arrayBuffer();
const indexLen = new DataView(bundleBuffer).getUint32(0, true);
const indexJson = new TextDecoder().decode(new Uint8Array(bundleBuffer, 4, indexLen));
bundleIndex = JSON.parse(indexJson);
dataOffset = 4 + indexLen;
});
}
await bundlePromise;
}
function getAsset(type, name) {
const entry = bundleIndex[`${type}/${name}`];
if (!entry) return null;
const [offset, size] = entry;
return bundleBuffer.slice(dataOffset + offset, dataOffset + offset + size);
}
export async function fetchAnimation(name) {
await loadBundle();
return getAsset('animations', name);
}
export async function fetchTexture(name) {
await loadBundle();
return getAsset('textures', name);
}
async function fetchBitmap(name) {
await loadBundle();
return getAsset('bitmaps', name);
}
async function fetchSound(name) {
await loadBundle();
return getAsset('sounds', name);
}
/**
* Build a WAV file from raw MxCh sound data.
* Layout: bytes 0-15 = PCMWAVEFORMAT, 16-19 = m_dataSize, 20-23 = m_flags, 24+ = PCM data.
* Uses actual available size since sector interleaving may clip the last chunk.
*/
function buildWav(buffer) {
const dataSize = buffer.byteLength - 24;
const wavSize = 44 + dataSize;
const wav = new ArrayBuffer(wavSize);
const view = new DataView(wav);
const bytes = new Uint8Array(wav);
// RIFF header
bytes.set([0x52, 0x49, 0x46, 0x46]); // "RIFF"
view.setUint32(4, wavSize - 8, true);
bytes.set([0x57, 0x41, 0x56, 0x45], 8); // "WAVE"
// fmt chunk — copy PCMWAVEFORMAT (16 bytes) directly from source header
bytes.set([0x66, 0x6D, 0x74, 0x20], 12); // "fmt "
view.setUint32(16, 16, true);
bytes.set(new Uint8Array(buffer, 0, 16), 20);
// data chunk
bytes.set([0x64, 0x61, 0x74, 0x61], 36); // "data"
view.setUint32(40, dataSize, true);
bytes.set(new Uint8Array(buffer, 24, dataSize), 44);
return wav;
}
export async function fetchSoundAsWav(name) {
const buffer = await fetchSound(name);
if (!buffer) return null;
return buildWav(buffer);
}
/**
* Decode a raw Windows DIB (no BM file header) into RGBA ImageData.
* Supports 8-bit indexed color only.
*/
function decodeDib(buffer) {
const view = new DataView(buffer);
const width = view.getInt32(4, true);
const height = view.getInt32(8, true);
const bpp = view.getUint16(14, true);
if (bpp !== 8) return null;
// Palette: 256 BGRA entries starting at offset 40
const palette = new Uint8Array(buffer, 40, 1024);
// Pixel data starts after header + palette
const pixelOffset = 40 + 1024;
const rowStride = (width + 3) & ~3; // rows padded to 4-byte boundary
const absHeight = Math.abs(height);
const bottomUp = height > 0;
const imageData = new ImageData(width, absHeight);
const pixels = new Uint8Array(buffer, pixelOffset);
for (let y = 0; y < absHeight; y++) {
const srcRow = bottomUp ? (absHeight - 1 - y) : y;
for (let x = 0; x < width; x++) {
const idx = pixels[srcRow * rowStride + x] * 4;
const dst = (y * width + x) * 4;
imageData.data[dst] = palette[idx + 2]; // R (from BGR)
imageData.data[dst + 1] = palette[idx + 1]; // G
imageData.data[dst + 2] = palette[idx]; // B
imageData.data[dst + 3] = 255;
}
}
return imageData;
}
/**
* Fetch a bitmap from an SI file and return a blob URL for use in <img> tags.
*/
export async function fetchBitmapAsURL(name) {
const buffer = await fetchBitmap(name);
if (!buffer) return null;
const imageData = decodeDib(buffer);
if (!imageData) return null;
const canvas = new OffscreenCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
const blob = await canvas.convertToBlob({ type: 'image/png' });
return URL.createObjectURL(blob);
}