2
我有問題用XNA繪製地形,特別是顏色(它發生在VertexPositionColorNormal和VertexPositionTextureNormal)。我的代碼如下:XNA 4.0奇怪的顏色錯誤
public BasicEffect GetEffectForColoredTerrain()
{
this.coloredTerrainEffect.EnableDefaultLighting();
this.coloredTerrainEffect.SpecularPower = 0.01f; //Power of the light
this.coloredTerrainEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f); //Color of the light when it reflects on a surface
this.coloredTerrainEffect.EmissiveColor = new Vector3(1, 0, 0);
this.coloredTerrainEffect.DirectionalLight0.Enabled = true; //Enable directional light
this.coloredTerrainEffect.DirectionalLight0.DiffuseColor = (new Vector3(0.2f, 0.2f, 0.2f)); //Diffuse color
this.coloredTerrainEffect.DirectionalLight0.SpecularColor = (new Vector3(0.2f, 0.2f, 0.2f)); //Specular color
this.coloredTerrainEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1, -1f, 1)); //Direction where the light comes from.
this.coloredTerrainEffect.View = Camera.GetInstance().GetViewMatrix();
this.coloredTerrainEffect.Projection = Camera.GetInstance().GetProjectionMatrix();
this.coloredTerrainEffect.Alpha = (float)((float)Configuration.GetInstance().TerrainOpacity/(float)100);
this.coloredTerrainEffect.VertexColorEnabled = true;
return this.coloredTerrainEffect;
}
而這種代碼繪製地形:
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
WorldContent.CommonGraphicsDevice.RasterizerState = rs;
//Restore things that SpriteBatch can have overriden
WorldContent.CommonGraphicsDevice.BlendState = BlendState.AlphaBlend;
WorldContent.CommonGraphicsDevice.DepthStencilState = DepthStencilState.Default;
WorldContent.CommonGraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
BasicEffect shader = ShadersHandler.GetInstance().GetEffectForColoredTerrain();
foreach (EffectPass pass in shader.CurrentTechnique.Passes)
{
pass.Apply();
WorldContent.CommonGraphicsDevice.Indices = indexBufferVertices;
WorldContent.CommonGraphicsDevice.SetVertexBuffer(vertexBufferVertices);
WorldContent.CommonGraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexPositionColorNormalList.Length, 0, indicesVertices.Length/3);
}
然而,結果是很奇怪的,因爲你可以在下面的圖片看到:
顏色是漸變f rom黃色到白色(黃色向上,白色向下)。不過,如果我使用效果文件從Riemers教程(從3D系列1 effects.fx),一切都是正確的,因爲你可以在這裏看到:
如果你願意,你可以在此處看到效果代碼:Effects file
因此問題:有人知道BasicEffect在這裏發生了什麼嗎?我想使用Riemers文件(一切似乎都正確),但我需要使用透明度,BasicEffect對象爲我提供了alpha屬性,這對於我所需要的是非常完美的。
PD:同樣的問題發生帶紋理的地形,使用VertexPositionNormalTexture
是的,如果我刪除DirectionalLight0屬性然後地形着色正確,但我不想要一個均勻的顏色,但取決於一個燈... – DarthRoman 2013-03-19 15:41:16
嘗試使用Emmisive顏色來影響地形的着色。 – borrillis 2013-05-10 16:27:45