Game Development Community

dev|Pro Game Development Curriculum

Combining Multiple Texture Layers

by Marc Schraffenberger · 04/20/2005 (6:53 pm) · 31 comments

Download Code File

Here is a simple datablock which can be used to customize your textures in game. I decided to use a datablock since we have a bunch of characters which will be sharing the same custom textures. But it will work just as well if you have one for each character, although it might be better to just slap the guts of this resource into your character class.

The datablock allows you to specify the base texture which is going to be used to blend the other layers onto. This base texture needs to be RGB format. You can then specify 2D "mount points" for where the layers should be added. (ex. where the eyes should be placed) For each "mount point" you specify a texture filename to blend at that point or some text you want to be rendered. The texture needs to be RGBA. The texture layer or text that you add will be placed centered at the "mount point". I also added in rotation, so that in the datablock you can specify (in degrees) how much each layer should be rotated. (it is just doing nearest neighbor interpolation, so the quality could be improved, but it seems to look ok for our stuff). When you specify text you can also give the font, size, and color.

To use this datablock you will also need to add to consoleTypes.h around line 69:

TypeTextureLayersDataPtr,

Here is an example datablock that is in the player.cs file:

datablock TextureLayersData(DefaultTextureLayersData)
{
  baseTexture     = "TextureLayerExample/data/shapes/player/player.png";
  
  points[0]       = "126 109";
  points[1]       = "325 64";
  points[2]       = "255 420";

  text[0]         = "kick me";
  font[0]         = "arial";
  fontColor[0]    = "255 0 0";
  fontSize[0]     = 28;   

  layerTexture[1] = "TextureLayerExample/data/shapes/player/logo1.png";
  rotation[1]     = 45;   
  
  layerTexture[2] = "TextureLayerExample/data/shapes/player/eyes1.png";
};

When the datablock has been added to the simulation, it will create the texture and store it in a TextureHandle object called texture. Here is what I added to my character class to get the texture to be applied to the shape:

bool WarUnit::onNewDataBlock(GameBaseData* dptr)
{
  mDataBlock = dynamic_cast<WarUnitData*>(dptr);
  if (!mDataBlock || !Parent::onNewDataBlock(dptr))
    return false;
  
  if( isClientObject() )
  {
    TSMaterialList* matlist = mShapeInstance->getMaterialList();

    if( mDataBlock->mTextureData->texture )
    {
      matlist->mMaterials[0] = mDataBlock->mTextureData->texture;
    }
  }
  
  scriptOnNewDataBlock();
  return true;
}

Our datablock has a mTextureData field which stores the texture layer datablock. That way you can have multiple texture layer datablocks if you have more then one material for your shape (we have one for the skin and one for the armor).

If you only have one material on your objects, you could also use the setOverrideTexture() method instead of changing the material list.

When you download the resource there is also an example mod showing
it in action. I also included the changes required to the player class in 1.3
to use this datablock.

I am pretty new to TGE (but lovin' it!) so if you see any problems with how I am doing this, please post a comment and let me know!

Enjoy!

About the author

Recent Blogs

Page «Previous 1 2
#1
04/20/2005 (6:54 pm)
This looks like a really kick ass resource, Marc. I like how you exposed the functionality in a datablock. Nice work.
#2
04/20/2005 (8:40 pm)
This is nice, after I finishing writing up some papers I will try this out. I think this was a thing I was looking for (but with lack-of-sleep pervading my thoughts, I don't really know anymore).

Robert
#3
04/20/2005 (11:09 pm)
Sounds cool. Do you think you could re-upload the .zip so we can try it out? ;)
#4
04/21/2005 (5:07 am)
@Josh: Thanks for letting me know. I re-uploaded it and it seems to work now.
#5
04/21/2005 (8:49 am)
This is very handy... 6 star resource...

What about rotation?
#6
04/21/2005 (9:45 am)
You forgot to tell everyone to add the new object type to consoleTypes.h.. around line 65:

TypeTextureLayersDataPtr,
#7
04/21/2005 (9:46 am)
@Chris: rotation sounds like a good idea. With that I definately think the points should reference the center of the layer.
#8
04/21/2005 (10:08 am)
Ok, I must be using this wrong because I crash on load. Would you mind posting an example of how you added this to your player class?
#9
04/21/2005 (10:28 am)
Center reference is the first thing Id change. Then Id put rotation into it... Then I'd probably make it editable through an ingame editor if at all possible.
#10
04/22/2005 (6:24 am)
@Chris: Thanks for the idea about rotation. I had not even thought about that. I just made some modifications to this resource and added in rotation and having the texture layers centered on the point. The rotation is specified in degrees. I will be making an ingame editor, not sure if it will be what you are looking for, but we need one for our game. I have not done much gui stuff in TGE, so it might take me a little bit.

@Josh: I will post an example using the player class soon. Not sure why it would crash though, did you get any error messages? When did it crash? Hopefully an example will try to make things more clear. Also thanks for reminding me about the console type. I completely forgot to mention that.

The next thing I want to add is rendering text to the base texture. I will be looking through the TGE classes to see if some code is already there. (maybe GFont?) But I know atleast one other person who is looking for this functionality.
#11
04/22/2005 (6:39 am)
Right! Me! :-)

I would love to have text rendering for this fantastic resource!!!

Cool stuff, dude!

Martin :)
#12
04/27/2005 (11:15 am)
An in-game editor that alows you to specify points and rotations without leaving the game to edit the datablock would be great.

Have you made any progress on this?
#13
04/28/2005 (6:07 am)
Ok. Here is an update. I added in support for text layering. The datablock example will show you the parameters to set. You can specify the text, font, size, and color. Rotation is applied. If you have both a texture and text on the same layer, it will place the text on top of the texture. Right now it does not support multiple lines of
text. (I did the text using the GFont class, so nothing outside of TGE is required)

I also put an example of the player class and a mod in the resource so that you can get this going easier. The diff files will be helpful to show what I actually added. If anyone has some problems with the example of code please let me know.

@Chris - no progress on the gui yet. I wanted to get the text stuff and example finished first. But that will be my next addition to this, besides the usual bug fixes.
#14
04/28/2005 (9:39 am)
Marc, maybe I'm just retarded, but I don't see an example anywhere. :/
#15
04/28/2005 (10:37 am)
I had been planning to re-do my skin modifier resource for the last Torque versions, but this looks like a good alternative approach.

My only question is: How feasible is it to be creating these datablocks at runtime?

Is it possible to create new datablocks during a mission and have them visible to all connected players? Or would they all need to be stitched together before the mission started, so they get transmitted all at once at the start of the mission?

I'll be digging through this on my own, of course...just wondering if anyone else has already worked it out.

-David
#16
04/28/2005 (12:47 pm)
@Josh - Nope you're not retarded. It did not upload it because the file was too big. I stripped down the example to include only the changed files. Hopefully the quick README file I wrote will explain how to get something going. If it needs more clarity just let me know.

@David - That is a great question to bring up. I will also be looking more into this. Originally the datablock idea was because we didn't need to do this at runtime. But it would be nice to make sure this thing can work during runtime as well. This might be done using the onStaticModified() method.

After looking into the datablock stuff again, I realized that I should be using the preload() method to do the layering instead of onAdd(). I will make that quick change and test it out.
#17
05/03/2005 (2:26 am)
This resource is great, it worked right out the gate for 1.3.
I'm trying to figure out if there is a way I can change the textures realtime, without having to destroy re-create the player.
Also, has anyone got this to work with the PlayerView gui?
#18
05/03/2005 (7:29 pm)
It would kick ass if you could switch texture layer datablocks in real time.
#19
05/12/2005 (12:31 pm)
You may want to take a look at how this programmer did his shape mod, you can change the shape of the player mid-game and it's ghosted to all the other players so they can see the change.

Custom Shape Mod
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7368
#20
06/15/2005 (5:19 am)
Nice ressource, but I am just wondering why the blit function use halfwidth and halfheight ?
Page «Previous 1 2