About Store Forum Documentation Contact



Post Reply 
Material Instantiating
Author Message
AndrewBGS Offline
Member

Post: #1
Material Instantiating
This is probably a noob question, but I can't seem to get it to work.
I'm trying to have an instance of a material; I.e., let's say i'm having 3 blocks of stone, all of the same material. I hit one of them, I want that block's material to change (somehow, doesn't matter). As I am using a MaterialPtr to set the blocks' materials nicely, they all point to the same material, so when i change one block object's material, all of them change.

Now I played a bit with material = new Material and other attempts, but I didn't manage to solve anything.

If possible, I'd like a solution on how to create a new instance of the material in the block's create method from the global MaterialPtr.

//Game class:
MaterialPtr mtrl=<some UID>;

//Block class:
void create(...){
super.create();
material = mtrl;
}

This is what I currently have. I want a way to create a new instance of that meterial.
material=new Material; material=mtrl; didn't make any difference.

Can someone tell me how this is done please? I know it's basic, but apparently I'm that bad.
10-05-2013 04:16 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #2
RE: Material Instantiating
The meshes/materials are pointers to a single object to save memory. Either you need to dynamically tell it which material to use each draw, or have a second mesh set up with the new texture that you can swap with.
10-05-2013 07:04 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #3
RE: Material Instantiating
I guess the first option is what I want; dynamically choosing the desired material; so how would I do that? How do I create a new separate material for each of my objects?

So I'm thinking about this < New(TYPE* &data) > function.... I have no idea how to use it though. Can't someone please help me? I'm damn frustrated here, I was hoping to send my friends some demos tonight for testing and now I'm stuck with this one last issue.
(This post was last modified: 10-05-2013 08:01 PM by AndrewBGS.)
10-05-2013 07:20 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #4
RE: Material Instantiating
Bump your head into the wall long enough and it will eventuall break I guess. So after tons of blind tests I found the solution. Here it is in case someone else is having this problem:

void create(...){
super.create(obj);
Material *mat=new Material;
*mat= *mtrl;
material= mat;
}

so yey, I'm going to steam greenlight concept tomorrow smile
10-05-2013 08:25 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #5
RE: Material Instantiating
I think what you are looking for is MaterialLock
10-05-2013 08:35 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #6
RE: Material Instantiating
Well I got no answer, that's what I found and worked.
10-05-2013 09:37 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #7
RE: Material Instantiating
If I'm not mistaken, the MaterialLock will use less memory-something you can keep in mind for optimization. smile

Good luck with the Green Light!
10-06-2013 03:35 AM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #8
RE: Material Instantiating
Thanks. Well I have no idea how to use MaterialLock. It's comment isn't enough for me to understand what it it is, what it does, and most important, HOW to use it. Honestly I like the way Esenthel is built, with the tutorials and explained functions, but sometimes I really wish those comments were better or had an example too.
10-06-2013 08:19 AM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #9
RE: Material Instantiating
MaterialLock = material;
//Draw
...
MaterialLock = null;

If I'm not mistaken.
10-07-2013 12:54 AM
Find all posts by this user Quote this message in a reply
andrake Offline
Member

Post: #10
RE: Material Instantiating
Rubeus is right

it's in render cycle
i use/draw for kimono other material and for body i use default material
[code]
void drawunit()
{
MaterialLock = Materials(UID(2390851165, 1317256765, 1535852195, 3501406879));
chr->mesh()->parts.first().draw(cskel);
MaterialLock = NULL;
chr->mesh()->parts.addr(1).draw(cskel);
}
[code]
04-22-2014 06:38 PM
Find all posts by this user Quote this message in a reply
PsychoBoy Offline
Member

Post: #11
RE: Material Instantiating
Add to your object's class attribute of material id and use like this:
Code:
class Block : Game::Obj
{
    UID materialId;
}
UInt Block::drawPrepare()
{
   if(mesh && Frustum(mesh->box,matrix()))
   {
      MaterialLock=Materials(materialId);
      mesh->draw(matrix());  
      MaterialLock=NULL;
   }
   return 0;
}
Now if you want to change material, you only need to change this attribute.
04-27-2014 01:21 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply