Re: How to dynamic modify the Material in Game with C#?

#6
If you use GetComponent<T>() in C# you can only get components that are exposed to C#. At the moment the only components that are exposed to C# are the components that are also made with C#. The default components like the Mesh component are made in C++ and can't be used in C# code.

To adjust the Material of an entity you can however get the IMaterial from the Entity, and set parameters with the SetGetMaterialParamVec3() method. For example:

Code: Select all

protected override void OnInitialize()
{
var material = Entity.Material;
string parameterName = "diffuse";
// IMaterials convert the xyz Vector3 to an rgb Color internally.
// The values for each component range from 0 - 1.
Vector3 color = new Vector3(0.467f, 0.867f, 0.467);
// The last parameter indicates whether to get or set the value.
material.SetGetMaterialParamVec3(parameterName, color, false);
}
There's also a method for float values, but it wasn't properly exposed by SWIG. I'll try to fix it as soon as possible in a future version of the engine. Most likely by creating new methods like this:

Code: Select all

material.SetMaterialParamFloat(parameterName, 1.0f);
float floatValue = material.GetMaterialParamFloat(parameterName);

material.SetMaterialParamVec3(parameterName, new Vector3(0.1f, 0.2f, 0.3f));
Vector3 vectorValue = material.GetMaterialParamVec3(parameterName);
Hopefully I'll be able to get this in before the final release of 5.5, otherwise it will come in a later version.

Who is online

Users browsing this forum: No registered users and 0 guests

cron