private static bool RaycastInOcTree(Ray ray, double epsilon, OcTreeNode node, ref RayCastHit hit) { bool raycast = false; if (node.bounds.Raycast(ray)) { for (int i = 0; i < node.geometries.Count; i++) { Geometry geometry = node.geometries[i]; if (geometry.RayCast(ray, epsilon, ref hit)) { raycast = true; } } for (int i = 0; i < 8; i++) { var child = node.GetChild(i); if (child != null) { if (RaycastInOcTree(ray, epsilon, child, ref hit)) { raycast = true; } } } } return(raycast); }
public OcTreeNode Insert(Geometry geometry, int depth, int maxDepth) { if (depth < maxDepth) { OcTreeNode node = GetContainerNode(geometry); if (node != null) { return(node.Insert(geometry, depth + 1, maxDepth)); } } m_Geometries.Add(geometry); return(this); }
public override void Build(List <Geometry> geometries) { Bounds bounds = geometries[0].bounds; for (int i = 1; i < geometries.Count; i++) { bounds.Encapsulate(geometries[i].bounds); } bounds.size *= 1.1; m_Node = new OcTreeNode(bounds); for (int i = 0; i < geometries.Count; i++) { m_Node.Insert(geometries[i], 0, 5); } }
private OcTreeNode GetContainerNode(ref OcTreeNode node, Vector3 centerPos, Vector3 size, Geometry geometry) { OcTreeNode result = null; Bounds bd = geometry.bounds; if (node == null) { Bounds bounds = new Bounds(centerPos, size); if (bounds.Contains(bd)) { node = new OcTreeNode(bounds); result = node; } } else if (node.bounds.Contains(bd)) { result = node; } return(result); }
private OcTreeNode GetContainerNode(Geometry geometry) { Vector3 halfSize = bounds.size * 0.5; OcTreeNode result = null; result = GetContainerNode(ref m_ForwardLeftBottom, bounds.center + new Vector3(-halfSize.x * 0.5, halfSize.y * 0.5, halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_ForwardLeftTop, bounds.center + new Vector3(-halfSize.x * 0.5, halfSize.y * 0.5, -halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_ForwardRightBottom, bounds.center + new Vector3(halfSize.x * 0.5, halfSize.y * 0.5, halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_ForwardRightTop, bounds.center + new Vector3(halfSize.x * 0.5, halfSize.y * 0.5, -halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_BackLeftBottom, bounds.center + new Vector3(-halfSize.x * 0.5, -halfSize.y * 0.5, halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_BackLeftTop, bounds.center + new Vector3(-halfSize.x * 0.5, -halfSize.y * 0.5, -halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_BackRightBottom, bounds.center + new Vector3(halfSize.x * 0.5, -halfSize.y * 0.5, halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } result = GetContainerNode(ref m_BackRightTop, bounds.center + new Vector3(halfSize.x * 0.5, -halfSize.y * 0.5, -halfSize.z * 0.5), halfSize, geometry); if (result != null) { return(result); } return(null); }