如何使用clip()来执行alpha测试?
这是一个 HLSL 问题,尽管如果您想在答案中引用该框架,我正在使用 XNA。
在 XNA 4.0 中,我们不再能够访问 DX9 的 AlphaTest 功能。
我想要:
- 将纹理渲染到后缓冲区,仅绘制纹理的不透明像素。
- 渲染纹理,其纹素仅绘制在步骤 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:
- Render a texture to the backbuffer, only drawing the opaque pixels of the texture.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您只想第一次绘制完全 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.
要写入模板缓冲区,您必须创建一个写入缓冲区的 DepthStencilState,然后绘制要绘制到模板缓冲区的任何几何图形,然后切换到使用相关 CompareFunction 的不同 DepthStencilState。
如果对将哪些 alpha 值绘制到模板缓冲区有一些限制,则在第一遍中使用着色器来调用
floor(alpha - val) 上的
其中clip()
内在函数- 1val
是 (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 onfloor(alpha - val) - 1
whereval
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