XNA和FBX着色问题

时间:2011-09-28 17:35:31

标签: xna game-development autodesk fbx

我的问题是,如果我使用BasicEffect(并设置VertexColorEnabled = true)或我自己的着色器,并且只使用有色(非纹理!)模型,则会出现Color0丢失的错误...不是吗奇怪的是.fbx模型没有配备COLOR0频道?

1 个答案:

答案 0 :(得分:0)

这是我发现的.....(Marshall Belew @ forums.create.msdn.com/forums/p/16066/553792.aspx#553792)救了我的一天......

解决方案很简单:BasicShader具有DiffuseColor属性。我只是在Toon着色器中添加了一个新字段,并且在没有纹理的情况下,我替换了颜色值。

我对这个解决方案更满意,因为现在我不必编写大量的顶点声明/绘制原语逻辑。

来自.fx文件:

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

替换效果(这是NonPhotoRealistic示例中的修改后的snipet)。

// 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); 
   } 
}