feat: add outdoor snapping, terrain types and procedural textures

This commit is contained in:
jhartworks committed 2026-11-16 13:02:18 +01:00
1 parent 992c3e18f2
commit d8cf076019
30 files changed
+2251 -1549

No files matched your search

+11 -8
View File
@@ -77,7 +77,7 @@ import { LibraryState, renderLibrary, renderPreview, type LibraryActions } from
import { arcFreeWall, arcRoomEdge, insertCorner } from "./editor/ops/rooms.ts";
import type { ArcActions, ArcEdit } from "./editor/forms/arc.ts";
import { dragStep, facingAngle, gridPositionAfter, hasLeftStart, headingAngle, outlineWithCornerAt, shifted, steppedDelta, straightEnd } from "./editor/ops/drags.ts";
import { snapToPlan } from "./editor/ops/snapping.ts";
import { snapOutdoorMove, snapToPlan, type SnapOptions } from "./editor/ops/snapping.ts";
import { draftLayer, guidesLayer } from "./editor/canvas/draft.ts";
import { devicesLayer, furnitureLayer, notesLayer } from "./editor/canvas/items.ts";
import { dimensionsLayer } from "./editor/canvas/plan.ts";
@@ -1239,10 +1239,10 @@ export class NfEditor extends LitElement {
// ------------------------------------------------------------------ snapping
/** The place in the plan a point lands on: corners and walls of other rooms, then the grid. `free` skips all of that. */
private snap(p: Vec2, skip?: { roomId: string; index?: number }, free = false): Vec2 {
private snap(p: Vec2, skip?: SnapOptions["skip"], free = false, outdoor = false): Vec2 {
this._guides = {};
if (free) return p;
const snapped = snapToPlan(p, { rooms: this.floor?.rooms ?? [], grid: this._doc.settings.grid, reach: SNAP_PX / this._view.scale, skip });
const snapped = snapToPlan(p, { rooms: this.floor?.rooms ?? [], outdoor: outdoor ? this.floor?.outdoor : undefined, grid: this._doc.settings.grid, reach: SNAP_PX / this._view.scale, skip });
this._guides = snapped.guides;
return snapped.point;
}
@@ -1481,7 +1481,7 @@ export class NfEditor extends LitElement {
private pressDrawingTool(press: Press): Gesture | undefined {
const tool = this._tool;
if (tool === "rect" || tool === "outdoor" || tool === "hole" || tool === "stairs") {
const start = this.snap(press.world, undefined, press.fine);
const start = this.snap(press.world, undefined, press.fine, tool === "outdoor");
return { kind: "rect", start, end: start, outdoor: tool === "outdoor", hole: tool === "hole", stairs: tool === "stairs" };
}
if (tool === "polygon" || tool === "measure") return { kind: "tap", startScreen: press.local, last: press.local, panning: false };
@@ -1613,7 +1613,7 @@ export class NfEditor extends LitElement {
const world = this.toWorld(...local);
const drag = this.drag;
if (!drag) {
if (this._tool !== "select" && this._tool !== "furniture" && this.floor) this._cursor = this.snap(world, undefined, e.altKey);
if (this._tool !== "select" && this._tool !== "furniture" && this.floor) this._cursor = this.snap(world, undefined, e.altKey, this._tool === "outdoor");
return;
}
this.moveDrag(drag, { local, world, fine: e.altKey, straight: e.shiftKey });
@@ -1661,7 +1661,7 @@ export class NfEditor extends LitElement {
drag.last = local;
return;
case "rect":
drag.end = this.snap(world, undefined, fine);
drag.end = this.snap(world, undefined, fine, !!drag.outdoor);
this.requestUpdate();
return;
case "freewall":
@@ -1786,13 +1786,16 @@ export class NfEditor extends LitElement {
drag.moved = true;
const area = this.floorOf(drag)?.outdoor.find((o) => o.id === drag.id);
if (!area) return;
const by = steppedDelta(drag.start, world, dragStep(grid, fine));
const initial = this.floorOf(drag)!;
const snapped = fine ? { by: steppedDelta(drag.start, world, dragStep(grid, true)), guides: {} } : snapOutdoorMove(area.points, [world[0] - drag.start[0], world[1] - drag.start[1]], { rooms: initial.rooms, outdoor: initial.outdoor, grid, reach: SNAP_PX / this._view.scale, skip: { outdoorId: drag.id } });
const { by } = snapped;
this._guides = snapped.guides;
this.live(drag, (_, floor) => void (floor.outdoor.find((o) => o.id === drag.id)!.points = area.points.map((p) => shifted(p, by))));
return;
}
case "outvertex": {
drag.moved = true;
const to = this.snap(world, undefined, fine);
const to = this.snap(world, { outdoorId: drag.id }, fine, true);
const area = this.floorOf(drag)?.outdoor.find((o) => o.id === drag.id);
if (!area) return;
this.live(drag, (_, floor) => {
@@ -49,7 +49,7 @@ export function renderOutdoorForm(host: EditorHost, area: OutdoorArea, actions:
${checkField(host, t("outdoor_outline"), area.outline !== false, (on) => set({ outline: on ? undefined : false }), { title: t("outdoor_outline_hint") })}
${area.type === "fence" || area.type === "pergola" ? checkField(host, t("outdoor_open"), !!area.open, (on) => set({ open: on || undefined }), { title: t("outdoor_open_hint") }) : nothing}
${area.type === "pergola" ? checkField(host, t("outdoor_bracing"), !!area.bracing, (on) => set({ bracing: on || undefined })) : nothing}
${checkField(host, t("outdoor_cut"), cutsOut(area), (on) => set({ cut: on === (area.type === "pool") ? undefined : on }), { title: t("outdoor_cut_hint") })}
${checkField(host, t("outdoor_cut"), cutsOut(area), (on) => set({ cut: on === cutsOut({ ...area, cut: undefined }) ? undefined : on }), { title: t("outdoor_cut_hint") })}
</div>
${area.type === "fence" ? fenceGates(host, area, set) : nothing}
${host.admin
@@ -1,7 +1,8 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { box } from "../../../entities/fixtures.ts";
import { snapToPlan } from "./snapping.ts";
import type { Vec2 } from "../../../model.ts";
import { snapOutdoorMove, snapToPlan } from "./snapping.ts";
const options = (extra = {}) => ({ rooms: [box("a", 0, 0, 4, 3)], grid: 0.1, reach: 0.2, ...extra });
@@ -41,3 +42,47 @@ test("walls along an axis are held to the grid when snapped to", () => {
const snapped = snapToPlan([2.37, 3.12], options({ grid: 0.5, reach: 0.3 }));
assert.deepEqual(snapped.point, [2.5, 3]);
});
const rect = (id: string, x: number, z: number, w: number, d: number) => ({ id, points: [[x, z], [x + w, z], [x + w, z + d], [x, z + d]] as Vec2[] });
const neighbor = rect("neighbor", 4.03, 0.07, 3, 3);
const outside = { rooms: [], outdoor: [neighbor], grid: 0.5, reach: 0.2 };
test("outdoor drawing and corner resizing snap to off-grid corners and edges", () => {
assert.deepEqual(snapToPlan([4.12, 0.14], outside).point, [4.03, 0.07]);
assert.deepEqual(snapToPlan([4.12, 1.2], outside).point, [4.03, 1]);
const self = rect("self", 0, 0, 4, 3);
const o = { ...outside, outdoor: [self, neighbor], skip: { outdoorId: "self" } };
assert.deepEqual(snapToPlan([4.06, 0.08], o).point, [4.03, 0.07]);
assert.deepEqual(snapToPlan([0.04, 0.06], { ...o, outdoor: [self] }).guides, {});
});
test("whole outdoor areas snap corners before rounding the pointer delta to a coarse grid", () => {
const moving = rect("moving", 0, 0, 2, 2);
const result = snapOutdoorMove(moving.points, [2.12, 0.14], { ...outside, outdoor: [moving, neighbor], skip: { outdoorId: "moving" } });
assert.deepEqual(result.by, [2.03, 0.07]);
assert.deepEqual(result.guides, { point: [4.03, 0.07] });
assert.deepEqual(moving.points, rect("moving", 0, 0, 2, 2).points, "never mutate the drag snapshot");
});
test("whole-area edges snap without changing shape, including a long edge covering a shorter target", () => {
const moving = rect("m", 0, -4, 2, 10);
const result = snapOutdoorMove(moving.points, [2.12, 0.32], outside);
assert.deepEqual(result.by, [2.03, 0.5]);
assert.deepEqual(result.guides, { x: 4.03 });
const diagonal = { id: "d", points: [[5, -2], [11, 4], [11, -2]] as Vec2[] };
const atEdge = snapOutdoorMove(rect("m", 0, 0, 1, 1).points, [7.1, 0], { ...outside, outdoor: [diagonal] });
assert.deepEqual(atEdge.by, [7.05, 0.05]);
});
test("distant areas keep grid translation and IDs never collide across rooms and outdoor areas", () => {
assert.deepEqual(snapOutdoorMove(rect("m", 0, 0, 1, 1).points, [10.12, 12.3], outside), { by: [10, 12.5], guides: {} });
const r = rect("same", 4.03, 0.07, 3, 3);
assert.deepEqual(snapToPlan([4.1, 0.1], { ...outside, rooms: [r], outdoor: [r], skip: { outdoorId: "same" } }).point, [4.03, 0.07]);
});
test("an open outdoor outline has no phantom closing edge to snap to", () => {
const o = { ...outside, outdoor: [{ id: "fence", open: true, points: [[0, 0], [4, 0], [4, 4]] as Vec2[] }] };
assert.equal(snapToPlan([2.1, 2.14], o).guides.point, undefined);
const moving = o.outdoor[0];
assert.deepEqual(snapOutdoorMove(moving.points, [0, 0], { ...outside, outdoor: [moving, rect("target", 1.9, 2, 0.2, 0.2)], skip: { outdoorId: moving.id } }).guides, {});
});
+61 -11
View File
@@ -1,19 +1,19 @@
// Where a point drawn in the plan lands: on a corner of another room, on another room's wall (so walls can meet in a T), or on the
// grid, lined up with other corners along each axis.
// Plan snapping: room corners/walls, optional outdoor corners/edges, then the grid and axis guides.
import type { Room, Vec2 } from "../../../model.ts";
import type { OutdoorArea, Room, Vec2 } from "../../../model.ts";
import type { Guides } from "../canvas/draft.ts";
const thousandths = (v: number) => Math.round(v * 1000) / 1000;
export interface SnapOptions {
rooms: readonly Room[];
rooms: readonly Pick<Room, "id" | "points">[];
outdoor?: readonly Pick<OutdoorArea, "id" | "points" | "open">[];
/** The grid step in metres. */
grid: number;
/** How close a point must be to something to snap to it, in metres. */
reach: number;
/** A room (or one corner of it) that is being moved and so is not something to snap to. */
skip?: { roomId: string; index?: number };
/** Exclude the moving outline; rooms can also exclude just one corner. */
skip?: { roomId?: string; outdoorId?: string; index?: number };
}
export interface Snapped {
@@ -23,7 +23,7 @@ export interface Snapped {
const distance = (a: Vec2, b: Vec2) => Math.hypot(a[0] - b[0], a[1] - b[1]);
/** The nearest corner of the other rooms within reach, if any. */
/** The nearest corner within reach, if any. */
function nearestCorner(corners: readonly Vec2[], p: Vec2, reach: number): Vec2 | null {
let best: Vec2 | null = null;
let bestDistance = reach;
@@ -35,12 +35,12 @@ function nearestCorner(corners: readonly Vec2[], p: Vec2, reach: number): Vec2 |
}
/** The point on a wall of another room that is nearest to p, within reach. Walls along an axis are held to the grid. */
function nearestOnWall(rooms: readonly Room[], p: Vec2, o: SnapOptions): Vec2 | null {
function nearestOnWall(rooms: readonly { id: string; points: Vec2[]; open?: boolean }[], p: Vec2, o: SnapOptions): Vec2 | null {
let best: Vec2 | null = null;
let bestDistance = o.reach;
for (const room of rooms) {
if (o.skip && room.id === o.skip.roomId) continue;
room.points.forEach((a, i) => {
if (room.open && i === room.points.length - 1) return;
const b = room.points[(i + 1) % room.points.length];
const [dx, dz] = [b[0] - a[0], b[1] - a[1]];
const squared = dx * dx + dz * dz;
@@ -79,10 +79,60 @@ function gridOrAligned(corners: readonly Vec2[], p: Vec2, o: SnapOptions): Snapp
}
export function snapToPlan(p: Vec2, o: SnapOptions): Snapped {
const corners = o.rooms.flatMap((room) => room.points.filter((_, i) => !(o.skip && room.id === o.skip.roomId && (o.skip.index === undefined || o.skip.index === i))));
// Separate namespaces: imported room and outdoor IDs need not be unique across both collections.
const areas = (o.outdoor ?? []).filter((a) => a.id !== o.skip?.outdoorId);
const corners = [...o.rooms.flatMap((room) => room.points.filter((_, i) => !(o.skip && room.id === o.skip.roomId && (o.skip.index === undefined || o.skip.index === i)))), ...areas.flatMap((a) => a.points)];
const corner = nearestCorner(corners, p, o.reach);
if (corner) return { point: [corner[0], corner[1]], guides: { point: corner } };
const wall = nearestOnWall(o.rooms, p, o);
const wall = nearestOnWall([...o.rooms.filter((r) => r.id !== o.skip?.roomId), ...areas], p, { ...o, skip: undefined });
if (wall) return { point: [thousandths(wall[0]), thousandths(wall[1])], guides: { point: wall } };
return gridOrAligned(corners, p, o);
}
/** Translate an entire outline onto nearby corners/edges, preserving every length and angle. */
export function snapOutdoorMove(points: readonly Vec2[], delta: Vec2, o: SnapOptions): { by: Vec2; guides: Guides } {
const targets = [...o.rooms.filter((r) => r.id !== o.skip?.roomId), ...(o.outdoor ?? []).filter((a) => a.id !== o.skip?.outdoorId)];
const moved = points.map((p): Vec2 => [p[0] + delta[0], p[1] + delta[1]]);
const movingOpen = o.outdoor?.find((a) => a.id === o.skip?.outdoorId)?.open;
const grid: Vec2 = delta.map((v) => thousandths(Math.round(v / o.grid) * o.grid)) as Vec2;
let corner: { by: Vec2; point: Vec2 } | undefined;
let best = o.reach;
for (const p of moved) for (const area of targets) for (const q of area.points) {
const d = distance(p, q);
if (d < best) {
best = d;
corner = { by: [delta[0] + q[0] - p[0], delta[1] + q[1] - p[1]], point: q };
}
}
if (corner) return { by: corner.by.map(thousandths) as Vec2, guides: { point: corner.point } };
let x: { offset: number; at: number } | undefined;
let z: { offset: number; at: number } | undefined;
let diagonal: { correction: Vec2; point: Vec2; d: number } | undefined;
const inspect = (p: Vec2, a: Vec2, b: Vec2, reverse = false) => {
const dx = b[0] - a[0], dz = b[1] - a[1], squared = dx * dx + dz * dz;
if (squared < 1e-9) return;
const t = ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / squared;
if (t < 0 || t > 1) return;
const foot: Vec2 = [a[0] + t * dx, a[1] + t * dz];
const d = distance(p, foot);
if (d >= o.reach) return;
const sign = reverse ? -1 : 1;
const correction: Vec2 = [sign * (foot[0] - p[0]), sign * (foot[1] - p[1])];
if (Math.abs(dx) < 1e-9) {
if (!x || d < Math.abs(x.offset)) x = { offset: correction[0], at: reverse ? p[0] : a[0] };
} else if (Math.abs(dz) < 1e-9) {
if (!z || d < Math.abs(z.offset)) z = { offset: correction[1], at: reverse ? p[1] : a[1] };
} else if (!diagonal || d < diagonal.d) diagonal = { correction, point: reverse ? p : foot, d };
};
for (const area of targets) {
const open = "open" in area && area.open;
for (let i = 0; i < area.points.length - (open ? 1 : 0); i++) {
const a = area.points[i], b = area.points[(i + 1) % area.points.length];
for (const p of moved) inspect(p, a, b);
}
// A longer moving edge can cover a shorter stationary edge without either moving corner projecting onto it.
for (const p of area.points) for (let i = 0; i < moved.length - (movingOpen ? 1 : 0); i++) inspect(p, moved[i], moved[(i + 1) % moved.length], true);
}
if (!x && !z && diagonal) return { by: [thousandths(delta[0] + diagonal.correction[0]), thousandths(delta[1] + diagonal.correction[1])], guides: { point: diagonal.point } };
return { by: [x ? thousandths(delta[0] + x.offset) : grid[0], z ? thousandths(delta[1] + z.offset) : grid[1]], guides: { ...(x ? { x: x.at } : {}), ...(z ? { z: z.at } : {}) } };
}
+13 -1
View File
@@ -962,13 +962,25 @@ export const editorStyles = css`
fill: rgba(61, 224, 160, 0.1);
stroke: rgba(61, 224, 160, 0.5);
}
.nf-out-pool polygon {
.nf-out-pool polygon,
.nf-out-river polygon,
.nf-out-stream polygon {
fill: color-mix(in srgb, var(--nf-accent) 18%, transparent);
stroke: var(--nf-accent);
}
.nf-out-terrace polygon {
fill: rgba(150, 130, 255, 0.12);
}
.nf-out-street polygon {
fill: rgba(100, 110, 125, 0.28);
}
.nf-out-sidewalk polygon {
fill: rgba(175, 185, 200, 0.24);
}
.nf-out-gravel polygon {
fill: rgba(184, 163, 130, 0.24);
stroke: rgba(184, 163, 130, 0.65);
}
.nf-free-wall {
cursor: grab;
}