From 27f76b4e698e866801e9005c3a2fcf264b1753e5 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Wed, 13 May 2026 00:44:35 +0200 Subject: [PATCH] Generalize terrain weight handling using stored paint weight count --- .../apply-terrain-brush-patch-command.ts | 6 +++-- src/geometry/spline-road-terrain.ts | 25 ++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/commands/apply-terrain-brush-patch-command.ts b/src/commands/apply-terrain-brush-patch-command.ts index c9122e65..87c1456a 100644 --- a/src/commands/apply-terrain-brush-patch-command.ts +++ b/src/commands/apply-terrain-brush-patch-command.ts @@ -6,9 +6,9 @@ import type { } from "../core/terrain-brush"; import { getOrCreateTerrainFoliageMask, + getTerrainStoredPaintWeightCount, isTerrainFoliageMaskEmpty, markTerrainRenderSamplesDirty, - TERRAIN_LAYER_COUNT, updateTerrainBoundsCacheAfterHeightPatch, type Terrain, type TerrainSampleBounds @@ -124,7 +124,9 @@ export function applyTerrainBrushPatchToDocument( renderDirtyBounds = mergeTerrainSampleIndexIntoBounds( renderDirtyBounds, terrain, - Math.floor(entry.index / (TERRAIN_LAYER_COUNT - 1)) + Math.floor( + entry.index / getTerrainStoredPaintWeightCount(terrain.layers.length) + ) ); } diff --git a/src/geometry/spline-road-terrain.ts b/src/geometry/spline-road-terrain.ts index 81c2a0e8..b0ea35a1 100644 --- a/src/geometry/spline-road-terrain.ts +++ b/src/geometry/spline-road-terrain.ts @@ -12,7 +12,7 @@ import { getTerrainPaintWeightSampleOffset, getTerrainSampleIndex, getTerrainSampleLayerWeights, - TERRAIN_LAYER_COUNT, + getTerrainStoredPaintWeightCount, type Terrain } from "../document/terrains"; import { createTerrainBrushPatchFromTerrains } from "./terrain-brush"; @@ -185,14 +185,12 @@ function getRoadPaintLayerIndex(path: ScenePath, terrain: Terrain): number | nul } function createRoadTargetWeights( + terrain: Terrain, layerIndex: number -): [number, number, number, number] { - return [ - layerIndex === 0 ? 1 : 0, - layerIndex === 1 ? 1 : 0, - layerIndex === 2 ? 1 : 0, - layerIndex === 3 ? 1 : 0 - ]; +): number[] { + return terrain.layers.map((_, currentLayerIndex) => + currentLayerIndex === layerIndex ? 1 : 0 + ); } function applyRoadPaintWeights( @@ -204,14 +202,13 @@ function applyRoadPaintWeights( changedPaintWeightIndices: Set ) { const currentWeights = getTerrainSampleLayerWeights(terrain, sampleX, sampleZ); - const targetWeights = createRoadTargetWeights(layerIndex); + const targetWeights = createRoadTargetWeights(terrain, layerIndex); const offset = getTerrainPaintWeightSampleOffset(terrain, sampleX, sampleZ); + const storedWeightCount = getTerrainStoredPaintWeightCount( + terrain.layers.length + ); - for ( - let layerOffset = 0; - layerOffset < TERRAIN_LAYER_COUNT - 1; - layerOffset += 1 - ) { + for (let layerOffset = 0; layerOffset < storedWeightCount; layerOffset += 1) { const paintWeightIndex = offset + layerOffset; const currentWeight = currentWeights[layerOffset + 1] ?? 0; const nextWeight = lerp(