About Store Forum Documentation Contact



Post Reply 
changing materials from code?
Author Message
jagatai Offline
Member

Post: #1
changing materials from code?
Greetings,

Apologies if this has been asked before, but wasn't able to find any references.

Are there any examples that show how to change materials that belong to a mesh? example, if i have a character and would like to change the material assigned to the head, how would i go about doing that ingame?

I'm not looking to change the mesh, but to simply change the material assigned to a part of the mesh.

hope this makes sense?

Thanks,
-J

i *think* i found what im looking for, would like to confirm this is the proper way to do it ES style lol smile

Code:
MaterialPtr myMat = UID(_insert_mat_here_);
      obje->mesh()->parts[idx].setMaterial(myMat);

So basically i call on mesh()->parts index and set its material upon obj creation?

and if done during rendering then i use MaterialLock?

Thanks,
-J
(This post was last modified: 05-31-2013 07:18 AM by jagatai.)
05-31-2013 06:44 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: changing materials from code?
Hello,

If the change needs to be permanent, then please use MeshPart.setMaterial
if it needs to be dynamic during rendering then please use MaterialLock
05-31-2013 01:18 PM
Find all posts by this user Quote this message in a reply
jagatai Offline
Member

Post: #3
RE: changing materials from code?
Thanks Esenthel.

-J
05-31-2013 10:32 PM
Find all posts by this user Quote this message in a reply
jagatai Offline
Member

Post: #4
RE: changing materials from code?
Just quick question, below is a code snip, basically im spawning NPC's and randomizing their head, jacket and pants materials so each one has a unique look, unfortunately the change seems to be global? i.e. ALL NPC's change rather than each one;

Code:
MaterialType *mat_head = &HeadType[Random(MatTypeHeadElms)];
MaterialType *mat_jacket = &JacketType[Random(MatTypeJacketElms)];
MaterialType *mat_pants = &PantsType[Random(MatTypePantsElms)];
                        
MaterialPtr part1 = mat_head.mat;
MaterialPtr part2 = mat_jacket.mat;
MaterialPtr part3 = mat_pants.mat;
                        
npc[idx].mesh->parts[0].setMaterial(part3);
npc[idx].mesh->parts[1].setMaterial(part1);
npc[idx].mesh->parts[2].setMaterial(part2);

**npc is a class from Chr, the MatType** is my array containing the material UID's.

**MaterialType is my class that contains the array elms.

EDIT: Wanted to add its not just changing ALL npc's but my player as well ;(

Thanks,
-J
(This post was last modified: 05-31-2013 11:54 PM by jagatai.)
05-31-2013 11:40 PM
Find all posts by this user Quote this message in a reply
gwald Offline
Member

Post: #5
RE: changing materials from code?
Hi, I think you need individual mesh for the life or chr.
Or if your changing mat on the fly, it matters where you put setMaterial ie in draw?
Not 100% sure tho, i'm still learning myself and haven't tried it.
06-01-2013 12:38 AM
Visit this user's website Find all posts by this user Quote this message in a reply
jagatai Offline
Member

Post: #6
RE: changing materials from code?
@gwald,

Thanks for the reply, yeah im currently trying to set the Mats after Game.World.objCreate() returns true, using setMaterial since its permanent.

Am hoping its not a per mesh, other wise my artist is going to have a heart attack as something like the above would allow us to generate a variety of npc's with different looks from 1 mesh by simply changing materials/textures.

-J
06-01-2013 12:52 AM
Find all posts by this user Quote this message in a reply
gwald Offline
Member

Post: #7
RE: changing materials from code?
Hey, You can reuse the mesh, ie if it's permanent, then you might want instantiate each mesh into your chr class rather then passing a pointer.
06-01-2013 01:37 AM
Visit this user's website Find all posts by this user Quote this message in a reply
jagatai Offline
Member

Post: #8
RE: changing materials from code?
@gwald,

yeah tried that as well and still got the same results, i need to take a step back and see what im missing.

thanks for the help.
-J
06-01-2013 08:03 PM
Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #9
RE: changing materials from code?
Hey there,

I had a similar issue in this project: http://www.esenthel.com/community/showth...p?tid=6235

Basically, what I did was duplicate the mesh and material:

Code:
Game.ObjParamsPtr tempParams = UID(3249259405, 1294192879, 2507658368, 1464910172); // get turret object parameters
  
   // Every Turret points to the same mesh-instance. We need to duplicate the mesh so that it's unique for each Turret.
   Game.ObjParamsPtr newParams;
   MeshPtr newMesh = new Mesh();
   newMesh->add(*tempParams->mesh());

   // Copy our material param too
   MaterialPtr newMat = new Material();
   newMat = tempParams->material(); // get the material from the .obj, assign to new material
   material = newMat; // Change pointer to that material

   // Apply all the duplicates to the new ObjParamsPtr
   newParams = tempParams;
   newParams->mesh(1, newMesh);
   newParams->material(1, material);

Hope that helps. If anyone has a better way, I'd be very interested to know. pfft
(This post was last modified: 06-01-2013 08:42 PM by Tottel.)
06-01-2013 08:41 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #10
RE: changing materials from code?
MeshPtr newMesh = new Mesh();
MaterialPtr newMat = new Material();
this will cause memory leaks if you don't delete the materials.

Why you're not using MaterialLock? That's the best way to achieve best performance and avoid creating secondary instances of Mesh objects (which requires additional memory)
06-02-2013 01:24 PM
Find all posts by this user Quote this message in a reply
jagatai Offline
Member

Post: #11
RE: changing materials from code?
(06-02-2013 01:24 PM)Esenthel Wrote:  Why you're not using MaterialLock? That's the best way to achieve best performance and avoid creating secondary instances of Mesh objects (which requires additional memory)

Sorry for the bother, is there an example i could see to implement MaterialLock? i *thought* it was inside the Draws, but not sure. im still wiggling my way around the engine.

Thanks,
-J
06-02-2013 11:28 PM
Find all posts by this user Quote this message in a reply
gwald Offline
Member

Post: #12
RE: changing materials from code?
Hey jagatal,
If you google this:
site:esenthel.com MaterialLock
You'll see some examples
06-02-2013 11:35 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #13
RE: changing materials from code?
Also Game Objects source code use that (just set it before calling mesh part draw, and set to NULL afterwards).
06-03-2013 12:08 PM
Find all posts by this user Quote this message in a reply
Post Reply