示例#1
0
    public HeightGrid Copy()
    {
        HeightGrid temp = new HeightGrid(size);

        temp.SetGrid(this.grid);
        return(temp);
    }
    void Awake()
    {
        generator    = GetComponent <Transform>();
        gridSize     = (int)Math.Pow(2, nVal) + 1;
        mesh         = GetComponent <MeshFilter>().mesh;
        meshCollider = GetComponent <MeshCollider>();
        MeshRenderer renderer = this.gameObject.AddComponent <MeshRenderer>();

        renderer.material = material;
        mesh.indexFormat  = UnityEngine.Rendering.IndexFormat.UInt32;
        verts             = new HeightGrid(gridSize);
        triangles         = new int[gridSize * gridSize * 6];
        uvs       = new Vector2[(int)Math.Pow(gridSize, 2)];
        maxHeight = baseMaxHeight;
    }
    void MedianFilter()
    {
        var   window = new List <float>();
        float newHeight;

        int checkWindowOffsetX;
        int checkWindowOffsetY;

        int adjustedWindowWidthX;
        int adjustedWindowWidthY;

        HeightGrid copy = verts.Copy();

        for (int x = 0; x < gridSize; x++)
        {
            adjustedWindowWidthX = GetAdjustedWindowWidth(x);
            checkWindowOffsetX   = GetWindowOffset(x);
            for (int y = 0; y < gridSize; y++)
            {
                adjustedWindowWidthY = GetAdjustedWindowWidth(y);
                checkWindowOffsetY   = GetWindowOffset(y);
                for (int fx = 0; fx < adjustedWindowWidthX; fx++)
                {
                    for (int fy = 0; fy < adjustedWindowWidthY; fy++)
                    {
                        window.Add(copy.GetHeight(new Vector2(x + fx - checkWindowOffsetX, y + fy - checkWindowOffsetY)));
                    }
                }
                window.Sort();
                newHeight = window[adjustedWindowWidthX * adjustedWindowWidthY / 2];

                verts.SetHeight(new Vector2(x, y), newHeight);

                window.Clear();
            }
        }
    }