Material Decorators
Material Decorator is a C# class that simplify the use of custom materials to the users. It allows to define how the effect resource layout elements are shown in the Material Editor. To generate material decorator of 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 that you will see from Visual Studio
Example
Parting 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 the following:
//------------------------------------------------------------------------------
// <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 to get/set class properties to make easier use of the effect from code.
Now when you open a material asset from Material Editor and the material uses MyGraphicEffect its properties as:
Customize Material Decorators
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 Vector3 property because this is the type using 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 the Evergine Studio, the Material Editor shows its color property as: