Shader Effect Files (.fx) can be added to FlatRedBall projects. At runtime this produces an Effect object which can be used to perform custom rendering. FlatRedBall FNA projects require extra steps to use FX files as explained below.
FlatRedBall can import existing .fx files, but also provides a standard way to create effect files for post processing. The easiest way to add a post processing effect file to your project is to use the Add File dialog:
Right-click on the Files folder where you would like to add your .fx file (such as Global Content Files or GameScreen) and select Add File -> New File
Select Effect (.fx) as the file type
Check the Post Processing Shader option
Verify that the Include YourFileName.cs Post Process File option is checked
Select the type of shader you would like to use. For example, Saturation.
Enter a name for your new effect file, such as SaturationEffect.
Click OK
This creates the following files in your project:
The .fx file. This is a text file using the HLSL syntax which you can edit
The .xnb file (if you are using MonoGame or targeting Web)
The accompanying PostProcess file
A FullscreenEffectWrapper.cs file. This file is only created one time when the first post processing effect is added
If you intend to use the effects as they are when created, you do not need to make any changes to these files. However, these files can also serve as a starting point for your own shaders so you may be interested in locating them.
Once a shader has been added, you can add it to the global effect list. If your shader is part of Global Content Files, the addition can be performed in Game1. The following code shows how to add the shader to post processing in Game1.
To use the built in GlobalPostProcessing, a SwapChain must be created. The code above creates default SwapChains for us in the CreateDefaultSwapChain
method. For more information on SwapChains, see the SwapChain page. If you need more control you can manually create a SwapChain as shown in the following block of code:
If your shader is part of a screen such as GameScreen, you can add it in the Screen's CustomInitialize
. Note that if you add it in the Screen's CustomInitialize
, you should also remove it in CustomDestroy
. The following code shows how an effect might be added through GameScreen.
Note that by using a SwapChain, FlatRedBall internally creates and assigns a RenderTarget when performing rendering, so you should not manually create and assign a RenderTarget prior to drawing FlatRedBall.
Effect files require the use of the MonoGame Content Pipeline. If you are using the FlatRedBall Editor, this is automatically handled for you. You can verify that this is the case by checking the UseContentPipeline property.
FNA does not provide a content pipeline, and use of XNA Content Pipeline is discouraged because it does not function in newer versions of Visual Studio.
Instead, FNA recommends compiling shader files in the fxc.exe tool which is available as part of the Windows SDK.
The fxc.exe program can also be downloaded directly from the FlatRedBall repository: https://github.com/vchelaru/FlatRedBall/tree/NetStandard/FRBDK/PrebuiltTools/fxc
fxc.exe can be used to build effect files which can be consumed by FNA (and FlatRedBall FNA) using syntax similar to the following command:
In the command above, ShaderFile.fx
is the input file and ShaderFile.fxb
is the output file. FlatRedBall recommends using the extension .fxb as the output file.
Once an .fxb file is built, it can be added to FlatRedBall just like any other file (by drag+dropping it into the FlatRedBall Editor) and it will be loaded into a standard Effect object.