示例#1
0
    /// <summary>
    /// apply filter to given heightmap
    /// </summary>
    public void FilterMapInRegion(GlobalCoordinates map, Area region, float epsilon)
    {
        int x_min = region.botLeft.x;
        int z_min = region.botLeft.z;

        int x_max = region.topRight.x;
        int z_max = region.topRight.z;

        int area = 1;
        GlobalCoordinates mapTmp = new GlobalCoordinates(100);

        for (int x = x_min; x < x_max; x++)
        {
            for (int z = z_min; z < z_max; z++)
            {
                if (map.IsDefined(x, z))
                {
                    float average = 0;
                    int neighboursCount = 0;
                    for (int _x = x - area; _x <= x + area; _x++)
                    {
                        for (int _z = z - area; _z <= z + area; _z++)
                        {
                            if (map.IsDefined(_x, _z))
                            {
                                average += map.GetValue(_x, _z, 0);
                                neighboursCount++;
                            }
                        }
                    }
                    if (neighboursCount != 0)
                        average /= neighboursCount;
                    else
                        average = 666;

                    if (average != 666)
                    {
                        float height = map.GetValue(x, z);

                        if (height < average - epsilon)
                            fg.SetGlobalValue(x, z, (average + epsilon), true, mapTmp);
                        else if (height > average + epsilon)
                            fg.SetGlobalValue(x, z, (average - epsilon), true, mapTmp);

                    }
                }
            }
        }
        for (int x = x_min; x < x_max; x++)
        {
            for (int z = z_min; z < z_max; z++)
            {
                if(mapTmp.IsDefined(x, z))
                    map.SetValue(x, z, mapTmp.GetValue(x, z));
            }
        }
    }
 /// <summary>
 /// returns filter value on given global coordiantes (0 if not defined)
 /// gc = global filter space
 /// </summary>
 public float GetGlobalValue(int x, int z, GlobalCoordinates gc)
 {
     float value = gc.GetValue(x, z);
     if (value != 666)
         return value;
     else
         return 0;
 }
 /// <summary>
 /// maps given local coordinates to global
 /// returns value on given coordinates
 /// 666 if not defined
 /// </summary>
 public float GetLocalValue(int x, int z, GlobalCoordinates gc)
 {
     //Debug.Log("getting " + x + "," + z);
     //Debug.Log("= " + (x + (int)center.x - terrainWidth) + "," + (z + (int)center.z - terrainHeight / 2));
     return gc.GetValue(x + (int)center.x - terrainWidth / 2, z + (int)center.z - terrainHeight / 2);
 }