/// <summary>
        /// Builds shaded relief.
        /// </summary>
        /// <param name="surfacePoints">A 3D-coordinates defining source</param>
        /// <param name="lightX">An X component of the light vector</param>
        /// <param name="lightY">A Y component of the light vector</param>
        /// <param name="lightZ">A Z component of the light vector</param>
        /// <param name="zFactor">A value at which to multiply z-values for luminosity calculation</param>
        /// <param name="luminosityLevelNumber">A number of resoluted luminosity levels</param>
        /// <returns>An array containing lightened polygons</returns>
        public LightenedPolygon[] BuildShadedRelief(IEnumerable <Coordinate3D> surfacePoints,
                                                    double lightX,
                                                    double lightY,
                                                    double lightZ,
                                                    double zFactor,
                                                    int luminosityLevelNumber)
        {
            List <ICoordinate> coords = new List <ICoordinate>();

            foreach (Coordinate3D c in surfacePoints)
            {
                coords.Add(c);
            }

            VoronoiBuilder     vb          = new VoronoiBuilder();
            VoronoiTesselation tesselation = vb.Build(coords, true);

            return(BuildShadedRelief(tesselation.Triangles, lightX, lightY, lightZ, zFactor, luminosityLevelNumber));
        }
Example #2
0
    public void Generate(int _type, float size, int _numberOfSites, float _mainRoadWidth, float _sideRoadWidth)
    {
        lStreets = new List <GameObject>();
        siteGen  = GetComponent <SiteGenerator>();
        vBuilder = GetComponent <VoronoiBuilder>();
        Ground.transform.localScale = new Vector3(size, 0.01f, size);
        groundRad     = size / 2f;
        numberOfSites = _numberOfSites;
        mainRoadWidth = _mainRoadWidth;
        sideRoadWidth = _sideRoadWidth;
        type          = (GenerationType)_type;


        List <Vector2> sites = new List <Vector2>();

        switch (type)
        {
        case GenerationType.Orgnic:
            sites = siteGen.GenerateRandom(new Vector2(Ground.transform.position.x, Ground.transform.position.z), groundRad, width, height, numberOfSites);
            break;

        case GenerationType.Circular:
            sites = siteGen.GenerateCircular(height, width, groundRad, new Vector2(Ground.transform.position.x, Ground.transform.position.z), 3, numberOfSites);
            break;

        case GenerationType.Grid:
            sites = siteGen.GenerateGrid(new Vector2(Ground.transform.position.x, Ground.transform.position.z), groundRad, 400, 400);
            break;

        default:
            break;
        }



        List <Edge> vEdges = vBuilder.GenerateVoronoi(sites, new Vector2(Ground.transform.position.x, Ground.transform.position.z), groundRad);
        int         i      = 0;

        for (i = 0; i < vEdges.Count; i++)
        {
            // check to see if the curent edge is entirely out of the worldbound
            if (CheckWholeEdge(vEdges[i]))
            {
                continue;
            }
            if (!CheckPositiontoWorldBounds(vEdges[i].start))
            {
                Vector2[] inter = EdgeToCircleIntersection(vEdges[i]);


                float dis1 = Vector2.Distance(vEdges[i].start, inter[0]);
                float dis2 = Vector2.Distance(vEdges[i].start, inter[1]);

                if (dis1 < dis2)
                {
                    vEdges[i].start = inter[0];
                }
                else
                {
                    vEdges[i].start = inter[1];
                }
            }
            if (!CheckPositiontoWorldBounds(vEdges[i].end))
            {
                Vector2[] inter = EdgeToCircleIntersection(vEdges[i]);


                float dis1 = Vector2.Distance(vEdges[i].start, inter[0]);
                float dis2 = Vector2.Distance(vEdges[i].start, inter[1]);

                if (dis1 < dis2)
                {
                    vEdges[i].end = inter[0];
                }
                else
                {
                    vEdges[i].end = inter[1];
                }
            }
            GameObject newMainRoad = Instantiate(mainRoadPF, new Vector3(vEdges[i].start.x, 0.1f, vEdges[i].start.y), Quaternion.identity);
            newMainRoad.GetComponent <MeshBuilderVoronoi>().GenerateMesh(vEdges[i].start, vEdges[i].end, mainRoadWidth);
            newMainRoad.transform.rotation = Quaternion.LookRotation(new Vector3(vEdges[i].ndir.x, 0, vEdges[i].ndir.y));
            newMainRoad.name = vEdges[i].LR;
            newMainRoad.GetComponent <AttachPoints>().CreateAttachPoints(Vector3.Distance(vEdges[i].start, vEdges[i].end), 5);

            Vector2 midPoint = new Vector2((vEdges[i].start.x + vEdges[i].end.x) / 2, (vEdges[i].start.y + vEdges[i].end.y) / 2f);
            Vector2 rotation = Vector2.Perpendicular(vEdges[i].ndir);

            GameObject       t  = Instantiate(turtlePf, new Vector3(midPoint.x, 0.1f, midPoint.y), Quaternion.LookRotation(new Vector3(rotation.x, 0, rotation.y)));
            TurtleController tc = t.GetComponent <TurtleController>();
            tc.streeWidth  = sideRoadWidth;
            tc.startStreet = newMainRoad;
            if (type == GenerationType.Circular)
            {
                float disToMid = Vector2.Distance(midPoint, Ground.transform.position);
                float p        = disToMid * 100 / groundRad;
                int   itt      = 0;
                if (p < 33.3f)
                {
                    itt = 3;
                }
                else if (p < 66.6f)
                {
                    itt = 2;
                }
                else
                {
                    itt = 1;
                }

                tc.Generate(itt);
            }
            else
            {
                tc.Generate(1);
            }
        }
    }
Example #3
0
        private Polyline[] buildIsolinesInternal(IEnumerable<Coordinate3D> surfacePoints, double[] zLevels, List<Triangle> triangles)
        {
            Polyline[] result = new Polyline[zLevels.Length];
            if (zLevels.Length > 0)
            {
                double minZ = zLevels.Min();
                double maxZ = zLevels.Max();
                double surfaceMin = surfacePoints.Min(coord => coord.Z);
                double surfaceMax = surfacePoints.Max(coord => coord.Z);

                if (surfaceMin > maxZ || surfaceMax < minZ)
                    return result;

                List<ICoordinate> coords = new List<ICoordinate>();
                surfacePoints.ToList().ForEach(coord => coords.Add(coord));

                // build tesselation
                VoronoiBuilder vb = new VoronoiBuilder();
                VoronoiTesselation tesselation = vb.Build(coords, true);
                tesselation.Triangles.ToList().ForEach(t => triangles.Add(t));

                vb = null;
                tesselation = null;

                Comparison<Triangle> comparision = delegate(Triangle t1, Triangle t2)
                {
                    if (t1 == t2)
                        return 0;

                    double z11 = ((Coordinate3D)t1.Cell1.DataPoint).Z;
                    double z12 = ((Coordinate3D)t1.Cell2.DataPoint).Z;
                    double z13 = ((Coordinate3D)t1.Cell3.DataPoint).Z;

                    double z21 = ((Coordinate3D)t2.Cell1.DataPoint).Z;
                    double z22 = ((Coordinate3D)t2.Cell2.DataPoint).Z;
                    double z23 = ((Coordinate3D)t2.Cell3.DataPoint).Z;

                    double z1 = Math.Min(Math.Min(z11, z12), z13);
                    double z2 = Math.Min(Math.Min(z21, z22), z23);

                    if (z1 < z2) return -1;
                    if (z1 > z2) return 1;
                    return 0;
                };

                // sort triangles by minimal z-value
                triangles.Sort(comparision);

                for (int i = 0; i < zLevels.Length; i++)
                {
                    if (zLevels[i] < surfaceMin)
                    {
                        result[i] = new Polyline();
                        continue;
                    }

                    result[i] = getIsoline(triangles, zLevels[i]);
                }
            }

            return result;
        }
        private Polyline[] buildIsolinesInternal(IEnumerable <Coordinate3D> surfacePoints, double[] zLevels, List <Triangle> triangles)
        {
            Polyline[] result = new Polyline[zLevels.Length];
            if (zLevels.Length > 0)
            {
                double minZ       = zLevels.Min();
                double maxZ       = zLevels.Max();
                double surfaceMin = surfacePoints.Min(coord => coord.Z);
                double surfaceMax = surfacePoints.Max(coord => coord.Z);

                if (surfaceMin > maxZ || surfaceMax < minZ)
                {
                    return(result);
                }

                List <ICoordinate> coords = new List <ICoordinate>();
                surfacePoints.ToList().ForEach(coord => coords.Add(coord));

                // build tesselation
                VoronoiBuilder     vb          = new VoronoiBuilder();
                VoronoiTesselation tesselation = vb.Build(coords, true);
                tesselation.Triangles.ToList().ForEach(t => triangles.Add(t));

                vb          = null;
                tesselation = null;

                Comparison <Triangle> comparision = delegate(Triangle t1, Triangle t2)
                {
                    if (t1 == t2)
                    {
                        return(0);
                    }

                    double z11 = ((Coordinate3D)t1.Cell1.DataPoint).Z;
                    double z12 = ((Coordinate3D)t1.Cell2.DataPoint).Z;
                    double z13 = ((Coordinate3D)t1.Cell3.DataPoint).Z;

                    double z21 = ((Coordinate3D)t2.Cell1.DataPoint).Z;
                    double z22 = ((Coordinate3D)t2.Cell2.DataPoint).Z;
                    double z23 = ((Coordinate3D)t2.Cell3.DataPoint).Z;

                    double z1 = Math.Min(Math.Min(z11, z12), z13);
                    double z2 = Math.Min(Math.Min(z21, z22), z23);

                    if (z1 < z2)
                    {
                        return(-1);
                    }
                    if (z1 > z2)
                    {
                        return(1);
                    }
                    return(0);
                };

                // sort triangles by minimal z-value
                triangles.Sort(comparision);

                for (int i = 0; i < zLevels.Length; i++)
                {
                    if (zLevels[i] < surfaceMin)
                    {
                        result[i] = new Polyline();
                        continue;
                    }

                    result[i] = getIsoline(triangles, zLevels[i]);
                }
            }

            return(result);
        }
        /// <summary>
        /// Builds shaded relief.
        /// </summary>
        /// <param name="surfacePoints">A 3D-coordinates defining source</param>
        /// <param name="lightX">An X component of the light vector</param>
        /// <param name="lightY">A Y component of the light vector</param>
        /// <param name="lightZ">A Z component of the light vector</param>
        /// <param name="zFactor">A value at which to multiply z-values for luminosity calculation</param>
        /// <param name="luminosityLevelNumber">A number of resoluted luminosity levels</param>
        /// <returns>An array containing lightened polygons</returns>
        public LightenedPolygon[] BuildShadedRelief(IEnumerable<Coordinate3D> surfacePoints,
            double lightX,
            double lightY,
            double lightZ,
            double zFactor,
            int luminosityLevelNumber)
        {
            List<ICoordinate> coords = new List<ICoordinate>();
            foreach(Coordinate3D c in surfacePoints)
                coords.Add(c);

            VoronoiBuilder vb = new VoronoiBuilder();
            VoronoiTesselation tesselation = vb.Build(coords, true);

            return BuildShadedRelief(tesselation.Triangles, lightX, lightY, lightZ, zFactor, luminosityLevelNumber);
        }