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.
Adding Files to FlatRedBall
Before writing any code, we’ll add a few files to our project. This requires an existing FlatRedBall project with at least one Screen.
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.
Creating an IDrawableBatch
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:
usingFlatRedBall;usingFlatRedBall.Graphics;usingMicrosoft.Xna.Framework.Graphics;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceShaderProject{publicclassCustomShaderSprite:PositionedObject,IDrawableBatch {Sprite sprite;Effect effect;short[] indexData =newshort[] {0,1,2,0,2,3 };publicTexture2D Texture { get; set; }publicbool UpdateEveryFrame {get {returntrue; } }publicCustomShaderSprite (Effect effect) { sprite =newSprite();sprite.TextureScale=1;this.effect= effect; }publicvoidDestroy() { }publicvoidDraw(Camera camera) {camera.SetDeviceViewAndProjection(effect, relativeToCamera:false);var textureParameter =effect.Parameters["CurrentTexture"];textureParameter.SetValue(Texture);effect.CurrentTechnique=effect.Techniques["TexturePixelShader_Point"];var graphicsDevice =FlatRedBallServices.GraphicsDevice;foreach(var pass ineffect.CurrentTechnique.Passes) {pass.Apply();graphicsDevice.DrawUserIndexedPrimitives( // Render a triangle:PrimitiveType.TriangleList, // The array of verts that we want to rendersprite.VerticesForDrawing, // The offset, which is 0 since we want to start // at the beginning of the verts array0,4, indexData,0,2); } }publicvoidUpdate() {sprite.Position=this.Position;sprite.RotationMatrix=this.RotationMatrix;sprite.Texture= Texture;SpriteManager.ManualUpdate(sprite); } }}
Adding CustomShaderSprite to your Screen
To add an instance of CustomShaderSprite to your screen, modify your Screen’s code so its methods are as shown in the following code: