示例#1
0
        private static IMesh GetStructuredDataMesh(Rectangle domain)
        {
            var mesh = GenericMesher.StructuredMesh(domain, SIZE, SIZE);

            mesh.Renumber();

            return(mesh);
        }
示例#2
0
        private void FormTopology_Load(object sender, EventArgs e)
        {
            mesh = (Mesh)GenericMesher.StructuredMesh(new Rectangle(0.0, 0.0, 4.0, 4.0), 4, 4);

            renderControl.Initialize(mesh);

            topoControlView.PrimitiveCommandInvoked += PrimitiveCommandHandler;

            current = default(Otri);
        }
    void GenMap()
    {
        /*
         *  <settings>
         */
        int starsAm = 10000;

        // What percentage of space is taken up by stars.
        int starDensity = 20;

        // Minimal corridor length relative to maximal star size.
        float minCorridorLengthCoeff = 3.0f;

        // What percentage of space is taken up by corridors.
        int corridorDensity = 60;

        float maxz = 3.0f;

        /*
         *  </settings>
         */

        float max_star_size = Mathf.Max(pos_star_sizes);

        Debug.Log("Max star size: " + max_star_size);
        float min_corridor_length         = max_star_size * minCorridorLengthCoeff;
        float min_corridor_length_squared = Mathf.Pow(min_corridor_length, 2);

        int starsx = Mathf.RoundToInt(Mathf.Sqrt(starsAm));
        int starsy = starsx;

        float width  = (starsx * max_star_size) + ((starsx - 1) * min_corridor_length);
        float height = (starsy * max_star_size) + ((starsy - 1) * min_corridor_length);

        background.transform.localScale = new Vector3(width * 0.11f, 1.0f, height * 0.11f);
        background.GetComponent <MeshRenderer>().material.SetTextureScale("_MainTex", new Vector2(width / 70.0f, height / 70.0f));

        Debug.Log("Dimensions: " + width + ", " + height);

        float minx = width * -0.5f;
        float miny = height * -0.5f;

        float maxx = width * 0.5f;
        float maxy = height * 0.5f;

        float max_offset_x = (width / starsx) - ((min_corridor_length - max_star_size) * 0.5f);
        float max_offset_y = (height / starsy) - ((min_corridor_length - max_star_size) * 0.5f);

        Debug.Log("Offsets: " + max_offset_x + ", " + max_offset_y);

        Rectangle bounds = new Rectangle(minx, miny, width, height);

        TriangleNet.Mesh mesh = (TriangleNet.Mesh)GenericMesher.StructuredMesh(bounds, starsx - 1, starsy - 1);

        List <TriangleNet.Geometry.Vertex> stars = new List <TriangleNet.Geometry.Vertex>();

        List <TriangleNet.Geometry.Vertex> shuffled_verts = new List <TriangleNet.Geometry.Vertex>(mesh.Vertices);

        shuffled_verts.Shuffle();

        int stars_amount = 0;

        foreach (TriangleNet.Geometry.Vertex v in shuffled_verts)
        {
            if (stars_amount > 3 && Random.Range(0, 101) > starDensity)
            {
                continue;
            }
            stars.Add(v);
            stars_amount++;
        }
        GenericMesher gm = new GenericMesher(new SweepLine());

        mesh = (TriangleNet.Mesh)gm.Triangulate(stars);

        StandardVoronoi sv = new StandardVoronoi(mesh);

        Polygon final = new Polygon(sv.Vertices.Count);

        Dictionary <int, TriangleNet.Geometry.Vertex> good_stars = new Dictionary <int, TriangleNet.Geometry.Vertex>();
        Dictionary <int, int> bad2goodstar  = new Dictionary <int, int>();
        List <int>            outBoundStars = new List <int>();

        foreach (TriangleNet.Topology.DCEL.Vertex v in sv.Vertices)
        {
            if (v.x < minx || v.x > maxx || v.y < miny || v.y > maxy)
            {
                outBoundStars.Add(v.id);
                continue;
            }

            bool invalid = false;
            foreach (TriangleNet.Geometry.Vertex other in good_stars.Values)
            {
                Vector2 v1 = new Vector2((float)v.x, (float)v.y);
                Vector2 v2 = new Vector2((float)other.x, (float)other.y);

                if ((v2 - v1).sqrMagnitude < min_corridor_length_squared)
                {
                    invalid = true;

                    bad2goodstar[v.id] = other.id;
                }
            }

            if (invalid)
            {
                continue;
            }

            TriangleNet.Geometry.Vertex new_v = new TriangleNet.Geometry.Vertex(v.x, v.y);
            new_v.id         = v.id;
            good_stars[v.id] = new_v;
            final.Add(new_v);
        }

        List <Segment> good_segments = new List <Segment>();

        foreach (Edge e in sv.Edges)
        {
            if (outBoundStars.Contains(e.P0) || outBoundStars.Contains(e.P1))
            {
                continue;
            }

            int P0_id;
            int P1_id;

            if (bad2goodstar.ContainsKey(e.P0))
            {
                P0_id = bad2goodstar[e.P0];
            }
            else
            {
                P0_id = e.P0;
            }

            if (bad2goodstar.ContainsKey(e.P1))
            {
                P1_id = bad2goodstar[e.P1];
            }
            else
            {
                P1_id = e.P1;
            }

            if (P0_id == P1_id)
            {
                continue;
            }

            good_segments.Add(new Segment(good_stars[P0_id], good_stars[P1_id]));
        }

        Dictionary <int, List <int> > connected_stars = new Dictionary <int, List <int> >();

        foreach (Segment s in good_segments)
        {
            if (!connected_stars.ContainsKey(s.P0))
            {
                connected_stars[s.P0] = new List <int>();
            }
            connected_stars[s.P0].Add(s.P1);

            if (!connected_stars.ContainsKey(s.P1))
            {
                connected_stars[s.P1] = new List <int>();
            }
            connected_stars[s.P1].Add(s.P0);
        }

        Debug.Log("Currently edges: " + good_segments.Count);

        List <Segment> temp_segments = new List <Segment>(good_segments);

        temp_segments.Shuffle();

        foreach (Segment s in temp_segments)
        {
            if (Random.Range(0, 101) > corridorDensity && RemovalConnectionsCheck(connected_stars, s.P0, s.P1))
            {
                connected_stars[s.P0].Remove(s.P1);
                connected_stars[s.P1].Remove(s.P0);
                good_segments.Remove(s);
            }
        }

        foreach (Segment s in good_segments)
        {
            final.Add(s);
        }

        Debug.Log("Stars after everything: " + final.Points.Count);
        Debug.Log("Corridors after everything: " + good_segments.Count);
        CreateMap(final, maxz, 0.0f);
    }