Texture2D:SetData不起作用

时间:2011-09-02 21:08:27

标签: c# xna texture2d

我想要一种带有程序化创建纹理的地板。我已经创建了工作所需的顶点和索引:

private VertexPositionNormalTexture[] verticiBase;
private short[] indici;

...

verticiBase = new VertexPositionNormalTexture[4];
verticiBase[0] = new VertexPositionNormalTexture(new Vector3(0.0f, 0.0f, 0.0f), Vector3.Up, new Vector2(0, 0));
verticiBase[1] = new VertexPositionNormalTexture(new Vector3(dimensioneVera.X, 0.0f, 0.0f), Vector3.Up, new Vector2(1, 0));
verticiBase[2] = new VertexPositionNormalTexture(new Vector3(0.0f, 0.0f, dimensioneVera.Y), Vector3.Up, new Vector2(0, 1));
verticiBase[3] = new VertexPositionNormalTexture(new Vector3(dimensioneVera.X, 0.0f, dimensioneVera.Y), Vector3.Up, new Vector2(1, 1));

graphics.GraphicsDevice.VertexDeclaration = new 
    VertexDeclaration(graphics.GraphicsDevice,
    VertexPositionNormalTexture.VertexElements);

indici = new short[6];
indici[0] = 0;
indici[1] = 1;
indici[2] = 2;
indici[3] = 1;
indici[4] = 3;
indici[5] = 2;

然后我创建了纹理和我想要显示的数据:

private Texture2D texture;
private Color[] textureData;

...

texture = new Texture2D(Game.GraphicsDevice, (int)dimensioneVera.X, (int)dimensioneVera.Y);
textureData = new Color[(int)dimensioneVera.X * (int)dimensioneVera.Y];

for (int x = 0; x < textureData.Length; x++)
    textureData[x] = Color.Red;

texture.SetData(textureData);

这是我以前编写的代码:

private BasicEffect effetti;

...

effetti = new BasicEffect(graphics.GraphicsDevice, null);

...

public override void Draw(GameTime gameTime)
{
    effetti.World = Matrix.Identity;
    effetti.View = camera.view;
    effetti.Projection = camera.projection;

    effetti.TextureEnabled = true;
    effetti.Texture = texture;

    effetti.Begin();
    effetti.EnableDefaultLighting();

    effetti.CurrentTechnique.Passes[0].Begin();
    graphics.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList,
        verticiBase, 0, verticiBase.Length, indici, 0, indici.Length / 3);

    effetti.CurrentTechnique.Passes[0].End();

    effetti.End();

    base.Draw(gameTime);
}

显示地板但纹理全黑。怎么了?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

我怀疑问题并不是你错误地创建了纹理。问题是你的照明;你打电话给EnableDefaultLighting()但没有提供任何灯光,所以一切都是完全黑暗(黑色)。尝试设置effetti.AmbientLightColor = Color.White并查看是否有帮助。