Page 1 of 1

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

Posted: Wed May 09, 2018 1:10 am
by bigclu
SUCH AS when mouse hit the entity in game , make it outer glow ,or make it change color.
thanks!

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

Posted: Wed May 09, 2018 2:21 am
by mknmknmknjk
get material , change material parameter in shader.

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

Posted: Wed May 09, 2018 3:27 am
by bigclu

Code: Select all

this.Entity.LoadMaterial(PlayerMaterialUrl);
i am just find loadMaterial, how can i change in shader?

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

Posted: Wed May 09, 2018 3:34 am
by bigclu
and how can i get Mesh componet use C#?

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

Posted: Wed May 09, 2018 4:38 am
by mknmknmknjk
GetOrCreateComponent< T >()

GetComponents< T >()

CryEngine.Entity.Material

there is SetGetMaterialParamFloat in c++
http://docs.cryengine.com/display/CPP/IMaterial

c# should have the same.

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

Posted: Wed May 09, 2018 9:50 am
by cry-miron
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.