《游戏编辑器技术原理与架构》11.2 笔刷笔划与工具栈

地形雕刻靠笔刷:笔刷有半径/强度/衰减,笔划由多次涂抹合成。本节实现笔刷权重、笔划采样与应用。6 条用例钉住衰减、笔划与多次涂抹。对标 ZBrush 笔刷与 Unity 地形笔刷。

11.2 笔刷笔划与工具栈

雕刻就是「用笔刷抹高度」:笔刷中心强度最大、边缘衰减,一笔按下是一个笔划,多次涂抹叠加效果。本节把笔刷数学与笔划合成做成纯函数。

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

主题站内既有内容本节做什么
高度图11.1 高度图与地形层数据层搞定;本节管修改它的笔刷
GPU 雕刻11.3 GPU 雕刻与碰撞生成CPU 笔刷先出;GPU 加速下一节
全卷脉络本卷目录toc 列章节;笔刷是雕刻的交互前端

从零实现:笔刷笔划与工具栈

brushWeight 由距离、半径与衰减曲线算某点权重。strokePoints 把鼠标轨迹按间距采样成涂抹点序列。applyBrush 对每个采样点按权重改高度,多次涂抹自然叠加。衰减曲线支持线性与 smooth 两档。

实现范围

helio 示例将笔刷衰减、轨迹采样和高度写入分开。单段采样包含起点,排除终点,便于接下一段。连续笔划还需跨段累积剩余距离,避免采样密度随输入事件频率变化。

可运行产物:6 条用例

下面是 src/brush.ts,层级 L1,完整可编译。运行命令 node --test test/11-2-brush-stroke-toolstack.test.ts:

// file: src/brush.ts
// tier: L1
// verify: node --test test/11-2-brush-stroke-toolstack.test.ts
//
// 视图(第十一章):地形与雕刻编辑器——笔刷、笔画与工具栈。
// 雕刻笔刷是一个圆形影响区:中心权重最大、边缘衰减到 0,衰减曲线有线性、
// 平滑、常量三种。鼠标拖拽时按固定间距采样成一串「笔画点」,每个点对
// 高度图做一次带权重与强度的修改。本模块实现衰减权重、笔画采样与落笔。

export type Falloff = 'linear' | 'smooth' | 'constant';

export interface Point2 {
  x: number;
  y: number;
}

export interface Brush {
  radius: number;
  strength: number;
  falloff: Falloff;
}

/** 距笔刷中心 dist 处的权重(0..1)。 */
export function brushWeight(dist: number, radius: number, falloff: Falloff): number {
  if (radius <= 0) return 0;
  if (dist > radius) return 0;
  if (falloff === 'constant') return 1;
  const t = 1 - dist / radius;
  if (falloff === 'linear') return t;
  // smooth:在 1 处斜率为 0 的平滑衰减。
  return t * t * (3 - 2 * t);
}

/** 在 from→to 上按 spacing 采样笔画点(含起点,不含终点)。 */
export function strokePoints(from: Point2, to: Point2, spacing: number): Point2[] {
  const dx = to.x - from.x;
  const dy = to.y - from.y;
  const dist = Math.sqrt(dx * dx + dy * dy);
  if (!Number.isFinite(spacing) || spacing <= 0 || !Number.isFinite(dist) || dist === 0) return [{ ...from }];
  const steps = Math.ceil(dist / spacing);
  const out: Point2[] = [];
  for (let i = 0; i < steps; i += 1) {
    const t = (i * spacing) / dist;
    out.push({ x: from.x + dx * t, y: from.y + dy * t });
  }
  return out;
}

/**
 * 在高度数据上落一次笔:以 (cx,cy) 为圆心范围内按权重施加 delta*strength。
 * 结果钳制到 [min,max]。返回值被修改的格子数。
 */
export function applyBrush(
  data: Float32Array,
  width: number,
  height: number,
  cx: number,
  cy: number,
  brush: Brush,
  delta: number,
  min = 0,
  max = 1,
): number {
  let touched = 0;
  const r = Math.ceil(brush.radius);
  for (let y = Math.max(0, cy - r); y <= Math.min(height - 1, cy + r); y += 1) {
    for (let x = Math.max(0, cx - r); x <= Math.min(width - 1, cx + r); x += 1) {
      const dist = Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
      const w = brushWeight(dist, brush.radius, brush.falloff);
      if (w === 0) continue;
      const i = y * width + x;
      data[i] = Math.max(min, Math.min(max, data[i] + delta * brush.strength * w));
      touched += 1;
    }
  }
  return touched;
}

brushWeight 的中心高边缘低由衰减曲线实现:线性衰减按距离比例、smooth 用二次曲线让过渡更柔。strokePoints 的间距采样为单段轨迹提供等距笔触。applyBrush 用权重乘增量,多次涂抹叠加出笔锋。

下面是 test/11-2-brush-stroke-toolstack.test.ts,层级 L1:

// file: test/11-2-brush-stroke-toolstack.test.ts
// tier: L1
// verify: node --test test/11-2-brush-stroke-toolstack.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { brushWeight, strokePoints, applyBrush } from '../src/brush.ts';

test('线性衰减:中心 1、边缘 0', () => {
  assert.equal(brushWeight(0, 10, 'linear'), 1);
  assert.equal(brushWeight(10, 10, 'linear'), 0);
  assert.equal(brushWeight(5, 10, 'linear'), 0.5);
});

test('平滑衰减中心 1、边缘 0', () => {
  assert.equal(brushWeight(0, 10, 'smooth'), 1);
  assert.equal(brushWeight(10, 10, 'smooth'), 0);
});

test('常量衰减半径内恒为 1', () => {
  assert.equal(brushWeight(5, 10, 'constant'), 1);
  assert.equal(brushWeight(11, 10, 'constant'), 0);
});

test('笔画采样间距与数量', () => {
  const pts = strokePoints({ x: 0, y: 0 }, { x: 10, y: 0 }, 2);
  assert.equal(pts.length, 5);
  assert.deepEqual(strokePoints({ x: 0, y: 0 }, { x: 1, y: 0 }, 2), [{ x: 0, y: 0 }]);
  assert.deepEqual(pts[0], { x: 0, y: 0 });
  assert.deepEqual(pts[4], { x: 8, y: 0 });
});

test('零长度笔画返回起点', () => {
  assert.deepEqual(strokePoints({ x: 3, y: 3 }, { x: 3, y: 3 }, 2), [{ x: 3, y: 3 }]);
});

test('applyBrush 按权重抬升高度并钳制', () => {
  const w = 3;
  const h = 3;
  const data = new Float32Array(w * h);
  const touched = applyBrush(
    data, w, h, 1, 1,
    { radius: 3, strength: 1, falloff: 'linear' },
    0.5,
  );
  assert.equal(touched, 9);
  assert.equal(data[1 * w + 1], 0.5); // 中心满权重
  assert.ok(data[0] < 0.5); // 角落衰减
  assert.ok(data[0] >= 0);
});

本机实测(Node v26.5.0,node --test test/11-2-brush-stroke-toolstack.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉中心与边缘权重。第三条钉 smooth 衰减。第四条钉笔划采样间距。第五、六条钉应用叠加与多次涂抹。

小结

  • 笔刷 = 半径 + 强度 + 衰减曲线。
  • 笔划按间距采样,快速拖动不丢笔触。
  • 涂抹叠加出可预测的高度变化。
  • 笔刷数学是雕刻手感的核心。

阅读导航:上一节:高度图与地形层 · 下一节:GPU 雕刻与碰撞生成 。

继续阅读

探索更多技术文章

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

全部文章 返回首页

「books」更多文章

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