Skip to content

Commit c31440d

Browse files
committed
tracking 7.1; overview gifs
1 parent c7331e3 commit c31440d

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed
2.46 MB
Loading
3.05 MB
Loading

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Every shader has two main parts: the pixel shader, which we've been using to cha
77

88
In this chapter, we're going to unlock the power of the vertex shader. we will write our own custom vertex shader from scratch, which will allow us to break out of the 2D plane. we will learn how to use a perspective projection to give our flat world a cool, dynamic 3D feel.
99

10+
At the end of this chapter, we will be able to make our sprites appear with 3d perspective.
11+
12+
| ![Figure 7-1: The main menu will have a 3d-esque title ](./gifs/overview-1.gif) | ![Figure 7-2: The game will have a 3d-esque world ](./gifs/overview-2.gif) |
13+
| :-----------------------------------------------------------------------------: | :------------------------------------------------------------------------: |
14+
| **Figure 7-1: The main menu will have a 3d-esque title** | **Figure 7-2: The game will have a 3d-esque world** |
15+
1016
If you are following along with code, here is the code from the end of the [previous chapter](https://github.com/MonoGame/MonoGame.Samples/tree/3.8.4/Tutorials/2dShaders/src/06-Color-Swap-Effect).
1117

1218
## Default Vertex Shader
@@ -129,9 +135,9 @@ When the game runs, the text will be missing. This is because we never created a
129135

130136
And now you should see the text normally again.
131137

132-
| ![Figure 7-1: The main menu, but rendered with a custom vertex shader](./images/basic.png) |
138+
| ![Figure 7-3: The main menu, but rendered with a custom vertex shader](./images/basic.png) |
133139
| :----------------------------------------------------------------------------------------: |
134-
| **Figure 7-1: The main menu, but rendered with a custom vertex shader** |
140+
| **Figure 7-3: The main menu, but rendered with a custom vertex shader** |
135141

136142
### Making it Move
137143

@@ -145,19 +151,19 @@ And change the vertex shader to add the `DebugOffset` to the `output.Position` a
145151

146152
The sprites now move around as we adjust the shader parameter values.
147153

148-
| ![Figure 7-2: We can control the vertex positions](./gifs/basic.gif) |
154+
| ![Figure 7-4: We can control the vertex positions](./gifs/basic.gif) |
149155
| :------------------------------------------------------------------: |
150-
| **Figure 7-2: We can control the vertex positions** |
156+
| **Figure 7-4: We can control the vertex positions** |
151157

152158
It is important to build intuition for the different coordinate systems involved. Instead of adding the `DebugOffset` _after_ the clip-space conversion, if you try to add it _before_, like in the code below:
153159

154160
[!code-hlsl[](./snippets/snippet-7-20.hlsl)]
155161

156162
Then you will not see much movement at all. This is because the `DebugOffset` values only go from `0` to `1`, and in world space, this really only amounts to a single pixel. In fact, exactly how much an addition of _`1`_ happens to make is entirely defined _by_ the conversion to clip-space. The `projection` matrix we created treats world space coordinates with an origin around the screen's center, where 1 unit maps to 1 pixel. Sometimes this is exactly what you want, and sometimes it can be confusing.
157163

158-
| ![Figure 7-3: Changing coordinates before clip-space conversion](./gifs/basic-2.gif) |
164+
| ![Figure 7-5: Changing coordinates before clip-space conversion](./gifs/basic-2.gif) |
159165
| :----------------------------------------------------------------------------------: |
160-
| **Figure 7-3: Changing coordinates before clip-space conversion** |
166+
| **Figure 7-5: Changing coordinates before clip-space conversion** |
161167

162168
### Perspective Projection
163169

@@ -200,25 +206,25 @@ Moving the `z` value uniformly in the shader will not be visually stimulating. A
200206

201207
And now when the debug parameter is adjusted, the text spins in a way that was never possible with the default `SpriteBatch` vertex shader.
202208

203-
| ![Figure 7-4: A spinning text](./gifs/spin-1.gif) |
209+
| ![Figure 7-6: A spinning text](./gifs/spin-1.gif) |
204210
| :-----------------------------------------------: |
205-
| **Figure 7-4: A spinning text** |
211+
| **Figure 7-6: A spinning text** |
206212

207213
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:
208214

209215
[!code-csharp[](./snippets/snippet-7-26.cs)]
210216

211217
And voilà, the text no longer disappears on its flip side.
212218

213-
| ![Figure 7-4: A spinning text with reverse sides](./gifs/spin-2.gif) |
219+
| ![Figure 7-7: A spinning text with reverse sides](./gifs/spin-2.gif) |
214220
| :------------------------------------------------------------------: |
215-
| **Figure 7-4: A spinning text with reverse sides** |
221+
| **Figure 7-7: A spinning text with reverse sides** |
216222

217223
You may find that the field of view is too high for your taste. Try lowering the field of view to 60, and you'll see something similar to this,
218224

219-
| ![Figure 7-5: A spinning text with reverse sides with smaller fov](./gifs/spin-3.gif) |
225+
| ![Figure 7-8: A spinning text with reverse sides with smaller fov](./gifs/spin-3.gif) |
220226
| :-----------------------------------------------------------------------------------: |
221-
| **Figure 7-5: A spinning text with reverse sides with smaller fov** |
227+
| **Figure 7-8: A spinning text with reverse sides with smaller fov** |
222228

223229
As a final touch, we should remove the hard-coded `screenSize` variable from the shader, and extract it as a shader parameter. While we are at it, clean up and remove the debug parameters as well:
224230

@@ -232,9 +238,9 @@ And instead of manually controlling the spin angle, we can make the title spin g
232238

233239
[!code-csharp[](./snippets/snippet-7-29.cs)]
234240

235-
| ![Figure 7-6: Spin controlled by the mouse](./gifs/spin-4.gif) |
241+
| ![Figure 7-9: Spin controlled by the mouse](./gifs/spin-4.gif) |
236242
| :------------------------------------------------------------: |
237-
| **Figure 7-6: Spin controlled by the mouse** |
243+
| **Figure 7-9: Spin controlled by the mouse** |
238244

239245
## Applying it to the Game
240246

@@ -320,9 +326,9 @@ And then apply all of the parameters to the single material:
320326

321327
Any place where the old `_colorSwapMaterial` is being referenced should be changed to use the `_gameMaterial` instead. Now, if you run the game, the color swap controls are still visible, but we can also manually control the tilt of the map.
322328

323-
| ![Figure 7-7: All of the effects in one](./gifs/uber.gif) |
329+
| ![Figure 7-10: All of the effects in one](./gifs/uber.gif) |
324330
| :-------------------------------------------------------: |
325-
| **Figure 7-7: All of the effects in one** |
331+
| **Figure 7-10: All of the effects in one** |
326332

327333
### Adjusting the Game
328334

@@ -332,19 +338,19 @@ Add this snippet to the top of the `GameScene`'s `Update()` method:
332338

333339
[!code-csharp[](./snippets/snippet-7-43.cs)]
334340

335-
| ![Figure 7-8: Camera follows the slime](./gifs/cam-follow.gif) |
341+
| ![Figure 7-11: Camera follows the slime](./gifs/cam-follow.gif) |
336342
| :------------------------------------------------------------: |
337-
| **Figure 7-8: Camera follows the slime** |
343+
| **Figure 7-11: Camera follows the slime** |
338344

339345
The clear color of the scene can be seen in the corners (the `CornflowerBlue`). Pick whatever clear color you think looks good for the color swapping:
340346

341347
[!code-csharp[](./snippets/snippet-7-44.cs)]
342348

343349
And to finish this chapter, the game looks like this,
344350

345-
| ![Figure 7-9: vertex shaders make it pop](./gifs/final.gif) |
351+
| ![Figure 7-12: vertex shaders make it pop](./gifs/final.gif) |
346352
| :---------------------------------------------------------: |
347-
| **Figure 7-9: vertex shaders make it pop** |
353+
| **Figure 7-12: vertex shaders make it pop** |
348354

349355
## Conclusion
350356

0 commit comments

Comments
 (0)