color keying

Started by
5 comments, last by drekkar 19 years, 6 months ago
well i dont know if the topic is the word for it, but I'm just not sure what's a more reasonable way to do this. For example take starcraft, how the units and building each had the team color on them depending on who owned them. I want to do something like this in my game (not RTS, but that doesnt matter :P), so I was thinking if a texture has something like a perfect grey (r==g==b) it will use my specified color as the shade (so I can have stuff shaded and color keyed). But I got to thinking how should I do this. Is there anything in DirectX that does this? The only solution I could think of is to copy the texture and change the colors, or lock the texture & make the changes, then do it each time I want a different color. But I got to thinking would it be sensible to have to change maybe 200 pixels each frame locking and unlocking the texture that much? That's why I thought it might be better to just make seperate textures for each color I need, but I am going to have so many things that I don't think it's worth it on the RAM (if it would even fit) :( Any suggestions?
Advertisement
I think the problem is that in games like Star Craft they use sprites rather than texturing meshes. Each sprite is simply made up of an index buffer into a colour table where a certain number (eg -1) represents that it should be replaced with the index to the owners custom colour. Not sure how you'd recreate this efficiently using textures and meshes. Maybe using shaders but I can still see it being a big drain. Unless you just have a different set of textures for each player rather than trying to change textures on the fly.
[size="1"] [size="4"]:: SHMUP-DEV ::
Motorherps solution would work, if the graphic card would support palettized textures. But most current cards don't do that.

There are several solutions to your problem :

a) The easiest and most memory consuming solution would be to have a given set of colors. You apply the colors to the inked portion of your sprites and thus create a sprite texture per color.

b) The second easiest solution would be to have 2 textures for your sprites. One texture with the static colors and one texture that contains only the part that has to be recolored dependently on the player color. You'll set the player color into the tcolor factor and draw the sprite in 2 passes.

c) The third solution would require a pixel shader that would take a color from the texture, compare it with a default color no one would ever take (0xff00ff) and replace that color by the player color.

Solution c) and b) will allow almost infinite player colors while a) is the fastest solution.

c) is quite easy to do but it requires your hardware to support pixel shaders.
----------------------------------------http://www.sidema.be----------------------------------------
Method b can produce far superior results. The second texture can be grayscale and then you modulate it with the team colour. One of these days I'm going to do a tutorial on this.
Stay Casual,KenDrunken Hyena
Right. With method (b) you'll have two sets of textures instead of one, but it's way better than loading a new texture for each color, especially in a game like Starcraft that supports eight different player colors. This "colorizing grayscale" technique has been used since time eternal. I first discovered it being used in Half-life, about the time I started 3D programming, but I'm sure it's way older than that.

Also note, if your target hardware supports multi-texture blending, you can set up two stages (one for the base texture, one for the colorized texture) and render in a single pass.

GDNet+. It's only $5 a month. You know you want it.

Quote:Original post by DrunkenHyena
Method b can produce far superior results. The second texture can be grayscale and then you modulate it with the team colour.


Yep, I agree on that. That's how we did it on Settlers IV (in fact we had 128 static colors, 96 blended colors and 32 pure player colors).
----------------------------------------http://www.sidema.be----------------------------------------
thanks for the replies guys :) If I was to use method B would I just lock the greyscaled texture and modulate it before each render of each object on the screen of a different color, or is there another way?

This topic is closed to new replies.

Advertisement