Add terrain reading functionality in migrate-scene-document.ts

This commit is contained in:
2026-04-18 19:44:57 +02:00
parent 6e78d66a11
commit 319668d38b

View File

@@ -235,6 +235,13 @@ import {
type ScenePath,
type ScenePathPoint
} from "./paths";
import {
createTerrain,
normalizeTerrainCellSize,
normalizeTerrainName,
normalizeTerrainSampleCount,
type Terrain
} from "./terrains";
import {
createDefaultProjectTimeSettings,
normalizeProjectStartDayNumber,
@@ -1824,6 +1831,66 @@ function readModelInstances(
return modelInstances;
}
function readTerrain(value: unknown, label: string): Terrain {
if (!isRecord(value)) {
throw new Error(`${label} must be an object.`);
}
const sampleCountX = normalizeTerrainSampleCount(
expectFiniteNumber(value.sampleCountX, `${label}.sampleCountX`),
`${label}.sampleCountX`
);
const sampleCountZ = normalizeTerrainSampleCount(
expectFiniteNumber(value.sampleCountZ, `${label}.sampleCountZ`),
`${label}.sampleCountZ`
);
const cellSize = normalizeTerrainCellSize(
expectFiniteNumber(value.cellSize, `${label}.cellSize`)
);
const heights = expectArray(value.heights, `${label}.heights`).map(
(heightValue, index) =>
expectFiniteNumber(heightValue, `${label}.heights.${index}`)
);
return createTerrain({
id: expectString(value.id, `${label}.id`),
name: normalizeTerrainName(
expectOptionalString(value.name, `${label}.name`)
),
visible: expectBoolean(value.visible, `${label}.visible`),
enabled: expectBoolean(value.enabled, `${label}.enabled`),
position: readVec3(value.position, `${label}.position`),
sampleCountX,
sampleCountZ,
cellSize,
heights
});
}
function readTerrains(value: unknown): SceneDocument["terrains"] {
if (value === undefined) {
return {};
}
if (!isRecord(value)) {
throw new Error("terrains must be a record.");
}
const terrains: SceneDocument["terrains"] = {};
for (const [terrainId, terrainValue] of Object.entries(value)) {
const terrain = readTerrain(terrainValue, `terrains.${terrainId}`);
if (terrain.id !== terrainId) {
throw new Error(`terrains.${terrainId}.id must match the registry key.`);
}
terrains[terrainId] = terrain;
}
return terrains;
}
function readVec2(value: unknown, label: string) {
if (!isRecord(value)) {
throw new Error(`${label} must be an object.`);