Chinar blog :www.chinar.xin
Chinar 的初衷是将一种简单的生活方式带给世人 使有限时间 具备无限可能 |
助力快速理解 Unity Bounds 包围盒,及使用场景 为初学者节省宝贵的时间,避免采坑! |
Bounds ——包围盒的官方解释我就不做赘述,看得懂,会用的也不会到处找攻略。
不过官方的 API 我们还是要仔细看看的,因为足够准确,且一定是最新的。(我们学习的态度一定要认真)
何况现如今 Unity 以为中国用户提供了中文版,以便于我们学习理解,真的非常不错!
Unity官方API - Bounds 包围盒
- 理解包围盒是什么;
- 学习求出包围盒8个顶点;
- 使用包围盒划线;
有需要看概念的,直通车 百度百科 —— 包围盒
Chinar总结:
AABB包围盒
:Axis aligned bounding box 轴对称包围盒,应用广泛最为常见。
是比较简单的一类包围盒,Bounds是 struct 结构体
优点:构造比较简单,存储空间小,计算速度快,包围盒相交测试简单
缺点:紧密性差,轴对齐的意思是无法将 bounds 进行轴旋转
物理的mesh.bounds.extents
可以得到该包围盒的范围。
是Bounds 的 size 的一半
通过这一个点(Vector3
) 可以理解为向量,我们可以得到8个点。
代码很简单,添加了注释
using System.Collections.Generic;
using UnityEngine;
///
/// Chinar包围盒-01
/// 获取包围盒的8个点,实例化预设物圆球帮助理解
///
public class ChinarVertex8 : MonoBehaviour
{
public List<Transform> TargetTransformList; //目标对象 列表。扔进去几个,生成几个
void Start()
{
TargetTransformList.ForEach(GetBound8Vertex);
}
///
/// 获取8个顶点
///
private void GetBound8Vertex(Transform targetTransform)
{
var targetBoundsExtents = targetTransform.GetComponent<MeshFilter>().mesh.bounds.extents; //得到目标物包围盒范围:extents
List<Vector3> vertex8List = new List<Vector3>(); //来一个空数组,准备装数据。
float x = targetBoundsExtents.x; //范围这里是三维向量,分别取得X Y Z
float y = targetBoundsExtents.y;
float z = targetBoundsExtents.z;
/*
* 点出8个点的位置信息,加载数组中备用
* 理解诀窍是:
* 1:保证X为正 yz只有4种可能
* 2:同理X为负 yz 同理
*/
vertex8List.Add(new Vector3(x, y, z));
vertex8List.Add(new Vector3(x, -y, z));
vertex8List.Add(new Vector3(x, y, -z));
vertex8List.Add(new Vector3(x, -y, -z));
vertex8List.Add(new Vector3(-x, y, z));
vertex8List.Add(new Vector3(-x, -y, z));
vertex8List.Add(new Vector3(-x, y, -z));
vertex8List.Add(new Vector3(-x, -y, -z));
//我们已经得到了8个点。
//来实例化 预设物到顶点,来帮助理解。
foreach (var t in vertex8List)
{
print(t);
Instantiate(Resources.Load<Transform>("Chinar-Gauge Point"), t + targetTransform.position, Quaternion.identity);
}
vertex8List.Clear();
}
}
未完待续…
Chinar 提供一站式《零》基础教程 使有限时间 具备无限可能! |
先点击领取 —— 阿里全产品优惠券 (享受最低优惠)
Chinar 免费服务器、建站教程全攻略!( Chinar Blog )
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究
对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: [email protected]
对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址