计算给定原点、方向和矩形的左上角和右下角尺寸

发布于 2024-12-10 18:24:40 字数 987 浏览 0 评论 0原文

我对此进行了搜索,但没有找到与我所追求的问题完全匹配的问题。我希望用户能够定义纹理平面。我拥有的参数是:

  • 大小(A Vector2)
  • 方向(A Vector3)
  • 原点(A Vector3)

因此,我希望能够根据上述信息计算矩形的 4 个顶点。因此,如果我想要一个面朝上、宽度和高度为 1000 的平面:

  • Size = (1000, 1000)
  • Direction = 0, 1, 0 (Up)
  • Origin = 0, 0, 0

所以这将在X& Z轴,朝上。我不明白的是如何根据这些信息计算 3D 空间中的 4 个角。我是否需要额外的信息,或者是否有更好的方法来任意指定飞机?

编辑:当前代码

在以下代码中:

  • Size = 10000, 10000
  • Center = 0, 0, 0
  • Normal = 0, 1, 0

     Vector3 arb = new Vector3(1, 1, 1);
            Vector3 planY = Vector3.Normalize(Vector3.Cross(Normal, arb));
            Vector3planeX = Vector3.Normalize(Vector3.Cross(Normal,planeY));
    
            平面X *= 尺寸.X / 2;
            平面Y *= 尺寸.Y / 2;
    
            Vector3[] ret = 新 Vector3[4] 
            {
                (中心 - X 平面 - Y 平面),
                (中心 - X 平面 + Y 平面),
                (中心 + 平面 X - 平面 Y),
                (中心+X平面+Y平面)
            };
    

I did a search on this, but didn't find a question that quite matched what I was after. I want the user to be able to define a textured plane. The parameters I have are:

  • Size (A Vector2)
  • Direction (A Vector3)
  • Origin (A Vector3)

So, I want to be able to calculate the 4 vertices of the rectangle given the above information. So, if I wanted a plane facing up, with a width and height of 1000:

  • Size = (1000, 1000)
  • Direction = 0, 1, 0 (Up)
  • Origin = 0, 0, 0

So this would define a plane on the X & Z axis, facing upwards. What I do not understand is how to calculate the 4 corners in 3D space given this information. Do I need extra information, or is there a better way to arbitrarily specify a plane?

Edit : Current Code

In the following code:

  • Size = 10000, 10000
  • Center = 0, 0, 0
  • Normal = 0, 1, 0

            Vector3 arb = new Vector3(1, 1, 1);
            Vector3 planeY = Vector3.Normalize(Vector3.Cross(Normal, arb));
            Vector3 planeX = Vector3.Normalize(Vector3.Cross(Normal, planeY));
    
            planeX *= Size.X / 2;
            planeY *= Size.Y / 2;
    
            Vector3[] ret =  new Vector3[4] 
            {
                (Center - planeX - planeY),
                (Center - planeX + planeY),
                (Center + planeX - planeY),
                (Center + planeX + planeY)
            };
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-12-17 18:24:40

您的平面尚未完全定义。您需要另一个沿着平面的向量,即所谓的“切线”向量。在上面的示例中,纹理的 Y 轴应该指向哪里?沿着X轴,沿着Z轴?或者也许是完全不同的用户定义轴?切向量应该指向平面 X 轴的大致方向。

假设我们还有一个切向量,它不一定需要沿着平面指向。您可以按如下方式构造平面:

        Vector3[] vertices(Vector2 size, Vector3 center, Vector3 normal, Vector3 tangent)
    {
        Vector3 planeY = Vector3.Normalize(Vector3.Cross(normal, tangent));
        Vector3 planeX = Vector3.Normalize(Vector3.Cross(normal, planeY));

        planeX *= size.X / 2;
        planeY *= size.Y / 2;

        vertices = new Vector3[]
        {
            (center - planeX - planeY),
            (center - planeX + planeY),
            (center + planeX - planeY),
            (center + planeX + planeY),
        };
        return vertices;
    }

planeX 和planeY 是沿着平面本身的X 轴和Y 轴指向的标准化向量。通过将它们乘以 size / 2,我们得到 2 个在 X 和 Y 方向上从平面中心到边缘的向量。通过以不同的方式将这两个加在一起,我们得到四个角。

这是一个图表,以便您可以在头脑中获得更好的画面。切向量T被“展平”到X轴上。
图表

Your plane isn't fully defined yet. You need another vector going along the plane, the so called 'tangent' vector. In your above example, where should the Y-axis of the texture be pointing? Along the X-axis, along the Z-axis? Or maybe a completely different user defined axis? Your tangent vector is a vector that should point in the general direction where the X-axis of the plane should go.

Let's say we have a tangent vector as well, it doesn't neccesarilly need to point along the plane. You can construct the plane as follows:

        Vector3[] vertices(Vector2 size, Vector3 center, Vector3 normal, Vector3 tangent)
    {
        Vector3 planeY = Vector3.Normalize(Vector3.Cross(normal, tangent));
        Vector3 planeX = Vector3.Normalize(Vector3.Cross(normal, planeY));

        planeX *= size.X / 2;
        planeY *= size.Y / 2;

        vertices = new Vector3[]
        {
            (center - planeX - planeY),
            (center - planeX + planeY),
            (center + planeX - planeY),
            (center + planeX + planeY),
        };
        return vertices;
    }

planeX and planeY are normalized vectors which point along X and Y axes of the plane itself. By multiplying these by size / 2, we get 2 vectors that span from the center to the edge of the plane in both X and Y directions. By adding these two together in different ways, we get the four corners.

Here's a diagram so you can get a better picture in your head. The tangent vector T is "flattened" onto the X-axis.
Diagram

病女 2024-12-17 18:24:40

这作为平面的定义很好:有一个点和法向量。您需要在平面上获取两个向量(A 和 B),并将一个向量(A * 大小值之一)添加到原点以获得第二个角点。将第二个向量(B * 另一个大小值)相加以获得第三个角点,并将两个向量 * 它们相应的大小值与原点相加以获得第四个角点。

要获得第一个向量,请计算法线向量(方向)与任意向量(不等于方向)的叉积。这将为您提供向量 A。要获得向量 B,请计算 A 和方向的叉积。

This is fine as a definition of a plane: you have a point and the normal vector. You need to get two vectors (A & B) on the plane and add one (A * one of the size values) to the origin to get the second corner. Add the second vector (B * the other size value) to get the third corner and add both vectors * their corresponding size values to the origin to get the forth corner.

To get the first vector, calculate the cross product of the normal vector (Direction) with an arbitrary vector (not equal to the direction). This will give you vector A. To get vector B calculate the cross product of A and the Direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文