You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/index.md
+26-20Lines changed: 26 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,12 @@ Every shader has two main parts: the pixel shader, which we've been using to cha
7
7
8
8
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.
9
9
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**|**Figure 7-2: The game will have a 3d-esque world**|
15
+
10
16
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).
11
17
12
18
## Default Vertex Shader
@@ -129,9 +135,9 @@ When the game runs, the text will be missing. This is because we never created a
129
135
130
136
And now you should see the text normally again.
131
137
132
-
||
138
+
||
|**Figure 7-2: We can control the vertex positions**|
156
+
|**Figure 7-4: We can control the vertex positions**|
151
157
152
158
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:
153
159
154
160
[!code-hlsl[](./snippets/snippet-7-20.hlsl)]
155
161
156
162
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.
157
163
158
-
||
164
+
||
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:
208
214
209
215
[!code-csharp[](./snippets/snippet-7-26.cs)]
210
216
211
217
And voilà, the text no longer disappears on its flip side.
212
218
213
-
||
219
+
||
|**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**|
222
228
223
229
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:
224
230
@@ -232,9 +238,9 @@ And instead of manually controlling the spin angle, we can make the title spin g
232
238
233
239
[!code-csharp[](./snippets/snippet-7-29.cs)]
234
240
235
-
||
241
+
||
@@ -320,9 +326,9 @@ And then apply all of the parameters to the single material:
320
326
321
327
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.
322
328
323
-
||
329
+
||
0 commit comments