Skip to content

Commit 6e814c5

Browse files
committed
tracking 7.9 numbered steps
1 parent 113c0fa commit 6e814c5

File tree

1 file changed

+21
-18
lines changed
  • articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect

1 file changed

+21
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,15 @@ MonoGame shaders can reference code from multiple files by using the `#include`
321321
>
322322
> `.fxh` is purely convention. Technically you can use whatever file extension you want, but `.fxh` implies the usage of the file is for shared code, and does not contain a standalone effect itself. The `h` references `header`.
323323
324-
Before we get started, we are going to be editing `.fxh` files, so it would be nice if the hot-reload system also listened to these `.fxh` file changes. Update the `Watch` configuration in the `DungeonSlime.csproj` file to include the `.fxh` file type:
324+
Follow the steps below to refactor the shader code to use the `#include` syntax.
325325

326-
[!code-xml[](./snippets/snippet-7-30.xml)]
326+
1. Before we get started, we are going to be editing `.fxh` files, so it would be nice if the hot-reload system also listened to these `.fxh` file changes. Update the `Watch` configuration in the `DungeonSlime.csproj` file to include the `.fxh` file type:
327327

328-
Let's start by factoring out some shared components a few different `.fxh` files.
328+
[!code-xml[](./snippets/snippet-7-30.xml)]
329329

330-
Create a file in the _MonoGameLibrary_'s shared effect content 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:
330+
2. Let's start by factoring out some shared components a few different `.fxh` files.
331+
332+
Create a file in the _MonoGameLibrary_'s shared effect content 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:
331333

332334
[!code-hlsl[](./snippets/snippet-7-31.hlsl)]
333335

@@ -336,23 +338,24 @@ Create a file in the _MonoGameLibrary_'s shared effect content folder called `co
336338
>
337339
> The `#include` syntax is taking the referenced file and inserting it into the code. If the same file was included twice, then the contents that file would be written out as code _twice_. Defining a `struct` or function this way would cause the compiler to fail, because the `struct` would be declared twice, which is illegal. To work around this, _a_ solution is to use a practice called "include guards", where the file itself defines a symbol (in the case above, the symbol is `COMMON`). The file only compiles to anything if the symbol has not yet been defined. The `#ifndef` stands for "if not yet defined". Once the `COMMON` symbol is defined once, any future inclusions of the file will not match the `#ifndef` clause.
338340
339-
Then, in the `3dEffect.fx` file, remove the `VertexShaderInput` and `VertexShaderOutput` structs and replace them with this line:
341+
3. Then, in the `3dEffect.fx` file, remove the `VertexShaderInput` and `VertexShaderOutput` structs and replace them with this line:
340342

341343
[!code-hlsl[](./snippets/snippet-7-32.hlsl)]
342344

343-
If you run the game, nothing should change, except that the shader code is more modular. To continue, create another file next to `3dEffect.fx` called `3dEffect.fxh`. Paste the contents:
345+
4. If you run the game, nothing should change, except that the shader code is more modular. To continue, create another file next to `3dEffect.fx` called `3dEffect.fxh`.
346+
Paste the contents:
344347

345348
[!code-hlsl[](./snippets/snippet-7-33.hlsl)]
346349

347-
And now in the `3dEffect.fx`, instead of `#include` referencing the `common.fxh`, we can directly reference `3dEffect.fxh`. We should also remove the code that was just pasted into the new common header file. Here is the entire contents of the slimmed down `3dEffect.fx` file:
350+
5. And now in the `3dEffect.fx`, instead of `#include` referencing the `common.fxh`, we can directly reference `3dEffect.fxh`. We should also remove the code that was just pasted into the new common header file. Here is the entire contents of the slimmed down `3dEffect.fx` file:
348351

349352
[!code-hlsl[](./snippets/snippet-7-34.hlsl)]
350353

351-
It is time to do the same thing for the `colorSwapEffect.fx` file. The goal is to split the file apart into a header file that defines the components of the effect, and leave the `fx` file itself without much _implementation_. Create a new file called `colors.fxh`, and paste the following:
354+
6. It is time to do the same thing for the `colorSwapEffect.fx` file. The goal is to split the file apart into a header file that defines the components of the effect, and leave the `fx` file itself without much _implementation_. Create a new file called `colors.fxh`, and paste the following:
352355

353356
[!code-hlsl[](./snippets/snippet-7-35.hlsl)]
354357

355-
Then, then `colorSwapEffect.fx` file can be re-written as this code:
358+
7. Then, then `colorSwapEffect.fx` file can be re-written as this code:
356359

357360
[!code-hlsl[](./snippets/snippet-7-36.hlsl)]
358361

@@ -363,27 +366,27 @@ Now most of the components we'd like to combine into a single effect have been s
363366
error PREPROCESS01: File not found: common.fxh in .(MonoGame.Effect.Preprocessor+MGFile)
364367
```
365368

366-
This happens because the `gameEffect.fx` file is in a different folder than the `common.fxh` file, and the `"common.fxh"` is treated as a relative _file path_ lookup. Instead, in the `gameEffect.fx` file, use this line:
369+
8. This happens because the `gameEffect.fx` file is in a different folder than the `common.fxh` file, and the `"common.fxh"` is treated as a relative _file path_ lookup. Instead, in the `gameEffect.fx` file, use this line:
367370

368371
[!code-hlsl[](./snippets/snippet-7-37.hlsl)]
369372

370-
Then, the `gameEffect.fx` file could also reference the other two `.fxh` files we created:
373+
9. Then, the `gameEffect.fx` file could also reference the other two `.fxh` files we created:
371374

372375
[!code-hlsl[](./snippets/snippet-7-38.hlsl)]
373376

374-
And the only thing the `gameEffect.fx` file needs to specify is which functions to use for the vertex shader and pixel shader functions:
375-
376-
[!code-hlsl[](./snippets/snippet-7-39.hlsl)]
377+
10. And the only thing the `gameEffect.fx` file needs to specify is which functions to use for the vertex shader and pixel shader functions:
377378

378-
The entire contents of the `gameEffect.fx` are written below:
379+
[!code-hlsl[](./snippets/snippet-7-39.hlsl)]
380+
381+
The entire contents of the `gameEffect.fx` are written below:
379382

380-
[!code-hlsl[](./snippets/snippet-7-40.hlsl)]
383+
[!code-hlsl[](./snippets/snippet-7-40.hlsl)]
381384

382-
To load it into the `GameScene`, we need to _delete_ the old class member for `_colorSwapMaterial`, and add a new one:
385+
11. To load it into the `GameScene`, we need to _delete_ the old class member for `_colorSwapMaterial`, and add a new one:
383386

384387
[!code-csharp[](./snippets/snippet-7-41.cs)]
385388

386-
And then apply all of the parameters to the single material:
389+
12. And then apply all of the parameters to the single material:
387390

388391
[!code-csharp[](./snippets/snippet-7-42.cs)]
389392

0 commit comments

Comments
 (0)