In carrying out my little 3D engine, I'd like to manage a number of different materials. Each material must be easily applied to a model and described using a file .xml and a file .fx.
The trouble is that in this context, I neither know the type nor the number of variables that will be transmitted to the graphics card, except the conventional matrix world, view, projection and lighting parameters.
For example, in the case of a water shader, I have many variable to pass:
- 2 textures: the maps of reflection and refraction.
- Several vectors: wind direction, wave height & co.
- Floats: such as the time elapsed since the last frame.
All these parameters are easy to pass, but I'd not like to do one class per material, the idea to add them via the editor, at the click. So at the risk of repeating myself, it would have all these parameters fully described in a file of materials (a .xml in my case).
Here is the line to add a parameter in a conventional manner (by the color of water):
Code:
this .effect.Parameters["xWaterColor"].SetValue( this .waterColor);
The worry is that SetValue () does not like generic types, it has an overload for each type supported.
So I thought of a solution that I find VERY ugly as follows:
Code:
private SerializableDictionary<string, float > floatList = new SerializableDictionary<string, float >();
private SerializableDictionary<string, int > intList = new SerializableDictionary<string, int >();
private SerializableDictionary <string, Vector2> vector2List = new SerializableDictionary <string, Vector2> ();
[...]
For each type of parameters supported, I have a dictionary described in XML. The dictionary key is the name of the variable in HLSL code and value, the value to pass. One is ugly, and absolutely not flexible, by not supporting variables changing at runtime, these are only static values and described very heavy.
Bookmarks