Advertisement
Guest User

Mask not working

a guest
Jan 6th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4.  
  5. namespace MaskingTest
  6. {
  7.     /// <summary>
  8.     /// This is the main type for your game.
  9.     /// </summary>
  10.     public class Game1 : Game
  11.     {
  12.         GraphicsDeviceManager graphics;
  13.         SpriteBatch spriteBatch;
  14.  
  15.         Texture2D mask, texture;
  16.  
  17.         public Game1()
  18.         {
  19.             graphics = new GraphicsDeviceManager(this);
  20.             graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
  21.  
  22.             Content.RootDirectory = "Content";
  23.         }
  24.  
  25.         protected override void Initialize()
  26.         {
  27.  
  28.             base.Initialize();
  29.         }
  30.  
  31.  
  32.         protected override void LoadContent()
  33.         {
  34.             spriteBatch = new SpriteBatch(GraphicsDevice);
  35.  
  36.             mask = Content.Load<Texture2D>("mask");
  37.             texture = Content.Load<Texture2D>("texture");
  38.         }
  39.  
  40.         protected override void UnloadContent()
  41.         {
  42.  
  43.         }
  44.  
  45.         protected override void Update(GameTime gameTime)
  46.         {
  47.  
  48.             base.Update(gameTime);
  49.         }
  50.  
  51.         protected override void Draw(GameTime gameTime)
  52.         {
  53.             DrawMask();
  54.  
  55.             base.Draw(gameTime);
  56.         }
  57.  
  58.         private void DrawMask()
  59.         {
  60.             GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.Transparent, 0, 0);
  61.  
  62.             var m = Matrix.CreateOrthographicOffCenter(0,
  63.                 graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
  64.                 graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
  65.                 0, 0, 1
  66.             );
  67.  
  68.             var a = new AlphaTestEffect(graphics.GraphicsDevice)
  69.             {
  70.                 Projection = m
  71.             };
  72.  
  73.             var s1 = new DepthStencilState
  74.             {
  75.                 StencilEnable = true,
  76.                 StencilFunction = CompareFunction.Always,
  77.                 StencilPass = StencilOperation.Replace,
  78.                 ReferenceStencil = 1,
  79.                 DepthBufferEnable = false,
  80.             };
  81.  
  82.             var s2 = new DepthStencilState
  83.             {
  84.                 StencilEnable = true,
  85.                 StencilFunction = CompareFunction.LessEqual,
  86.                 StencilPass = StencilOperation.Keep,
  87.                 ReferenceStencil = 1,
  88.                 DepthBufferEnable = false,
  89.             };
  90.  
  91.             spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s1, null, a);
  92.             spriteBatch.Draw(mask, Vector2.Zero, Color.White); //The mask                                  
  93.             spriteBatch.End();
  94.  
  95.             spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s2, null, a);
  96.             spriteBatch.Draw(texture, Vector2.Zero, Color.White); //The background
  97.             spriteBatch.End();
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement