[SOLVED] Can't enable lighting on SkinnedEffect

Hi,

I’ve been trying to make a simple 3D game with Monogame 3.5 Windows Desktop (recently upgraded from 3.4), and I’ve been trying to get 3D skinned animations working from .fbx. I’ve sort of gotten bone transforms to work (for some reason Blender has a weird bug when it exports weighted .fbx’s), but the .fbx is still mostly functional when viewed in a .fbx viewer:

Now, overlooking that bug, when I load the skinned model and its armature into Monogame, it looks like this:

I’ve gotten models to load with textures and lighting in the game before (as they were BasicEffects), and you can see a white diamond in the corner for comparison (it’s affected by lighting as it should be), but SkinnedEffect seems to be missing the LightingEnabled option, and even if I do the following:

        effect.World = Matrix.CreateTranslation(Vector3.Zero);
        effect.Projection = MainGame.drawEffect.Projection;

        //effect.LightingEnabled = MainGame.drawEffect.LightingEnabled;

        effect.EnableDefaultLighting();
        effect.DiffuseColor = Vector3.One*10f;
        effect.FogEnabled = false;

        //effect.PreferPerPixelLighting = true;
        
        effect.DirectionalLight0.Enabled = MainGame.drawEffect.DirectionalLight0.Enabled;
        effect.DirectionalLight0.DiffuseColor = MainGame.drawEffect.DirectionalLight0.DiffuseColor;
        effect.DirectionalLight0.Direction = MainGame.drawEffect.DirectionalLight0.Direction;
        effect.DirectionalLight0.SpecularColor = MainGame.drawEffect.DirectionalLight0.SpecularColor;

        effect.DirectionalLight1.Enabled = MainGame.drawEffect.DirectionalLight1.Enabled;
        effect.DirectionalLight1.DiffuseColor = MainGame.drawEffect.DirectionalLight1.DiffuseColor;
        effect.DirectionalLight1.Direction = MainGame.drawEffect.DirectionalLight1.Direction;
        effect.DirectionalLight1.SpecularColor = MainGame.drawEffect.DirectionalLight1.SpecularColor;`

It still appears perfectly black.

Also, on an unrelated note, but how would I go about reading animations from my .fbx and using them with my model? I’ve been having trouble finding documentation on it (except for “Better Skinned 3D Animations”, but when I try to use that script, it’s unable to cast my Model as a ModelExtra, hence why I’m trying to create my own solution).

Thanks in advance for any help,

  • Milun
1 Like

Hi Milun,

could it be that the effect needs a view matrix for the lighting? As far as I can see you did not set one to the effect…

Justin

Sorry, my bad, should have included this as well.

This is called when the object is created:

            model = _model;

            foreach (ModelMesh m in model.Meshes)
            { 
                foreach (Effect effect in m.Effects)
                {
                    // Initiate all effects with default lighting here.
                    SkinnedEffect s = (SkinnedEffect)effect;
                    if (s != null) InitEffect(s); // I ran tests and this IS being called.
                }
            }

And this is called every frame while the previous code is just called once on the models creation:

    public override void Draw()
        {
            CalculateAbsoluteBoneTransforms();

            foreach (ModelMesh e in model.Meshes)
            {
                int parentBoneIndex = e.ParentBone.Index;

                for (int i = 0; i < e.Effects.Count; i++)
                {
                    SkinnedEffect s = (SkinnedEffect)e.Effects[i];

                    if (s == null) {
                       
                        continue;

                    }

                    IEffectMatrices matrices = s as IEffectMatrices;
                    if (matrices != null)
                    {
                        matrices.World = AbsoluteBoneTransforms[parentBoneIndex] * transform;
                        matrices.View = MainGame.camera.View;

                        s.SetBoneTransforms(AbsoluteBoneTransforms);
                    }
                }

                e.Draw();
            }
        }

There is this issue and this one which may be related to your problem

Have you try a known working model ? (coming from a tutorial or a sample).

1 Like

Thanks InfiniteProductions! That first link did the charm!

So I guess all my skinned models NEED to have a texture applied to them for the lighting to work (also, I had made another mistake in my first attempt by accidentally assigning a .psd (Photoshop) document as a texture instead of a .png).

I’ll mark this as solved!