当前位置:   article > 正文

Unity Bounds包围盒详细教程-图文帮助初学者理解-Chianr_unity bounds 图片

unity bounds 图片

Chinar blog www.chinar.xin

Bounds 包围盒教程


本文提供全流程,中文翻译。

Chinar 的初衷是将一种简单的生活方式带给世人

使有限时间 具备无限可能

Chinar —— 心分享、心创新!

助力快速理解 Unity Bounds 包围盒,及使用场景

为初学者节省宝贵的时间,避免采坑!

Chinar 教程效果:
在这里插入图片描述



全文高清图片,点击即可放大观看 (很多人竟然不知道)


1

Intro —— 简介


Bounds ——包围盒的官方解释我就不做赘述,看得懂,会用的也不会到处找攻略。

不过官方的 API 我们还是要仔细看看的,因为足够准确,且一定是最新的。(我们学习的态度一定要认真)

何况现如今 Unity 以为中国用户提供了中文版,以便于我们学习理解,真的非常不错!

Unity官方API - Bounds 包围盒

举个例子

  1. 理解包围盒是什么;
  2. 学习求出包围盒8个顶点;
  3. 使用包围盒划线;

2

What is Bounds —— 包围盒是什么?


有需要看概念的,直通车 百度百科 —— 包围盒

Chinar总结:

AABB包围盒:Axis aligned bounding box 轴对称包围盒,应用广泛最为常见。

是比较简单的一类包围盒,Bounds是 struct 结构体

优点:构造比较简单,存储空间小,计算速度快,包围盒相交测试简单
缺点:紧密性差,轴对齐的意思是无法将 bounds 进行轴旋转
举个例子

在这里插入图片描述
其他图后边再放。


3

8 Vertex —— 包围盒的8个点


物理的mesh.bounds.extents可以得到该包围盒的范围。

是Bounds 的 size 的一半

通过这一个点(Vector3) 可以理解为向量,我们可以得到8个点。
举个例子
在这里插入图片描述

代码很简单,添加了注释

using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// Chinar包围盒-01
/// 获取包围盒的8个点,实例化预设物圆球帮助理解
/// </summary>
public class ChinarVertex8 : MonoBehaviour
{
    public List<Transform> TargetTransformList; //目标对象 列表。扔进去几个,生成几个


    void Start()
    {
        TargetTransformList.ForEach(GetBound8Vertex);
    }


    /// <summary>
    /// 获取8个顶点
    /// </summary>
    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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

未完待续…


支持

May Be —— 开发者,总有一天要做的事!


拥有自己的服务器,无需再找攻略

Chinar 提供一站式《零》基础教程

使有限时间 具备无限可能!

先点击领取 —— 阿里全产品优惠券 (享受最低优惠)


Chinar 免费服务器、建站教程全攻略!( Chinar Blog )


Chinar

END

本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究

对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: ichinar@icloud.com

对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/390800
推荐阅读
相关标签
  

闽ICP备14008679号