66 lines
2.0 KiB
Markdown
66 lines
2.0 KiB
Markdown
---
|
||
title: 屏幕对齐网格
|
||
date: 2026-05-03 00:00:00
|
||
excerpt: 将网格直接渲染到屏幕空间,用于 UI/HUD 叠加元素
|
||
tags:
|
||
- ARC
|
||
- Rendering
|
||
- VertexFactory
|
||
rating: ⭐
|
||
---
|
||
|
||
# 屏幕对齐网格
|
||
|
||
返回 [[Rendering]]
|
||
|
||
## 概述
|
||
|
||
屏幕对齐网格(ScreenAlignedMesh)将 3D 网格直接投影到屏幕空间坐标系中渲染,用于实现 UI 元素、HUD 叠加或其他需要固定屏幕位置的网格效果。
|
||
|
||
## 实现
|
||
|
||
在所有顶点着色器中通过 `USES_SCREEN_ALIGNED_MESH` 宏启用:
|
||
|
||
```hlsl
|
||
#if USES_SCREEN_ALIGNED_MESH
|
||
{
|
||
float2 p = Input.Position.xz * flip;
|
||
float3 offset = GetMaterialScreenAlignedMeshOffset(VertexParameters);
|
||
float3 scale = GetMaterialScreenAlignedMeshScale(VertexParameters);
|
||
|
||
// 基于 1280x720 的参考分辨率
|
||
float2 ScreenPos = p.xy * scale.xy;
|
||
ScreenPos.x += offset.x / 640.0f;
|
||
ScreenPos.y += -offset.y / 360.0f;
|
||
ScreenPos.xy += GetMaterialWorldPositionOffset(VertexParameters).xy;
|
||
|
||
const float z = 1.0f - 0.00001f; // 接近远裁面
|
||
WorldPosition = 0;
|
||
Output.Position = float4(ScreenPos, z, 1);
|
||
}
|
||
#endif
|
||
```
|
||
|
||
关键点:
|
||
- 使用 `Input.Position.xz`(不是 xy)作为屏幕坐标输入
|
||
- 参考分辨率为 1280x720,offset 以像素为单位
|
||
- 深度设为接近远裁面(`1.0 - 0.00001`),确保不遮挡 3D 场景
|
||
- 跳过雾效计算(`TRANSLUCENCY_NEEDS_BASEPASS_FOGGING` 对 ScreenAlignedMesh 禁用)
|
||
|
||
## 材质控制
|
||
|
||
通过 [[自定义材质属性]] 中的两个属性控制:
|
||
- `MP_ScreenAlignedMeshOffset` — 屏幕偏移(像素,基于 1280x720)
|
||
- `MP_ScreenAlignedMeshScale` — 缩放系数
|
||
|
||
材质标记 `bScreenAlignedMesh` 启用此模式。
|
||
|
||
## 修改文件列表
|
||
|
||
| 文件 | 修改类型 |
|
||
|------|---------|
|
||
| `Shaders/Private/BasePassVertexShader.usf` | 屏幕空间变换逻辑 |
|
||
| `Shaders/Private/DepthOnlyVertexShader.usf` | 同上 |
|
||
| `Shaders/Private/HitProxyVertexShader.usf` | 同上 |
|
||
| `Shaders/Private/DebugViewModeVertexShader.usf` | 同上 |
|