Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I encounter a problem for set a background color on my model instantiated. I can change the texture but not change color. Is there a parameter for change the color ? Or change the opacity ? I am looking for a way to highlight a model.

thank you so much

Here is my code used: Everything works except the two last line. I searched but can not find name parameter for color and opacity

//Does not work

effect.Parameters["Color"].SetValue(new Vector3(0,1,0)); effect.Parameters["Opacity"].SetValue(0.5f);

        void DrawModelHardwareInstancing(Model model, Matrix[] modelBones,
                                             Matrix[] instances, Matrix view, Matrix projection)

                foreach (ModelMesh mesh in model.Meshes)
                    foreach (ModelMeshPart meshPart in mesh.MeshParts)
                        // Tell the GPU to read from both the model vertex buffer plus our instanceVertexBuffer.
                            new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
                            new VertexBufferBinding(instanceVertexBuffer, 0, 1)

                        Game.GraphicsDevice.Indices = meshPart.IndexBuffer;

                        // Set up the instance rendering effect.
                        Effect effect = meshPart.Effect;
                        //effect.CurrentTechnique = effect.Techniques["HardwareInstancing"];

                        //Work perfect
                        /* ***************************** */
                        //Does not work
                        effect.Parameters["Color"].SetValue(new Vector3(0,1,0));
                        /* ***************************** */

                        // Draw all the instance copies in a single call.
                        foreach (EffectPass pass in effect.CurrentTechnique.Passes)

                            Game.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,
                                                                   meshPart.NumVertices, meshPart.StartIndex,
                                                                   meshPart.PrimitiveCount, instances.Length);
share|improve this question
up vote 1 down vote accepted

Changing color to a texture with an effect file can be done in this way:


And then a simple test game:

VertexBuffer bufferVertex;
IndexBuffer bufferIndex;
Effect effect;
Matrix View, Projection;

protected override void LoadContent()

    effect = Content.Load<Effect>("Color");
    Projection = Matrix.CreateOrthographic(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, -10000f, 10000f);

    View =
    Matrix.CreateScale(1.0f) *
    Matrix.CreateRotationY(MathHelper.ToRadians(225)) *
    Matrix.CreateRotationX(MathHelper.ToRadians(30)) *
    Matrix.CreateTranslation(0, 0, 0);

    VertexPositionTexture[] vertices = new VertexPositionTexture[4];

    vertices[0] = new VertexPositionTexture(new Vector3(-128, 0, -128), new Vector2(0, 1));
    vertices[1] = new VertexPositionTexture(new Vector3(128, 0, -128), new Vector2(1, 1));
    vertices[2] = new VertexPositionTexture(new Vector3(-128, 0, 128), new Vector2(0, 0));
    vertices[3] = new VertexPositionTexture(new Vector3(128, 0, 128), new Vector2(1, 0));

    bufferVertex = new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, 4, BufferUsage.WriteOnly);

    int[] indexs = new int[6] { 0, 1, 2, 2, 1, 3 };

    bufferIndex = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, 6, BufferUsage.WriteOnly);


protected override void Draw(GameTime gameTime)

    effect.CurrentTechnique = effect.Techniques["Color"];

    effect.Parameters["xAmbient"].SetValue(new Color(255, 255, 255).ToVector4());

    foreach (EffectPass pass in effect.CurrentTechnique.Passes)

    GraphicsDevice.Indices = bufferIndex;
    GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);


Example Default:

enter image description here

effect.Parameters["xAmbient"].SetValue(new Color(255, 255, 255).ToVector4());
effect.Parameters["xColor"].SetValue(new Color(0, 0, 0).ToVector4());

Example Red:

enter image description here

effect.Parameters["xAmbient"].SetValue(new Color(255, 0, 0, 0).ToVector4());
effect.Parameters["xColor"].SetValue(new Color(255, 0, 0, 0).ToVector4());

Hope that this can be applied anywhere else.

share|improve this answer
If the model has a single texture, it can be changed by creating a single color texture also and effect.Parameters["xTexture"].SetValue(ColorToTexture(Color.Red)) for example. This has worked for me before:… – Deukalion Feb 7 '13 at 19:38
thanks a lot amigo ^^ And sory for my slowness. This work very finnnne – Mehdi Bugnard Mar 11 '13 at 15:06

This is not really dynamically coloring but you can alter the lighting effects to achieve the same thing. The AmbientLightColor effect should work.

effect.AmbientLightColor = new Color(r, b, g);

Also there is an effect.alpha that you use for transparency iirc which takes a float between 0-1.

share|improve this answer
Unfortunately effect.AmbientLightColor does not exist! AmbienLightColor is a method from the Class "BasicEffect" and not the Class "Effect". Thank you for trying to help me. – Mehdi Bugnard Feb 7 '13 at 14:38
Pretty sure its is always a basic effect unless you modified the content processor. Try doing (effect as BasicEffect).AmbientLightColor; – ClassicThunder Feb 7 '13 at 14:50

Your Answer


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.