XNA 4.0奇怪的颜色错误

时间:2013-03-07 10:51:34

标签: colors xna-4.0 light

我在使用XNA绘制地形时遇到问题,特别是使用颜色(使用VertexPositionColorNormal和VertexPositionTextureNormal)。我的代码如下:

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

但是,结果非常奇怪,如下图所示:

Terrain laterally

Terrain bottom

颜色是从黄色到白色的渐变色(黄色向上,白色向下)。但是,如果我使用Riemers教程中的效果文件(3D系列1中的effects.fx),一切都是正确的,如下所示:

Terrain good laterally

Terrain good bottom

如果您愿意,可以在此处查看效果代码:Effects file

所以问题:有人知道BasicEffect会发生什么吗?我想使用Riemers文件(一切看起来都是正确的),但是我需要使用透明度,而BasicEffect对象为我提供了alpha属性,这对我正在寻找的东西来说是完美的。

PD:使用VertexPositionNormalTexture

,纹理地形会出现同样的问题

1 个答案:

答案 0 :(得分:0)

观察图像时,变色似乎是一个非常漂亮的圆圈,就像从方向光一样。我会尝试删除您在BasicEffect示例中使用的DirectionalLight0属性设置,看看是否可以纠正变色。