Unity3D 8叉樹
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
}
相關文章
- n叉樹vs二叉樹二叉樹
- 滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜尋樹(二叉查詢樹)和最優二叉樹二叉樹
- 二叉樹 & 二叉查詢樹二叉樹
- 排序二叉樹和平衡二叉樹排序二叉樹
- 二叉查詢樹(二叉排序樹)排序
- 二叉樹(順序儲存二叉樹,線索化二叉樹)二叉樹
- 面試8:找二叉樹的下個結點面試二叉樹
- 手擼二叉樹——二叉查詢樹二叉樹
- 手擼二叉樹——AVL平衡二叉樹二叉樹
- 資料結構之樹結構概述(含滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜尋樹、紅黑樹、B-樹、B+樹、B*樹)資料結構二叉樹
- 二叉樹二叉樹
- 資料結構的故事之二叉樹, 字首樹, N叉樹資料結構二叉樹
- 二叉樹的應用(1)--二叉樹排序樹基本操作二叉樹排序
- 判斷二叉樹是否為滿二叉樹二叉樹
- 資料結構中的樹(二叉樹、二叉搜尋樹、AVL樹)資料結構二叉樹
- 自己動手作圖深入理解二叉樹、滿二叉樹及完全二叉樹二叉樹
- 二叉樹、B樹以及B+樹二叉樹
- 平衡二叉樹,B樹,B+樹二叉樹
- 深入學習二叉樹 (一) 二叉樹基礎二叉樹
- 相同二叉樹和鏡面二叉樹問題二叉樹
- 判斷某棵二叉樹是否二叉排序樹二叉樹排序
- 平衡二叉樹(AVL樹)和 二叉排序樹轉化為平衡二叉樹 及C語言實現二叉樹排序C語言
- 樹和二叉樹簡介二叉樹
- Chapter 3 樹與二叉樹APT二叉樹
- 樹(2)--二叉樹的遍歷(非遞迴)+線索二叉樹二叉樹遞迴
- 迭代二叉樹二叉樹
- 二叉樹深度二叉樹
- 重建二叉樹二叉樹
- javascript二叉樹JavaScript二叉樹
- 平衡二叉樹二叉樹
- 二叉樹---深度二叉樹
- Java二叉樹Java二叉樹
- JS二叉樹JS二叉樹
- 二叉排序樹排序
- 二叉樹——高度二叉樹
- 二叉蘋果樹蘋果
- 滿二叉樹二叉樹
- 12、二叉樹二叉樹