1

这是一个 HLSL 问题,尽管如果您想在答案中引用该框架,我正在使用 XNA。

在 XNA 4.0 中,我们不再能够访问 DX9 的 AlphaTest 功能。

我想要:

  1. 将纹理渲染到后缓冲区,仅绘制纹理的不透明像素。
  2. 渲染一个纹理,它的纹素只在步骤 1 中没有绘制不透明像素的地方绘制。

我怎样才能做到这一点?如果我需要在 HLSL 中使用 clip(),如何从我的 HLSL 代码中检查在步骤 1 中绘制的模板缓冲区?

到目前为止,我已经完成了以下工作:

_sparkStencil = new DepthStencilState
{
    StencilEnable = true,
    StencilFunction = CompareFunction.GreaterEqual,
    ReferenceStencil = 254,
    DepthBufferEnable = true
};


DepthStencilState old = gd.DepthStencilState;
gd.DepthStencilState = _sparkStencil;

// Only opaque texels should be drawn.
DrawTexture1();

gd.DepthStencilState = old;

// Texels that were rendered from texture1 should
// prevent texels in texture 2 from appearing.
DrawTexture2();
4

2 回答 2

0

听起来您只想在第一次绘制完整 Alpha (1.0, 255) 的 epsilon 内的像素,而第二次不影响完整 Alpha 的 epsilon 内的像素。

我不是图形专家,而且我的睡眠时间太短,但是您应该可以通过效果脚本文件从这里到达那里。

于 2011-02-19T23:24:32.023 回答
0

要写入模板缓冲区,您必须创建一个写入缓冲区的 DepthStencilState,然后绘制要绘制到模板缓冲区的任何几何图形,然后切换到使用相关 CompareFunction 的不同 DepthStencilState。

如果将哪些 alpha 值绘制到模板缓冲区有一些限制,则在第一遍中使用一个着色器,该着色器调用clip()内部函数,floor(alpha - val) - 1其中val(0,1) 中的一个数字限制了绘制的 alpha 值。

我在这里写了一个更详细的答案:

XNA 4 中的模板测试

于 2011-02-20T15:46:06.433 回答