Skip to content

Commit

Permalink
fix(map): correct various splat issues when loading map chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 31, 2023
1 parent 21368c1 commit efc9d16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/lib/map/MapArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/lib/map/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum MAP_CHUNK_FLAG {
}

enum MAP_LAYER_PROPERTY {
HAS_SPLAT = 0x100,
SPLAT_COMPRESSED = 0x200,
}

Expand Down

0 comments on commit efc9d16

Please sign in to comment.