[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
121 lines
2.9 KiB
TypeScript
121 lines
2.9 KiB
TypeScript
import type { Terrain } from "../document/terrains";
|
|
import { TERRAIN_LAYER_COUNT } from "../document/terrains";
|
|
|
|
export type TerrainBrushTool =
|
|
| "raise"
|
|
| "lower"
|
|
| "smooth"
|
|
| "flatten"
|
|
| "paint";
|
|
|
|
export interface TerrainBrushSettings {
|
|
radius: number;
|
|
strength: number;
|
|
falloff: number;
|
|
}
|
|
|
|
export interface ArmedTerrainSculptBrushState extends TerrainBrushSettings {
|
|
terrainId: string;
|
|
tool: Exclude<TerrainBrushTool, "paint">;
|
|
}
|
|
|
|
export interface ArmedTerrainPaintBrushState extends TerrainBrushSettings {
|
|
terrainId: string;
|
|
tool: "paint";
|
|
layerIndex: number;
|
|
}
|
|
|
|
export type ArmedTerrainBrushState =
|
|
| ArmedTerrainSculptBrushState
|
|
| ArmedTerrainPaintBrushState;
|
|
|
|
export interface TerrainBrushStrokeCommit {
|
|
terrain: Terrain;
|
|
commandLabel: string;
|
|
tool: TerrainBrushTool;
|
|
}
|
|
|
|
export const DEFAULT_TERRAIN_BRUSH_RADIUS = 2;
|
|
export const DEFAULT_TERRAIN_BRUSH_STRENGTH = 0.35;
|
|
export const DEFAULT_TERRAIN_BRUSH_FALLOFF = 0.6;
|
|
export const MIN_TERRAIN_BRUSH_RADIUS = 0.25;
|
|
export const MAX_TERRAIN_BRUSH_RADIUS = 12;
|
|
export const MIN_TERRAIN_BRUSH_STRENGTH = 0.05;
|
|
export const MAX_TERRAIN_BRUSH_STRENGTH = 1;
|
|
export const MIN_TERRAIN_BRUSH_FALLOFF = 0;
|
|
export const MAX_TERRAIN_BRUSH_FALLOFF = 1;
|
|
|
|
function clamp(value: number, min: number, max: number): number {
|
|
return Math.min(max, Math.max(min, value));
|
|
}
|
|
|
|
export function clampTerrainBrushRadius(radius: number): number {
|
|
if (!Number.isFinite(radius)) {
|
|
return DEFAULT_TERRAIN_BRUSH_RADIUS;
|
|
}
|
|
|
|
return clamp(radius, MIN_TERRAIN_BRUSH_RADIUS, MAX_TERRAIN_BRUSH_RADIUS);
|
|
}
|
|
|
|
export function clampTerrainBrushStrength(strength: number): number {
|
|
if (!Number.isFinite(strength)) {
|
|
return DEFAULT_TERRAIN_BRUSH_STRENGTH;
|
|
}
|
|
|
|
return clamp(
|
|
strength,
|
|
MIN_TERRAIN_BRUSH_STRENGTH,
|
|
MAX_TERRAIN_BRUSH_STRENGTH
|
|
);
|
|
}
|
|
|
|
export function clampTerrainBrushFalloff(falloff: number): number {
|
|
if (!Number.isFinite(falloff)) {
|
|
return DEFAULT_TERRAIN_BRUSH_FALLOFF;
|
|
}
|
|
|
|
return clamp(
|
|
falloff,
|
|
MIN_TERRAIN_BRUSH_FALLOFF,
|
|
MAX_TERRAIN_BRUSH_FALLOFF
|
|
);
|
|
}
|
|
|
|
export function createDefaultTerrainBrushSettings(): TerrainBrushSettings {
|
|
return {
|
|
radius: DEFAULT_TERRAIN_BRUSH_RADIUS,
|
|
strength: DEFAULT_TERRAIN_BRUSH_STRENGTH,
|
|
falloff: DEFAULT_TERRAIN_BRUSH_FALLOFF
|
|
};
|
|
}
|
|
|
|
export function clampTerrainPaintLayerIndex(layerIndex: number): number {
|
|
if (!Number.isFinite(layerIndex)) {
|
|
return 0;
|
|
}
|
|
|
|
return Math.min(
|
|
TERRAIN_LAYER_COUNT - 1,
|
|
Math.max(0, Math.round(layerIndex))
|
|
);
|
|
}
|
|
|
|
export function getTerrainBrushToolLabel(tool: TerrainBrushTool): string {
|
|
switch (tool) {
|
|
case "raise":
|
|
return "Raise";
|
|
case "lower":
|
|
return "Lower";
|
|
case "smooth":
|
|
return "Smooth";
|
|
case "flatten":
|
|
return "Flatten";
|
|
case "paint":
|
|
return "Paint";
|
|
}
|
|
}
|
|
|
|
export function getTerrainBrushCommandLabel(tool: TerrainBrushTool): string {
|
|
return `${getTerrainBrushToolLabel(tool)} terrain`;
|
|
}
|