Exemplo n.º 1
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);
        }