我刚开始接触XNA,很确定自己错过了一些非常简单的东西。我绘制了一个四边形平面作为地面,并在上面贴了一个2D纹理。这个纹理靠近时看起来很好,但是当我移动相机时,我看到一堆白色伪像遍布整个画面。它们随着我靠近它们而消失。我猜测这是采样问题或类似的问题,但我卡住了。
首先,我创建了一个QuadDrawer类来为我绘制平面。它派生自DrawableGameComponent。
QuadDrawer:
class QuadDrawer : DrawableGameComponent
{
private string _textureName;
private Texture2D _texture;
private BasicEffect _effect;
private float _size = 100;
private VertexPositionNormalTexture[] _vertices;
private int[] _indices;
public QuadDrawer(Game game, float size, string textureName)
: base(game)
{
this._size = size;
this._textureName = textureName;
}
public override void Initialize()
{
BuildVertices();
base.Initialize();
}
private void BuildVertices()
{
_vertices = new VertexPositionNormalTexture[4];
_indices = new int[6];
_vertices[0].Position = Vector3.Forward + Vector3.Left;
_vertices[0].TextureCoordinate = new Vector2(0.0f, 1.0f);
_vertices[1].Position = Vector3.Backward + Vector3.Left;
_vertices[1].TextureCoordinate = new Vector2(0.0f, 0.0f);
_vertices[2].Position = Vector3.Forward + Vector3.Right;
_vertices[2].TextureCoordinate = new Vector2(1.0f, 1.0f);
_vertices[3].Position = Vector3.Backward + Vector3.Right;
_vertices[3].TextureCoordinate = new Vector2(1.0f, 0.0f);
for (int i = 0; i < _vertices.Length; i++)
{
_vertices[i].Normal = Vector3.Up;
_vertices[i].Position *= _size;
_vertices[i].TextureCoordinate *= _size;
}
_indices[5] = 0; _indices[4] = 1; _indices[3] = 2;
_indices[2] = 2; _indices[1] = 1; _indices[0] = 3;
}
protected override void LoadContent()
{
_texture = this.Game.Content.Load<Texture2D>(_textureName);
_effect = new BasicEffect(this.GraphicsDevice);
_effect.EnableDefaultLighting();
_effect.PreferPerPixelLighting = true;
_effect.SpecularColor = new Vector3(0.1f, 0.1f, 0.1f);
_effect.World = Matrix.Identity;
_effect.TextureEnabled = true;
_effect.Texture = _texture;
base.LoadContent();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
public override void Draw(GameTime gameTime)
{
MyGame game = this.Game as MyGame;
GraphicsDevice.SamplerStates[0] = SamplerState.AnisotropicWrap;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
_effect.View = game.Camera.ViewMatrix;
_effect.Projection = game.Camera.ProjectionMatrix;
foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, _vertices, 0, 4, _indices, 0, 2);
}
base.Draw(gameTime);
}
}
在我的主游戏类的Initialize()方法中,我只需实例化这个对象并传入GameObject、Size和我想要使用的Texture名称:
_floor = new QuadDrawer(this, 100.0f, "Textures/checker");
this.Components.Add(_floor);
一旦对象被添加到我的组件集合中,我的QuadDrawer的绘制方法就会被调用,一切都应该很好。这是我所看到的图像。请注意,这应该是纯灰色的,带有网格样式的浅色线条。白色伪影似乎随着你移动摄像机在表面上移动而移动,但当你靠近时就消失了。
下面是展示白色伪影的图片:
![Fuzzy](https://istack.dev59.com/cFLCv.webp)
![Close Up](https://istack.dev59.com/pLl8i.webp)
![Better view of the problem](https://istack.dev59.com/y8X1I.webp)
![enter image description here](https://istack.dev59.com/Gqrpn.webp)
如果需要更多信息,请告诉我。
谢谢,
-Scott