Material Decorators
Material Decorator is a C# class that simplifies the use of custom materials for users. It allows defining how the effect resource layout elements are shown in the Material Editor. To generate a material decorator for your effects, you need to use the Effect Editor.
Generate Material Decorator
From the Effect Editor toolbox, push the button to generate the Material Decorator.
A new C# class will be created in your C# solution, which you will see in Visual Studio.
Example
Starting from the following effect resource layout block (This is the section marked in the effect code with the [Begin_ResourceLayout]
and [End_ResourceLayout]
tags):
[Begin_ResourceLayout]
cbuffer PerDrawCall : register(b0)
{
float4x4 WorldViewProj : packoffset(c0); [WorldViewProjection]
};
cbuffer Parameters : register(b1)
{
float3 Color : packoffset(c0); [Default(0.3, 0.3, 1.0)]
};
[End_ResourceLayout]
The generated Material Decorator will be as follows:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DocumentationWorkBench.Effects
{
using Evergine.Common.Graphics;
using Evergine.Framework.Graphics;
using Evergine.Framework.Graphics.Effects;
using Evergine.Mathematics;
[Evergine.Framework.Graphics.MaterialDecoratorAttribute("67d3f67f-e1f0-4075-894d-5a58d3697fb6")]
public partial class MyGraphicEffect : Evergine.Framework.Graphics.MaterialDecorator
{
public MyGraphicEffect(Evergine.Framework.Graphics.Effects.Effect effect) : base(new Material(effect))
{}
public MyGraphicEffect(Evergine.Framework.Graphics.Material material)
: base(material)
{}
public Evergine.Mathematics.Matrix4x4 PerDrawCall_WorldViewProj
{
get { return this.material.CBuffers[0].GetBufferData<Evergine.Mathematics.Matrix4x4>(0);}
set { this.material.CBuffers[0].SetBufferData(value, 0); }
}
public Evergine.Mathematics.Vector3 Parameters_Color
{
get { return this.material.CBuffers[1].GetBufferData<Evergine.Mathematics.Vector3>(0); }
set { this.material.CBuffers[1].SetBufferData(value, 0); }
}
}
}
The above Material Decorator example shows how the effect resource layout is translated into get/set class properties to make it easier to use the effect from code.
Now, when you open a material asset from the Material Editor and the material uses MyGraphicEffect, its properties will be shown as:
Customize Material Decorators
By editing the Material Decorator C# class, you can modify how the properties are shown in the Material Editor. For example, the Color property appears as a Vector3 property because this is the type used in the HLSL effect code. You can change it to use a Color picker to configure this parameter with the following code change:
public Evergine.Common.Graphics.Color Parameters_Color
{
get
{
Vector3 v = this.material.CBuffers[1].GetBufferData<Vector3>(0);
return Color.FromVector3(ref v);
}
set
{
this.material.CBuffers[1].SetBufferData(value.ToVector3(), 0);
}
}
After this change, if you reload the project in Evergine Studio, the Material Editor will show its color property as follows: