System.InvalidOperationException on mesh.Draw when i enable default lighting

Hello guys,

I am relatively new to monogame, and I am just trying to draw a basic 3d cube and enable lighing.
So drawing the cube works, but if enable Lighting via “effect.EnableDefaultLighting()” i get the following error.
System.InvalidOperationException: “An error occurred while preparing to draw. This is probably because the current vertex declaration does not include all the elements required by the current vertex shader. The current vertex declaration includes these elements: SV_Position0, COLOR0.”

I have read through quite a bit of similar errors, but i couldn’t fix it.
i importet it with the fbx importet and defaulteffect is set to BasicEffect.

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

        foreach (ModelMesh mesh in model.Meshes)
        {

            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.VertexColorEnabled = true;
                effect.EnableDefaultLighting();
                effect.View = viewMatrix;
                effect.World = worldMatrix;
                effect.Projection = projectionMatrix;
                //effect.LightingEnabled = true; // turn on the lighting subsystem.
                effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light
                effect.DirectionalLight0.Direction = new Vector3(1, 0, 0);  // coming along the x-axis
                effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights
                
            }
                mesh.Draw();
        }
        base.Draw(gameTime);
    }

Hi!

The mesh you’re loading should have “color” information, but most probably it doesn’t. You should set VertexColorEnabled to false if this is the case.

hi, unfortunately i still get the same error.
I added the VertexColorEnabled line later, in a attempt to fix the problem.

is the error the same when using VertexColorEnable=false ?

(the important part of the message is " The current vertex declaration includes is these elements: SV_Position0, COLOR0. ")

yes, the error is still: “The current vertex declaration includes these elements: SV_Position0, COLOR0.”

if i comment out the “effect.EnableDefaultLighting();” the model loads normal with color.
now funnily enough, if i uncomment effect.LightingEnabled = true; i get the exact same error.

I don’t know what’s happening but I think I have an faint idea, sorry beforehand if I’m wrong. (it’s been a long time since I used BasicEffect, xna models or model exporting with the pipeline)

That error message always messes my mind, it tells you what your model has, not what the vertex shader requires (I always take that the reverse)

Lightning should require at least Normals to be present, but your model only has “SV_Position, Color0”. Is it possible that you exported/created the model without normals?

2 Likes

You are correct.
I created the model in magicaVoxel, but it doesn’t seem to have normals.
I imported it to blender and exported it again and now it works.
Thanks for the help.

1 Like