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
+21-18Lines changed: 21 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -321,13 +321,15 @@ MonoGame shaders can reference code from multiple files by using the `#include`
321
321
>
322
322
> `.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`.
323
323
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.
325
325
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:
327
327
328
-
Let's start by factoring out some shared components a few different `.fxh` files.
328
+
[!code-xml[](./snippets/snippet-7-30.xml)]
329
329
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:
331
333
332
334
[!code-hlsl[](./snippets/snippet-7-31.hlsl)]
333
335
@@ -336,23 +338,24 @@ Create a file in the _MonoGameLibrary_'s shared effect content folder called `co
336
338
>
337
339
> 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.
338
340
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:
340
342
341
343
[!code-hlsl[](./snippets/snippet-7-32.hlsl)]
342
344
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:
344
347
345
348
[!code-hlsl[](./snippets/snippet-7-33.hlsl)]
346
349
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:
348
351
349
352
[!code-hlsl[](./snippets/snippet-7-34.hlsl)]
350
353
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:
352
355
353
356
[!code-hlsl[](./snippets/snippet-7-35.hlsl)]
354
357
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:
356
359
357
360
[!code-hlsl[](./snippets/snippet-7-36.hlsl)]
358
361
@@ -363,27 +366,27 @@ Now most of the components we'd like to combine into a single effect have been s
363
366
error PREPROCESS01: File not found: common.fxh in .(MonoGame.Effect.Preprocessor+MGFile)
364
367
```
365
368
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:
367
370
368
371
[!code-hlsl[](./snippets/snippet-7-37.hlsl)]
369
372
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:
371
374
372
375
[!code-hlsl[](./snippets/snippet-7-38.hlsl)]
373
376
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:
377
378
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:
379
382
380
-
[!code-hlsl[](./snippets/snippet-7-40.hlsl)]
383
+
[!code-hlsl[](./snippets/snippet-7-40.hlsl)]
381
384
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:
383
386
384
387
[!code-csharp[](./snippets/snippet-7-41.cs)]
385
388
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:
0 commit comments