Difference between revisions of "Field of View / Mask"
(→Original Source) |
(→Mask XNB) |
||
Line 68: | Line 68: | ||
spriteBatch.End(); | spriteBatch.End(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
=Original Source= | =Original Source= | ||
https://gamedev.stackexchange.com/questions/38118/best-way-to-mask-2d-sprites-in-xna/38150#38150 | https://gamedev.stackexchange.com/questions/38118/best-way-to-mask-2d-sprites-in-xna/38150#38150 |
Latest revision as of 13:06, 24 May 2024
This will help you to create a field of view using a mask. The effect will look like this:
Contents
Stage 1
You will need to find your 'graphics' or '_graphics' variable. You will need to add the following parameter:
graphics = new GraphicsDeviceManager(this) {
PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8
};
Stage 2
You then need to find your Draw method. You will then need to add the following
var m = Matrix.CreateOrthographicOffCenter(0,
graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
0, 0, 1
);
var a = new AlphaTestEffect(graphics.GraphicsDevice) {
Projection = m
};
You then need to create your first DepthStencilState:
var s1 = new DepthStencilState {
StencilEnable = true,
StencilFunction = CompareFunction.Always,
StencilPass = StencilOperation.Replace,
ReferenceStencil = 1,
DepthBufferEnable = false,
};
You then need to create your second DepthStencilState:
var s2 = new DepthStencilState {
StencilEnable = true,
StencilFunction = CompareFunction.LessEqual,
StencilPass = StencilOperation.Keep,
ReferenceStencil = 1,
DepthBufferEnable = false,
};
You could alternatively define all of these within the Game1 class itself, you would need to replace 'var' with the data type.
Stage 3
Now you need to draw your mask using your first DepthStencilState:
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s1, null, a);
spriteBatch.Draw(huh, Vector2.Zero, Color.White); //The mask
spriteBatch.End();
Now draw the rest with your second DepthStencilState:
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s2, null, a);
spriteBatch.Draw(color, Vector2.Zero, Color.White); //The background
spriteBatch.End();
Original Source
https://gamedev.stackexchange.com/questions/38118/best-way-to-mask-2d-sprites-in-xna/38150#38150