《游戏编辑器技术原理与架构》4.2 坐标系、枢轴与吸附

同样的拖拽在不同坐标系/枢轴下行为不同。本节实现世界/局部坐标系转换、枢轴模式(中心/对象)与数值吸附。6 条用例钉住吸附取整、坐标轴转换与枢轴定位。对标 Unity 的 Pivot/Center、Global/Local 与 Snapping。

4.2 坐标系、枢轴与吸附

上一节的 Gizmo 有个前提:往哪条轴拖、绕哪个点转。这就是坐标系与枢轴:Global 用世界轴、Local 用对象自身轴;枢轴可以取对象位置,也可以取所选对象位置的算术平均值。吸附则让所有拖拽结果规整到网格。

本章与站内既有内容的分工

主题站内既有内容本节做什么
Gizmo 基础4.1 Gizmo 与变换工具上节做手柄;本节管手柄的参照系
网格吸附3.2 网格与参考叠加网格给候选步长;本节把吸附接到拖拽
全卷脉络本卷目录toc 列章节;枢轴与吸附决定拖拽的数值

从零实现:坐标系、枢轴与吸附

snapValue 把连续值规整到最近步长整数倍,snapAngle 处理旋转的度数吸附。axisToWorld 把局部轴向经旋转矩阵变换到世界轴;pivotPoint 按模式返回对象位置或选中位置的算术平均值。拖拽的值先过吸附再落到对象,保证规整。

对标轴向、枢轴和吸附

Unity 区分工具手柄的位置与朝向,平移手柄的网格吸附以 Global 朝向为条件。helio 的 axisToWorld 用示例规定的 XYZ 欧拉顺序变换轴。多选中心是输入位置的算术平均值,对象模式则使用各自位置。 Unity Grid Snapping · Unity EditorSnapSettings

可运行产物:6 条用例

下面是 src/gizmo-space.ts,层级 L1,完整可编译。运行命令 node --test test/04-2-space-pivot-snap.test.ts:

// file: src/gizmo-space.ts
// tier: L1
// verify: node --test test/04-2-space-pivot-snap.test.ts
//
// 视图(第四章):Gizmo 与变换工具——坐标系、枢轴与吸附。
// 同一根手柄在不同设置下含义不同:世界坐标系沿全局轴,本地坐标系沿物体
// 自身旋转后的轴;枢轴可以是多对象中心、各对象自身、或当前激活对象;
// 吸附把连续拖拽量化到离散步长。这三者是编辑器「手感」的核心开关。

import { vec3, scale, add, normalize, cross, dot } from './camera-control.ts';
import type { Vec3 } from './camera-control.ts';

export type CoordSpace = 'world' | 'local';
export type PivotMode = 'center' | 'individual' | 'active';
export type Axis = 'x' | 'y' | 'z';

/** 把值吸附到 step 的整数倍。 */
export function snapValue(value: number, step: number): number {
  if (step <= 0) return value;
  return Math.round(value / step) * step;
}

/** 把角度(弧度)吸附到按 degree 计的步长。 */
export function snapAngle(rad: number, degreeStep: number): number {
  if (degreeStep <= 0) return rad;
  const stepRad = (degreeStep * Math.PI) / 180;
  return Math.round(rad / stepRad) * stepRad;
}

/**
 * 把某轴上的单位位移转换到世界空间:world 直接返回,local 则按物体
 * 欧拉角旋转该轴。
 */
export function axisToWorld(space: CoordSpace, axis: Axis, rotation: Vec3): Vec3 {
  const base: Vec3 =
    axis === 'x' ? vec3(1, 0, 0) : axis === 'y' ? vec3(0, 1, 0) : vec3(0, 0, 1);
  if (space === 'world') return base;
  // XYZ 顺序的欧拉旋转(先 X 后 Y 后 Z)。
  const cx = Math.cos(rotation.x), sx = Math.sin(rotation.x);
  const cy = Math.cos(rotation.y), sy = Math.sin(rotation.y);
  const cz = Math.cos(rotation.z), sz = Math.sin(rotation.z);
  const afterX = vec3(base.x, base.y * cx - base.z * sx, base.y * sx + base.z * cx);
  const afterY = vec3(afterX.x * cy + afterX.z * sy, afterX.y, -afterX.x * sy + afterX.z * cy);
  const afterZ = vec3(afterY.x * cz - afterY.y * sz, afterY.x * sz + afterY.y * cz, afterY.z);
  return normalize(afterZ);
}

/** 根据枢轴模式求多选拖拽的旋转/缩放中心点。 */
export function pivotPoint(
  positions: Vec3[],
  mode: PivotMode,
  activeIndex = 0,
): Vec3 {
  if (positions.length === 0) return vec3(0, 0, 0);
  if (mode === 'individual') return positions[Math.min(activeIndex, positions.length - 1)];
  if (mode === 'active') return positions[Math.min(activeIndex, positions.length - 1)];
  let sum = vec3(0, 0, 0);
  for (const p of positions) sum = add(sum, p);
  return scale(sum, 1 / positions.length);
}

/** 枢轴模式下某个对象的旋转中心:individual 用自身,其余用统一枢轴。 */
export function objectPivot(
  position: Vec3,
  sharedPivot: Vec3,
  mode: PivotMode,
): Vec3 {
  return mode === 'individual' ? position : sharedPivot;
}

/** 世界轴与相机朝向的夹角余弦,用于判断轴是否近乎「看向屏幕」。 */
export function axisFacing(axisWorld: Vec3, viewDir: Vec3): number {
  return Math.abs(dot(normalize(axisWorld), normalize(viewDir)));
}

/** 右手系:给定两轴求第三轴。 */
export function thirdAxis(a: Vec3, b: Vec3): Vec3 {
  return normalize(cross(a, b));
}

axisToWorld 是 Global/Local 的核心:取对象的旋转矩阵,把局部轴乘出去。snapValue 的规整算法取 floor 到步长与 ceil 之间最近者。枢轴模式只在拖拽时起作用——对象自身位置不变,拖拽基准点不同。

下面是 test/04-2-space-pivot-snap.test.ts,层级 L1:

// file: test/04-2-space-pivot-snap.test.ts
// tier: L1
// verify: node --test test/04-2-space-pivot-snap.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  snapValue,
  snapAngle,
  axisToWorld,
  pivotPoint,
  objectPivot,
} from '../src/gizmo-space.ts';
import { vec3 } from '../src/camera-control.ts';

test('snapValue 量化到步长', () => {
  assert.equal(snapValue(2.3, 0.5), 2.5);
  assert.equal(snapValue(2.2, 0.5), 2);
  assert.equal(snapValue(7, 0), 7);
});

test('snapAngle 量化到角度步长', () => {
  const maxStep = 15;
  const r = snapAngle((Math.PI / 180) * 22, maxStep);
  const deg = (r * 180) / Math.PI;
  assert.ok(Math.abs(deg - 15) < 1e-9);
});

test('世界坐标系轴不随旋转变化', () => {
  const d = axisToWorld('world', 'x', vec3(0, Math.PI / 2, 0));
  assert.deepEqual(d, vec3(1, 0, 0));
});

test('本地坐标系轴随物体旋转', () => {
  const d = axisToWorld('local', 'x', vec3(0, Math.PI / 2, 0));
  assert.ok(Math.abs(d.x) < 1e-9);
  assert.ok(Math.abs(d.z + 1) < 1e-9);
});

test('枢轴:center 为质心、individual 为各对象自身', () => {
  const ps = [vec3(0, 0, 0), vec3(10, 0, 0)];
  assert.deepEqual(pivotPoint(ps, 'center'), vec3(5, 0, 0));
  assert.deepEqual(pivotPoint(ps, 'individual', 1), vec3(10, 0, 0));
  assert.deepEqual(objectPivot(vec3(3, 0, 0), vec3(5, 0, 0), 'individual'), vec3(3, 0, 0));
  assert.deepEqual(objectPivot(vec3(3, 0, 0), vec3(5, 0, 0), 'center'), vec3(5, 0, 0));
});

test('active 枢轴取激活对象', () => {
  const ps = [vec3(1, 1, 1), vec3(9, 9, 9)];
  assert.deepEqual(pivotPoint(ps, 'active', 0), vec3(1, 1, 1));
});

本机实测(Node v26.5.0,node --test test/04-2-space-pivot-snap.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉数值与角度吸附。第三条钉局部轴经旋转变世界轴。第四条钉枢轴按模式选取。第五、六条钉对象枢轴与吸附到最近点。

小结

  • 坐标系决定「拖哪条轴」,枢轴决定「绕哪个点」。
  • 吸附把连续拖拽规整到步长,数值干净。
  • Global/Local、Pivot/Center 是编辑器给用户的参照系选择。
  • 变换工具的数值纪律在本节定下。

阅读导航:上一节:Gizmo 与变换工具 · 下一节:对齐工具与约束 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路