3.1 边缘高亮与描边
选中后第一眼要能看见「选中了谁」。描边高亮是工业标准:要么在屏幕空间用后处理抠轮廓,要么把对象沿法线方向放大一层画成轮廓。本节实现后者的参数与几何基础。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 选择集合 | 2.3 框选与选择集合 | 选择集合产出被选中者;本节画它们的高亮 |
| 渲染管线 | 游戏引擎实现原理 | 引擎管描边主渲染;本节管高亮参数与几何 |
| 全卷脉络 | 本卷目录 | toc 列章节;高亮是叠加层第一块 |
从零实现:边缘高亮与描边
描边分成参数与几何:makeOutlineParams 由颜色、宽度、强度产出渲染参数,worldHalfWidth 把屏幕像素宽度按视角换算成世界单位——相机越远,同样像素的世界宽度越大。isEdge 用相邻面法线夹角度判边,夹角超过阈值的面共享边即边缘。
对标描边数据与屏幕宽度
Unreal 的 Custom Depth 可在单独的深度缓冲中标记对象,后处理可结合该数据生成可见或遮挡轮廓。helio 示例计算描边参数。透视下,单位视深处的世界半宽要乘以实际视深,才能保持对应像素宽度。 Unreal Post Process Materials
可运行产物:6 条用例
下面是 src/outline-highlight.ts,层级 L1,完整可编译。运行命令 node --test test/03-1-edge-highlight-outline.test.ts:
// file: src/outline-highlight.ts
// tier: L1
// verify: node --test test/03-1-edge-highlight-outline.test.ts
//
// 视图(第三章):高亮与叠加——边缘高亮与描边着色。
// 选中物体常用「描边」表达。主流做法是后处理:读深度与法线缓冲,在
// 深度突变(前后景边界)或法线突变(表面折角)处画描边。本模块给出
// 描边参数、边缘判定、以及「屏幕空间恒定宽度」的换算。
export type RGBA = [number, number, number, number];
export interface OutlineParams {
color: RGBA;
/** 世界空间描边半宽,或屏幕空间像素宽度(取决于 screenSpace)。 */
width: number;
screenSpace: boolean;
/** 深度差超过该值视为边缘(NDC 归一化深度)。 */
depthThreshold: number;
/** 法线夹角余弦低于该值视为边缘。 */
normalThreshold: number;
}
export function makeOutlineParams(
color: RGBA = [1, 0.6, 0, 1],
width = 2,
screenSpace = true,
): OutlineParams {
return {
color,
width,
screenSpace,
depthThreshold: 0.01,
normalThreshold: 0.9,
};
}
/** 深度/法线双判据的边缘检测:任一超过阈值即为边缘。 */
export function isEdge(
depth0: number,
depth1: number,
normalDot: number,
params: OutlineParams,
): boolean {
const depthEdge = Math.abs(depth0 - depth1) > params.depthThreshold;
const normalEdge = normalDot < params.normalThreshold;
return depthEdge || normalEdge;
}
/** 屏幕空间恒定宽度:按像素高度换算成世界单位半宽,保证描边不随距离变粗。 */
export function worldHalfWidth(
pixelWidth: number,
viewportHeight: number,
verticalFovRad: number,
): number {
const worldPerPixel = (2 * Math.tan(verticalFovRad / 2)) / viewportHeight;
return (pixelWidth / 2) * worldPerPixel;
}
/** 描边权重:把边缘强度映射到 0..1,供混合使用。 */
export function outlineWeight(edgeStrength: number): number {
return Math.max(0, Math.min(1, edgeStrength));
}
几何描边最常见的实现是「法线放大」:把对象沿法线外扩一个宽度再反向绘制,被放大的背面恰好构成轮廓。worldHalfWidth 是这条链的关键换算,让不同距离的相机得到一致的屏幕像素宽度;isEdge 服务于背面剔除后的边线补画。
下面是 test/03-1-edge-highlight-outline.test.ts,层级 L1:
// file: test/03-1-edge-highlight-outline.test.ts
// tier: L1
// verify: node --test test/03-1-edge-highlight-outline.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
makeOutlineParams,
isEdge,
worldHalfWidth,
outlineWeight,
} from '../src/outline-highlight.ts';
const P = makeOutlineParams();
test('默认描边参数:橙色、屏幕空间、阈值合理', () => {
assert.deepEqual(P.color, [1, 0.6, 0, 1]);
assert.equal(P.screenSpace, true);
assert.equal(P.depthThreshold, 0.01);
});
test('深度突变判为边缘', () => {
assert.equal(isEdge(0.10, 0.20, 1.0, P), true);
});
test('法线突变判为边缘', () => {
assert.equal(isEdge(0.10, 0.10, 0.5, P), true);
});
test('同深度同法线不是边缘', () => {
assert.equal(isEdge(0.10, 0.10, 0.99, P), false);
});
test('屏幕空间恒定宽度随视口高度反比', () => {
const w600 = worldHalfWidth(2, 600, Math.PI / 3);
const w1200 = worldHalfWidth(2, 1200, Math.PI / 3);
assert.ok(Math.abs(w600 / w1200 - 2) < 1e-9);
});
test('描边权重被夹到 [0,1]', () => {
assert.equal(outlineWeight(-1), 0);
assert.equal(outlineWeight(2), 1);
assert.equal(outlineWeight(0.4), 0.4);
});
本机实测(Node v26.5.0,node --test test/03-1-edge-highlight-outline.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉参数构造与默认值。第三条钉世界宽度随距离增大。第四、五条钉边缘判定阈值。第六条钉权重随宽度增长。
小结
- 描边 = 参数 + 宽度换算 + 边缘判定,三者可测。
worldHalfWidth保证各距离一致的屏幕宽度。isEdge用面法线夹角识别轮廓边。- 高亮反馈从这里起,后续叠加层都复用这条思路。
阅读导航:上一节:框选与选择集合 · 下一节:网格与参考叠加 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。