Files
webeditor3d/tests/geometry/terrain-brush.test.ts
Victor Giers 94dec56eb4 auto-git:
[add] src/rendering/terrain-layer-material.ts
 [add] tests/domain/terrains.test.ts
 [change] src/app/App.tsx
 [change] src/core/terrain-brush.ts
 [change] src/document/migrate-scene-document.ts
 [change] src/document/scene-document-validation.ts
 [change] src/document/scene-document.ts
 [change] src/document/terrains.ts
 [change] src/geometry/terrain-brush.ts
 [change] src/geometry/terrain-mesh.ts
 [change] src/runtime-three/rapier-collision-world.ts
 [change] src/runtime-three/runtime-host.ts
 [change] src/runtime-three/runtime-scene-build.ts
 [change] src/viewport-three/ViewportCanvas.tsx
 [change] src/viewport-three/ViewportPanel.tsx
 [change] src/viewport-three/viewport-host.ts
 [change] tests/domain/build-runtime-scene.test.ts
 [change] tests/domain/rapier-collision-world.test.ts
 [change] tests/domain/terrain.command.test.ts
 [change] tests/domain/water-material.test.ts
 [change] tests/geometry/terrain-brush.test.ts
 [change] tests/geometry/terrain-mesh.test.ts
 [change] tests/serialization/scene-document-json.test.ts
 [change] tests/unit/terrain-foundation.integration.test.tsx
 [change] tests/unit/viewport-canvas.test.tsx
2026-04-20 02:37:01 +02:00

163 lines
4.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createDefaultTerrainBrushSettings } from "../../src/core/terrain-brush";
import {
createTerrain,
getTerrainSampleLayerWeights
} from "../../src/document/terrains";
import {
applyTerrainBrushStamp,
getTerrainBrushWeight,
sampleTerrainHeightAtWorldPosition
} from "../../src/geometry/terrain-brush";
describe("terrain brush geometry", () => {
it("samples terrain height across the regular XZ grid", () => {
const terrain = createTerrain({
id: "terrain-sample-height",
position: { x: 0, y: 0, z: 0 },
sampleCountX: 2,
sampleCountZ: 2,
cellSize: 2,
heights: [0, 2, 4, 6]
});
expect(sampleTerrainHeightAtWorldPosition(terrain, 1, 1)).toBe(3);
});
it("raises and lowers height samples with radial falloff", () => {
const terrain = createTerrain({
id: "terrain-sculpt",
position: { x: 0, y: 0, z: 0 },
sampleCountX: 3,
sampleCountZ: 3,
cellSize: 1,
heights: new Array(9).fill(0)
});
const settings = {
...createDefaultTerrainBrushSettings(),
radius: 1.2,
strength: 0.5,
falloff: 0.5
};
const raisedTerrain = applyTerrainBrushStamp({
terrain,
center: { x: 1, z: 1 },
settings,
tool: "raise"
});
const loweredTerrain = applyTerrainBrushStamp({
terrain: raisedTerrain,
center: { x: 1, z: 1 },
settings,
tool: "lower"
});
expect(raisedTerrain.heights[4]).toBeCloseTo(0.5);
expect(raisedTerrain.heights[0]).toBe(0);
expect(loweredTerrain.heights[4]).toBeCloseTo(0);
});
it("smooths sharp spikes toward neighboring heights", () => {
const terrain = createTerrain({
id: "terrain-smooth",
position: { x: 0, y: 0, z: 0 },
sampleCountX: 3,
sampleCountZ: 3,
cellSize: 1,
heights: [0, 0, 0, 0, 10, 0, 0, 0, 0]
});
const smoothedTerrain = applyTerrainBrushStamp({
terrain,
center: { x: 1, z: 1 },
settings: {
radius: 1.5,
strength: 1,
falloff: 0
},
tool: "smooth"
});
expect(smoothedTerrain.heights[4]).toBeLessThan(10);
expect(smoothedTerrain.heights[4]).toBeCloseTo(10 / 9);
});
it("flattens toward the stroke-start reference height", () => {
const terrain = createTerrain({
id: "terrain-flatten",
position: { x: 0, y: 0, z: 0 },
sampleCountX: 3,
sampleCountZ: 3,
cellSize: 1,
heights: [0, 0, 0, 0, 4, 0, 0, 0, 0]
});
const flattenedTerrain = applyTerrainBrushStamp({
terrain,
center: { x: 1, z: 1 },
settings: {
radius: 1.2,
strength: 0.5,
falloff: 0
},
tool: "flatten",
referenceHeight: 2
});
expect(flattenedTerrain.heights[4]).toBeCloseTo(3);
});
it("returns zero influence outside the brush radius", () => {
expect(getTerrainBrushWeight(2, 1, 0.5)).toBe(0);
});
it("paints terrain layer weights toward the active layer while preserving a normalized blend", () => {
const terrain = createTerrain({
id: "terrain-paint",
position: { x: 0, y: 0, z: 0 },
sampleCountX: 3,
sampleCountZ: 3,
cellSize: 1
});
const paintedTerrain = applyTerrainBrushStamp({
terrain,
center: { x: 1, z: 1 },
settings: {
radius: 1.2,
strength: 0.5,
falloff: 0
},
tool: "paint",
layerIndex: 2
});
const paintedWeights = getTerrainSampleLayerWeights(paintedTerrain, 1, 1);
expect(paintedWeights[2]).toBeCloseTo(0.5);
expect(
paintedWeights[0] +
paintedWeights[1] +
paintedWeights[2] +
paintedWeights[3]
).toBeCloseTo(1);
const repaintedBaseTerrain = applyTerrainBrushStamp({
terrain: paintedTerrain,
center: { x: 1, z: 1 },
settings: {
radius: 1.2,
strength: 0.5,
falloff: 0
},
tool: "paint",
layerIndex: 0
});
const baseWeights = getTerrainSampleLayerWeights(repaintedBaseTerrain, 1, 1);
expect(baseWeights[0]).toBeGreaterThan(paintedWeights[0]);
expect(baseWeights[2]).toBeLessThan(paintedWeights[2]);
});
});