从 3D 管道立即访问 2D 框的 Unity 函数? [英] Unity function to access the 2D box immediately from the 3D pipeline?

查看:17
本文介绍了从 3D 管道立即访问 2D 框的 Unity 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Unity 中,假设您有一个 3D 对象,

In Unity, say you have a 3D object,

当然,获得 AABB 是微不足道的,Unity 有直接的功能,

Of course, it's trivial to get the AABB, Unity has direct functions for that,

(您可能必须以通常的方式将渲染器的所有边界框加起来",没问题.)

(You might have to "add up all the bounding boxes of the renderers" in the usual way, no issue.)

现在,对于有问题的相机,按照定位,AABB 确实覆盖了某个 2D 边界框......

Now, for the Camera in question, as positioned, that AABB indeed covers a certain 2D bounding box ...

事实上......是否有某种内置的直接方法可以在 Unity 中找到那个橙色的 2D 框??

In fact ... is there some sort of built-in direct way to find that orange 2D box in Unity??

(请注意,要手动完成,您只需为 AABB 的 8 个点制作光线(或使用世界来筛选空间,正如 Draco 提到的,相同);将它们封装在 2D 中以制作橙色框.

(Note that to do it manually you just make rays (or use world to screen space as Draco mentions, same) for the 8 points of the AABB; encapsulate those in 2D to make the orange box.)

我不需要手动解决方案,我在问引擎是否每帧都从管道中以某种方式提供此解决方案?

I don't need a manual solution, I'm asking if the engine gives this somehow from the pipeline every frame?

有电话吗?

(确实,有这个会更好......)

(Indeed, it would be even better to have this ...)

我的感觉是其中一项或全部

My feeling is that one or all of the

  • 特别是遮挡系统
  • 着色器
  • 渲染器

肯定会知道橙色框,甚至可能知道管道内的蓝色框,就在显卡旁边,就像它知道给定网格的 AABB 一样.

would surely know the orange box, and perhaps even the blue box inside the pipeline, right off the graphics card, just as it knows the AABB for a given mesh.

我们知道,Unity 可以让您在给定网格的每一帧中立即点击 AABB 3D 框:事实上,Unity 是否提供了2D 截头体边界"?如图所示?

We know that Unity lets you tap the AABB 3D box instantly every frame for a given mesh: In fact does Unity give the "2D frustrum bound" as shown here?

推荐答案

据我所知,没有内置用于此.

然而,自己找到极端真的很容易.获取网格的边界框(屏幕截图中显示的长方体)就是这样做的,您只是在变换后的空间中进行操作.

As far as I am aware, there is no built in for this.

However, finding the extremes yourself is really pretty easy. Getting the mesh's bounding box (the cuboid shown in the screenshot) is just how this is done, you're just doing it in a transformed space.

  1. 循环遍历网格的所有顶点,执行以下操作:
  2. 变换点从本地空间到世界空间(这处理处理缩放和旋转)
  3. 转换点从世界空间到屏幕空间
  4. 确定新点的 X 和 Y 是否高于/低于存储的最小值/最大值,如果是,则使用新值更新存储的最小值/最大值
  5. 循环遍历所有顶点后,您将获得 4 个值:min-X、min-Y、max-X 和 max-Y.现在您可以构建边界矩形
  1. Loop through all the verticies of the mesh, doing the following:
  2. Transform the point from local to world space (this handles dealing with scale and rotation)
  3. Transform the point from world space to screen space
  4. Determine if the new point's X and Y are above/below the stored min/max values, if so, update the stored min/max with the new value
  5. After looping over all vertices, you'll have 4 values: min-X, min-Y, max-X, and max-Y. Now you can construct your bounding rectangle

您可能还希望先执行一个礼物首先包装模型,并且只处理产生的凸包(因为任何不属于凸包的点都不会超出凸包的边界).如果您打算在模型在屏幕上移动、缩放或旋转时绘制此屏幕空间矩形,并且必须重新计算边界框,那么您需要这样做并缓存结果.

You may also wish to first perform a Gift Wrapping of the model first, and only deal with the resulting convex hull (as no points not part of the convex hull will ever be outside the bounds of the convex hull). If you intend to draw this screen space rectangle while the model moves, scales, or rotates on screen, and have to recompute the bounding box, then you'll want to do this and cache the result.

请注意,如果模型动画(例如,如果您的人形机器人站起来并进行跳跃),这不起作用.求解动画情况要困难得多,因为出于凸包求解的目的,您必须将每个动画的每一帧都视为原始网格的一部分(以确保您的任何动画都不会移动网格的一部分凸包外),将复杂度增加了一个幂.

Note that this does not work if the model animates (e.g. if your humanoid stands up and does jumping jacks). Solving for the animated case is much more difficult, as you would have to treat every frame of every animation as part of the original mesh for the purposes of the convex hull solving (to insure that none of your animations ever move a part of the mesh outside the convex hull), increasing the complexity by a power.

这篇关于从 3D 管道立即访问 2D 框的 Unity 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆