Instanced Materials
Materials instancing is a technique reffering to reusing the same material asset but with modified parameters values. In that way the base material appearance can be modified without an exensive recompilation of the material. Also, materials and shaders compilation is available only in the editor (no shaders compilation at runtime) so using material instances is one of the ways to customize the look of the objects in game.
With that being said instanced materials can be dynamically modified at runtime or even generated from code to randomize material parameters per object.
Creating Material Instance
There are two ways to create a MaterialInstance
asset.
- By using the Content window
public class MyScript : Script
{
[Tooltip("Base material asset to override its properties")]
public Material BaseMaterial;
public override void OnStart()
{
// Create dynamic material instance and modify parameter
var instance = BaseMaterial.CreateVirtualInstance();
instance.SetParameterValue("Color", new Color(0xff00ff));
// Assign instance to the material
Actor.As<StaticModel>().SetMaterial(0, instance);
}
}
To learn more about using material instances from code go to the C# scripting API here.
Editing parameters
After creating a new Material Instance asset, double-click on it in a Content window. In the Material instance editor window you can pick a base material for the instance and customize its properties. All your changes will be visible live in both the editor window and in the game. This makes adjusting parameters easier.
To set the base material, simply drag & drop it from the Content window or any asset picker right to the Base Material property.
Then all the public material parameters may be modified.