Unity3D 8叉樹

zsdeus133發表於2018-07-25

這裡寫圖片描述

Octree.cs

using UnityEngine;
using System.Collections.Generic;

// 八叉樹節點類
public struct OctreeNode<T> {
    // 節點包圍盒的尺寸和資訊
    public Bounds m_Bounds;

    // 節點當前深度
    public int m_CurrentDepth;

    // 節點包含物件列表
    public  List<GameObject> m_ObjectList;

    // 節點的子節點
    public OctreeNode<T>[] m_ChildNodes;
}

public class Octree<T>
{
    /// <summary>
    /// 根節點
    /// </summary>
    private OctreeNode<T> m_Root;
    public OctreeNode<T> Root
    {
        get
        {
            return m_Root;
        }

        set
        {
            m_Root = value;
        }
    }

    /// <summary>
    /// 最大深度
    /// </summary>
    private int m_MaxDepth;

    /// <summary>
    /// 建構函式
    /// </summary>
    /// <param name="center">樹中心</param>
    /// <param name="size">樹區域大小</param>
    /// <param name="maxDepth">樹最大深度</param>
    public Octree(Vector3 center, Vector3 size, int maxDepth, List<GameObject> m_ObjectList)
    {
        // 初始化根節點
        this.m_MaxDepth = maxDepth;
        this.m_Root = new OctreeNode<T>();
        this.m_Root.m_Bounds = new Bounds(center, size);
        this.m_Root.m_CurrentDepth = 0;
        this.m_Root.m_ChildNodes = new OctreeNode<T>[8];
        this.m_Root.m_ObjectList = new List<GameObject>();
        this.m_Root.m_ObjectList = m_ObjectList;


        SplitBounds(ref this.m_Root, m_Root.m_ObjectList, this.m_Root.m_CurrentDepth);

    }

    /// <summary>
    /// 分八個區
    /// </summary>
    /// <param name="childNodes">八個子節點</param>
    /// <param name="oneBounds">當前節點的資訊</param>
    private void SplitBounds(ref OctreeNode<T> currentNode, List<GameObject> objectList, int deep)
    {
        if (deep < m_MaxDepth)
        {
            currentNode.m_ObjectList = new List<GameObject>();
            // 1、判斷是否相交 相交就新增到當前節點的列表裡
            foreach (var gameObject in objectList)
            {
                if (gameObject.GetComponent<SphereCollider>() != null)
                {
                    var gameObjectBounds = gameObject.GetComponent<Collider>().bounds;
                    // 如果gameobject與分割槽相交 就新增到當前分割槽的列表裡
                    if (currentNode.m_Bounds.Intersects(gameObjectBounds))
                    {
                        currentNode.m_ObjectList.Add(gameObject);
                    }
                }
            }

            // 2、分八個區域
            currentNode.m_ChildNodes = new OctreeNode<T>[8];
            currentNode.m_CurrentDepth = deep;

            var oneBounds = currentNode.m_Bounds;
            Vector3 half2Vector = oneBounds.size / 4;
            // 八塊空間的位置
            currentNode.m_ChildNodes[0].m_Bounds.center = oneBounds.center - half2Vector;
            currentNode.m_ChildNodes[0].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[1].m_Bounds.center = currentNode.m_ChildNodes[0].m_Bounds.center + new Vector3(oneBounds.size.x / 2, 0, 0);
            currentNode.m_ChildNodes[1].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[2].m_Bounds.center = currentNode.m_ChildNodes[1].m_Bounds.center + new Vector3(0, 0, oneBounds.size.z / 2);
            currentNode.m_ChildNodes[2].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[3].m_Bounds.center = currentNode.m_ChildNodes[0].m_Bounds.center + new Vector3(0, 0, oneBounds.size.z / 2);
            currentNode.m_ChildNodes[3].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[4].m_Bounds.center = currentNode.m_ChildNodes[0].m_Bounds.center + new Vector3(0, oneBounds.size.y / 2, 0);
            currentNode.m_ChildNodes[4].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[5].m_Bounds.center = currentNode.m_ChildNodes[1].m_Bounds.center + new Vector3(0, oneBounds.size.y / 2, 0);
            currentNode.m_ChildNodes[5].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[6].m_Bounds.center = currentNode.m_ChildNodes[2].m_Bounds.center + new Vector3(0, oneBounds.size.y / 2, 0);
            currentNode.m_ChildNodes[6].m_Bounds.size = oneBounds.size / 2;
            currentNode.m_ChildNodes[7].m_Bounds.center = currentNode.m_ChildNodes[3].m_Bounds.center + new Vector3(0, oneBounds.size.y / 2, 0);
            currentNode.m_ChildNodes[7].m_Bounds.size = oneBounds.size / 2;

            // 3、遍歷8個區域 遞迴呼叫
            for (int i = 0; i < currentNode.m_ChildNodes.Length; i++)
            {
                // 遞迴呼叫
                if (currentNode.m_ObjectList.Count != 0)
                {
                    // check if child node intersect
                    bool isIntersects = false;
                    currentNode.m_ChildNodes[i].m_ObjectList = new List<GameObject>();
                    foreach (var gameObject in currentNode.m_ObjectList)
                    {
                        if (gameObject.GetComponent<SphereCollider>() != null)
                        {
                            var gameObjectBounds = gameObject.GetComponent<Collider>().bounds;
                            if (currentNode.m_ChildNodes[i].m_Bounds.Intersects(gameObjectBounds))
                            {
                                currentNode.m_ChildNodes[i].m_ObjectList.Add(gameObject);
                                isIntersects = true;
                            }
                        }
                    }
                    if (isIntersects) {
                        if (currentNode.m_CurrentDepth != 0
                            && currentNode.m_ObjectList.Count == currentNode.m_ChildNodes[i].m_ObjectList.Count)
                        {
                            continue;
                        }
                        currentNode.m_CurrentDepth++;
                        SplitBounds(ref currentNode.m_ChildNodes[i], currentNode.m_ObjectList, currentNode.m_CurrentDepth);
                    }
                }
            }
        }
    }
}

Init.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Init : MonoBehaviour {

    public GameObject root;
    Octree<GameObject> octree;
    // Use this for initialization
    void Start () {
        GameObject[] gameObjects = FindObjectsOfType(typeof(GameObject)) as GameObject[];

        List<GameObject> listGameObject = new List<GameObject>(gameObjects);

        Bounds rootBound = root.GetComponent<BoxCollider>().bounds;
        octree = new Octree<GameObject>(rootBound.center, rootBound.size, 10, listGameObject);
    }

    // Update is called once per frame
    void Update () {

    }

    // 畫八叉樹方框
#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        if (octree != null)
        {
            DrawCube(octree.Root.m_ChildNodes);
        }
    }

    private void DrawCube(OctreeNode<GameObject>[] octreeNode)
    {
        for (int i = 0; i < octreeNode.Length; i++)
        {
            if (octreeNode[i].m_ChildNodes != null)
            {
                DrawCube(octreeNode[i].m_ChildNodes);
            }
            octreeNode[i].m_Bounds.DrawBounds(new Color(0,0.7f,0));
        }
    }

#endif


}

相關文章