About Store Forum Documentation Contact



Post Reply 
Problem with MaterialLock
Author Message
fatcoder Offline
Member

Post: #1
Problem with MaterialLock
I was trying to use MaterialLock to change the material on an object's mesh in the drawPrepare() method. The problem is that the mesh is just drawn with no material, i.e. it is just white.

I have reproduced the problem using the "Extending Game Object Class" tutorial. Here are the steps to reproduce.

1. Modify the drawPrepare() method in the tutorial like this.

Code:
UInt NewObj::drawPrepare()
{
   if(mesh && Frustum(mesh->box,matrix()))
   {
      MaterialLock = Materials("Mtrl/Brick/0.mtrl");
      mesh->draw(matrix());
      MaterialLock = NULL;
   }
   return 0;
}

If you just run the tutorial as is, with just this modification, it should work just fine. You should see the three barrels with the brick texture applied to them.

2. Now go to the Model Editor and create a new mesh (using Add from the Mesh menu - doesn't matter which one, a box is fine). Then save this mesh to the Obj/Item/Misc/Barrel folder as "1.mesh"

3. Finally, change the create() method in the tutorial code to load this mesh instead of the default barrel mesh. Like this.

Code:
void NewObj::create(Game::ObjParams &obj_params)
{
   position=obj_params.matrixFinal().pos;
   mesh    =Meshes("Obj/Item/Misc/Barrel/1.mesh");
}

Now run the tutorial and you'll see the problem. The new mesh will be drawn white (no material). I haven't been able to figure out what the difference is between the barrel mesh and a new mesh that you can create in the model editor. This problem is happening with any mesh that I create or import from 3ds max. I'm wondering if there is some bug here.

I have found a strange work around for the problem though (which makes me believe there is a bug in EE). If you apply a material to the new mesh in the Model Editor first and then save it with this material, the code then works correctly.

For example, load the "1.mesh" that you previously created back into the Model Editor. Now load a texture into the Material editor (doesn't matter which one - I picked Mtrl/Grass/0.mtrl) and click Set to apply it to the mesh. Finally save the mesh and then run the modified tutorial code again. It should now work correctly. You won't see the grass material on the mesh but the brick one instead.

On a side note, setMaterial() on the mesh always works correctly. It is just MaterialLock that isn't working. I would prefer to use MaterialLock as it gives a big performance boost in my situation (lots of texture swapping).
04-16-2012 01:28 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Problem with MaterialLock (Possible Bug)
This is not a bug, but functionality limitation, to clarify I have made the comments for 'MaterialLock' more clear (available in next SDK), as below:

Code:
*MaterialLock; // Material to override during drawing. Every Mesh rendered with 'MaterialLock' value different than NULL will use 'MaterialLock' material instead of its default materials. In order for this functionality to work, the default Mesh materials must be "compatible" with the 'MaterialLock' material. Material compatibility means having the same amount of textures and the same shader technique specified in both materials, so that the same shader would be loaded for the Mesh when using both materials. This requirement exists because when using 'MaterialLock' the Mesh does not have new shaders loaded according to the new material, but it still uses the old shaders for its default materials. Default value for 'MaterialLock'=NULL
04-22-2012 07:51 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #3
RE: Problem with MaterialLock
Thanks. That is much clearer now... makes sense.
04-22-2012 08:25 AM
Find all posts by this user Quote this message in a reply
Post Reply