说我有这个界面:
interface Drawable {
Vector2 DrawPosition { get; }
Texture2D Texture { get; }
float Rotation { get; }
Vector2 Origin { get; }
Vector2 Scale { get; }
bool FlipHorizontally { get; }
}
在扩展Microsoft.Xna.Framework.Game的类中,我重写了Draw(GameTime),这段代码就在那里:
Drawable d = ...;
spriteBatch.Begin();
spriteBatch.Draw(d.Texture, d.DrawPosition, new Rectangle(0, 0, d.Texture.Width, d.Texture.Height), Color.White, d.Rotation, d.Origin, d.Scale, d.FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
spriteBatch.End();
这使用SpriteBatch.Draw(Texture2D纹理,Vector2位置,Nullable sourceRectangle,Color color,float rotation,Vector2 origin,Vector2 scale,SpriteEffects效果,float layerDepth)重载。
假设我有一组顶点,这些顶点构成了d.Texture返回的图像的粗略轮廓(也就是说,如果我在Microsoft Paint中打开图像并从顶点集中的每个点开始,它会非常适合)。如果我想绘制这些点以便它们使用GraphicsDevice.DrawUserPrimitives()遍历纹理,是否有办法仅使用矩阵转换顶点?关键是它只能使用矩阵,我没有其他的绘图选择,因为我实际上也需要将变换后的顶点用于其他事物。我已经尝试了像
这样的东西Matrix.CreateTranslation(new Vector3(-d.Origin, 0))
* Matrix.CreateScale(new Vector3(d.Scale, 0))
* Matrix.CreateRotationZ(d.Rotation)
* Matrix.CreateTranslation(new Vector3(d.DrawPosition, 0)));
但它很难完成。这个问题有解决方案吗?
答案 0 :(得分:2)
您的矩阵代码适用于 World 矩阵(将模型放置在世界空间中)。所以我猜这是其中之一:
您的基元在模型空间中位于错误的位置。 Sprite批处理创建一个多边形,其中(0,0)是精灵的左上角,({texture width},{texture height})是右下角。你的原语需要大小和位置相同。
您的投影矩阵错误。见this answer。请注意,SpriteBatch
使用翻转(客户端空间)坐标系。
您的背面剔除模式错误(不考虑翻转坐标系)。
您遇到了深度缓冲问题(您需要在远处和近处飞机上绘图,并且您是否被任何东西深度剔除?)
如果您仍有问题,请从DirectX SDK获取PIX并使用它来识别您的游戏实际绘制的内容。
答案 1 :(得分:0)
abstract class Drawable
{
public abstract Vector2 DrawPosition { get; }
public abstract Texture2D Texture { get; }
public abstract float Rotation { get; }
public abstract Vector2 Origin { get; }
public abstract Vector2 Scale { get; }
public abstract bool FlipHorizontally { get; }
public abstract Vector2[] Vertices { get; }
public Matrix TransformationMatrix
{
get
{
return Matrix.CreateTranslation(-new Vector3(Texture.Width * Scale.X / 2, 0, 0))
* Matrix.CreateScale(new Vector3(FlipHorizontally ? -1 : 1, 1, 1))
* Matrix.CreateTranslation(new Vector3(Texture.Width * Scale.X / 2, 0, 0))
* Matrix.CreateTranslation(-new Vector3(Origin, 0))
* Matrix.CreateScale(new Vector3(Scale, 0))
* Matrix.CreateRotationZ(Rotation)
* Matrix.CreateTranslation(new Vector3(DrawPosition, 0));
}
}
}
class Camera
{
private readonly Viewport viewport;
public Matrix GetViewMatrix()
{
return Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(0, viewport.Height, 0);
}
public Vector2 MouseToWorld(int x, int y)
{
return Vector2.Transform(new Vector2(x, y), Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(0, viewport.Height, 0));
}
}
class Game1 : Microsoft.Xna.Framework.Game
{
private Drawable avatar;
private Camera camera;
...
protected override void Initialize() {
avatar = ...;
camera = new Camera(graphics.GraphicsDevice.Viewport);
basicEffect = new BasicEffect(graphics.GraphicsDevice);
basicEffect.VertexColorEnabled = true;
basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height, 0, 0, 1);
base.Initialize();
}
...
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.GetViewMatrix());
spriteBatch.Draw(avatar.Texture, avatar.DrawPosition, new Rectangle(0, 0, avatar.Texture.Width, avatar.Texture.Height), Color.White, avatar.Rotation, avatar.Origin, avatar.Scale, avatar.FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
spriteBatch.End();
basicEffect.CurrentTechnique.Passes[0].Apply();
VertexPositionColor[] vertices = new VertexPositionColor[avatar.Vertices.Length + 1];
Matrix m = MakeAffineTransform(avatar);
for (int i = 0; i < avatar.Vertices.Length; i++)
{
vertices[i] = new VertexPositionColor(Vector3.Transform(new Vector3(Vector2.Transform(avatar.Vertices[i], m), 0), camera.GetViewMatrix()), Color.Black);
Console.WriteLine(vertices[i]);
}
vertices[vertices.Length - 1] = vertices[0];
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, vertices.Length - 1);
base.Draw(gameTime);
}
...
}
效果很好!这实际上会翻转原点,使其位于左下角并翻转y轴,以便增加值向上,减少值向下。相机可以是一个很好的基础,可以很容易地更新(例如,如果你想让它跟随屏幕上的某些东西),这样你就可以将世界坐标传递给它(原点在左下角)并且它' ll返回屏幕坐标。