refactor: optimize terrain color extraction in GroundTruthData

Replaced tile sampling for terrain colors with direct extraction from the theme object, significantly improving performance. Updated shore, water, shoreline water, plains, highland, and mountain color computations to utilize theme properties, eliminating the need for tile searches. This change enhances efficiency in terrain color management while maintaining visual fidelity.
This commit is contained in:
scamiv
2026-01-16 22:09:31 +01:00
parent b4139e3463
commit 2cec5df845
@@ -310,83 +310,43 @@ export class GroundTruthData {
} }
this.needsTerrainParamsUpload = false; this.needsTerrainParamsUpload = false;
// Sample theme colors by finding representative tiles // Extract theme colors directly from theme object (much faster than sampling tiles)
// We'll search for a shore tile, water tile, and compute base terrain colors const themeAny = this.theme as any;
let shoreColor = { r: 204, g: 203, b: 158, a: 255 }; // Default pastel const isDark = themeAny.darkShore !== undefined;
let waterColor = { r: 70, g: 132, b: 180, a: 255 }; // Default pastel
let shorelineWaterColor = { r: 100, g: 143, b: 255, a: 255 }; // Default pastel
// Find a shore tile (land adjacent to water) // Get shore color
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) { const shore = isDark ? themeAny.darkShore : themeAny.shore;
if (this.game.isShore(i)) { const shoreColor = shore?.rgba ?? { r: 204, g: 203, b: 158, a: 255 };
const color = this.theme.terrainColor(this.game, i);
shoreColor = color.rgba;
break;
}
}
// Find a deep water tile (magnitude > 5) and shoreline water // Get water colors
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) { const water = isDark ? themeAny.darkWater : themeAny.water;
if (this.game.isWater(i)) { const waterColor = water?.rgba ?? { r: 70, g: 132, b: 180, a: 255 };
if (this.game.isShoreline(i)) {
const color = this.theme.terrainColor(this.game, i);
shorelineWaterColor = color.rgba;
} else if (this.game.magnitude(i) > 5) {
const color = this.theme.terrainColor(this.game, i);
waterColor = color.rgba;
}
if (waterColor.r !== 70 || shorelineWaterColor.r !== 100) {
// Found both, can break
if (this.game.isShoreline(i) && this.game.magnitude(i) > 5) {
break;
}
}
}
}
// Compute terrain base colors by sampling at magnitude 0, 10, 20 const shorelineWater = isDark
// Find a plains tile (magnitude < 10, land, not shore) ? themeAny.darkShorelineWater
let plainsColor = { r: 190, g: 220, b: 138, a: 255 }; : themeAny.shorelineWater;
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) { const shorelineWaterColor = shorelineWater?.rgba ?? {
if ( r: 100,
this.game.isLand(i) && g: 143,
!this.game.isShore(i) && b: 255,
this.game.magnitude(i) < 10 a: 255,
) { };
const color = this.theme.terrainColor(this.game, i);
plainsColor = color.rgba;
break;
}
}
// Find a highland tile at magnitude 10 (for accurate formula computation) // Compute terrain base colors from formulas (no tile sampling needed)
let highlandColor = { r: 200, g: 183, b: 138, a: 255 }; // Plains at mag 0: rgb(190, 220, 138) for pastel, rgb(140, 170, 88) for dark
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) { const plainsColor = isDark
if ( ? { r: 140, g: 170, b: 88, a: 255 }
this.game.isLand(i) && : { r: 190, g: 220, b: 138, a: 255 };
!this.game.isShore(i) &&
this.game.magnitude(i) === 10 // Highland at mag 10: rgb(220, 203, 158) for pastel, rgb(170, 153, 108) for dark
) { const highlandColor = isDark
const color = this.theme.terrainColor(this.game, i); ? { r: 170, g: 153, b: 108, a: 255 }
highlandColor = color.rgba; : { r: 220, g: 203, b: 158, a: 255 };
break;
} // Mountain at mag 20: rgb(240, 240, 240) for pastel, rgb(190, 190, 190) for dark
} const mountainColor = isDark
// If no mag 10 found, try any highland tile ? { r: 190, g: 190, b: 190, a: 255 }
if (highlandColor.r === 200 && highlandColor.g === 183) { : { r: 240, g: 240, b: 240, a: 255 };
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) {
if (
this.game.isLand(i) &&
!this.game.isShore(i) &&
this.game.magnitude(i) >= 10 &&
this.game.magnitude(i) < 20
) {
const color = this.theme.terrainColor(this.game, i);
highlandColor = color.rgba;
break;
}
}
}
// Store colors as vec4f (RGBA normalized to 0-1) // Store colors as vec4f (RGBA normalized to 0-1)
// Index 0-3: shore color // Index 0-3: shore color
@@ -407,34 +367,6 @@ export class GroundTruthData {
this.terrainParamsData[10] = shorelineWaterColor.b / 255; this.terrainParamsData[10] = shorelineWaterColor.b / 255;
this.terrainParamsData[11] = 1.0; this.terrainParamsData[11] = 1.0;
// Find a mountain tile at magnitude 20 (for accurate formula computation)
let mountainColor = { r: 230, g: 230, b: 230, a: 255 };
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) {
if (
this.game.isLand(i) &&
!this.game.isShore(i) &&
this.game.magnitude(i) === 20
) {
const color = this.theme.terrainColor(this.game, i);
mountainColor = color.rgba;
break;
}
}
// If no mag 20 found, try any mountain tile
if (mountainColor.r === 230 && mountainColor.g === 230) {
for (let i = 0; i < Math.min(1000, this.mapWidth * this.mapHeight); i++) {
if (
this.game.isLand(i) &&
!this.game.isShore(i) &&
this.game.magnitude(i) >= 20
) {
const color = this.theme.terrainColor(this.game, i);
mountainColor = color.rgba;
break;
}
}
}
// Index 12-15: plains base color (magnitude 0) // Index 12-15: plains base color (magnitude 0)
this.terrainParamsData[12] = plainsColor.r / 255; this.terrainParamsData[12] = plainsColor.r / 255;
this.terrainParamsData[13] = plainsColor.g / 255; this.terrainParamsData[13] = plainsColor.g / 255;