Skip to content

Commit 5b29b39

Browse files
committed
07 highlighting
1 parent 6154cdb commit 5b29b39

File tree

7 files changed

+62
-27
lines changed

7 files changed

+62
-27
lines changed

articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ Create a new file in the _MonoGameLibrary_'s graphics folder called `SpriteCamer
231231

232232
And now instead of creating an orthographic matrix in the `TitleScene`, we can use the new class:
233233

234-
[!code-csharp[](./snippets/snippet-7-24.cs)]
234+
[!code-csharp[](./snippets/snippet-7-24.cs?highlight=9)]
235235

236236
Moving the `z` value uniformly in the shader will not be visually stimulating. A more impressive demonstration of the _perspective_ projection would be to rotate the vertices around the center of the sprite:
237237

238-
[!code-hlsl[](./snippets/snippet-7-25.hlsl)]
238+
[!code-hlsl[](./snippets/snippet-7-25.hlsl?highlight=7-22)]
239239

240240
> [!note]
241241
> What does this do, `output.Position /= output.Position.w` ?
@@ -254,7 +254,7 @@ And now when the debug parameter is adjusted, the text spins in a way that was n
254254

255255
The text disappears for half of the rotation. That happens because as the vertices are rotated, the triangle itself started to point _away_ from the camera. By default, `SpriteBatch` will cull any faces that point away from the camera. Change the `rasterizerState` to `CullNone` when beginning the sprite batch:
256256

257-
[!code-csharp[](./snippets/snippet-7-26.cs)]
257+
[!code-csharp[](./snippets/snippet-7-26.cs?highlight=4)]
258258

259259
And voilà, the text no longer disappears on its flip side.
260260

@@ -278,7 +278,7 @@ Then, make sure to set the ScreenSize parameter correctly from C#:
278278

279279
And instead of manually controlling the spin angle, we can make the title spin gentle following the mouse position. In the `Update()` function the `TitleScreen`, add the following snippet:
280280

281-
[!code-csharp[](./snippets/snippet-7-29.cs)]
281+
[!code-csharp[](./snippets/snippet-7-29.cs?highlight=5-7)]
282282

283283
| ![Figure 7-10: Spin controlled by the mouse](./gifs/spin-4.gif) |
284284
| :------------------------------------------------------------: |
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
// Load the 3d effect
2-
_3dMaterial = Core.SharedContent.WatchMaterial("effects/3dEffect");
3-
_3dMaterial.IsDebugVisible = true;
1+
public override void LoadContent()
2+
{
3+
// ...
44

5-
var camera = new SpriteCamera3d();
6-
_3dMaterial.SetParameter("MatrixTransform", camera.CalculateMatrixTransform());
5+
// Load the 3d effect
6+
_3dMaterial = Core.SharedContent.WatchMaterial("effects/3dEffect");
7+
_3dMaterial.IsDebugVisible = true;
8+
9+
var camera = new SpriteCamera3d();
10+
_3dMaterial.SetParameter("MatrixTransform", camera.CalculateMatrixTransform());
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
// ...
2+
13
float2 ScreenSize;
24
float SpinAngle;
5+
6+
// ...
7+
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
var spinAmount = Core.Input.Mouse.X / (float)Core.GraphicsDevice.Viewport.Width;
2-
spinAmount = MathHelper.SmoothStep(-.1f, .1f, spinAmount);
3-
_3dMaterial.SetParameter("SpinAmount", spinAmount);
1+
public override void Update(GameTime gameTime)
2+
{
3+
_3dMaterial.Update();
4+
5+
var spinAmount = Core.Input.Mouse.X / (float)Core.GraphicsDevice.Viewport.Width;
6+
spinAmount = MathHelper.SmoothStep(-.1f, .1f, spinAmount);
7+
_3dMaterial.SetParameter("SpinAmount", spinAmount);
8+
9+
// ...
10+
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
// Load the game material
2-
_gameMaterial = Content.WatchMaterial("effects/gameEffect");
3-
_gameMaterial.IsDebugVisible = true;
4-
_gameMaterial.SetParameter("ColorMap", _colorMap);
5-
var camera = new SpriteCamera3d();
6-
_gameMaterial.SetParameter("MatrixTransform", camera.CalculateMatrixTransform());
7-
_gameMaterial.SetParameter("ScreenSize", new Vector2(Core.GraphicsDevice.Viewport.Width, Core.GraphicsDevice.Viewport.Height));
1+
public override void LoadContent()
2+
{
3+
// ...
4+
5+
// Load the game material
6+
_gameMaterial = Content.WatchMaterial("effects/gameEffect");
7+
_gameMaterial.IsDebugVisible = true;
8+
_gameMaterial.SetParameter("ColorMap", _colorMap);
9+
var camera = new SpriteCamera3d();
10+
_gameMaterial.SetParameter("MatrixTransform", camera.CalculateMatrixTransform());
11+
_gameMaterial.SetParameter("ScreenSize", new Vector2(Core.GraphicsDevice.Viewport.Width, Core.GraphicsDevice.Viewport.Height));
12+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
// Set the camera view to look at the player slime
2-
var viewport = Core.GraphicsDevice.Viewport;
3-
var center = .5f * new Vector2(viewport.Width, viewport.Height);
4-
var slimePosition = new Vector2(_slime?.GetBounds().X ?? center.X, _slime?.GetBounds().Y ?? center.Y);
5-
var offset = .01f * (slimePosition - center);
6-
_camera.LookOffset = offset;
7-
_gameMaterial.SetParameter("MatrixTransform", _camera.CalculateMatrixTransform());
1+
public override void Update(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// Set the camera view to look at the player slime
6+
var viewport = Core.GraphicsDevice.Viewport;
7+
var center = .5f * new Vector2(viewport.Width, viewport.Height);
8+
var slimePosition = new Vector2(_slime?.GetBounds().X ?? center.X, _slime?.GetBounds().Y ?? center.Y);
9+
var offset = .01f * (slimePosition - center);
10+
_camera.LookOffset = offset;
11+
_gameMaterial.SetParameter("MatrixTransform", _camera.CalculateMatrixTransform());
12+
13+
// ...
14+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
Core.GraphicsDevice.Clear(new Color(32, 16, 20));
1+
public override void Draw(GameTime gameTime)
2+
{
3+
// Clear the back buffer.
4+
Core.GraphicsDevice.Clear(new Color(32, 16, 20));
5+
6+
// ...
7+
}

0 commit comments

Comments
 (0)