XNA 和 FBX 着色问题

Posted

技术标签:

【中文标题】XNA 和 FBX 着色问题【英文标题】:XNA and FBX Coloring Problem 【发布时间】:2011-11-27 02:02:02 【问题描述】:

我的问题是,如果我使用 BasicEffect(并设置 VertexColorEnabled = true)或我自己的着色器,并且只使用彩色(不是纹理!)模型,它会给出缺少 Color0 的错误......不是吗奇怪的是 .fbx 模型不带有 COLOR0 通道?

【问题讨论】:

如果您接受并赞成之前问题的一些答案,那就太好了。 抱歉,我马上就去做 这是我发现的...... (Marshall Belew @ forums.create.msdn.com/forums/p/16066/553792.aspx#553792) 拯救了我的一天...... 那么,请随意回答您自己的问题。您甚至可以接受自己的答案。 【参考方案1】:

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

解决方案很简单:BasicShader 有一个 DiffuseColor 属性。我只是在卡通着色器中添加了一个新字段,只要没有纹理,我就替换颜色值。

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

来自 .fx 文件:

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

替换效果(这是来自 NonPhotoRealistic 示例的修改片段)。

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

【讨论】:

以上是关于XNA 和 FBX 着色问题的主要内容,如果未能解决你的问题,请参考以下文章

如何使用切线和纹理坐标从3DSMAX导出到FBX

球体在渲染纹理时会显示多边形

XNA 4:导入 FBX 问题

对于多个网格的 FBX 模型,网格未显示在正确位置

Blender导出.fbx模型到Unity贴图丢失

使用 XNA 4.0 从 fbx 模型中提取顶点