XNA 4 中的模板测试

发布于 2024-10-18 08:53:54 字数 1111 浏览 1 评论 0原文

我能够在 XNA 3.1 中执行此操作,但是我看到我们现在在 XNA 4 中使用状态对象,这当然是一个改进,尽管到目前为止我无法完成我想要的:)

我正在尝试:

清除模板buffer 设置为 0。

将纹理绘制到模板缓冲区,将绘制纹理的模板缓冲区设置为 1。

绘制另一个纹理,该纹理仅出现在模板缓冲区不为 1 的地方。

这是我到目前为止所做的,它似乎对纹理 2 的绘制没有影响:

BlendState blend = new BlendState();
blend.ColorWriteChannels = ColorWriteChannels.None;


_preSparkStencil = new DepthStencilState();
_preSparkStencil.StencilEnable = true;
_preSparkStencil.StencilFunction = CompareFunction.Always;
_preSparkStencil.ReferenceStencil = 1;
_preSparkStencil.DepthBufferEnable = true;

_sparkStencil = new DepthStencilState();
_sparkStencil.StencilEnable = true;
_sparkStencil.StencilFunction = CompareFunction.NotEqual;
_sparkStencil.ReferenceStencil = 1;
_sparkStencil.DepthBufferEnable = true;

gd.DepthStencilState = _preSparkStencil;

gd.Clear(ClearOptions.Stencil, Color.Black, 0, 0);

sb.Begin(SpriteSortMode.Deferred, blend);

DrawTexture1();

sb.End();

gd.DepthStencilState = _sparkStencil;

sb.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
DrawTexture2();
sb.End();

gd.DepthStencilState = old;

I was able to do this in XNA 3.1, however I see that we now use state objects in XNA 4, which is certainly an improvement, although I can't accomplish what I want so far :)

I am trying to:

Clear the stencil buffer to 0.

Draw a texture to the stencil buffer, setting the stencil buffer to 1 where the texture is drawn.

Draw another texture that will only appear where the stencil buffer is not 1.

Here is what I have so far, which appears to have no effect on the drawing of texture 2:

BlendState blend = new BlendState();
blend.ColorWriteChannels = ColorWriteChannels.None;


_preSparkStencil = new DepthStencilState();
_preSparkStencil.StencilEnable = true;
_preSparkStencil.StencilFunction = CompareFunction.Always;
_preSparkStencil.ReferenceStencil = 1;
_preSparkStencil.DepthBufferEnable = true;

_sparkStencil = new DepthStencilState();
_sparkStencil.StencilEnable = true;
_sparkStencil.StencilFunction = CompareFunction.NotEqual;
_sparkStencil.ReferenceStencil = 1;
_sparkStencil.DepthBufferEnable = true;

gd.DepthStencilState = _preSparkStencil;

gd.Clear(ClearOptions.Stencil, Color.Black, 0, 0);

sb.Begin(SpriteSortMode.Deferred, blend);

DrawTexture1();

sb.End();

gd.DepthStencilState = _sparkStencil;

sb.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
DrawTexture2();
sb.End();

gd.DepthStencilState = old;

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

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

发布评论

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

评论(1

丑丑阿 2024-10-25 08:53:54

问题是 RenderState 需要传递到 SpriteBatch 中,否则 SpriteBatch 将使用它自己的 RenderState。

    sb.Begin(SpriteSortMode.Deferred, BlendState.Opaque, 
        SamplerState.LinearWrap, _preSparkStencil, 
        RasterizerState.CullCounterClockwise, CLM.AlphaClip);

The problem was that the RenderState needs to be passed into SpriteBatch, or else the SpriteBatch will use it's own RenderState.

    sb.Begin(SpriteSortMode.Deferred, BlendState.Opaque, 
        SamplerState.LinearWrap, _preSparkStencil, 
        RasterizerState.CullCounterClockwise, CLM.AlphaClip);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文