Texture2D:SetData 不起作用
我想要一种带有以编程方式创建的纹理的地板。我已经创建了工作所需的顶点和索引:
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);
}
显示了地板,但纹理全是黑色。怎么了?
感谢您抽出时间。
I'd like to have a sort of floor with a programmaically created texture on it. I already created the vertices and the indexes needed for the work:
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;
Then I created the texture and the data which I'd like to show:
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);
And this is the code I used to draw:
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);
}
The floor is displayed but the texture is all black. What's wrong?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑问题不在于您错误地创建了纹理。问题在于你的照明;您已调用
EnableDefaultLighting()
但未提供任何灯光,因此一切都是完全黑暗的(黑色)。尝试设置 effetti.AmbientLightColor = Color.White 看看是否有帮助。I suspect the problem is not that you've created the texture incorrectly. The problem is your lighting; you've called
EnableDefaultLighting()
but not provided any lights, so everything is completely dark (black). Try settingeffetti.AmbientLightColor = Color.White
and see if that helps.