Exemplo n.º 1
0
        public void Render(IDoseObject doseObject, Camera camera, IRenderContext context, Rectd screenRect, LineType lineType)
        {
            if (doseObject == null || doseObject.Grid == null)
            {
                return;
            }

            if (doseObject.Grid.GetNormalisationAmount() == 0)
            {
                return;
            }

            //Translates screen to world points and vice versa
            var ms = new MarchingSquares();
            List <PlanarPolygon> polygons = new List <PlanarPolygon>();
            var interpolatedDoseGrid      = new InterpolatedDoseGrid(doseObject, MaxNumberOfGridPoints, camera, screenRect);

            foreach (ContourInfo contourInfo in ContourInfo)
            {
                var contour = ms.GetContour(interpolatedDoseGrid.Data, interpolatedDoseGrid.Rows, interpolatedDoseGrid.Columns, interpolatedDoseGrid.Coords, contourInfo.Threshold, contourInfo.Color);
                //var polygon = contour.ToPlanarPolygon(camera);

                Point2d screenPoint1 = new Point2d();
                Point2d screenPoint2 = new Point2d();
                Point3d worldPoint1  = new Point3d();
                Point3d worldPoint2  = new Point3d();

                var screenVertices = getScreenVertices(contour.Vertices, camera);

                context.DrawLines(screenVertices, contourInfo.Color);
            }
        }
    void MarchingSquaresWithManagedArray()
    {
        for (int x = 0; x < chunkSize.x; x++)
        {
            for (int y = 0; y < chunkSize.y; y++)
            {
                float      density      = -1.0f;
                Vector2Int gridPosition = new Vector2Int(x, y);

                foreach (Circle circle in circles)
                {
                    float distance = Vector2.Distance(circle.transform.position, gridPosition);
                    density += Mathf.Clamp(circle.Radius - distance, 0, float.MaxValue);
                }

                voxels[x, y].Density = density;
            }
        }

        MarchingSquares.GenerateMarchingSquares(voxels, chunkSize, chunkScale, enableInterpolation, enableTriangleIndexing, enableGreedyMeshing, vertices, triangles);

        mesh.Clear();
        mesh.SetVertices(vertices);
        mesh.SetTriangles(triangles, 0);
    }
Exemplo n.º 3
0
        private MeshBuilder Generate(bool[,] map)
        {
            var builder = new FlatMeshBuilder()
            {
                Options = EMeshBuildingOptions.NONE
            };

            var width  = map.GetWidth();
            var height = map.GetHeight();

            var wallOffset = new Vector3(0.0f, 0.0f, wallHeight);

            var vertices = MarchingSquares.Vertices();

            for (int y = 0; y < height - 1; y++)
            {
                for (int x = 0; x < width - 1; x++)
                {
                    var active0 = !map[x, y];
                    var active1 = !map[x, y + 1];
                    var active2 = !map[x + 1, y + 1];
                    var active3 = !map[x + 1, y];

                    var configuration = MarchingSquares.GetConfiguration(active0, active1, active2, active3);
                    var triangles     = MarchingSquares.Triangles[configuration];

                    var offset = new Vector3(x, y) - wallOffset;

                    int i = 0;
                    for (; i < triangles.Length && triangles[i] != -1; i += 3)
                    {
                        var t0 = triangles[i + 0];
                        var t1 = triangles[i + 1];
                        var t2 = triangles[i + 2];

                        var v0 = (Vector3)vertices[t0] + offset;
                        var v1 = (Vector3)vertices[t1] + offset;
                        var v2 = (Vector3)vertices[t2] + offset;

                        builder.AddTriangle(v0, v1, v2);
                    }

                    if (configuration > 0 && configuration < MarchingSquares.Triangles.Length - 1)
                    {
                        var wt0 = triangles[i - 1];
                        var wt1 = triangles[i - 2];

                        var wv0 = (Vector3)vertices[wt0] + offset;
                        var wv1 = (Vector3)vertices[wt1] + offset;
                        var wv2 = wv1 + wallOffset;
                        var wv3 = wv0 + wallOffset;

                        builder.AddTriangle(wv0, wv1, wv2);
                        builder.AddTriangle(wv0, wv2, wv3);
                    }
                }
            }

            return(builder);
        }
Exemplo n.º 4
0
    void GenerateMap()
    {
        FillMap();
        CellularAutomata(SmoothIterations);

        ProcessMap();

        int[,] borderedMap = new int[Width + BorderSize * 2, Height + BorderSize * 2];

        for (int x = 0; x < borderedMap.GetLength(0); x++)
        {
            for (int y = 0; y < borderedMap.GetLength(1); y++)
            {
                if (x >= BorderSize && x < Width + BorderSize && y >= BorderSize && y < Height + BorderSize)
                {
                    borderedMap[x, y] = _map[x - BorderSize, y - BorderSize];
                }
                else
                {
                    borderedMap[x, y] = 1;
                }
            }
        }

        MarchingSquares meshGenerator = GetComponent <MarchingSquares>();

        meshGenerator.GenerateMesh(borderedMap, 1);
    }
    // Update is called once per frame
    void Update()
    {
        MarchingSquares ms = new MarchingSquares(20, 20, 1.0f, 1.0f, -10.0f, -10.0f, func);

        mesh_.vertices = ms.vertices;
        mesh_.SetIndices(ms.indices, MeshTopology.Triangles, 0);
        mesh_filter_.mesh = mesh_;
    }
	public void Awake() {
		if (instance != null && instance != this) {
			Destroy(this.gameObject);
			return;
		} else {
			instance = this;
		}
		DontDestroyOnLoad(this.gameObject);
	}
Exemplo n.º 7
0
    void StartMarchingSquares()
    {
        MarchingSquares marchingSquares     = GetComponent <MarchingSquares>();
        MarchingSquares lavaMarchingSquares = GameObject.FindGameObjectWithTag("Lava").GetComponent <MarchingSquares>();


        marchingSquares.GenerateMesh(grid, 1);
        lavaMarchingSquares.GenerateMesh(grid, 1);
    }
Exemplo n.º 8
0
        protected override void DrawTool(Ray ray)
        {
            base.DrawTool(ray);
            var casts   = Physics.RaycastAll(ray, Mathf.Infinity, ~(GetParameter <Ignore>().layer));
            var closest = Mathf.Infinity;

            for (int k = 0; k < casts.Length; k++)
            {
                var cast = casts[k];
                if (cast.distance < closest)
                {
                    closest    = cast.distance;
                    raycastHit = cast;
                }
            }
            if (GetParameter <Shape>().Texture != null)
            {
                if (shape == null || previousTexture != GetParameter <Shape>().Texture)
                {
                    var t = GetParameter <Shape>().Texture;
                    previousTexture = t;
                    var textureMap = new int[t.width, t.height];

                    int invert = GetParameter <Shape>().Invert ? 0 : 1;

                    var pixels = t.GetPixels();

                    for (int i = 0; i < textureMap.GetLength(0); i++)
                    {
                        for (int j = 0; j < textureMap.GetLength(1); j++)
                        {
                            textureMap[i, j] = pixels[i + j * t.width].r >= .6f ? invert : 1 - invert;
                        }
                    }
                    MarchingSquares ms = new MarchingSquares();
                    shape     = ms.GenerateMesh(textureMap, 0.01f * GetParameter <Radius>().value);
                    shapeSide = ms.CreateMeshOutline();
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                }
                else
                {
                    DrawShape();
                }
            }
            else
            {
                Handles.color = toolColor;
                Handles.DrawSolidDisc(raycastHit.point, raycastHit.normal, GetParameter <Radius>().value);
                Handles.color = Color.white;
                Handles.DrawWireDisc(raycastHit.point, raycastHit.normal, GetParameter <Radius>().value);
                Handles.color = Color.yellow;
                Handles.DrawLine(raycastHit.point, raycastHit.point + raycastHit.normal * GetParameter <Gap>().value);
                Handles.color = Color.white;
            }
        }
Exemplo n.º 9
0
    public void UpdateMesh()
    {
        MarchingSquares.GenerateMarchingSquares(voxels, chunkSize, chunkScale, enableInterpolation, enableTriangleIndexing, enableGreedyMeshing, verticies, triangles);

        mesh.Clear();
        mesh.SetVertices(verticies);
        mesh.SetTriangles(triangles, 0);

        dirty = false;
    }
 void FixedUpdate()
 {
     if (pathMap != null && pathAnimate)
     {
         if (pathRadius < pathMap.GetLength(0) / 2 - 1)
         {
             pMeshF.mesh       = MarchingSquares.Generate(pathMap, MapCenter, pathRadius);
             pMeshC.sharedMesh = pMeshF.mesh;
             pathRadius       += 15 * Time.deltaTime;
         }
     }
 }
Exemplo n.º 11
0
 public void Awake()
 {
     if (instance != null && instance != this)
     {
         Destroy(this.gameObject);
         return;
     }
     else
     {
         instance = this;
     }
     DontDestroyOnLoad(this.gameObject);
 }
    public void GenerateMap()
    {
        map = new int[mapSize, mapSize];

        RandomFillMap();

        //Smooth the map to create a cave shape
        for (int i = 0; i < numberOfSmooths; i++)
        {
            SmoothMap();
        }

        //Generate the mesh with marching squares
        grid = MarchingSquares.GenerateMesh(map, 1f);
    }
Exemplo n.º 13
0
    private void PerformTrace()
    {
        var spriteRenderer = GetComponent <SpriteRenderer> ();
        var sprite         = spriteRenderer.sprite;

        var ms = new MarchingSquares(
            sprite.texture,
            alphaThreshold: AlphaThreshold,
            clockWise: ClockWise,
            mipLevel: MipLevel
            );

        polyPath = ms.TraceContours();
        float pixelsPerUnit = RasterHelper.GetPixelsPerUnit(sprite);
        float scale         = (1 << MipLevel) / pixelsPerUnit;
        var   pivot         = RasterHelper.GetPivot(sprite);
        var   offset        = -Vector2.Scale(sprite.bounds.size, pivot);

        polyPath.Scale(scale);
        polyPath.Translate(offset);

        int   pointCountBeforeOpt = polyPath.TotalPointCount;
        float worldScale          = Common.WorldScale(spriteRenderer.transform);

        if (ReduceByMinDistance)
        {
            polyPath.ReduceByMinDistance(MinDistance, MinVertexCount);
        }

        if (ReduceByMinTriangleArea)
        {
            float globalScaleSq = worldScale * worldScale;

            polyPath.ReduceByMinTriangleArea(MinTriangleArea / globalScaleSq, MinVertexCount);
        }

        if (ReduceCodirected)
        {
            polyPath.ReduceCodirected(MinAngle * Mathf.Deg2Rad, MinVertexCount);
        }

        int pointCountAfterOpt = polyPath.TotalPointCount;

        print(string.Format(
                  "Reduction: {0}/{1} ({2:P})",
                  pointCountAfterOpt, pointCountBeforeOpt, 1.0f - ( float )pointCountAfterOpt / pointCountBeforeOpt
                  ));
    }
    public void GenerateMap()
    {
        if (squareSize <= 0f)
        {
            Debug.LogError("Square size has to be greate than 0");

            return;
        }

        int squares = Mathf.FloorToInt(mapSize / squareSize);

        map = new float[squares, squares];

        FillMap();

        //Generate the mesh with marching squares algorithm
        grid = MarchingSquares.GenerateMesh(map, squareSize, shouldSmooth: true);
    }
Exemplo n.º 15
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        MarchingSquares March = (MarchingSquares)target;

        if (!March.AutoUpdate)
        {
            if (GUILayout.Button("Generate Mesh"))
            {
                March.GenerateMesh();
            }
            if (GUILayout.Button("Clear Mesh"))
            {
                March.CompleteClear();
            }
        }
    }
Exemplo n.º 16
0
    public void Initialize(Vector2 offset, byte squareCount, float pointScale, Material material)
    {
        polyCollider.offset = offset;

        if (marching != null)
        {
            Debug.LogError("PointCloud.Initialize() : Method has already been called!");
        }

        pointSize = pointScale;
        Size      = squareCount;
        marching  = new MarchingSquares(this, pointScale);

        cloud = new byte[Size + 1][];
        for (int i = 0; i <= Size; i++)
        {
            cloud[i] = new byte[Size + 1];
        }

        GetComponent <MeshRenderer>().material = material;
    }
Exemplo n.º 17
0
    private PolygonPath BuildPolyPath(SpriteRenderer spriteRenderer)
    {
        var sprite = spriteRenderer.sprite;

        float buildPathStartTime = Time.realtimeSinceStartup;
        float msStartTime        = Time.realtimeSinceStartup;
        var   ms = new MarchingSquares(
            sprite.texture,
            alphaThreshold: AlphaThreshold,
            clockWise: ClockWise,
            mipLevel: MipLevel
            );
        float msTimeElapsed = Time.realtimeSinceStartup - msStartTime;

        float traceStartTime   = Time.realtimeSinceStartup;
        var   polyPath         = ms.TraceContours();
        float traceTimeElapsed = Time.realtimeSinceStartup - traceStartTime;

        float pixelsPerUnit = RasterHelper.GetPixelsPerUnit(sprite);
        float scale         = (1 << MipLevel) / pixelsPerUnit;
        var   pivot         = RasterHelper.GetPivot(sprite);
        var   offset        = -Vector2.Scale(sprite.bounds.size, pivot);

        float transformStartTime = Time.realtimeSinceStartup;

        polyPath.Scale(scale);
        polyPath.Translate(offset);
        float transformTimeElapsed = Time.realtimeSinceStartup - transformStartTime;

#if MUTABLE_COLLIDER_STATS
        float buildPathTimeElapsed = Time.realtimeSinceStartup - buildPathStartTime;

        print(string.Format(
                  "Build path timing -- Trace: {0}, Transform: {1}, Get pixels: {2}, Total: {3}",
                  traceTimeElapsed, transformTimeElapsed, msTimeElapsed, buildPathTimeElapsed
                  ));
#endif

        return(polyPath);
    }
Exemplo n.º 18
0
        private void UpdatePolys()
        {
            World.Clear();
            _sw.Start();

            _aabb = new AABB {
                LowerBound = new Vector2(0, 0), UpperBound = new Vector2(_terrainTex.Width, _terrainTex.Height),
            };
            _polys = MarchingSquares.DetectSquares(_aabb, _gridSize, _gridSize, Eval, _level, _combine);

            _sw.Stop();
            _time = _sw.Elapsed.TotalMilliseconds;
            _sw.Reset();

            for (int i = 0; i < _polys.Count; i++)
            {
                Vertices poly = _polys[i];
                poly.Translate(ref _translate);

                poly.Scale(ref _scale);
                poly.ForceCounterClockWise();

                if (!poly.IsConvex())
                {
                    List <Vertices> verts = EarclipDecomposer.ConvexPartition(poly);

                    for (int j = 0; j < verts.Count; j++)
                    {
                        Vertices v = verts[j];
                        FixtureFactory.CreatePolygon(World, v, 1);
                    }
                }
                else
                {
                    FixtureFactory.CreatePolygon(World, poly, 1);
                }
            }
        }
    unsafe void MarchingSquaresWithJob()
    {
        int arraySize = gridSize.x * gridSize.y;

        NativeArray <Vector3> nativePositions = new NativeArray <Vector3>(circles.Length, Allocator.TempJob);
        NativeArray <float>   nativeRadiuses  = new NativeArray <float>(circles.Length, Allocator.TempJob);
        NativeArray <Voxel>   nativeVoxels    = new NativeArray <Voxel>(arraySize, Allocator.TempJob);

        for (int i = 0; i < circles.Length; i++)
        {
            UnsafeUtility.WriteArrayElement(nativeRadiuses.GetUnsafePtr(), i, circles[i].Radius);
            UnsafeUtility.WriteArrayElement(nativePositions.GetUnsafePtr(), i, circles[i].transform.position);
        }

        MetaballDensityJob job = new MetaballDensityJob
        {
            voxels     = nativeVoxels,
            gridSize   = gridSize,
            positions  = nativePositions,
            radiuses   = nativeRadiuses,
            numCircles = circles.Length
        };

        JobHandle handle = job.Schedule(arraySize, 32);

        handle.Complete();

        MarchingSquares.GenerateMarchingSquaresWithJob(nativeVoxels, chunkSize, chunkScale, enableInterpolation, enableTriangleIndexing, enableGreedyMeshing, vertices, triangles);

        mesh.Clear();
        mesh.SetVertices(vertices);
        mesh.SetTriangles(triangles, 0);

        nativePositions.Dispose();
        nativeRadiuses.Dispose();
        nativeVoxels.Dispose();
    }
Exemplo n.º 20
0
    //creates the mesh from the voxel data and assigns it to the mesh filter and mesh collider
    public override void Render()
    {
        //if(Time.time<10)
        //{
        MeshBuilder mb = MarchingCubes.CreateMesh(voxVals, voxSubs);

        //build all the skirts using marching squares with the edge values
        if (scale != 1)
        {
            for (int i = 0; i < 6; i++)
            {
                float[,] slice = new float[chunkSize + 1, chunkSize + 1];
                Sub[,] subs    = new Sub[chunkSize, chunkSize];
                for (int x = 0; x < chunkSize + 1; x++)
                {
                    for (int y = 0; y < chunkSize + 1; y++)
                    {
                        switch (i)
                        {
                        case 0:
                            slice[x, y] = voxVals[x, y, 0];

                            if (x < chunkSize && y < chunkSize)
                            {
                                subs[x, y] = voxSubs[x, y, 0];
                            }
                            break;

                        case 1:
                            slice[x, y] = voxVals[chunkSize - x, y, chunkSize];
                            if (x < chunkSize && y < chunkSize)
                            {
                                subs[x, y] = voxSubs[chunkSize - 1 - x, y, chunkSize - 1];
                            }
                            break;

                        case 2:
                            slice[x, y] = voxVals[0, y, chunkSize - x];
                            if (x < chunkSize && y < chunkSize)
                            {
                                subs[x, y] = voxSubs[0, y, chunkSize - 1 - x];
                            }
                            break;

                        case 3:
                            slice[x, y] = voxVals[chunkSize, y, x];
                            if (x < chunkSize && y < chunkSize)
                            {
                                subs[x, y] = voxSubs[chunkSize - 1, y, x];
                            }

                            break;

                        case 4:
                            slice[x, y] = voxVals[x, chunkSize, y];
                            if (x < chunkSize && y < chunkSize)
                            {
                                subs[x, y] = voxSubs[x, chunkSize - 1, y];
                            }
                            break;

                        case 5:
                            slice[x, y] = voxVals[x, 0, chunkSize - y];
                            if (x < chunkSize && y < chunkSize)
                            {
                                subs[x, y] = voxSubs[x, 0, chunkSize - 1 - y];
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }

                switch (i)
                {
                case 0:
                    mb.addMesh(MarchingSquares.buildMesh(slice, subs), Vector3.zero, Quaternion.identity);
                    break;

                case 1:
                    mb.addMesh(MarchingSquares.buildMesh(slice, subs), new Vector3(chunkSize, 0, chunkSize), Quaternion.Euler(0, 180, 0));
                    break;

                case 2:
                    mb.addMesh(MarchingSquares.buildMesh(slice, subs), new Vector3(0, 0, chunkSize), Quaternion.Euler(0, 90, 0));
                    break;

                case 3:
                    mb.addMesh(MarchingSquares.buildMesh(slice, subs), new Vector3(chunkSize, 0, 0), Quaternion.Euler(0, -90, 0));
                    break;

                case 4:
                    mb.addMesh(MarchingSquares.buildMesh(slice, subs), new Vector3(0, chunkSize, 0), Quaternion.Euler(90, 0, 0));
                    break;

                case 5:
                    mb.addMesh(MarchingSquares.buildMesh(slice, subs), new Vector3(0, 0, chunkSize), Quaternion.Euler(-90, 0, 0));
                    break;

                default:
                    break;
                }
            }
        }



        Mesh mesh = mb.getMesh();

        //Mesh mesh = MarchingCubes.CreateMesh(voxVals, voxType);
        //Mesh mesh = MarchingCubes2.CreateMesh(voxVals);
        //mesh.RecalculateNormals();//not sure what this does at the moment
        filter.mesh = mesh;

        //only add colliders for level 0 terrain objects (scale 1)
        if (scale == 1)
        {
            //coll = gameObject.AddComponent<MeshCollider>();
            coll.sharedMesh = mesh;
        }
        //transform.position.localScale = new Vector3(scale, scale, scale);
        //}
    }
Exemplo n.º 21
0
 public void SetupMarchingSquaresTest()
 {
     marchingSquares = new MarchingSquares();
 }
    void CreatePath()
    {
        lowresPathMap = new bool[(int)VillageSize.x, (int)VillageSize.y];
        //creates a larger map of the village
        pathMap = new bool[(int)VillageSize.x * 3, (int)VillageSize.y * 3];

        for (int x = 0; x < VillageSize.x; x++)
        {
            for (int y = 0; y < VillageSize.y; y++)
            {
                //if the tile is clear
                if (!buildingMap[x, y])
                {
                    //check the 8 sides of the tile, to see if its next to a wall
                    if (x - 1 >= 0)
                    {
                        if (buildingMap[x - 1, y])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }
                    if (y - 1 >= 0)
                    {
                        if (buildingMap[x, y - 1])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }
                    if (x + 1 < VillageSize.x)
                    {
                        if (buildingMap[x + 1, y])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }
                    if (y + 1 < VillageSize.y)
                    {
                        if (buildingMap[x, y + 1])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }

                    //diagonals

                    if (x - 1 >= 0 && y - 1 >= 0)
                    {
                        if (buildingMap[x - 1, y - 1])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }

                    if (x + 1 < buildingMap.GetLength(0) && y - 1 >= 0)
                    {
                        if (buildingMap[x + 1, y - 1])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }

                    if (x - 1 >= 0 && y + 1 < buildingMap.GetLength(1))
                    {
                        if (buildingMap[x - 1, y + 1])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }

                    if (x + 1 < buildingMap.GetLength(0) && y + 1 < buildingMap.GetLength(1))
                    {
                        if (buildingMap[x + 1, y + 1])
                        {
                            lowresPathMap[x, y] = true;

                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    pathMap[x * 3 + i, y * 3 + j] = true;
                                }
                            }
                            continue;
                        }
                    }
                }
            }
        }

        //creates a new map to smooth over
        bool[,] newPathMap = new bool[pathMap.GetLength(0), pathMap.GetLength(1)];

        for (int x = 0; x < pathMap.GetLength(0); x++)
        {
            for (int y = 0; y < pathMap.GetLength(1); y++)
            {
                //if its a path, count all the neigbours
                if (pathMap[x, y])
                {
                    int count = 0;
                    //look at the 4 neigbours, if all are true, include it in the new map
                    if (x - 1 >= 0)
                    {
                        if (pathMap[x - 1, y])
                        {
                            count++;
                        }
                    }
                    if (y - 1 >= 0)
                    {
                        if (pathMap[x, y - 1])
                        {
                            count++;
                        }
                    }

                    if (x + 1 < pathMap.GetLength(0))
                    {
                        if (pathMap[x + 1, y])
                        {
                            count++;
                        }
                    }
                    if (y + 1 < pathMap.GetLength(1))
                    {
                        if (pathMap[x, y + 1])
                        {
                            count++;
                        }
                    }

                    if (count == 4)
                    {
                        newPathMap[x, y] = true;
                    }
                }
            }
        }

        pathMap = newPathMap;

        //creates the path gameobject
        GameObject Path = new GameObject();

        Path.name             = "Path";
        Path.layer            = LayerMask.NameToLayer("Path");
        Path.transform.parent = village.transform;

        MeshRenderer pMeshR = Path.AddComponent <MeshRenderer>();

        pMeshR.material = pathMaterial;
        pMeshF          = Path.AddComponent <MeshFilter>();

        pMeshF.mesh              = MarchingSquares.Generate(pathMap, MapCenter, 0);
        Path.transform.position -= new Vector3(-.5f, -.1f, -.5f);

        pMeshC            = Path.AddComponent <MeshCollider>();
        pMeshC.sharedMesh = pMeshF.mesh;

        Path.AddComponent <NavMeshSourceTag>();

        pathRadius = 0;
    }