Edit: There are Clone() functions inside the Material and Model classes. Could be better to also have copy constructors to follow typical C++ style. I assumed not having copy constructor meant “is not copyable”, but they are.
Edit2: This was a bad idea. I’ve used to much Qt… ITT: me complaining about Urho not being like Qt but being better…
It would be nice if Urho had directly copyable materials (like with copy constructors). For example to give the same model different shader parameters (like for custom team colors or whatever).
I wrote an (incomplete) function that copies a material by creating a new one and copying the attributes from the original to the new one:
Material* copy_material(Material* mat)
{
Material* ret=new Material(mat->GetContext());
for(int i=0;i<14;i++) // there are 14 entries in the TextureUnit enum in GraphicsDefs.h
{
Texture* t=mat->GetTexture((TextureUnit)i);
ret->SetTexture((TextureUnit)i,t);
}
for(int i=0;i<mat->GetNumTechniques();i++)
ret->SetTechnique(i,mat->GetTechnique(i));
return ret;
}
Example usage: giving each material (aka each of the 400 cubes) a different MatDiffColor:
for(int x=-10;x<10;x++)
for(int y=-10;y<10;y++)
{
Node* boxNode_=scene_->CreateChild("Box");
boxNode_->SetPosition(Vector3(x,-3,y));
StaticModel* boxObject=boxNode_->CreateComponent<StaticModel>();
boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
Material* mat1=cache->GetResource<Material>("Materials/Stone.xml");
Material* mat2=copy_material(mat1);
mat2->SetShaderParameter("MatDiffColor",Color((x+10)/20.0f,(y+10)/20.0f,.5)); // the diffuse colors are between (0, 0, 0.5) and (1, 1, 0.5)
boxObject->SetMaterial(mat2);
boxObject->SetCastShadows(true);
boxNode_->SetScale(.9);
}
There may be also other classes where it may be useful to have a copy option (which doesn’t exist yet).
Is it a good idea to copy materials like that? Are textures & techniques shared?