Urho3D Wiki
mNo edit summary
Tags: Visual edit apiedit
(Added note regarding SharedPtr issue)
Tags: Visual edit apiedit
Line 15: Line 15:
 
boxObject->SetMaterial(mat2);
 
boxObject->SetMaterial(mat2);
 
</syntaxhighlight>
 
</syntaxhighlight>
  +
'''NOTE:''' Material::Clone() is returning a Urho3D::SharedPtr<Urho3D::Material>, you need to either save this SharedPtr somewhere or Detach() it or the Material will get deleted and Urho crashes.
  +
 
You can use this to make every model unique by randomizing colors or color models in a team color. You can also pass custom parameters to control things like current weather or different (localized) lighting scenes.
 
You can use this to make every model unique by randomizing colors or color models in a team color. You can also pass custom parameters to control things like current weather or different (localized) lighting scenes.
   

Revision as of 00:02, 25 July 2015

Normally you assign materials like this:

StaticModel* boxObject=boxNode_->CreateComponent<StaticModel>();
boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));

You can also load materials, copy them, and/or change parameters:

Material* mat1=cache->GetResource<Material>("Materials/Stone.xml");
Material* mat2=mat1->Clone();    // copy material
// change the ciffuse color
mat2->SetShaderParameter("MatDiffColor",Color(0.1,0.4,0.9));
boxObject->SetMaterial(mat2);

NOTE: Material::Clone() is returning a Urho3D::SharedPtr<Urho3D::Material>, you need to either save this SharedPtr somewhere or Detach() it or the Material will get deleted and Urho crashes.

You can use this to make every model unique by randomizing colors or color models in a team color. You can also pass custom parameters to control things like current weather or different (localized) lighting scenes.

An example of 400 uniquely colored cubes:

Copy material