gpt4 book ai didi

c# - 使用渲染器(世界空间)(Unity)在游戏对象周围绘制边界矩形(屏幕空间)

转载 作者:行者123 更新时间:2023-12-05 03:46:12 25 4
gpt4 key购买 nike

我有一个游戏对象存在于场景中的世界空间中。我想获取此游戏对象渲染器在屏幕空间中的边界矩形角的坐标,因为我有一些 UI 元素想要围绕此框定位。

上下文:我正在制作一个教程,我正在使用面板使所有内容变暗,但游戏对象除外,该对象将保持不变暗。我可以使用已经存在于屏幕空间中并对其进行矩形变换的按钮轻松地做到这一点,但我无法弄清楚如何围绕世界空间中的游戏对象执行此操作。我们正在使用具有正交投影的相机并且正在使用Unity版本2019.2.17f1。

这是我尝试过的:

public void FocusOnRenderer(Renderer renderer) {

// left, top, right, and bottom are Panels whose pivots are set as follows:
// top: (1, 0)
// right: (0, 0)
// bottom: (0, 1)
// left: (1, 1)
// so when their positions are set to be the corners of the target bounding box, they will fit together nicely.

left.gameObject.SetActive(true);
top.gameObject.SetActive(true);
right.gameObject.SetActive(true);
bottom.gameObject.SetActive(true);

Vector3 center = HandleUtility.WorldToGUIPoint(renderer.bounds.center); // center of bounding box
Vector3 halfSize = HandleUtility.WorldToGUIPoint(renderer.bounds.extents)); // half size of bounding box

Vector3 topRight = center + halfSize;
Vector3 topLeft = center + new Vector3(-halfSize.x, halfSize.y, halfSize.z);
Vector3 bottomRight = center + new Vector3(halfSize.x, -halfSize.y, halfSize.z);
Vector3 bottomLeft = center + new Vector3(-halfSize.x, -halfSize.y, halfSize.z);

left.position = topLeft;
top.position = topRight;
right.position = bottomRight;
bottom.position = bottomLeft;
}

我认为这是错误的,因为我使用渲染器的边界来计算 halfSize 和 center 并没有给我一个边界矩形。我希望有一种简单的内置方法可以做到这一点,但到目前为止我还没有找到任何东西。

感谢您的帮助!

最佳答案

我找到了答案(来自 Unity 论坛上的 @SparrowsNest 的视频建议)!这是从相关时间戳开始的视频:https://youtu.be/2Tgqr1_ajqE?t=1061

步骤:

  1. 从渲染器的边界获取边界框的角
  2. 将这些角转换成屏幕空间
  3. 获取最小和最大 x 和 y 值
  4. 使用那些最小和最大 x 和 y 值设置我的面板的位置

这是我的代码:

    public void FocusOnBounds(Bounds bounds) {

// left, top, right, and bottom are Panels whose pivots are set as follows:
// top: (1, 0)
// right: (0, 0)
// bottom: (0, 1)
// left: (1, 1)
// so when their positions are set to be the corners of the target bounding box, they will fit together nicely.

left.gameObject.SetActive(true);
top.gameObject.SetActive(true);
right.gameObject.SetActive(true);
bottom.gameObject.SetActive(true);

Vector3 c = bounds.center;
Vector3 e = bounds.extents;

Vector3[] worldCorners = new [] {
new Vector3( c.x + e.x, c.y + e.y, c.z + e.z ),
new Vector3( c.x + e.x, c.y + e.y, c.z - e.z ),
new Vector3( c.x + e.x, c.y - e.y, c.z + e.z ),
new Vector3( c.x + e.x, c.y - e.y, c.z - e.z ),
new Vector3( c.x - e.x, c.y + e.y, c.z + e.z ),
new Vector3( c.x - e.x, c.y + e.y, c.z - e.z ),
new Vector3( c.x - e.x, c.y - e.y, c.z + e.z ),
new Vector3( c.x - e.x, c.y - e.y, c.z - e.z ),
};

IEnumerable<Vector3> screenCorners = worldCorners.Select(corner => Camera.main.WorldToScreenPoint(corner));
float maxX = screenCorners.Max(corner => corner.x);
float minX = screenCorners.Min(corner => corner.x);
float maxY = screenCorners.Max(corner => corner.y);
float minY = screenCorners.Min(corner => corner.y);

Vector3 topRight = new Vector3(maxX, maxY, 0);
Vector3 topLeft = new Vector3(minX, maxY, 0);
Vector3 bottomRight = new Vector3(maxX, minY, 0);
Vector3 bottomLeft = new Vector3(minX, minY, 0);

left.position = topLeft;
top.position = topRight;
right.position = bottomRight;
bottom.position = bottomLeft;
}

关于c# - 使用渲染器(世界空间)(Unity)在游戏对象周围绘制边界矩形(屏幕空间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65417634/

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