Use custom effect

Mar 31, 2010 at 8:31 AM
Edited Mar 31, 2010 at 10:02 AM

I have XNAnimation integrated into my project. It draws and animates the models fine using the following in my Draw() loop

            foreach (ModelMesh modelMesh in skinnedModel.Model.Meshes)
                foreach (ModelMeshPart meshPart in modelMesh.MeshParts)
                    SkinnedModelBasicEffect basicEffect = (SkinnedModelBasicEffect)meshPart.Effect;

                    basicEffect.World = world;//Matrix.CreateRotationY(this.Rotation);// *Matrix.CreateTranslation(5, 0, 0);
                    basicEffect.Bones = animationController.SkinnedBoneTransforms;
                    basicEffect.View = camera.View; //cameraView[activeSkinnedModel];
                    basicEffect.Projection = camera.Projection; //cameraProjection;

                    // OPTIONAL - Configure material
                    basicEffect.Material.DiffuseColor = new Vector3(0.8f);
                    basicEffect.Material.SpecularColor = new Vector3(0.3f);
                    basicEffect.Material.SpecularPower = 8;

                    basicEffect.NormalMapEnabled = false;//enableNormalTexture;

                    basicEffect.SpecularMapEnabled = true;//enableSpecularTexture;
                    //basicEffect.SpecularMap = specularTexture;
                    //if (enableSpecularTexture)
                    basicEffect.Material.SpecularColor = new Vector3(1.0f);

                    // OPTIONAL - Configure lights
                    basicEffect.AmbientLightColor = new Vector3(0.1f);
                    basicEffect.LightEnabled = true;
                    basicEffect.EnabledLights = EnabledLights.One;
                    basicEffect.PointLights[0].Color = Vector3.One;
                    basicEffect.PointLights[0].Position = new Vector3(100, 100, 100);

                    //meshPart.Effect = Shader_Toon.ToonEffect;


This code works good, but what do i have to do if i want to add a custom effect to the model?? Like in the last 3 lines of code i tried to add a Toon shader effect to the model meshpart. This used to work, but now i get the following error when i try to run this code:

Unable to cast object of type 'Microsoft.Xna.Framework.Graphics.Effect' to type 'XNAnimation.Effects.SkinnedModelBasicEffect'.

So obviously i cannot just add other shader effects to the model. I'm sure there's a way to do it, i just don't know how. So could someone please help me out with this??

Thanks in advance.




Mar 31, 2010 at 9:53 AM
Edited Mar 31, 2010 at 10:04 AM

If i just use my shader effect and comment out the SkinnedModelBasicEffect then the game runs, but my model isn't drawn.

Apr 1, 2010 at 9:07 AM

I would like to know this aswell. I added shadercode to the SkinnedModelEffect.fx file and tried to change some Parameters in the same foreach loop dat nofx posted. But since my code is all in the SkinnedModelEffect.fx file i used basicEffect to alter the variables in the shader. But why can't i directly change the values of it like so:

basicEffect.Parameters["World"].SetValue ( world );

Why can't i do it directly like that...???