2011-09-28 120 views
0

我的問題是,如果我使用BasicEffect(和設置VertexColorEnabled = true)或我自己的着色器,並且只使用有色(無紋理)模型,它會給出顏色0缺失的錯誤.. .fbx模型不帶有COLOR0通道,這不奇怪嗎?XNA和FBX着色問題

+0

如果您接受並提出了以前問題的一些答案,那將會非常好。 – thedaian

+0

哎呀抱歉,我馬上就會這麼做 – JML

+1

這是我發現的...... (Marshall Belew @ http://forums.create.msdn.com/forums/p/16066/553792.aspx# 553792) 保存我的一天... – JML

回答

0

這裏是我發現.....(馬歇爾貝爾柳@ 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; 

更換的效果(這是來自非照片寫實樣品改性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); 
    } 
}