Sign in to follow this  

Dynamically change colors on objects?

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this