positions | 存储 3D 位置所在位置的数组。 |
transform | 用于更改包围盒计算位置、旋转和大小的矩阵。 |
Bounds 计算轴对齐的包围盒。
根据给定的 positions 数组和变换矩阵计算包围盒。
GeometryUtility.CalculateBounds generates a Bounds bounding box. The positions parameter stores a 3d point array. Each point is inside the generated axis-aligned bounding box. The transform parameter provides a transformation Matrix4x4 that uses a Vector3 position, Quaternion rotation, and Vector3 scale.
以下示例显示了如何使用 CalculateBounds。一个 LightProbeGroup 将 positions 传入 CalculateBounds。示例代码使用八个光照探针创建一个包围盒。默认情况下,光照探针距单位立方体每个角为一个单位。
要运行该示例,请执行以下操作:
创建一个项目并添加一个名为 GameObject
的 GameObject。
添加一个 3D 立方体作为 GameObject
的子项并将其命名为 Cube
。
将一个 LightProbeGroup 组件添加到 Cube
。
将以下脚本添加到 Cube
。
运行 Project
并切回到 Scene
视图。
在 Scene
视图中,您可以看到 LightProbeGroup。当游戏运行时,旋转在 Awake
中变化。八个黄色球体指示 LightProbeGroup 改变位置,Cube 显示为旋转。Scene
模式显示 Cube
在 x、y 和 z 轴上旋转,并显示在本地工具手柄
中。旋转 Cube
会改变八个 LightProbeGroup 光照探针的位置。Scene
模式进行旋转并缩放以显示光照探针。此外,Cube
也旋转且 LightProbeGroup 进行更新。如果重新缩放 /Cube/,LightProbeGroup 将改变大小。
using UnityEngine; using System.Collections;
// GeometryUtility.CalculateBounds - example
public class Example : MonoBehaviour { void Awake() { transform.position = new Vector3(0.0f, 0.0f, 0.0f); transform.Rotate(10.0f, 30.0f, -50.0f, Space.Self);
Debug.Log(transform.localToWorldMatrix); }
void OnDrawGizmosSelected() { LightProbeGroup lightProbeGroup = GetComponent<LightProbeGroup>();
Vector3 center = transform.position; Bounds bounds = GeometryUtility.CalculateBounds(lightProbeGroup.probePositions, transform.localToWorldMatrix); Gizmos.color = new Color(1, 1, 1, 0.25f); Gizmos.DrawCube(center, bounds.size); Gizmos.DrawWireCube(center, bounds.size); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.