public CSGNode(List <CSGPolygon> _polygons, CSGPlane _plane, CSGNode _frontNode, CSGNode _backNode)
 {
     m_polygons  = _polygons;
     m_plane     = _plane;
     m_frontNode = _frontNode;
     m_backNode  = _backNode;
 }
 public CSGPolygon(List <CSGVertex> _vertices, bool _flip = false)
 {
     m_vertices = _vertices;
     if (_flip)
     {
         m_vertices.Reverse();
     }
     m_plane = new CSGPlane(_vertices[0].m_position, _vertices[1].m_position, _vertices[2].m_position);
 }
        // Build a BSP tree out of `polygons`. When called on an existing tree, the
        // new polygons are filtered down to the bottom of the tree and become new
        // nodes there. Each set of polygons is partitioned using the first polygon
        // (no heuristic is used to pick a good split).
        public void Build(List <CSGPolygon> _polygons, int _recursionCount = 0)
        {
            if (_recursionCount > C_MaxRecursions)
            {
                Debug.LogWarning("Hit RecursionCap");
                return;
            }

            if (_polygons.Count < 1)
            {
                return;
            }

            if (m_plane == null || !m_plane.Valid())
            {
                m_plane              = new CSGPlane();
                m_plane.m_normal     = _polygons[0].m_plane.m_normal;
                m_plane.m_planeWidth = _polygons[0].m_plane.m_planeWidth;
            }


            if (m_polygons == null)
            {
                m_polygons = new List <CSGPolygon>();
            }

            List <CSGPolygon> frontPolygons = new List <CSGPolygon>();
            List <CSGPolygon> backPolygons  = new List <CSGPolygon>();

            for (int i = 0; i < _polygons.Count; i++)
            {
                m_plane.SplitPolygon(_polygons[i], m_polygons, m_polygons, frontPolygons, backPolygons);
            }

            if (frontPolygons.Count > 0)
            {
                if (m_frontNode == null)
                {
                    m_frontNode = new CSGNode();
                }

                m_frontNode.Build(frontPolygons, _recursionCount + 1);
            }

            if (backPolygons.Count > 0)
            {
                if (m_backNode == null)
                {
                    m_backNode = new CSGNode();
                }

                m_backNode.Build(backPolygons, _recursionCount + 1);
            }
        }