Skip to content

Commit d95ce15

Browse files
committed
more chap7 notes
1 parent 056a034 commit d95ce15

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ To check, try to modify the shader code to adjust the `z` value based on one of
262262
> [!TIP]
263263
> Near and Far plane clipping.
264264
>
265-
> Keep in mind that if you modify the `z` value _too_ much, it will likely step outside of the near and far planes of the orthographic projection matrix. If this happens, the sprite will vanish, because it the projection matrix does not handle coordinates outside of the near and far planes. In the example above, they were defined as `0` and `-1`.
265+
> Keep in mind that if you modify the `z` value _too_ much, it will likely step outside of the near and far planes of the orthographic projection matrix. If this happens, the sprite will vanish, because it the projection matrix does not handle coordinates outside of the near and far planes. The value must be between the near and far plane of the matrix we created a few steps ago. We set the values in the `CreateOrthographicOffCenter()` function to `0` and `1`.
266266
>
267267
> [!code-csharp[](./snippets/snippet-7-22.cs)]
268268
>
@@ -283,16 +283,6 @@ To fix this, we need to use a _perspective_ projection matrix instead of an orth
283283

284284
[!code-hlsl[](./snippets/snippet-7-25.hlsl?highlight)]
285285

286-
> [!NOTE]
287-
> What does `output.Position /= output.Position.w` do?
288-
>
289-
> Long story short, the `w` component of the `.Position` must be `_1_`. Dividing any number by itself results in _1_, so the dividing `output.Position` by its own `w` component does two things,
290-
>
291-
> 1. sets the `w` component to _1_,
292-
> 2. uniformly adjusts the other components to accomodate the change.
293-
>
294-
> The math to fully explain why this is required is beyond the scope of this tutorial series. Read about [homogenous coordinates](https://www.tomdalling.com/blog/modern-opengl/explaining-homogenous-coordinates-and-projective-geometry/) and the [perspective divide](https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/projection-matrix-GPU-rendering-pipeline-clipping.html)
295-
296286
And now when the debug `X` parameter is adjusted (y does nothing at this point), the text spins in a way that was not possible with the default `SpriteBatch` vertex shader.
297287

298288
| ![Figure 7-8: A spinning text](./gifs/spin-1.gif) |
@@ -356,9 +346,9 @@ It was helpful to use the `TitleScene` to build intuition for the vertex shader,
356346

357347
### Making One Big Shader
358348

359-
A problem emerges right away. The `GameScene` is already using the color swapping effect to draw the sprites, and `SpriteBatch` can only use a single per batch.
349+
A problem emerges right away. The `GameScene` is already using the color swapping effect to draw the sprites, and `SpriteBatch` can only use a single shader per batch.
360350

361-
To solve this problem, we will collapse our shaders into a single shader that does it all, _both_ the color swapping _and_ the vertex manipulation. Writing code to be re-usable is a challenge for all programming languages, and shader languages are no different.
351+
To solve this problem, we will collapse our shaders into a single shader that does it all, _both_ the color swapping _and_ the vertex manipulation. Writing code to be re-usable is a challenge for all programming languages, and shader languagess are no different.
362352

363353
> [!NOTE]
364354
> The _Uber_ Shader
@@ -383,7 +373,12 @@ Follow the steps below to refactor the shader code, and to use the `#include` sy
383373

384374
2. Time to start factoring out some shared components into a few different `.fxh` files.
385375

386-
Create a file in the _MonoGameLibrary_'s `SharedContent/effects` folder called `common.fxh`. This file will contain utilities that can be shared for all effects, such as the `struct` types that define the inputs and outputs of the vertex and pixel shaders:
376+
Create a file in the _MonoGameLibrary_'s `SharedContent/effects` folder called `common.fxh`.
377+
378+
> [!TIP]
379+
> `.fxh` files do not be added to your MonoGame Content Builder file.
380+
381+
This file will contain utilities that can be shared for all effects, such as the `struct` types that define the inputs and outputs of the vertex and pixel shaders:
387382

388383
[!code-hlsl[](./snippets/snippet-7-31.hlsl)]
389384

@@ -401,9 +396,9 @@ Follow the steps below to refactor the shader code, and to use the `#include` sy
401396
[!code-hlsl[](./snippets/snippet-7-32.hlsl)]
402397

403398
> [!NOTE]
404-
> If you recall what was stated earlier about the processing order of the shader, the file is read in sequence, so the include MUST be defined BEFORE its contents are used. You can put it at the top of the file (like a using) but only if it does not interfere with the shader processing. THere is no hard and fast rule, so just use common sense, if it does not compile or it errors, then you need to change it.
399+
> If you recall what was stated earlier about the processing order of the shader, the file is read in sequence, so the include MUST be defined BEFORE its contents are used. You can put it at the top of the file (like a using) but only if it does not interfere with the shader processing. There is no hard and fast rule, so just use common sense, if it does not compile or it errors, then you need to change it.
405400

406-
4. If you run the game, nothing should change, except that the shader code is more modular. To continue, create another header file next to accompany the `3dEffect.fx` shader called `3dEffect.fxh` in the same folder.
401+
4. If you run the game, nothing should change, except that the shader code is more modular. To continue, create another header file next to the `3dEffect.fx` shader called `3dEffect.fxh` in the same folder.
407402
Paste the contents:
408403

409404
[!code-hlsl[](./snippets/snippet-7-33.hlsl)]
@@ -425,7 +420,10 @@ Follow the steps below to refactor the shader code, and to use the `#include` sy
425420

426421
Now most of the components we would like to combine into a single effect have been split into various `.fxh` header files, but their relative location is **CRUCIAL** when refering to related functionality, to demonstrate this, we will "break" a shader and show how to fix it.
427422

428-
8. Create a new "sprite effect" using the MGCB editor in the `_DungeonSlime_`'s content `effects` folder called `gameEffect.fx`, and simply add `#include "common.fxh"` to refer to the previously created common header file, you will see an error like this:
423+
8. Create a new "sprite effect" using the MGCB editor in the **`_DungeonSlime_`'s** content `effects` folder called `gameEffect.fx`, and simply add `#include "common.fxh"` to refer to the previously created common header file, you will see an error like this:
424+
425+
> [!WARNING]
426+
> We have been adding a lot of files to the _MonoGameLibrary_, but this shader should go into the _DungeonSlime_ project, because it is a game specific shader.
429427

430428
```text
431429
error PREPROCESS01: File not found: common.fxh in .(MonoGame.Effect.Preprocessor+MGFile)
@@ -462,6 +460,8 @@ Follow the steps below to refactor the shader code, and to use the `#include` sy
462460

463461
[!code-csharp[](./snippets/snippet-7-42.cs)]
464462

463+
13. Somewhat optionally, remember to add the `rasterizerState: RasterizerState.CullNone` to the `SpriteBatch.Draw()` call if you do not want the game to vanish when the `SpinAmount` goes beyond half a rotation. In practice, we will not be be spinning the game world that much, so it does not really matter.
464+
465465
Any remaining places where the old `_colorSwapMaterial` is being referenced should be changed to use the `_gameMaterial` instead, including in the `Update` and `Draw` methods. (if you still have any old references to the `_grayscaleEffect` make sure to remove those as well as they are no longer used).
466466

467467
Now, if you run the game, the color swap controls are still visible, but we can also manually control the tilt of the map.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
> zNearPlane: 0, zFarPlane: -1,
2-
>
1+
zNearPlane: 0, zFarPlane: -1

articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/snippets/snippet-7-25.hlsl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ VertexShaderOutput MainVS(VertexShaderInput input)
3232
// shift the position away from the center of rotation
3333
pos.xz += centerXZ;
3434

35-
3635
output.Position = mul(pos, MatrixTransform);
37-
output.Position /= output.Position.w; // re-normalize the position vector.
3836
output.Color = input.Color;
3937
output.TextureCoordinates = input.TexCoord;
4038
return output;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ...
22

33
float2 ScreenSize;
4-
float SpinAngle;
4+
float SpinAmount;
55

66
// ...
77

articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/snippets/snippet-7-33.hlsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ VertexShaderOutput MainVS(VertexShaderInput input)
3838
pos.xz += centerXZ;
3939

4040
output.Position = mul(pos, MatrixTransform);
41-
output.Position /= output.Position.w; // re-normalize the position vector.
4241
output.Color = input.Color;
4342
output.TextureCoordinates = input.TexCoord;
4443
return output;

0 commit comments

Comments
 (0)