XNA 4 中的模板测试 [英] Stencil testing in XNA 4
问题描述
我能够在 XNA 3.1 中做到这一点,但是我看到我们现在在 XNA 4 中使用状态对象,这当然是一个改进,尽管到目前为止我无法完成我想要的 :)
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 :)
我正在尝试:
将模板缓冲区清零.
将纹理绘制到模板缓冲区,将绘制纹理的模板缓冲区设置为 1.
Draw a texture to the stencil buffer, setting the stencil buffer to 1 where the texture is drawn.
绘制另一个纹理,该纹理只会出现在模板缓冲区不是 1 的地方.
Draw another texture that will only appear where the stencil buffer is not 1.
这是我到目前为止所拥有的,它似乎对纹理 2 的绘制没有影响:
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;
推荐答案
问题是RenderState需要传入SpriteBatch,否则SpriteBatch会使用自己的RenderState.
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);
这篇关于XNA 4 中的模板测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!