Refactor terrain data handling and add comprehensive unit tests for brush, mesh, and serialization.
This commit is contained in:
@@ -891,12 +891,12 @@ function buildRuntimeTerrain(
|
|||||||
sampleCountX: terrain.sampleCountX,
|
sampleCountX: terrain.sampleCountX,
|
||||||
sampleCountZ: terrain.sampleCountZ,
|
sampleCountZ: terrain.sampleCountZ,
|
||||||
cellSize: terrain.cellSize,
|
cellSize: terrain.cellSize,
|
||||||
heights: [...terrain.heights],
|
heights: terrain.heights,
|
||||||
layers: terrain.layers.map((layer) => ({
|
layers: terrain.layers.map((layer) => ({
|
||||||
materialId: layer.materialId,
|
materialId: layer.materialId,
|
||||||
material: resolveRuntimeMaterial(document, layer.materialId)
|
material: resolveRuntimeMaterial(document, layer.materialId)
|
||||||
})),
|
})),
|
||||||
paintWeights: [...terrain.paintWeights]
|
paintWeights: terrain.paintWeights
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9701,7 +9701,7 @@ export class ViewportHost {
|
|||||||
|
|
||||||
const committed =
|
const committed =
|
||||||
this.terrainBrushCommitHandler?.({
|
this.terrainBrushCommitHandler?.({
|
||||||
terrain: cloneTerrain(finalPreviewTerrain),
|
terrain: finalPreviewTerrain,
|
||||||
commandLabel: getTerrainBrushCommandLabel(toolState.tool),
|
commandLabel: getTerrainBrushCommandLabel(toolState.tool),
|
||||||
tool: toolState.tool
|
tool: toolState.tool
|
||||||
}) === true;
|
}) === true;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
getTerrainSampleLayerWeights
|
getTerrainSampleLayerWeights
|
||||||
} from "../../src/document/terrains";
|
} from "../../src/document/terrains";
|
||||||
import {
|
import {
|
||||||
|
applyTerrainBrushStampInPlace,
|
||||||
applyTerrainBrushStamp,
|
applyTerrainBrushStamp,
|
||||||
getTerrainBrushWeight,
|
getTerrainBrushWeight,
|
||||||
sampleTerrainHeightAtWorldPosition
|
sampleTerrainHeightAtWorldPosition
|
||||||
@@ -112,6 +113,41 @@ describe("terrain brush geometry", () => {
|
|||||||
expect(getTerrainBrushWeight(2, 1, 0.5)).toBe(0);
|
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", () => {
|
it("paints terrain layer weights toward the active layer while preserving a normalized blend", () => {
|
||||||
const terrain = createTerrain({
|
const terrain = createTerrain({
|
||||||
id: "terrain-paint",
|
id: "terrain-paint",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { createTerrain } from "../../src/document/terrains";
|
import { createTerrain } from "../../src/document/terrains";
|
||||||
import {
|
import {
|
||||||
buildTerrainDerivedMeshData,
|
buildTerrainDerivedMeshData,
|
||||||
|
buildTerrainLodChunkMeshData,
|
||||||
buildTerrainLodMeshData,
|
buildTerrainLodMeshData,
|
||||||
resolveTerrainLodLevelIndex,
|
resolveTerrainLodLevelIndex,
|
||||||
resolveTerrainLodLevelIndexWithHysteresis
|
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", () => {
|
it("generates smaller terrain LoD levels as stride increases", () => {
|
||||||
const terrain = createTerrain({
|
const terrain = createTerrain({
|
||||||
sampleCountX: 65,
|
sampleCountX: 65,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
createEmptySceneDocument,
|
createEmptySceneDocument,
|
||||||
createProjectDocumentFromSceneDocument
|
createProjectDocumentFromSceneDocument
|
||||||
} from "../../src/document/scene-document";
|
} from "../../src/document/scene-document";
|
||||||
|
import { createTerrain } from "../../src/document/terrains";
|
||||||
import { serializeSceneDocument } from "../../src/serialization/scene-document-json";
|
import { serializeSceneDocument } from "../../src/serialization/scene-document-json";
|
||||||
import {
|
import {
|
||||||
DEFAULT_SCENE_DRAFT_STORAGE_KEY,
|
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", () => {
|
it("loads older raw scene-document drafts without requiring viewport layout state", () => {
|
||||||
const storage = new MemoryStorage();
|
const storage = new MemoryStorage();
|
||||||
storage.setItem(
|
storage.setItem(
|
||||||
|
|||||||
Reference in New Issue
Block a user