【问题标题】:XNA 4.0 weird color errorXNA 4.0 奇怪的颜色错误
【发布时间】:2013-03-07 10:51:34
【问题描述】:

我在使用 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

【问题讨论】:

    标签: colors xna-4.0 light


    【解决方案1】:

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

    【讨论】:

    • 是的,如果我删除 DirectionalLight0 属性,则地形颜色正确,但我不想要均匀的颜色,而是取决于光线...
    • 尝试使用发射色来影响地形的颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多