1
\$\begingroup\$

I'm trying to create a colored 3D model for my XNA game in Blender. I don't want to use textures - I need only solid colors. Here's the model, as seen in Blender:

3D model seen in Blender

After exporting to FBX and opening in Visual Studio 2012 (some kind of built-in viewer I guess), the lightning isn't perfect but the colors are preserved:

enter image description here

But something's wrong when I try to display it using XNA:

enter image description here

Here's my code for displaying objects:

protected void DrawModel(GameObject obj, bool semitransparent = false, float angle = 0.0f) { var transforms = new Matrix[obj.Model.Bones.Count]; obj.Model.CopyAbsoluteBoneTransformsTo(transforms); foreach(var mesh in obj.Model.Meshes) { foreach(BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); Game.GraphicsDevice.BlendState = semitransparent ? BlendState.AlphaBlend : BlendState.Opaque; effect.Alpha = semitransparent ? 0.5f : 1.0f; if(obj is Player == false) effect.DiffuseColor = obj.IsHighlighted ? new Vector3(1.0f, 0.0f, 0.0f) : new Vector3(1.0f, 1.0f, 1.0f); effect.World = transforms[mesh.ParentBone.Index]*Matrix.CreateRotationY(angle)*obj.TransformMatrix; effect.View = ViewMatrix; effect.Projection = ProjectionMatrix; } mesh.Draw(); } } 

Why is the whole model white instead of colored? I set colors of the parts by creating a new material and setting its diffuse component to the color I want. All other settings have their default values.

I thought that it's a matter of setting effect.VertexColorEnabled to true, but that gives me an InvalidOperationException saying The current vertex declaration does not include all the elements required by the current vertex shader. Color0 is missing.. Do I need some kind of shader that will understand Blender's vertex properties and will display the model correctly?

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

I would say that your vertex declaration isn't feeding the color to the vertex shader.

I think that the shader you are currently using is expecting a different kind of vertex declaration, which is why it is saying that it expects Color0. Can you post your shader code, or if you're using a standard one, just post the code that initializes all your various 3d effects?

A Vertex Declaration is a way for the program to communicate to the video device what kind of data it will be sending over. It's usually a simple struct with some information about a vertex that's relevant to the rendering method.

On the gpu side, there's a piece of shader code to define how the video card should process the information it receives. This includes a definition of the vertex declaration, which might look something like this in HLSL:

struct VertexInputType { float3 position : POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD0; }; 

The problem is that you need to make sure that both sides of the equation have the same vertex declaration, or you'll run into trouble. Try looking for examples that make use of VertexPositionColor, and see how the effects are configured - Riemers' tutorials might have what you need. If you want to include a normal value, you might need to build your own vertex declaration.

edit - I should note that I've never imported a mesh in xna, so the process might be different to my experience using 3d in xna, which always made use of geometry generated in-program.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your suggestion and sorry for my late feedback. I put my solution in the answer. \$\endgroup\$ Commented May 28, 2014 at 12:47
0
\$\begingroup\$

It turned out that the problem was caused by explicit assigning some color to effect.DiffuseColor. It seems like setting this property also sets something inside the effect object and there's no value that works like a 'transparent' color.

After setting it to Color.White.ToVector3(), all textures are clearly visible, but the vertices without textures (only with colors) are plain white. On the other hand, after setting it to Color.Black.ToVector3(), everything becomes black.

I was setting DiffuseColor of the object to make it look selected/highlighted. As a workaround, a separate effect should do the work.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.