float2 screenUV = GetDefaultSceneTextureUV(Parameters, 25);
float2 texelSize = View.ViewSizeAndInvSize.zw;
// 确保 sampleCount 至少为 1,避免除以 0 的错误
// 注意:在 Custom 节点中,你需要添加两个输入引脚:
// 1. samples (例如 16, 32,采样点数量)
// 2. strokeRadius (例如 2.0, 5.0,描边的像素半径)
int sampleCount = max(1, (int)samples);
int targetCount = 0;
// 黄金角,约 137.5 度 (弧度制)
float goldenAngle = 2.39996323;
for (int i = 0; i < sampleCount; ++i)
{
// Vogel 螺旋圆盘分布:计算当前点的归一化面积距离 (t) 和半径 (r)
float t = (i + 0.5) / sampleCount;
float r = sqrt(t) * pixelRadius;
float angle = i * goldenAngle;
// 计算当前采样点的偏移
float2 offset = float2(cos(angle), sin(angle)) * r * texelSize;
float2 sampleUV = saturate(screenUV + offset);
// 采样 CustomStencil (25)
MaterialFloat4 stencilSample = SceneTextureLookup(Parameters, sampleUV, 25, false);
// 读取 R 通道并四舍五入
float stencil = round(stencilSample.r);
targetCount += (stencil == targetStencil);
}
// 判断边缘逻辑:
// targetCount 必须大于 0(说明碰到了目标 Stencil)
// targetCount 必须小于 sampleCount(说明没被目标完全填满,碰到了背景或其他物体)
// 使用 0.5 和 sampleCount - 0.5 作为 step 的边界来替代原来的 0.01 和 8.99
return step(0.5, targetCount) * step(targetCount, sampleCount - 0.5 - thresholdBias);在 UE 材质的 Custom 节点中使用这段代码,需要为节点添加以下 4 个输入引脚(Inputs) :
1. samples (Float / Int):
– 作用 :总采样次数。
– 建议值 : 8 、 16 或 32 。值越大,粗描边越平滑,但性能开销越高。
2. pixelRadius (Float):
– 作用 :描边的粗细(以绝对像素为单位)。
– 建议值 : 1.0 到 5.0 。
3. targetStencil (Float / Int):
– 作用 :你想给哪个 Stencil ID 的物体描边。
– 例如 :如果你在场景中把主角的 Custom Depth Stencil 设为了 2 ,那这里就输入 2.0 。
4. thresholdBias (Float):
– 作用 :边缘判定的严格程度阈值偏移(通常用于消除由于抗锯齿 TAA 导致的细微杂边)。
– 建议值 :默认填 0.0 。如果发现描边内部有空洞或杂点,可以尝试微调。




没有回复内容