Search code examples
xnagame-developmentautodeskfbx

XNA and FBX Coloring Problem


My problem is that, if I use BasicEffect (and setup VertexColorEnabled = true) or my own shader, and use only colored (not textured!) models, it gives the error that Color0 is missing...Isn't it weird that .fbx models do not come with COLOR0 channel ?


Solution

  • Here is what I found..... (Marshall Belew @ forums.create.msdn.com/forums/p/16066/553792.aspx#553792) Saved my day...

    The solution is simple: the BasicShader has a DiffuseColor property. I merely added a new field into the Toon shader, and any time there was no texture, I substituted the color value.

    I am happier with this solution because now I don't have to write a ton of vertex declaration / draw primitive logic.

    From the .fx file:

    // Pixel shader applies a cartoon shading algorithm. 
    float4 ToonPixelShader(LightingPixelShaderInput input) : COLOR0 
    { 
        float4 color = TextureEnabled ? tex2D(Sampler, input.TextureCoordinate) : DiffuseColor; 
    

    Replacing the effects (this is a modified snipet from the NonPhotoRealistic sample).

    // Scan over all the effects currently on the mesh. 
    foreach (BasicEffect oldEffect in mesh.Effects) 
    { 
       // If we haven't already seen this effect... 
       if (!effectMapping.ContainsKey(oldEffect)) 
       { 
          // Make a clone of our replacement effect. We can't just use 
          // it directly, because the same effect might need to be 
          // applied several times to different parts of the model using 
          // a different texture each time, so we need a fresh copy each 
          // time we want to set a different texture into it. 
          Effect newEffect = replacementEffect.Clone( 
                                      replacementEffect.GraphicsDevice); 
    
          // Copy across the texture from the original effect. 
          newEffect.Parameters["Texture"].SetValue(oldEffect.Texture); 
    
          newEffect.Parameters["TextureEnabled"].SetValue( 
                                              oldEffect.TextureEnabled); 
    
          Vector4 color = new Vector4(oldEffect.DiffuseColor, 1.0f); 
          newEffect.Parameters["DiffuseColor"].SetValue(color); 
    
          effectMapping.Add(oldEffect, newEffect); 
       } 
    }