private IEnumerator generate()
    {
        this.manager.generatingRegions.Add(this);
        this.computingGeneration = true;

        for (int i = 0; i < REGIONSIZE; i++)
        {
            for (int j = 0; j < REGIONSIZE; j++)
            {
                if (this.points[i, j] != null)
                {
                    Destroy(this.points[i, j].gameObject);
                }
            }
        }

        Material material = (Material)Instantiate(this.level.resources[60]);

        int pointsBuffer = 0;

        for (int i = 0; i < REGIONSIZE; i++)
        {
            for (int j = 0; j < REGIONSIZE; j++)
            {
                GameObject pointObj = new GameObject("WorldRegionPoint");
                pointObj.transform.position = this.gameObject.transform.position
                                              + new Vector3(REGIONPRECISION * i, 0.0f, REGIONPRECISION * j);
                pointObj.transform.SetParent(this.gameObject.transform);

                WorldRegionPoint point = new WorldRegionPoint(pointObj, material);
                this.points[i, j] = point;

                pointsBuffer++;
                if (pointsBuffer >= WorldRegionManager.REGIONSPOINTSGENERATEBUFFERSIZE)
                {
                    pointsBuffer = 0;
                    yield return(new WaitForEndOfFrame());
                }
            }
        }

        this.status = Status.DirtyDisplay;
        //Debug.Log("WorldRegion "+this.ToString()+" generated");
        StartCoroutine(this.generateCollision());

        this.manager.generatingRegions.Remove(this);
        this.computingGeneration = false;
    }
 public void setLod(int newLod)
 {
     this.lod    = newLod;
     this.status = Status.Ungenerated;
 }
    private IEnumerator display(int lod)
    {
        this.manager.displayingRegions.Add(this);
        this.computingDisplay = true;

        MeshFilter filter = this.gameObject.GetComponent <MeshFilter>();

        if (filter != null)
        {
            Destroy(filter.mesh);
            filter.mesh = null;
        }

        int pointsBuffer = 0;

        for (int i = 0; i < REGIONSIZE - lod; i = i + lod)
        {
            for (int j = 0; j < REGIONSIZE - lod; j = j + lod)
            {
                float[] pointHeightmap = new float[] {
                    this.heightmap[i, j],
                    this.heightmap[i + lod, j],
                    this.heightmap[i, j + lod],
                    this.heightmap[i + lod, j + lod]
                };

                this.points[i, j].display(lod, pointHeightmap);

                pointsBuffer++;
                if (pointsBuffer >= WorldRegionManager.REGIONSPOINTSDISPLAYBUFFERSIZE)
                {
                    pointsBuffer = 0;
                    yield return(new WaitForEndOfFrame());
                }
            }
        }

        List <MeshFilter> meshFilters = new List <MeshFilter>();

        for (int i = 0; i < REGIONSIZE; i++)
        {
            for (int j = 0; j < REGIONSIZE; j++)
            {
                if (this.points[i, j].gameObject.GetComponent <MeshFilter>() != null)
                {
                    meshFilters.Add(this.points[i, j].gameObject.GetComponent <MeshFilter>());
                }
            }
        }
        CombineInstance[] combineInstances = new CombineInstance[meshFilters.Count];
        for (int i = 0; i < combineInstances.Length; i++)
        {
            if (meshFilters[i] != null)
            {
                combineInstances[i].mesh      = meshFilters[i].mesh;
                combineInstances[i].transform = Matrix4x4.TRS(this.gameObject.transform.InverseTransformPoint(meshFilters[i].gameObject.transform.position), Quaternion.Inverse(meshFilters[i].gameObject.transform.rotation), Vector3.one);
            }
        }

        MeshRenderer renderer = this.gameObject.GetComponent <MeshRenderer>();

        if (renderer == null)
        {
            renderer = this.gameObject.AddComponent <MeshRenderer>();
        }

        filter = this.gameObject.GetComponent <MeshFilter>();
        if (filter == null)
        {
            filter = this.gameObject.AddComponent <MeshFilter>();
            Mesh mesh = new Mesh();
            filter.mesh = mesh;
        }
        filter.mesh.CombineMeshes(combineInstances);
        filter.mesh.RecalculateBounds();
        filter.mesh.RecalculateNormals();

        switch (this.manager.regionsType)
        {
        case WorldRegionManager.RegionsType.Sand:
            renderer.material       = (Material)Instantiate(this.level.resources[61]);
            renderer.sharedMaterial = (Material)Instantiate(this.level.resources[61]);
            break;

        case WorldRegionManager.RegionsType.Rock:
            renderer.material       = (Material)Instantiate(this.level.resources[62]);
            renderer.sharedMaterial = (Material)Instantiate(this.level.resources[62]);
            break;
        }

        for (int i = 0; i < REGIONSIZE; i++)
        {
            for (int j = 0; j < REGIONSIZE; j++)
            {
                Destroy(this.points[i, j].gameObject);
                this.points[i, j] = null;
            }
        }

        this.manager.displayingRegions.Remove(this);
        this.computingDisplay = false;
        this.status           = Status.Displayed;
        //Debug.Log("WorldRegion " + this.ToString() + " displayed");
    }