4.3 对齐工具与约束
框选一批对象后,手工拖到对齐又慢又不准。对齐工具批量解决:让一批对象在某轴上对齐、等距分布、或沿固定平面移动。约束则让单个拖拽沿着锁定方向走。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 枢轴与吸附 | 4.2 坐标系、枢轴与吸附 | 单对象拖拽的参照;本节扩展成多对象批量 |
| 选择集合 | 2.3 框选与选择集合 | 多选输入来自选择集合 |
| 全卷脉络 | 本卷目录 | toc 列章节;对齐工具是变换章的收尾 |
从零实现:对齐工具与约束
alignPositions 把一批对象在某轴对齐到同一坐标(最小/最大/中心三种基准)。distributePositions 在最小最大值之间均匀拉开间距。constrainDelta 把任意位移向量投影到允许的轴向集合;constrainPlane 把位移限制到平面上。centerAround 把对象组整体平移到指定中心。
对标对齐与增量吸附
Unity 的增量吸附量化移动、旋转或缩放操作。helio 的对齐示例则批量处理已提供的坐标,在指定轴上设置共同位置或分配等距位置。几何边缘对齐与等间隙分布还需要对象包围范围。 Unity Grid Snapping
可运行产物:6 条用例
下面是 src/align-tool.ts,层级 L1,完整可编译。运行命令 node --test test/04-3-align-tool-constraints.test.ts:
// file: src/align-tool.ts
// tier: L1
// verify: node --test test/04-3-align-tool-constraints.test.ts
//
// 视图(第四章):Gizmo 与变换工具——对齐工具与约束。
// 除了逐对象拖拽,编辑器还提供批量排布:对齐(左/中/右统一到同一线)、
// 等距分布(首尾固定、中间均分)、轴约束(拖拽时锁定到单轴)。它们都是
// 对位置数组的纯函数变换,输入输出都不共享引用,便于撤销。
import { vec3 } from './camera-control.ts';
import type { Vec3 } from './camera-control.ts';
export type Axis3 = 'x' | 'y' | 'z';
export type AlignMode = 'min' | 'center' | 'max';
/** 按某轴对齐:所有对象在该轴取 min/mean/max。 */
export function alignPositions(
positions: Vec3[],
axis: Axis3,
mode: AlignMode,
): Vec3[] {
if (positions.length === 0) return [];
const values = positions.map((p) => p[axis]);
let target: number;
if (mode === 'min') target = Math.min(...values);
else if (mode === 'max') target = Math.max(...values);
else target = values.reduce((a, b) => a + b, 0) / values.length;
return positions.map((p) => ({ ...p, [axis]: target }) as Vec3);
}
/** 沿某轴等距分布:首尾对象不动,中间对象在首尾之间均分。 */
export function distributePositions(positions: Vec3[], axis: Axis3): Vec3[] {
const n = positions.length;
if (n < 3) return positions.map((p) => ({ ...p }));
const indexed = positions
.map((p, i) => ({ p, i }))
.sort((a, b) => a.p[axis] - b.p[axis]);
const first = indexed[0].p[axis];
const last = indexed[indexed.length - 1].p[axis];
const step = (last - first) / (n - 1);
const out = positions.map((p) => ({ ...p }));
indexed.forEach((entry, rank) => {
out[entry.i] = { ...out[entry.i], [axis]: first + step * rank } as Vec3;
});
return out;
}
/** 轴约束:把拖拽增量限制到单轴,其余轴归零;axis=null 表示自由。 */
export function constrainDelta(delta: Vec3, axis: Axis3 | null): Vec3 {
if (axis === null) return { ...delta };
return vec3(axis === 'x' ? delta.x : 0, axis === 'y' ? delta.y : 0, axis === 'z' ? delta.z : 0);
}
/** 平面约束:把拖拽增量投影到由两轴张成的平面。 */
export function constrainPlane(delta: Vec3, axes: [Axis3, Axis3]): Vec3 {
const keep = new Set(axes);
return vec3(
keep.has('x') ? delta.x : 0,
keep.has('y') ? delta.y : 0,
keep.has('z') ? delta.z : 0,
);
}
/** 把一组位置平移到以 target 为中点。 */
export function centerAround(positions: Vec3[], target: Vec3): Vec3[] {
if (positions.length === 0) return [];
const mean = vec3(
positions.reduce((s, p) => s + p.x, 0) / positions.length,
positions.reduce((s, p) => s + p.y, 0) / positions.length,
positions.reduce((s, p) => s + p.z, 0) / positions.length,
);
const delta = vec3(target.x - mean.x, target.y - mean.y, target.z - mean.z);
return positions.map((p) => vec3(p.x + delta.x, p.y + delta.y, p.z + delta.z));
}
对齐的语义要区分基准:min 对齐让所有对象贴到最小值侧,center 让中心对齐到组中心。distributePositions 保证首尾不动、中间均匀,最后一份间距和前面一致。约束是拖拽的过滤器:只保留允许轴的分量。
下面是 test/04-3-align-tool-constraints.test.ts,层级 L1:
// file: test/04-3-align-tool-constraints.test.ts
// tier: L1
// verify: node --test test/04-3-align-tool-constraints.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
alignPositions,
distributePositions,
constrainDelta,
constrainPlane,
centerAround,
} from '../src/align-tool.ts';
import { vec3 } from '../src/camera-control.ts';
test('对齐到 min/center/max', () => {
const ps = [vec3(0, 0, 0), vec3(4, 0, 0), vec3(10, 0, 0)];
assert.deepEqual(alignPositions(ps, 'x', 'min').map((p) => p.x), [0, 0, 0]);
assert.deepEqual(alignPositions(ps, 'x', 'max').map((p) => p.x), [10, 10, 10]);
const mid = alignPositions(ps, 'x', 'center');
assert.equal(mid[0].x, mid[2].x);
assert.ok(Math.abs(mid[0].x - 14 / 3) < 1e-9);
});
test('等距分布:首尾不动、中间均分', () => {
const ps = [vec3(0, 0, 0), vec3(1, 0, 0), vec3(9, 0, 0)];
const out = distributePositions(ps, 'x');
assert.deepEqual(out.map((p) => p.x), [0, 4.5, 9]);
});
test('少于 3 个对象分布不变', () => {
const ps = [vec3(0, 0, 0), vec3(5, 0, 0)];
assert.deepEqual(distributePositions(ps, 'x'), ps);
});
test('轴约束只保留单轴', () => {
const d = vec3(1, 2, 3);
assert.deepEqual(constrainDelta(d, 'y'), vec3(0, 2, 0));
assert.deepEqual(constrainDelta(d, null), d);
});
test('平面约束保留两轴', () => {
assert.deepEqual(constrainPlane(vec3(1, 2, 3), ['x', 'z']), vec3(1, 0, 3));
});
test('centerAround 把中点移到目标', () => {
const ps = [vec3(0, 0, 0), vec3(10, 0, 0)];
const out = centerAround(ps, vec3(0, 0, 0));
assert.deepEqual(out, [vec3(-5, 0, 0), vec3(5, 0, 0)]);
});
本机实测(Node v26.5.0,node --test test/04-3-align-tool-constraints.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉 min/center 对齐。第三条钉均匀分布间距。第四条钉中心居中。第五、六条钉轴向与平面约束。
小结
- 对齐 = 按基准批量改坐标,min/center/max 三种。
- 分布 = 首尾不动中间均匀,间距可断言。
- 约束 = 拖拽位移的投影过滤。
- 变换章四节齐了:手柄、参照、对齐、约束。
阅读导航:上一节:坐标系、枢轴与吸附 · 下一节:Inspector 的数据模型与嵌套数组 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。