Dynamically change colors on objects?

Started by
5 comments, last by Smittens 15 years, 10 months ago
Hi, This is something I've been wondering about for a while, and have not found a solution (either online or by coming up with one myself). I think the main problem is that I don't know what to call it, but I can describe it! :) The effect I'm looking to create is like in Age of Empires (and many other games), units appear different colors depending on what color the player is. But I'm assuming that there is not a separate model for each color? I'm wondering about it because I'm making a sports game, and I'd like the player to be able to create their own team and choose their own colors. So how is this done? Thanks, Smittens
Advertisement
I'm not sure, haven't done it before, but I believe an approach would be to tint certain materials of the player's models; such as the base helmet texture.
You could use two textures, bound the texture units 0 and 1 respectively. The first texture is the usual unit texture, the second one is an alpha texture that masks which parts should be coloured in the team colors.
In the pixel shader you would combine those two textures like this:
pixel = basetexel * (1 - masktexel) + unitcolor * masktexel
Quote:Original post by Deaken Frost
You could use two textures, bound the texture units 0 and 1 respectively. The first texture is the usual unit texture, the second one is an alpha texture that masks which parts should be coloured in the team colors.
In the pixel shader you would combine those two textures like this:
pixel = basetexel * (1 - masktexel) + unitcolor * masktexel


From a performance perspective or if you don't want to fiddle around with shaders it might be easier to generate textures for every team on startup rather than using alpha blending. When you have the texture in memory, before sending it to the card, you can replace pixels.
One way to do this is to replace pixels with a certain alpha value, but you could also use a predefined color, for example bright pink, to be replaced.

A second way, and this will give you terrible results probably, is to use GL_COLOR_MATERIAL (or whatever the DX equivalent is). It will multiply the current color with the texture color. So for the red team you could use a current color of RGB(1, 0.5, 0.5) to make red more dominant.
You'll have to try it out. If the color material thing works well for you it's the easiest thing I can think of.
STOP THE PLANET!! I WANT TO GET OFF!!
Thanks for the replies. Out of curiosity, (my project is 3d) is there a way to do this on 2d sprites?

Most 2D sprite APIs allow you to pass in a seperate color by which the texture you draw will be modulated. This can be used to get some quick results that may be acceptable. An easy (though a tad unefficient) way to get good results would be to modulate a mask texture and simply draw that over the original texture. So basically you'd draw the 'team colored' parts on top of the base colored parts, if that makes any sense [smile]

For masking in one pass, you'll still have to rely on texture stages (or the OpenGL equivalent) or shaders, but it should typically work much like in 3D.
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Thanks for the help. I had figured it might involve drawing two layers, but was wondering about an easier way.

This topic is closed to new replies.

Advertisement