gpt4 book ai didi

xna - 如何对旋转的矩形执行剪辑?

转载 作者:行者123 更新时间:2023-12-04 21:25:02 25 4
gpt4 key购买 nike

所以我有这个面板类。它有点像一个窗口,您可以在其中调整大小、关闭、添加按钮、 slider 等。如果您还记得的话,它就像 Morrowind 中的状态屏幕。我想要的行为是,当一个 Sprite 在面板的边界之外时,它不会被绘制,如果它部分在外面,只有里面的部分会被绘制。
所以它现在要做的是首先获取一个代表面板边界的矩形,以及一个用于 Sprite 的矩形,它找到两者之间的相交矩形,然后将该相交转换为 Sprite 矩形的局部坐标并使用它对于源矩形。它的工作原理和我觉得代码一样聪明,我无法摆脱这种感觉,即有更好的方法来做到这一点。此外,通过这种设置,我无法为我的 2D 相机使用全局变换矩阵,“世界”中的所有内容都必须通过相机参数进行绘制。无论如何,这是我的代码:
对于十字路口:

     public static Rectangle? Intersection(Rectangle rectangle1, Rectangle rectangle2)
{
if (rectangle1.Intersects(rectangle2))
{
if (rectangle1.Contains(rectangle2))
{
return rectangle2;
}
else if (rectangle2.Contains(rectangle1))
{
return rectangle1;
}
else
{
int x = Math.Max(rectangle1.Left, rectangle2.Left);
int y = Math.Max(rectangle1.Top, rectangle2.Top);
int height = Math.Min(rectangle1.Bottom, rectangle2.Bottom) - Math.Max(rectangle1.Top, rectangle2.Top);
int width = Math.Min(rectangle1.Right, rectangle2.Right) - Math.Max(rectangle1.Left, rectangle2.Left);
return new Rectangle(x, y, width, height);
}
}
else
{
return null;
}
}

并在面板上实际绘图:
    public void DrawOnPanel(IDraw sprite, SpriteBatch spriteBatch)
{
Rectangle panelRectangle = new Rectangle(
(int)_position.X,
(int)_position.Y,
_width,
_height);
Rectangle drawRectangle = new Rectangle();

drawRectangle.X = (int)sprite.Position.X;
drawRectangle.Y = (int)sprite.Position.Y;
drawRectangle.Width = sprite.Width;
drawRectangle.Height = sprite.Height;

if (panelRectangle.Contains(drawRectangle))
{
sprite.Draw(
spriteBatch,
drawRectangle,
null);
}
else if (Intersection(panelRectangle, drawRectangle) == null)
{
return;
}
else if (Intersection(panelRectangle, drawRectangle).HasValue)
{
Rectangle intersection = Intersection(panelRectangle, drawRectangle).Value;

if (Intersection(panelRectangle, drawRectangle) == drawRectangle)
{
sprite.Draw(spriteBatch, intersection, intersection);
}
else
{
sprite.Draw(
spriteBatch,
intersection,
new Rectangle(
intersection.X - drawRectangle.X,
intersection.Y - drawRectangle.Y,
intersection.Width,
intersection.Height));
}
}
}

所以我想我的问题是,有没有更好的方法来做到这一点?

更新:刚刚发现了 ScissorRectangle 属性。这似乎是一种不错的方法;它需要制作一个 RasterizerState 对象并将其传递给接受它的 spritebatch.Begin 重载。似乎这可能是最好的选择。还有我显然可以改变的视口(viewport)。想法? :)

最佳答案

有几种方法可以将绘图限制在屏幕的一部分。如果该区域是矩形的(这里似乎就是这种情况),您可以将视口(viewport)(参见 GraphicsDevice)设置为面板的表面。

对于非矩形区域,您可以使用模板缓冲区或使用深度缓冲区的一些技巧。在模板缓冲区或深度缓冲区中绘制表面的形状,将渲染状态设置为仅绘制位于模板/深度缓冲区中刚刚渲染的形状中的像素,最后渲染您的 Sprite 。

关于xna - 如何对旋转的矩形执行剪辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4950845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com