Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is it possible to change a specific color of a model to another?

Example:

Everything that's blue should be red or something like that.

And is it possible to do this without the use of effects? I still want to use the basic effect.

share|improve this question
2  
Why the no effects limitation? There is an entire example dedicated to doing this, xbox.create.msdn.com/en-US/education/catalog/sample/… , but it uses shaders. –  Seth Battin Apr 16 '13 at 15:37
    
How are you calculating color information? Is your model texture mapped or are you using per-vertex color attributes? –  Mokosha Apr 16 '13 at 15:49
    
I'm using textures attached to the model with SketchUp. I don't want to use effects, because I want to use the basic effect. –  user1990950 Apr 16 '13 at 16:33

2 Answers 2

Because you are unwilling to employ a non BasicEffect to accomplish this, here is your only other option. This is quoted from the description of the complete, free, and functional example project created by the developers of the XNA framework, Color Replacement.

When it's the Red Team versus the Blue Team, or when you want to drive a car painted with your favorite color, you either need to create a texture for each color or perform color replacement programmatically.

In this quote, the pragmatically refers to using an instance of Effect and a shader. So you can see what you are left with. (In case you would consider changing the model's data at runtime, don't forget that it is compiled into binary before the game ever runs.)

I strongly recommend that you reconsider your position about only using BasicEffect. Drawing models does not require it.

share|improve this answer
    
"the pragmatically refers to using an instance of Effect and a shader" Or you could clone the texture and edit the color array. –  ClassicThunder Apr 17 '13 at 19:46
    
@ClassicThunder Yup. There are all kinds of memory- and CPU-intensive texture-swapping solutions. –  Seth Battin Apr 17 '13 at 19:55
    
Trading performance for a simplicity in regards to a one time operation in a homebrewed project may be perfectly acceptable in this situation. –  ClassicThunder Apr 17 '13 at 19:58

You can create a texture of the same size, alter the colors programmatically, and then use it in place of the original. Then you can replace the altered texture using the BasicEffect.Texture property.

Texture2D newt = new Texture2D(graphicsDevice, t.width, t.height);

Color[] data = new Color[t.Width * t.Height];
t.GetData(data);

for (int x = 0; x < t.width * t.height) {
    if (data[x] == Color.White) {
        data[x] = Color.Blue;
    }
}

newt.SetData(data);

Note that this is much more memory intensive than using a shader to recolor the texture dynamically since you need as many textures as number models * active colors.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.