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.

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.
                        Game.GraphicsDevice.SetVertexBuffers(
                            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"];
                        effect.Parameters["World"].SetValue(modelBones[mesh.ParentBone.Index]);

                        //Work perfect
                        effect.Parameters["View"].SetValue(view);
                        effect.Parameters["Projection"].SetValue(projection);
                        effect.Parameters["Texture"].SetValue(textureInstancedModel);
                        /* ***************************** */
                        //Does not work
                        effect.Parameters["Color"].SetValue(new Vector3(0,1,0));
                        effect.Parameters["Opacity"].SetValue(0.5f);
                        /* ***************************** */

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

                            Game.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,
                                                                   meshPart.NumVertices, meshPart.StartIndex,
                                                                   meshPart.PrimitiveCount, instances.Length);
                        }
                    }
               ........
}
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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

HLSL CODE

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);
    bufferVertex.SetData(vertices);

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

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

Drawing

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    effect.CurrentTechnique = effect.Techniques["Color"];
    effect.Parameters["Projection"].SetValue(Projection);
    effect.Parameters["View"].SetValue(View);
    effect.Parameters["World"].SetValue(Matrix.Identity);

    effect.Parameters["xTexture"].SetValue(Content.Load<Texture2D>("grass"));
    effect.Parameters["xAmbient"].SetValue(new Color(255, 255, 255).ToVector4());
    effect.Parameters["xColor"].SetValue(Color.Black.ToVector4());

    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        pass.Apply();
    }


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

    base.Draw(gameTime);
}

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
1  
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: gamedev.stackexchange.com/questions/45464/… –  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
add comment

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
1  
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
add comment

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.