예제 #1
0
    private static void GenerateFloorPlan(BuildrData data)
    {
        RandomGen rgen = constraints.rgen;

        BuildrPlan      plan   = ScriptableObject.CreateInstance <BuildrPlan>();
        List <Vector2z> bounds = new List <Vector2z>();

        bounds.Add(new Vector2z(rgen.OutputRange(-5, -15), rgen.OutputRange(-5, -15)));
        bounds.Add(new Vector2z(rgen.OutputRange(5, 15), rgen.OutputRange(-5, -15)));
        bounds.Add(new Vector2z(rgen.OutputRange(5, 15), rgen.OutputRange(5, 15)));
        bounds.Add(new Vector2z(rgen.OutputRange(-5, -15), rgen.OutputRange(5, 15)));

        if (rgen.output < 0.25f)//should we split the volume?
        {
            float ratio = rgen.OutputRange(0.25f, 0.75f);
            bounds.Insert(1, Vector2z.Lerp(bounds[0], bounds[1], ratio));
            bounds.Insert(4, Vector2z.Lerp(bounds[3], bounds[4], ratio));

            plan.AddVolume(new [] { bounds[0], bounds[1], bounds[4], bounds[5] });
            plan.AddVolume(1, 2, new [] { bounds[2], bounds[3] });
        }
        else
        {
            plan.AddVolume(bounds.ToArray());
        }

        data.plan = plan;
    }
예제 #2
0
    private static void GenerateFloorPlan(BuildrData data)
    {
        BuildrGenerateConstraints constraints = data.generatorConstraints;
        RandomGen rgen = constraints.rgen;

        BuildrPlan      plan            = ScriptableObject.CreateInstance <BuildrPlan>();
        List <Vector2z> bounds          = new List <Vector2z>();
        Rect            floorplanBounds = new Rect(-15, -15, 30, 30);

        if (constraints.constrainPlanByArea)
        {
            floorplanBounds = constraints.area;
        }
        bounds.Add(new Vector2z(rgen.OutputRange(-5, floorplanBounds.xMin), rgen.OutputRange(-5, floorplanBounds.yMin)));
        bounds.Add(new Vector2z(rgen.OutputRange(5, floorplanBounds.xMax), rgen.OutputRange(-5, floorplanBounds.yMin)));
        bounds.Add(new Vector2z(rgen.OutputRange(5, floorplanBounds.xMax), rgen.OutputRange(5, floorplanBounds.yMax)));
        bounds.Add(new Vector2z(rgen.OutputRange(-5, floorplanBounds.xMin), rgen.OutputRange(5, floorplanBounds.yMax)));

        if (rgen.output < 0.25f)//should we split the volume?
        {
            float ratio = rgen.OutputRange(0.25f, 0.75f);
            bounds.Insert(1, Vector2z.Lerp(bounds[0], bounds[1], ratio));
            bounds.Insert(4, Vector2z.Lerp(bounds[3], bounds[4], ratio));

            plan.AddVolume(new [] { bounds[0], bounds[1], bounds[4], bounds[5] });
            plan.AddVolume(1, 2, new [] { bounds[2], bounds[3] });
        }
        else
        {
            plan.AddVolume(bounds.ToArray());
        }

        data.plan = plan;
    }
예제 #3
0
    public static int[] Triangulate(Vector2z[] points)
    {
        int        numberOfPoints = points.Length;
        List <int> usePoints      = new List <int>();

        for (int p = 0; p < numberOfPoints; p++)
        {
            usePoints.Add(p);
        }
        int        numberOfUsablePoints = usePoints.Count;
        List <int> indices = new List <int>();

        if (numberOfPoints < 3)
        {
            return(indices.ToArray());
        }

        bool freeOfIntersections;
        int  it = 100;

        while (numberOfUsablePoints > 2)
        {
            for (int i = 0; i < numberOfUsablePoints; i++)
            {
                int a, b, c;

                a = usePoints[i];
                b = usePoints[(i + 1) % numberOfUsablePoints];
                c = usePoints[(i + 2) % numberOfUsablePoints];

                Vector2 pA = points[a].vector2;
                Vector2 pB = points[b].vector2;
                Vector2 pC = points[c].vector2;

                float dA = Vector2.Distance(pA, pB);
                float dB = Vector2.Distance(pB, pC);
                float dC = Vector2.Distance(pC, pA);

                float angle = Mathf.Acos((Mathf.Pow(dB, 2) - Mathf.Pow(dA, 2) - Mathf.Pow(dC, 2)) / (2 * dA * dC)) * Mathf.Rad2Deg * Mathf.Sign(Sign(points[a], points[b], points[c]));
                if (angle < 0)
                {
                    continue;                    //angle is not reflex
                }

                freeOfIntersections = true;
                //check that no point is inside the new triangle
                for (int p = 0; p < numberOfUsablePoints; p++)
                {
                    int pu = usePoints[p];
                    if (pu == a || pu == b || pu == c)
                    {
                        continue;
                    }

                    if (IntersectsTriangle2(points[a], points[b], points[c], points[pu]))
                    {
                        freeOfIntersections = false;
                        break;
                    }
                }

                //check that no line midpoint is inside the new triangle
                for (int p = 0; p < numberOfUsablePoints; p++)
                {
                    int pa = usePoints[p];
                    if (pa == a || pa == b || pa == c)
                    {
                        continue;
                    }
                    int      pb  = (p + 1) % numberOfPoints;
                    Vector2z pab = Vector2z.Lerp(points[pa], points[pb], 0.5f);

                    if (IntersectsTriangle2(points[a], points[b], points[c], pab))
                    {
                        freeOfIntersections = false;
                        break;
                    }
                }

                if (freeOfIntersections)
                {
                    indices.Add(a);
                    indices.Add(b);
                    indices.Add(c);
                    usePoints.Remove(b);
                    numberOfUsablePoints = usePoints.Count;
                    i--;
                    it = 100;
                    break;
                }
            }
            it--;
            if (it < 0)
            {
                indices.Reverse();
                return(indices.ToArray());
            }
        }

        indices.Reverse();

        return(indices.ToArray());
    }