From efc9d163376bc02f5b2862c5da74e97491582050 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sun, 31 Dec 2023 15:54:56 -0600 Subject: [PATCH] fix(map): correct various splat issues when loading map chunks --- src/lib/map/MapArea.ts | 41 ++++++++++++++++++++++++----------------- src/lib/map/const.ts | 1 + 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/lib/map/MapArea.ts b/src/lib/map/MapArea.ts index 1db7dae..8072cf9 100644 --- a/src/lib/map/MapArea.ts +++ b/src/lib/map/MapArea.ts @@ -75,23 +75,30 @@ class MapArea { for (const { textureId, properties, alphaOffset, effectId } of chunkData.get('MCLY') ?? []) { const texture = textures[textureId]; - // Normalize all MCAL splats into 8-bit depth splats - let splat: Uint8Array; - if (!alpha || alpha.length === 0) { - splat = null; - } else if (this.#layerSplatDepth === 4) { - const rawSplat = new Uint8Array( - alpha.buffer, - alpha.byteOffset + alphaOffset, - MAP_LAYER_SPLAT_SIZE / 2, - ); - const needsFix = (chunkHeader.flags & MAP_CHUNK_FLAG.FIXED_LAYER_SPLAT) === 0; - splat = splat4To8(rawSplat, needsFix); - } else if (properties & MAP_LAYER_PROPERTY.SPLAT_COMPRESSED) { - const rawSplat = new Uint8Array(alpha.buffer, alpha.byteOffset, alphaOffset); - splat = splatCompressedTo8(rawSplat); - } else { - splat = new Uint8Array(alpha.buffer, alpha.byteOffset + alphaOffset, MAP_LAYER_SPLAT_SIZE); + let splat: Uint8Array = null; + const hasSplat = (properties & MAP_LAYER_PROPERTY.HAS_SPLAT) !== 0; + + if (hasSplat && alpha && alpha.length > 0) { + // Normalize 4-bit and compressed splats into 8-bit depth splats + + if (this.#layerSplatDepth === 4) { + const rawSplat = new Uint8Array( + alpha.buffer, + alpha.byteOffset + alphaOffset, + MAP_LAYER_SPLAT_SIZE / 2, + ); + const needsFix = (chunkHeader.flags & MAP_CHUNK_FLAG.FIXED_LAYER_SPLAT) === 0; + splat = splat4To8(rawSplat, needsFix); + } else if (properties & MAP_LAYER_PROPERTY.SPLAT_COMPRESSED) { + const rawSplat = new Uint8Array(alpha.buffer, alpha.byteOffset + alphaOffset); + splat = splatCompressedTo8(rawSplat); + } else { + splat = new Uint8Array( + alpha.buffer, + alpha.byteOffset + alphaOffset, + MAP_LAYER_SPLAT_SIZE, + ); + } } const layer = new MapLayer(texture, splat, effectId); diff --git a/src/lib/map/const.ts b/src/lib/map/const.ts index 2ba63b2..4774175 100644 --- a/src/lib/map/const.ts +++ b/src/lib/map/const.ts @@ -38,6 +38,7 @@ enum MAP_CHUNK_FLAG { } enum MAP_LAYER_PROPERTY { + HAS_SPLAT = 0x100, SPLAT_COMPRESSED = 0x200, }