简体   繁体   English

从相机显示具有特定旋转的完整对象C#

[英]Display complete object from camera with specific rotation in unity C#

I have different kind of objects with variable dimensions and placed at different position in a scene. 我有不同类型的具有可变尺寸的物体,并放置在场景中的不同位置。 I want to focus/display each object with the camera with a hard code rotation (north rotation). 我想用相机进行硬编码旋转(北旋)来聚焦/显示每个对象。 Like with specific camera rotation I want to focus the object that it completely show to the camera and center of the screen. 与特定的相机旋转一样,我想将它完全显示的对象聚焦到相机和屏幕中心。 For this reason, I have write this code snippet that 1 出于这个原因,我已经写了这个代码片段1

  1. Get the position behind the specific focus object using TransformPoint 使用TransformPoint获取特定焦点对象后面的位置

  2. Provide the elevation using the largest extent of the bound so that it display maximum area of the object. 使用最大extentbound提供高程,以便显示对象的最大区域。

  3. Assign the position and the fixed rotation 分配position和固定rotation

      Vector3 dest = destination.transform.TransformPoint(0, 0, behindPositionDistance);// GetBehindPosition(destination.transform, behindPositionDistance, elevation); Debug.Log(dest); float eleveMax = Mathf.Max(destination.GetComponent<MeshRenderer>().bounds.extents.x, destination.GetComponent<MeshRenderer>().bounds.extents.z); dest = new Vector3(dest.x, eleveMax, dest.z); camera.transform.position = dest; camera.transform.rotation = lookNorth; 

But the problem is, it is not accurately working with all objects as every object is at different position and dimension. 但问题是,由于每个物体处于不同的位置和尺寸,因此不能准确地处理所有物体。 I want to focus the full object using the camera but without changing the rotation. 我想使用相机聚焦整个物体但不改变旋转。

Maybe create an empty gameobject as a child of your objects. 也许创建一个空的游戏对象作为你的对象的孩子。 Use that as your Camera position at runtime. 在运行时将其用作相机位置。

If you can use an orthographic camera, you can set the camera's orthographicSize dynamically based on the size of the bounds of the GameObject. 如果您可以使用正交相机,则可以根据GameObject boundssize动态设置相机的orthographicSize This is probably the easiest solution. 这可能是最简单的解决方案。

If you need a perspective camera, you can get the Plane s or corners of the camera frustum via GeometryUtility.CalculateFrustumPlanes(Camera.main) or Camera.CalculateFrustumCorners and use the results from either to manually test that your Bounds is entirely inside. 如果你需要一个透视摄像机,你可以通过GeometryUtility.CalculateFrustumPlanes(Camera.main)Camera.CalculateFrustumCorners获得摄像机PlaneCamera.CalculateFrustumCorners并使用其中任何一个的结果来手动测试你的Bounds是否完全在里面。

With a perspective camera, you could also compute the necessary distance from the object based on the size of the object's bounds and the FOV of the camera. 使用透视摄像头,您还可以根据对象边界的大小和摄像机的FOV计算与对象之间的必要距离。 If I'm not mistaken, this would be something along the lines of distance = 0.5 * size / tan(0.5 * fov) , but the function doesn't have to be precise; 如果我没有弄错的话,这将是distance = 0.5 * size / tan(0.5 * fov) ,但功能不必精确; it just needs to work for your camera. 它只需要为你的相机工作。 Alternatively, you could keep distance constant and compute FOV from the size of the object, although I wouldn't recommend that because frequent FOV changes sound disorienting for the viewer; 或者,您可以保持距离不变并根据物体的大小计算FOV,但我不建议这样做,因为频繁的FOV变化听起来让观众迷失方向; my point is that there are many options. 我的观点是,有很多选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM