Skip to content

Commit

Permalink
feat(mapobj): add vertex color fix logic to group loading
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Feb 26, 2024
1 parent 6d7730e commit 7d75e19
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
57 changes: 56 additions & 1 deletion src/lib/mapobj/MapObjGroup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IoMode, IoSource, openStream } from '@wowserhq/io';
import { indexChunks } from '../util.js';
import * as groupIo from './io/group.js';
import { MAP_OBJ_GROUP_FLAG } from './const.js';
import { MAP_OBJ_FLAG, MAP_OBJ_GROUP_FLAG } from './const.js';
import { MapObjBatch } from './types.js';

class MapObjGroup {
#version = 17;
#objFlags = 0x0;

#batches: MapObjBatch[] = [];

Expand All @@ -15,6 +16,10 @@ class MapObjGroup {
#colors: Uint8Array;
#vertices: Float32Array;

constructor(objFlags: number) {
this.#objFlags = objFlags;
}

get batches() {
return this.#batches;
}
Expand Down Expand Up @@ -85,6 +90,10 @@ class MapObjGroup {
const colorsChunk = groupData.get('MOCV');
if (colorsChunk) {
this.#colors = colorsChunk;

if (!(this.#objFlags & MAP_OBJ_FLAG.FLAG_CVERTS_FIXED)) {
this.#fixColors(groupHeader);
}
}

const verticesChunk = groupData.get('MOVT');
Expand All @@ -94,6 +103,52 @@ class MapObjGroup {

return this;
}

#fixColors(header: any) {
const intBatchStart =
header.transBatchCount > 0 ? this.#batches[header.transBatchCount].lastVertex + 1 : 0;

for (let i = 0; i < this.#colors.length / 4; i++) {
const colorOfs = i * 4;

if (i >= intBatchStart) {
// Int / ext batches

let r = this.#colors[colorOfs + 2];
let g = this.#colors[colorOfs + 1];
let b = this.#colors[colorOfs + 0];
let a = this.#colors[colorOfs + 3];

r += ((r * a) / 64) | 0;
g += ((g * a) / 64) | 0;
b += ((b * a) / 64) | 0;

r = Math.min((r / 2) | 0, 255);
g = Math.min((g / 2) | 0, 255);
b = Math.min((b / 2) | 0, 255);
a = 255;

this.#colors[colorOfs + 2] = r;
this.#colors[colorOfs + 1] = g;
this.#colors[colorOfs + 0] = b;
this.#colors[colorOfs + 3] = a;
} else {
// Trans batches

let r = this.#colors[colorOfs + 2];
let g = this.#colors[colorOfs + 1];
let b = this.#colors[colorOfs + 0];

r = (r / 2) | 0;
g = (g / 2) | 0;
b = (b / 2) | 0;

this.#colors[colorOfs + 2] = r;
this.#colors[colorOfs + 1] = g;
this.#colors[colorOfs + 0] = b;
}
}
}
}

export default MapObjGroup;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mapobj/const.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
enum MAP_OBJ_FLAG {
FLAG_CVERTS_TRANS_ATTENUATED = 0x1,
FLAG_UNIFIED_SHADING = 0x2,
FLAG_CVERTS_ALPHA_FIXED = 0x8,
FLAG_CVERTS_FIXED = 0x8,
}

enum MAP_OBJ_GROUP_FLAG {
Expand Down

0 comments on commit 7d75e19

Please sign in to comment.