Difference between revisions of "Field of View / Mask"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Stage 1= You will need to find your 'graphics' or '_graphics' variable. You will need to add the following parameter: <syntaxhighlight lang=csharp> graphics = new GraphicsDev...")
 
Line 5: Line 5:
 
     PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8
 
     PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8
 
};  
 
};  
 +
</syntaxhighlight>
 +
 +
=Stage 2=
 +
You then need to find your Draw method. You will then need to add the following
 +
 +
<syntaxhighlight lang=csharp>
 +
var m = Matrix.CreateOrthographicOffCenter(0,
 +
    graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
 +
    graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
 +
    0, 0, 1
 +
);
 +
var a = new AlphaTestEffect(graphics.GraphicsDevice) {
 +
    Projection = m
 +
};
 +
</syntaxhighlight>
 +
 +
You then need to create your first DepthStencilState:
 +
 +
<syntaxhighlight lang=csharp>
 +
var s1 = new DepthStencilState {
 +
    StencilEnable = true,
 +
    StencilFunction = CompareFunction.Always,
 +
    StencilPass = StencilOperation.Replace,
 +
    ReferenceStencil = 1,
 +
    DepthBufferEnable = false,
 +
};
 +
</syntaxhighlight>
 +
 +
You then need to create your second DepthStencilState:
 +
 +
<syntaxhighlight lang=csharp>
 +
var s2 = new DepthStencilState {
 +
    StencilEnable = true,
 +
    StencilFunction = CompareFunction.LessEqual,
 +
    StencilPass = StencilOperation.Keep,
 +
    ReferenceStencil = 1,
 +
    DepthBufferEnable = false,
 +
};
 +
</syntaxhighlight>
 +
 +
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:
 +
 +
<syntaxhighlight lang=csharp>
 +
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s1, null, a);
 +
spriteBatch.Draw(huh, Vector2.Zero, Color.White); //The mask                                 
 +
spriteBatch.End();
 +
</syntaxhighlight>
 +
 +
Now draw the rest with your second DepthStencilState:
 +
 +
 +
<syntaxhighlight lang=csharp>
 +
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, s2, null, a);           
 +
spriteBatch.Draw(color, Vector2.Zero, Color.White); //The background
 +
spriteBatch.End();
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 17:48, 29 March 2024

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();