2011-04-25 6 views
2

스텐실을 이해하려고합니다. 나는 그들이 어떻게 작동하는지 설명하는 좋은 자습서를 사용할 수 있지만 여기에 그 동안 내가 함께 일하고 있어요 무슨 :스텐실 이해 (XNA 4.0, Windows Phone 7)

DepthStencilState _StencilAlways; 
DepthStencilState _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); 

일부 전위/위생 오류가있을 수 있습니다 만, 어떤 아이디어는 내가 잘못을 뭘하는지?

답변

1

코드의 경우, 앱 허브 포럼에 내 대답을 참조하십시오

http://forums.create.msdn.com/forums/p/81189/499989.aspx#499989 당신은 ReferenceStencil 값이 한 두 번되고 싶어요.

또한 AlphaFunction = CompareGreater, ReferenceAlpha = 0 및 Matrix.OrthographicOffCenter (0, width, height, 0, 0, 1)로 만든 적절한 투영 행렬로 AlphaTestEffect 인스턴스를 만들어야합니다. .

그런 다음 스텐실을 그릴 때와 스텐실을 원하는 경우를 다시 그릴 때 다시 SpriteBatch.Begin에 전달해야합니다.

또한 스텐실을 먼저 그린 다음 스텐실로 원하는 것을 그려야합니다. 위의 경우 DrawString을 Draw가있는 곳으로 이동하고 DrawString을 DrawString으로 드래그합니다. 스텐실을 먼저 그립니다 (텍스트). 그런 다음 항목 (텍스처)을 그려 스텐실이 말한 항목의 부분 만 유지합니다.

그리고 아마도 지우기 색상에 새 색상 (0,0,0,1) 대신 Color.Transparent를 사용하고 싶을 것입니다.

+0

이 답변과 App Hub 포럼에 게시하면 정말 어떻게 작동하는지 이해하는데 많은 도움이되었습니다. :) ** 큰 감사합니다! :) ** – Venemo