【发布时间】:2011-09-20 21:38:05
【问题描述】:
我刚刚开始使用 XNA,我确信我错过了一些非常简单的东西。我有一个为地面绘制的四边形平面。在这架飞机上,我包裹了一个 2D 纹理。近距离看纹理看起来不错,但是当我四处移动相机时,我看到一堆白色伪影遍布整个地方。当我靠近它们时,它们消失了。我猜这是采样问题或类似问题,但我被卡住了。
首先,我创建了一个 QuadDrawer 类来为我绘制平面。它派生自 DrawableGameComponent。
四抽屉:
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() 方法中,我简单地实例化这个对象并传入我想要使用的游戏对象、大小和纹理名称:
_floor = new QuadDrawer(this, 100.0f, "Textures/checker");
this.Components.Add(_floor);
一旦对象被添加到我的组件集合中,我的 QuadDrawer 的 Draw 方法就会被调用,一切都应该很好。这是我所看到的图像。请注意,这应该是纯灰色,浅色线条以网格图案运行。当您在表面上移动相机时,白色伪影似乎会移动,但当您靠近时会消失。
这是显示白色伪影的图片:
这是一个特写,展示了它的外观:
这是另一张照片,显示它从远处会变得多么糟糕:
从远处看应该是这样的:
图片不是最好的,但你可以看到我在说什么。当相机移动时,它变得非常糟糕。我在其他地方看到过这种纹理,效果很好。知道它可能是什么吗?
如果您需要,我很乐意提供更多信息。
谢谢,
-斯科特
【问题讨论】:
-
尽管工件对您来说可能很明显,但我仍然无法看到您所描述的问题。您是否能够获得更好的示例图像并可能圈出所述工件的实例?谢谢。
-
我添加了另一张显示效果更好的图片。所有的白线都是不应该存在的。在新图像中,您可以看到白色网格线在远离相机时如何开始旋转。我将上传另一张我期望它看起来像的照片。顺便说一句,我应用的任何纹理都会发生这种情况,而不仅仅是这个。谢谢!
-
@Layoric - 我在最底部添加了另一张图片,显示了从远处看场景应该是什么样子。任何帮助都非常感谢......
标签: xna