如何使用clip()来执行alpha测试?

发布于 2024-10-18 08:31:56 字数 780 浏览 2 评论 0原文

这是一个 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();

This is an HLSL question, although I'm using XNA if you want to reference that framework in your answer.

In XNA 4.0 we no longer have access to DX9's AlphaTest functionality.

I want to:

  1. Render a texture to the backbuffer, only drawing the opaque pixels of the texture.
  2. Render a texture, whose texels are only drawn in places where no opaque pixels from step 1 were drawn.

How can I accomplish this? If I need to use clip() in HLSL, how to I check the stencilbuffer that was drawn to in step 1, from within my HLSL code?

So far I have done the following:

_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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-10-25 08:31:56

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

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

Sounds like you want to only draw pixels that are within epsilon of full Alpha (1.0, 255) the first time, while not affecting pixels that are within epsilon of full Alpha the second.

I'm not a graphics expert and I'm operating on too little sleep, but you should be able to get there from here through an effect script file.

最单纯的乌龟 2024-10-25 08:31:56

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

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

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

XNA 4 中的模板测试

To write to the stencil buffer you must create a DepthStencilState that writes to the buffer, then draw any geometry that is to be drawn to the stencil buffer, then switch to a different DepthStencilState that uses the relevant CompareFunction.

If there is some limit on which alpha values are to be drawn to the stencil buffer, then use a shader in the first pass that calls the clip() intrinsic on floor(alpha - val) - 1 where val is a number in (0,1) that limits the alpha values drawn.

I have written a more detailed answer here:

Stencil testing in XNA 4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文