开发者

Trying to Understand Stencils (XNA 4.0, Windows Phone 7)

开发者 https://www.devze.com 2023-02-28 09:23 出处:网络
I\'m trying to understand stencils.I could use a good tutorial explaining how they work, but in the mean time here\'s what I\'m working with:

I'm trying to understand stencils. I could use a good tutorial explaining how they work, but in the mean time here's what I'm working with:

DepthStencilState _StencilAlways;
DepthStencilSta开发者_如何转开发te _StencilKeepIfZero;

SpriteBatch _StencilBatch;
SpriteBatch _MaskBatch;

_StencilAlways = new DepthStencilState();
_StencilAlways.StencilEnable = true;
_StencilAlways.StencilFunction = CompareFunction.Always;
_StencilAlways.StencilPass = StencilOperation.Replace;
_StencilAlways.ReferenceStencil = 1;
_StencilAlways.DepthBufferEnable = false; 

_StencilKeepIfZero = new DepthStencilState();
_StencilKeepIfZero.StencilEnable = true;
_StencilKeepIfZero.StencilFunction = CompareFunction.Equal;
_StencilKeepIfZero.StencilPass = StencilOperation.Keep;
_StencilKeepIfZero.ReferenceStencil = 0;
_StencilKeepIfZero.DepthBufferEnable = false;

RenderTarget2D MaskRenderTarget = new RenderTarget2D(device, Width, Height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents);

GraphicsDevice.SetRenderTarget(MaskRenderTarget);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, new Color(0, 0, 0, 1), 0, 0);

_MaskBatch.Begin(SpriteSortMode.Immediate, null, null, _StencilAlways, null);
_MaskBatch.Draw(
    Texture,
    Position,
    null,
    Shade,
    0,
    Vector2.Zero,
    Scale,
    SpriteEffects.None,
    0);
_MaskBatch.End();

_StencilBatch.Begin(SpriteSortMode.Immediate, null, null, _StencilKeepIfZero, null);
_StencilBatch.DrawString(
    _Font, 
    Line, 
    Position2, 
    Shade);
_StencilBatch.End();

_RenderedTexture = MaskRenderTarget;

GraphicsDevice.SetRenderTargets(null);

There might be some transposition/sanitation errors, but any ideas what I'm doing wrong?


I would recomend searching for use of stencil buffer, that is unrelated to XNA / Windows Phone. In the end the values or usage is completly same.

Its basic function that all graphics cards support and both DirectX and OpenGL have bindings to.

http://en.wikipedia.org/wiki/Stencil_buffer or http://www.google.cz/search?q=stencil+buffer


For the code, see my answer on the App Hub forums: http://forums.create.msdn.com/forums/p/81189/499989.aspx#499989

You want the ReferenceStencil value to be 1 both times.

You also need to create an AlphaTestEffect instance with AlphaFunction = CompareGreater, ReferenceAlpha = 0, and an appropriate Projection matrix created with Matrix.OrthographicOffCenter(0, width, height, 0, 0, 1); .

You've then gotta pass that in to SpriteBatch.Begin both when drawing the stencil and then again when drawing the thing you want stenciled.

Also, you should initially draw the stencil and then draw what you want stenciled. In your case above, move DrawString to where Draw is and Draw to where DrawString is. It draws the stencil first (the text). Then it draws the item (your texture) and only keeps the parts of the item where the stencil says it should.

And you probably want to use Color.Transparent instead of new Color(0,0,0,1) for the Clear color.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号