Rendering Sprites with Shaders
Last updated
Was this helpful?
Last updated
Was this helpful?
Sprite appearance can be modified through a number of properties including Texture, ColorOperation, and BlendOperation. Full rendering control can be achieved by using custom shaders, applied through an IDrawableBatch. This guide covers how to render a Sprite using a custom IDrawableBatch.
Before writing any code, we’ll add a few files to our project. This requires an existing FlatRedBall project with at least one Screen.
To add a shader file:
Download this file: Shader.fx
Add the file to your FlatRedBall screen (such as GameScreen)
To add an Image file:
Right-click on your FlatRedBall screen's Files folder (such as GameScreen)
Select Add File -> New File
Select Texture (.png)
Both files should now be in your Screen so that we can reference them in the code below.
Next we’ll create a IDrawableBatch which will handle our rendering. To do this:
Add a new file to your project in Visual Studio
Name the file CustomShaderSprite
Replace your file with the following (you may need to change the namespace to match your project’s namespace:
To add an instance of CustomShaderSprite to your screen, modify your Screen’s code so its methods are as shown in the following code:
Running the project will result in the following being shown on screen: