Refactor terrain data handling and add comprehensive unit tests for brush, mesh, and serialization.

This commit is contained in:
2026-04-30 02:57:29 +02:00
parent ee7fcce1b3
commit 2b2cd81054
5 changed files with 92 additions and 3 deletions

View File

@@ -891,12 +891,12 @@ function buildRuntimeTerrain(
sampleCountX: terrain.sampleCountX,
sampleCountZ: terrain.sampleCountZ,
cellSize: terrain.cellSize,
heights: [...terrain.heights],
heights: terrain.heights,
layers: terrain.layers.map((layer) => ({
materialId: layer.materialId,
material: resolveRuntimeMaterial(document, layer.materialId)
})),
paintWeights: [...terrain.paintWeights]
paintWeights: terrain.paintWeights
};
}

View File

@@ -9701,7 +9701,7 @@ export class ViewportHost {
const committed =
this.terrainBrushCommitHandler?.({
terrain: cloneTerrain(finalPreviewTerrain),
terrain: finalPreviewTerrain,
commandLabel: getTerrainBrushCommandLabel(toolState.tool),
tool: toolState.tool
}) === true;

View File

@@ -6,6 +6,7 @@ import {
getTerrainSampleLayerWeights
} from "../../src/document/terrains";
import {
applyTerrainBrushStampInPlace,
applyTerrainBrushStamp,
getTerrainBrushWeight,
sampleTerrainHeightAtWorldPosition
@@ -112,6 +113,41 @@ describe("terrain brush geometry", () => {
expect(getTerrainBrushWeight(2, 1, 0.5)).toBe(0);
});
it("can stamp terrain in place and report dirty sample bounds", () => {
const terrain = createTerrain({
id: "terrain-stamp-in-place",
position: { x: 0, y: 0, z: 0 },
sampleCountX: 5,
sampleCountZ: 5,
cellSize: 1,
heights: new Array(25).fill(0)
});
const originalHeights = terrain.heights;
const originalPaintWeights = terrain.paintWeights;
const result = applyTerrainBrushStampInPlace({
terrain,
center: { x: 2, z: 2 },
settings: {
radius: 0.6,
strength: 0.5,
falloff: 0
},
tool: "raise"
});
expect(result.changed).toBe(true);
expect(result.dirtyBounds).toEqual({
minSampleX: 2,
maxSampleX: 2,
minSampleZ: 2,
maxSampleZ: 2
});
expect(terrain.heights).toBe(originalHeights);
expect(terrain.paintWeights).toBe(originalPaintWeights);
expect(terrain.heights[2 + 2 * 5]).toBeCloseTo(0.5);
});
it("paints terrain layer weights toward the active layer while preserving a normalized blend", () => {
const terrain = createTerrain({
id: "terrain-paint",

View File

@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
import { createTerrain } from "../../src/document/terrains";
import {
buildTerrainDerivedMeshData,
buildTerrainLodChunkMeshData,
buildTerrainLodMeshData,
resolveTerrainLodLevelIndex,
resolveTerrainLodLevelIndexWithHysteresis
@@ -131,6 +132,25 @@ describe("terrain mesh generation", () => {
});
});
it("can rebuild one terrain LoD chunk from its starting sample", () => {
const terrain = createTerrain({
sampleCountX: 130,
sampleCountZ: 70
});
const chunk = buildTerrainLodChunkMeshData(terrain, 64, 0);
expect(chunk).toMatchObject({
chunkX: 1,
chunkZ: 0,
startSampleX: 64,
startSampleZ: 0,
endSampleX: 128,
endSampleZ: 64
});
expect(chunk?.levels.length).toBeGreaterThan(1);
});
it("generates smaller terrain LoD levels as stride increases", () => {
const terrain = createTerrain({
sampleCountX: 65,

View File

@@ -10,6 +10,7 @@ import {
createEmptySceneDocument,
createProjectDocumentFromSceneDocument
} from "../../src/document/scene-document";
import { createTerrain } from "../../src/document/terrains";
import { serializeSceneDocument } from "../../src/serialization/scene-document-json";
import {
DEFAULT_SCENE_DRAFT_STORAGE_KEY,
@@ -246,6 +247,38 @@ describe("local draft storage", () => {
});
});
it("skips oversized terrain-heavy autosaves and clears stale drafts", () => {
const storage = new MemoryStorage();
storage.setItem(DEFAULT_SCENE_DRAFT_STORAGE_KEY, "stale draft");
const terrain = createTerrain({
id: "terrain-large-draft",
sampleCountX: 17,
sampleCountZ: 17
});
const document = {
...createEmptyProjectDocument(),
scenes: {
"scene-main": {
...createEmptyProjectScene({
id: "scene-main",
name: "Terrain Draft"
}),
terrains: {
[terrain.id]: terrain
}
}
}
};
const result = saveSceneDocumentDraft(storage, document, null, undefined, {
maxSerializedBytes: 512
});
expect(result.status).toBe("skipped");
expect(result.message).toContain("Autosave skipped");
expect(storage.getItem(DEFAULT_SCENE_DRAFT_STORAGE_KEY)).toBeNull();
});
it("loads older raw scene-document drafts without requiring viewport layout state", () => {
const storage = new MemoryStorage();
storage.setItem(