Esempio n. 1
0
    public void Generate(Voronoi voronoi, Generation world, Zone zone)
    {
        List <LineSegment> roads     = voronoi.SpanningTree(KruskalType.MINIMUM);
        List <Triangle>    triangles = ReturnSiteTriangles.SiteTriangles(voronoi, new Vector2(transform.position.x, transform.position.z), world.worldSize);

        foreach (Triangle triangle in triangles)
        {
            foreach (var road in roads)
            {
                if (InTriangle((Vector2)road.p0, triangle.sites[0].Coord, triangle.sites[1].Coord, triangle.sites[2].Coord))
                {
                    Vector2 a        = (Vector2)road.p0;
                    Vector2 b        = (Vector2)road.p1;
                    Vector3 startPos = new Vector3(a.x, 100f, a.y);
                    Vector3 endPos   = new Vector3(b.x, 100f, b.y);

                    float distance        = Vector3.Distance(startPos, endPos);
                    int   roadSampleCount = Mathf.RoundToInt(distance) * samples;

                    for (int i = 0; i < roadSampleCount; i++)
                    {
                        float   progress = 1f / roadSampleCount * i;
                        Vector3 spawnPos = Vector3.Lerp(startPos, endPos, progress);

                        RaycastHit hit;

                        if (Physics.SphereCast(spawnPos, 1f, Vector3.down, out hit, 200f, layerMask))
                        {
                            // Kill fences and trees
                            if (hit.collider.tag != "Terrain")
                            {
                                hit.transform.GetComponent <Collider> ().enabled = false;
                                Destroy(hit.transform.gameObject);
                            }

                            if (hit.collider.tag == "Terrain")
                            {
                                MeshFilter meshFilter = hit.transform.gameObject.GetComponent <MeshFilter> ();
                                Mesh       mesh       = meshFilter.mesh;
                                Vector3[]  vertices   = mesh.vertices;
                                Color[]    colors     = mesh.colors;

                                for (int j = 0; j < vertices.Length; j++)
                                {
                                    // World positon of the vertex
                                    Vector3 worldPositionVertex = meshFilter.transform.TransformPoint(vertices[j]);
                                    if (Vector3.Distance(worldPositionVertex, hit.point) < pathDrawDistance)
                                    {
                                        colors[j] = zone.roadColour;
                                    }
                                }
                                mesh.colors     = colors;
                                meshFilter.mesh = mesh;
                            }
                        }
                    }
                }
            }
        }
    }
Esempio n. 2
0
    public void Generate(Voronoi voronoi, Generation world, Zone zone)
    {
        if (zone.tintGround)
        {
            List <Triangle> triangles = ReturnSiteTriangles.SiteTriangles(voronoi, new Vector2(transform.position.x, transform.position.z), world.worldSize);

            foreach (var triangle in triangles)
            {
                foreach (MeshFilter meshFilter in world.terrainMeshFilters)
                {
                    Mesh      mesh     = meshFilter.mesh;
                    Vector3[] vertices = mesh.vertices;
                    Color[]   colors   = mesh.colors;

                    for (int i = 0; i < vertices.Length; i++)
                    {
                        // World positon of the vertex
                        Vector3 worldPositionVertex = meshFilter.transform.TransformPoint(vertices[i]);



                        if (InTriangle(new Vector2(worldPositionVertex.x, worldPositionVertex.z), triangle.sites[0].Coord, triangle.sites[1].Coord, triangle.sites[2].Coord))
                        {
                            colors[i] = zone.groundColour;
                        }
                    }
                    mesh.colors     = colors;
                    meshFilter.mesh = mesh;
                }
            }
        }
    }
    public void Generate(Voronoi voronoi, Generation world, Zone zone, PointOfInterest pointOfInterest)
    {
        // We set up data about the region

        List <Vector2> boundaryPoints = voronoi.Region(new Vector2(transform.position.x, transform.position.z));
        Bounds         boundaryBounds = new Bounds();

        foreach (Vector2 point in boundaryPoints)
        {
            boundaryBounds.Encapsulate(new Vector3(point.x, 100f, point.y));
        }

        List <Triangle> triangles = ReturnSiteTriangles.SiteTriangles(voronoi, new Vector2(transform.position.x, transform.position.z), world.worldSize);

        // We pick how much foliage we'll actually have
        int treeSpawnAmmount          = Mathf.RoundToInt((boundaryBounds.size.magnitude * 0.01f) * baseTreeDensity * zone.treeDensity);
        int treeGroundCoverageAmmount = Mathf.RoundToInt((boundaryBounds.size.magnitude * 0.01f) * baseGroundCoverageDensity * zone.groundCoverageDensity);

        for (int i = 0; i < treeSpawnAmmount; i++)
        {
            Triangle   triangle = triangles[Random.Range(0, triangles.Count)];
            Vector2    spawnPos = RandomWithinTriangle(triangle);
            GameObject tree     = Instantiate(zone.trees[Random.Range(0, zone.trees.Length)], new Vector3(spawnPos.x, 100f, spawnPos.y), Quaternion.identity);
            tree.transform.localScale = Vector3.one * Random.Range(zone.treeSizes.x, zone.treeSizes.y);
        }

        for (int i = 0; i < treeGroundCoverageAmmount; i++)
        {
            Triangle   triangle = triangles[Random.Range(0, triangles.Count)];
            Vector2    spawnPos = RandomWithinTriangle(triangle);
            GameObject tree     = Instantiate(zone.groundCoverage[Random.Range(0, zone.groundCoverage.Length)], new Vector3(spawnPos.x, 100f, spawnPos.y), Quaternion.identity);
        }

        StartCoroutine(EndGeneration(voronoi, world, pointOfInterest));
    }