Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace MaskingTest
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D mask, texture;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- mask = Content.Load<Texture2D>("mask");
- texture = Content.Load<Texture2D>("texture");
- }
- protected override void UnloadContent()
- {
- }
- protected override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- DrawMask();
- base.Draw(gameTime);
- }
- private void DrawMask()
- {
- GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.Transparent, 0, 0);
- var m = Matrix.CreateOrthographicOffCenter(0,
- graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
- graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
- 0, 0, 1
- );
- var a = new AlphaTestEffect(graphics.GraphicsDevice)
- {
- Projection = m
- };
- var s1 = new DepthStencilState
- {
- StencilEnable = true,
- StencilFunction = CompareFunction.Always,
- StencilPass = StencilOperation.Replace,
- ReferenceStencil = 1,
- DepthBufferEnable = false,
- };
- var s2 = new DepthStencilState
- {
- StencilEnable = true,
- StencilFunction = CompareFunction.LessEqual,
- StencilPass = StencilOperation.Keep,
- ReferenceStencil = 1,
- DepthBufferEnable = false,
- };
- spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s1, null, a);
- spriteBatch.Draw(mask, Vector2.Zero, Color.White); //The mask
- spriteBatch.End();
- spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s2, null, a);
- spriteBatch.Draw(texture, Vector2.Zero, Color.White); //The background
- spriteBatch.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement