3.3 透明与遮挡叠加
场景里总有「看不见」的麻烦:墙挡住背后的灯、玻璃半透明看不清里面。编辑模式对透明和遮挡的处理要比运行时宽容——工程师需要透视、需要知道背后有什么。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 网格叠加 | 3.2 网格与参考叠加 | 网格给空间参照;透明给穿透能力 |
| 渲染管线 | 游戏引擎实现原理 | 引擎管运行时混合;本节管编辑模式排序 |
| 全卷脉络 | 本卷目录 | toc 列章节;叠加层三件套在此收束 |
从零实现:透明与遮挡叠加
透明正确渲染的第一件事是排序:sortBackToFront 按相机到对象的距离从远到近排,先画远的再混合近的。isOccluded 判定一个点是否被另一对象挡住(距离远近 + 是否穿过 AABB)。alphaCutout 把低于阈值的 alpha 裁剪掉,blendOver 用源/目标 alpha 做标准透明混合。
对标遮挡、排序与混合
Unreal 的 Custom Depth 示例展示如何在后处理区分可见与遮挡对象。helio 用深度值演示遮挡判定,并用由远至近的顺序近似叠加透明对象。对象互相穿插时,仅按对象深度排序无法保证每个像素的顺序正确。 Unreal Post Process Materials
可运行产物:6 条用例
下面是 src/occlusion.ts,层级 L1,完整可编译。运行命令 node --test test/03-3-transparency-occlusion.test.ts:
// file: src/occlusion.ts
// tier: L1
// verify: node --test test/03-3-transparency-occlusion.test.ts
//
// 视图(第三章):高亮与叠加——半透明与遮挡处理。
// 半透明物体必须按「由远及近」绘制才能得到正确的混合结果;被完全不
// 透明的物体挡住的半透明片可以整片剔除;alpha 低于阈值的片按全透明
// 处理以省填充率。本模块把这三件事写成可测的纯函数。
export interface TransparentItem {
id: string;
/** 相机空间深度(正数,越大越远)。 */
depth: number;
alpha: number;
}
/** 半透明排序:由远及近(深度大在前)。同深度保持稳定顺序。 */
export function sortBackToFront(items: TransparentItem[]): TransparentItem[] {
return items
.map((it, i) => ({ it, i }))
.sort((a, b) => b.it.depth - a.it.depth || a.i - b.i)
.map((x) => x.it);
}
export interface OcclusionSample {
/** 半透明片在此像素的深度。 */
fragmentDepth: number;
/** 深度缓冲(不透明几何)在此像素的深度。 */
opaqueDepth: number;
/** 该片 alpha。 */
alpha: number;
}
/** 半透明片是否被不透明几何完全遮挡。 */
export function isOccluded(sample: OcclusionSample, epsilon = 1e-4): boolean {
return sample.fragmentDepth > sample.opaqueDepth + epsilon;
}
/** alpha 低于阈值的片直接丢弃。 */
export function alphaCutout(alpha: number, threshold = 0.01): boolean {
return alpha < threshold;
}
export type RGB = [number, number, number];
/** 标准 over 混合:result = overlay*alpha + base*(1-alpha)。 */
export function blendOver(base: RGB, overlay: RGB, alpha: number): RGB {
return [
overlay[0] * alpha + base[0] * (1 - alpha),
overlay[1] * alpha + base[1] * (1 - alpha),
overlay[2] * alpha + base[2] * (1 - alpha),
];
}
编辑模式常把「遮挡的」画成线框或半透明,让被挡对象仍然可见。isOccluded 为这类叠加提供判定:视线路径上有更近的对象就算遮挡。blendOver 用给定的 alpha 把前景颜色合成到不透明背景,输出 RGB。若背景也有透明度,应另外保留和计算输出 alpha。
下面是 test/03-3-transparency-occlusion.test.ts,层级 L1:
// file: test/03-3-transparency-occlusion.test.ts
// tier: L1
// verify: node --test test/03-3-transparency-occlusion.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
sortBackToFront,
isOccluded,
alphaCutout,
blendOver,
} from '../src/occlusion.ts';
import type { RGB } from '../src/occlusion.ts';
test('半透明排序:由远及近', () => {
const items = [
{ id: 'near', depth: 1, alpha: 0.5 },
{ id: 'far', depth: 9, alpha: 0.5 },
{ id: 'mid', depth: 5, alpha: 0.5 },
];
assert.deepEqual(
sortBackToFront(items).map((x) => x.id),
['far', 'mid', 'near'],
);
});
test('同深度保持稳定顺序', () => {
const items = [
{ id: 'a', depth: 3, alpha: 0.5 },
{ id: 'b', depth: 3, alpha: 0.5 },
];
assert.deepEqual(sortBackToFront(items).map((x) => x.id), ['a', 'b']);
});
test('落在不透明几何后面的片被遮挡', () => {
assert.equal(isOccluded({ fragmentDepth: 9, opaqueDepth: 5, alpha: 0.5 }), true);
assert.equal(isOccluded({ fragmentDepth: 3, opaqueDepth: 5, alpha: 0.5 }), false);
});
test('alpha 低于阈值被剔除', () => {
assert.equal(alphaCutout(0.005), true);
assert.equal(alphaCutout(0.5), false);
});
test('over 混合半透明叠加', () => {
const base: RGB = [0, 0, 0];
const over: RGB = [1, 1, 1];
assert.deepEqual(blendOver(base, over, 0.5), [0.5, 0.5, 0.5]);
assert.deepEqual(blendOver(base, over, 1), [1, 1, 1]);
});
test('alpha=0 完全不改变底色', () => {
const base: RGB = [0.2, 0.4, 0.6];
assert.deepEqual(blendOver(base, [1, 0, 0], 0), base);
});
本机实测(Node v26.5.0,node --test test/03-3-transparency-occlusion.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉远近距离排序。第三条钉遮挡判定。第四条钉 alpha 裁剪阈值。第五、六条钉半透明混合结果与顺序敏感性。
小结
- 透明正确性 = 先排序后混合。
isOccluded为穿透/线框提示提供判定。blendOver是叠加层共享的混合原语。- 高亮与叠加三件套齐了:描边、网格、透明。
阅读导航:上一节:网格与参考叠加 · 下一节:Gizmo 与变换工具 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。