Exemplo n.º 1
0
        static void FillFromNoiseMap(TerrainData terrainData, SharpNoise.NoiseMap noiseMap)
        {
            var max = noiseMap.Data.Max();
            var min = noiseMap.Data.Min();

            Parallel.For(0, noiseMap.Data.Length, i =>
            {
                var v = noiseMap.Data[i];                   // [-1 .. 1]

                v -= min;
                v /= (max - min);                       // [0 .. 1]

                v *= terrainData.Depth * 8 / 10;
                v += terrainData.Depth * 2 / 10;

                noiseMap.Data[i] = v;
            });

            Parallel.For(0, terrainData.Height, y =>
            {
                for (int x = 0; x < terrainData.Width; ++x)
                {
                    var v = noiseMap[x, y];

                    int iv = (int)v;

                    for (int z = terrainData.Depth - 1; z >= 0; --z)
                    {
                        var p = new IntVector3(x, y, z);

                        /* above ground */
                        if (z > iv)
                        {
                            terrainData.SetTileDataNoHeight(p, TileData.EmptyTileData);
                        }
                        /* surface */
                        else if (z == iv)
                        {
                            terrainData.SetTileDataNoHeight(p, TileData.EmptyTileData);
                        }
                        /* underground */
                        else if (z < iv)
                        {
                            terrainData.SetTileDataNoHeight(p, TileData.GetNaturalWall(MaterialID.Granite));
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a new NoiseMap from the given source NoiseMap using bilinear filtering.
        ///
        /// When the sample is outside the source BorderValue is used by default. If <see cref="clamp"/>/>
        /// is set clamping is used instead.
        /// </summary>
        /// <param name="src">The source NoiseMap</param>
        /// <param name="width">Width of the new NoiseMap</param>
        /// <param name="height">Height of the new NoiseMap</param>
        /// <param name="clamp">Use clamping when the sample is outside the source NoiseMap</param>
        /// <returns>The new NoiseMap</returns>
        public static NoiseMap BilinearFilter(NoiseMap src, int width, int height, bool clamp = false)
        {
            var dest = new NoiseMap(width, height);

            var xratio = (float)src.Width / dest.Width;
            var yratio = (float)src.Height / dest.Height;

            Parallel.For(0, dest.Height, y =>
            {
                for (var x = 0; x < dest.Width; ++x)
                {
                    var u = (x + 0.5f) * xratio - 0.5f;
                    var v = (y + 0.5f) * yratio - 0.5f;

                    var x0 = NoiseMath.FastFloor(u);
                    var y0 = NoiseMath.FastFloor(v);
                    var x1 = x0 + 1;
                    var y1 = y0 + 1;

                    var xf = u - x0;
                    var yf = v - y0;

                    if (clamp)
                    {
                        x0 = NoiseMath.Clamp(x0, 0, src.Width - 1);
                        x1 = NoiseMath.Clamp(x1, 0, src.Width - 1);
                        y0 = NoiseMath.Clamp(y0, 0, src.Height - 1);
                        y1 = NoiseMath.Clamp(y1, 0, src.Height - 1);
                    }

                    var c00 = src.GetValue(x0, y0);
                    var c01 = src.GetValue(x0, y1);
                    var c10 = src.GetValue(x1, y0);
                    var c11 = src.GetValue(x1, y1);

                    var val = NoiseMath.Bilinear(xf, yf, c00, c01, c10, c11);

                    dest.SetValue(x, y, val);
                }
            });

            return(dest);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public NoiseMap(NoiseMap other)
     : base(other)
 {
 }
Exemplo n.º 4
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public NoiseSphere(NoiseMap other)
     : base(other)
 {
 }