Class MaterialParameters
Changes a compiled material at runtime through its parameters, the way the engine's own gizmo materials do: no rebuild, the value is picked up by the next draw.
public static class MaterialParameters
- Inheritance
-
MaterialParameters
Remarks
Once Material.New has run, the features are gone; what remains is a shader and the constants
the features registered. A number in a slot registers under the slot's key (GlossinessValue,
EmissiveIntensity); a colour or texture node can carry a key of your own
(new ComputeColor(Color.White) { Key = MaterialParameters.ColorKey("MyGame.Tint") }), which is
how a value is animated without touching the shader. Every texture node also registers its scale and
offset, so a texture scrolls through SetTextureOffset(Material, Vector2, int) with no recompile.
Two things the generator did at compile time have to be repeated for a colour set at runtime, or the two disagree: it converted the colour from sRGB to linear and premultiplied it by its alpha. SetColor(Material, ValueParameterKey<Color4>, Color4, bool) does both. And a material may have several passes (clear coat two, hair three, thin glass two or four), each with its own parameters, so every setter here writes to all of them.
Methods
ColorKey(string)
A colour key of your own for a ComputeColor node, named so it reads in a debugger.
public static ValueParameterKey<Color4> ColorKey(string name)
Parameters
namestringA unique name, such as
"MyGame.Tint".
Returns
FloatKey(string)
A number key of your own for a ComputeFloat node.
public static ValueParameterKey<float> FloatKey(string name)
Parameters
namestringA unique name, such as
"MyGame.Wear".
Returns
SetColor(Material, ValueParameterKey<Color4>, Color, bool)
Sets a colour parameter on every pass, from a byte colour, converted like the generator would.
public static void SetColor(this Material material, ValueParameterKey<Color4> key, Color colour, bool premultiply = true)
Parameters
materialMaterialkeyValueParameterKey<Color4>colourColorpremultiplybool
SetColor(Material, ValueParameterKey<Color4>, Color4, bool)
Sets a colour parameter on every pass, converted the way the generator converted the node's
initial value: to linear, and premultiplied by its alpha unless premultiply is off.
public static void SetColor(this Material material, ValueParameterKey<Color4> key, Color4 colour, bool premultiply = true)
Parameters
materialMaterialThe material.
keyValueParameterKey<Color4>The colour's key: one of MaterialKeys, or the key the node was given.
colourColor4The colour, in sRGB, as you would write it.
premultiplyboolWhether to premultiply by alpha; the node's default is to.
SetTextureOffset(Material, Vector2, int)
Sets the offset of a texture node on every pass, in UV units: raise it every frame and the texture scrolls, with no recompile.
public static void SetTextureOffset(this Material material, Vector2 offset, int textureIndex = 0)
Parameters
materialMaterialThe material.
offsetVector2The offset, in UV units.
textureIndexintWhich texture node, 0 for the first.
SetTextureScale(Material, Vector2, int)
Sets the tiling of a texture node on every pass: how many times it repeats across the UV range. The diffuse texture is the material's first, the others follow in the order the generator visited the slots.
public static void SetTextureScale(this Material material, Vector2 scale, int textureIndex = 0)
Parameters
materialMaterialThe material.
scaleVector2The tiling; the node's default is one.
textureIndexintWhich texture node, 0 for the first.
Set<T>(Material, ObjectParameterKey<T>, T)
Sets an object parameter - a texture, a sampler - on every pass of the material.
public static void Set<T>(this Material material, ObjectParameterKey<T> key, T value) where T : class
Parameters
materialMaterialkeyObjectParameterKey<T>valueT
Type Parameters
T
Set<T>(Material, ValueParameterKey<T>, T)
Sets a value parameter on every pass of the material.
public static void Set<T>(this Material material, ValueParameterKey<T> key, T value) where T : struct
Parameters
materialMaterialkeyValueParameterKey<T>valueT
Type Parameters
T
TextureKey(string)
A texture key of your own for a ComputeTextureColor or ComputeTextureScalar node.
public static ObjectParameterKey<Texture> TextureKey(string name)
Parameters
namestringA unique name, such as
"MyGame.Screen".
Returns
TextureKeyAt(ValueParameterKey<Vector2>, int)
The key a texture node's scale or offset registered under: the base key for the first texture,
then the base key composed with i1, i2 and so on, which is how the generator
names a key's later uses.
public static ValueParameterKey<Vector2> TextureKeyAt(ValueParameterKey<Vector2> key, int textureIndex)
Parameters
keyValueParameterKey<Vector2>textureIndexintWhich texture node, 0 for the first.
Returns
ToMaterialValue(Color4, bool)
What a ComputeColor node's value becomes in the material's constants: linear, and
premultiplied by its alpha. Setting the raw sRGB colour instead makes the runtime value brighter
than the one compiled in.
public static Color4 ToMaterialValue(Color4 colour, bool premultiply = true)