Example #1
0
    public bool createSplitMeshes(List <Vector3> outer, List <Vector3> inner)
    {
        Rect rectOfOuter = Misc.GetRectOfVectorList(outer);
        // Vector3 centerOuter = Misc.GetCenterOfVectorList(outer);
        Vector3 centerInner = Misc.GetCenterOfVectorList(inner);

        if (Misc.IsPointInsideRect(centerInner, rectOfOuter))
        {
            // Now try to split this with a line going from this point straight left and right (x axis)
            Vector3 intersectionCheckPoint = centerInner - new Vector3(rectOfOuter.width, 0f, 0f);
            Vector3 intersectionCheckLine  = new Vector3(rectOfOuter.width * 2, 0f, 0f);

            // Loop through outers in pairs and keep them in separate lists
            List <Vector3> beforeSplitOuter        = new List <Vector3>();
            List <Vector3> afterSplitOuter         = new List <Vector3>();
            List <Vector3> outerIntersectionPoints = new List <Vector3>();
            bool           beforeSplit             = true;
            for (int i = 0; i < outer.Count - 1; i++)
            {
                Vector3 vec1 = outer[i];
                Vector3 vec2 = outer[i + 1];
                // Add current vector
                if (beforeSplit)
                {
                    beforeSplitOuter.Add(vec1);
                }
                else
                {
                    afterSplitOuter.Add(vec1);
                }

                Vector3 intersectionPoint;
                bool    intersected = Math3d.LineLineIntersection(out intersectionPoint, vec1, vec2 - vec1, intersectionCheckPoint, intersectionCheckLine);
                if (intersected)
                {
                    // We have crossed the intersection point, add this to both and shift the boolean value, to push coming values to the other list
                    beforeSplitOuter.Add(intersectionPoint);
                    afterSplitOuter.Add(intersectionPoint);
                    // Also keep track of the actual intersection points
                    outerIntersectionPoints.Add(intersectionPoint);
                    beforeSplit = !beforeSplit;
                }
            }

            // Loop through inners in pairs and keep them in separate lists
            List <Vector3> beforeSplitInner        = new List <Vector3>();
            List <Vector3> afterSplitInner         = new List <Vector3>();
            List <Vector3> innerIntersectionPoints = new List <Vector3>();
            beforeSplit = true;
            for (int i = 0; i < inner.Count - 1; i++)
            {
                Vector3 vec1 = inner[i];
                Vector3 vec2 = inner[i + 1];
                // Add current vector
                if (beforeSplit)
                {
                    beforeSplitInner.Add(vec1);
                }
                else
                {
                    afterSplitInner.Add(vec1);
                }

                Vector3 intersectionPoint;
                bool    intersected = Math3d.LineLineIntersection(out intersectionPoint, vec1, vec2 - vec1, intersectionCheckPoint, intersectionCheckLine);
                if (intersected)
                {
                    // We have crossed the intersection point, add this to both and shift the boolean value, to push coming values to the other list
                    beforeSplitInner.Add(intersectionPoint);
                    afterSplitInner.Add(intersectionPoint);
                    // Also keep track of the actual intersection points
                    innerIntersectionPoints.Add(intersectionPoint);
                    beforeSplit = !beforeSplit;
                }
            }

            // TODO - If we have more than two intersection points for either inner or outer, we should try and rotate the intersectionCheckLine and redo above
            // - If eg. outer would happen to be irregular, a third intersection could occur, probably failing everything

            // We now have two lists for outer and two lists for inner, and one list for each to know where the intersections are at
            List <Vector3> topMostOuters    = Misc.GetTopMost(beforeSplitOuter, afterSplitOuter);
            List <Vector3> topMostInners    = Misc.GetTopMost(beforeSplitInner, afterSplitInner);
            List <Vector3> bottomMostOuters = topMostOuters == beforeSplitOuter ? afterSplitOuter : beforeSplitOuter;
            List <Vector3> bottomMostInners = topMostInners == beforeSplitInner ? afterSplitInner : beforeSplitInner;

            // Tie together topmost outer and inner, so they make one solid block
            List <Vector3> topPart = Misc.TieTogetherOuterAndInner(topMostOuters, topMostInners, outerIntersectionPoints, innerIntersectionPoints);
            // Tie together bottommost outer and inner, so they make one solid block
            List <Vector3> bottomPart = Misc.TieTogetherOuterAndInner(bottomMostOuters, bottomMostInners, outerIntersectionPoints, innerIntersectionPoints);

            if (topPart != null && bottomPart != null)
            {
                // Create the mesh
                // DebugFn.print(intersectionCheckPoint);
                // DebugFn.print(intersectionCheckLine);
                // Debug.Log(outerIntersectionPoints.Count);
                // Debug.Log(innerIntersectionPoints.Count);
                // DebugFn.square(topPart[0]);
                // DebugFn.square(bottomPart[0]);
                // DebugFn.print(topPart);
                // DebugFn.print(bottomPart);
                // DebugFn.DebugPath(topPart);
                // DebugFn.DebugPath(bottomPart);

                GameObject   top   = createMesh(topPart, "top", this.transform);
                BuildingRoof topBR = top.AddComponent <BuildingRoof>();
                topBR.createMeshCollider(false);
                topBR.slave  = true;
                topBR.parent = this;
                slaves.Add(topBR);

                GameObject   bottom   = createMesh(bottomPart, "bottom", this.transform);
                BuildingRoof bottomBR = bottom.AddComponent <BuildingRoof>();
                bottomBR.createMeshCollider(false);
                bottomBR.slave  = true;
                bottomBR.parent = this;
                slaves.Add(bottomBR);

                return(true);
            }
        }

        return(false);
    }