Harry
Member
|
Changing material only on one object
I've got new struct based on Item. I've got sht like this to check wchih object I use:
Code:
REPA(policeCars)
{
if(Dist(policeCars[i].actor.pos(),Players[0].ctrl.actor.pos())<=3)whichCar=i;
}
Everythng is ok except material. When I change materials they change in all this objects:
Code:
REP(policeCars[whichCar]mesh->parts())
{ if(policeCars[whichCar].mesh->part(i).material[0]==Materials("Obj/Pojazdy i helikopter/Radiowoz/Frontlights.mtrl")) policeCars[whichCar].mesh->part(i).setMaterial(Materials("Obj/Pojazdy i helikopter/Radiowoz/FrontlightsGlow.mtrl"));
Why materials don't change only in object which I'm using now?
(This post was last modified: 11-26-2009 03:46 PM by Harry.)
|
|
11-26-2009 03:46 PM |
|
Esenthel
Administrator
|
RE: Changing material only on one object
because many Mesh* (pointers) point to one Mesh (mesh object)
you need to change material in object draw method, and then set it back
virtual void draw()
{
Material *temp=mesh.material;
mesh.material=custom material;
__super::draw();
mesh.material=temp;
}
|
|
11-26-2009 03:52 PM |
|
Harry
Member
|
RE: Changing material only on one object
Code:
UInt PoliceCar::drawPrepare()
{
Material *frontlgt =Materials("material");
if(frontLights)mesh->part(4).setMaterial(Materials("material2"));
UInt mode=__super::drawPrepare();
if(!frontLights)mesh->part(4).setMaterial(frontlgt);
return mode;
}
What is wrong here? I have no idea :( Materials sometimes change on all meshes (I have 4 on map) or never change. This meshes are load in .obj file. When I load meshes in code (Mesh mesh; mesh.load), I haven't got this problem.
(This post was last modified: 03-18-2010 03:38 PM by Harry.)
|
|
03-18-2010 03:38 PM |
|
Esenthel
Administrator
|
RE: Changing material only on one object
try this:
Bool swap_mat;
Material *new_mat;
{
Material *temp;
if(swap_mat)
{
temp=mesh->part(4).material();
mesh->part(4).setMaterial(new_mat);
}
draw
if(swap_mat)mesh->part(4).setMaterial(temp);
}
if this wont help let me know
|
|
03-23-2010 05:35 PM |
|
Harry
Member
|
RE: Changing material only on one object
Ok. This way finally helps me Thank you a lot. But I have a question, because I don't understand in 100% how this code works. Before __super and after it is two if instructions with if(swap_mat). Why in second time you didn't use (!swamp_mat) or after first if "else" ?
|
|
03-23-2010 09:13 PM |
|
Esenthel
Administrator
|
RE: Changing material only on one object
you have 2 cases:
1. default material:
mesh.draw()
2. swapped material
remember old material
set new material
mesh.draw
set old material
|
|
03-23-2010 09:54 PM |
|
Harry
Member
|
RE: Changing material only on one object
Ah ok I understand it now Thanks again.
|
|
03-23-2010 10:01 PM |
|