Hello all,
I’ve been working in MonoGame for a while, but I have just started getting into shaders. I wrote my first shader, which replicates this blended rainbow effect.
I want this shader to be used only on entities that have performed a certain action, and entities have their own Draw methods with a solid base that can be overridden. I have a class wrapped around SpriteBatch, called SpriteRenderer, that allows me to stop and start new batches from anywhere, but I don’t want to have separate batches for each entity that is using that shader. Essentially, I want to be able to batch all entities using a particular shader.
I came up with an idea for the following:
- Store all SpriteBatch info into a new struct (SpriteBatch, Texture, Position, SourceRect, Color, etc.)
- Add to a dictionary which has the shader as the key (null is a valid key) and a list of those structs as the value
- Handle all rendering at once with the data in the dictionary, starting new batches for each shader
- Clear the dictionary after finishing rendering
The entity would pass that information into each call of SpriteRenderer, which would handle everything else. An example would be something like:
SpriteRenderer.Instance.Draw(myShader, myTex, Position, TintColor);
However, I’m unsure if this approach would work very well. My main concerns are:
-Changing shader variables for individual objects. If one object wants to change the values of a shader for itself, the new values will be applied to all objects using that shader. I’m using a FrontToBack SpriteSortMode. Ideally, I’d like to have only one instance of the shader (if that’s possible).
-Likely too much memory usage (will need to be tested).
What would be a good practice for handling rendering with numerous shaders in a flexible manner like this? Are there any examples out there?
You can find my wrapper and open source code here.
Thanks in advance!