Modifying materials at runtime
In this section we're going to modify a material at runtime.
Note
Similar to IStatObj
, we can also clone our material to avoid making changes to all of the objects using it currently. To do so, call IMaterialManager::CloneMaterial
, which is accessible via gEnv->p3DEngine->GetMaterialManager()
.
The first thing we need to do is obtain an instance of the material we want to edit. If we have an entity nearby, we can use IEntity::GetMaterial
as shown:
IMaterial *pMaterial = pEntity->GetMaterial();
Note
Note that IEntity::GetMaterial
returns null if no custom material has been set. If this is the case, you might want to rely on a function such as IStatObj::GetMaterial
.
Cloning a material
Note that IMaterial
instances can be used for multiple objects. This means that modifying an object's parameters can result in changes on objects other than that you retrieved the object from.
To resolve this, we can simply clone the material before modifying it via the...