Exemple #1
0
        public override double GetValue(double x, double y, double z)
        {
            if (SourceModule1 == null || SourceModule2 == null || WeightModule == null)
            {
                throw new NullReferenceException("No source module can be null");
            }

            return(NoiseMath.LinearInterpolate(SourceModule1.GetValue(x, y, z), SourceModule2.GetValue(x, y, z),
                                               (WeightModule.GetValue(x, y, z) + 1.0) / 2.0));
        }
Exemple #2
0
        public override double GetValue(double x, double y, double z)
        {
            if (SourceModule == null)
            {
                throw new InvalidOperationException("Source Module cannot be null");
            }

            // Get the output value from the source module.
            double sourceModuleValue = SourceModule.GetValue(x, y, z);

            // Find the first element in the control point array that has a value
            // larger than the output value from the source module.
            int indexPos;

            for (indexPos = 0; indexPos < ControlPoints.Length; indexPos++)
            {
                if (sourceModuleValue < ControlPoints[indexPos])
                {
                    break;
                }
            }

            // Find the two nearest control points so that we can map their values
            // onto a quadratic curve.
            int index0 = NoiseMath.ClampValue(indexPos - 1, 0, ControlPoints.Length - 1);
            int index1 = NoiseMath.ClampValue(indexPos, 0, ControlPoints.Length - 1);

            // If some control points are missing (which occurs if the output value from
            // the source module is greater than the largest value or less than the
            // smallest value of the control point array), get the value of the nearest
            // control point and exit now.
            if (index0 == index1)
            {
                return(ControlPoints[index1]);
            }

            // Compute the alpha value used for linear interpolation.
            double value0 = ControlPoints[index0];
            double value1 = ControlPoints[index1];
            double alpha  = (sourceModuleValue - value0) / (value1 - value0);

            if (InvertTerraces)
            {
                alpha = 1.0 - alpha;
                double temp = value0;
                value0 = value1;
                value1 = temp;
            }

            // Squaring the alpha produces the terrace effect.
            alpha *= alpha;

            // Now perform the linear interpolation given the alpha value.
            return(NoiseMath.LinearInterpolate(value0, value1, alpha));
        }
        internal static double GradientCoherentNoise(double x, double y, double z, int seed, NoiseQuality noiseQuality)
        {
            int x0 = (x > 0.0 ? (int)x : (int)x - 1);
            int x1 = x0 + 1;
            int y0 = (y > 0.0 ? (int)y : (int)y - 1);
            int y1 = y0 + 1;
            int z0 = (z > 0.0 ? (int)z : (int)z - 1);
            int z1 = z0 + 1;

            double xs = 0, ys = 0, zs = 0;

            switch (noiseQuality)
            {
            case NoiseQuality.Low:
                xs = (x - x0);
                ys = (y - y0);
                zs = (z - z0);
                break;

            case NoiseQuality.Standard:
                xs = NoiseMath.SCurve3(x - x0);
                ys = NoiseMath.SCurve3(y - y0);
                zs = NoiseMath.SCurve3(z - z0);
                break;

            case NoiseQuality.High:
                xs = NoiseMath.SCurve5(x - x0);
                ys = NoiseMath.SCurve5(y - y0);
                zs = NoiseMath.SCurve5(z - z0);
                break;
            }

            double n0  = GradientNoise(x, y, z, x0, y0, z0, seed);
            double n1  = GradientNoise(x, y, z, x1, y0, z0, seed);
            double ix0 = NoiseMath.LinearInterpolate(n0, n1, xs);

            n0 = GradientNoise(x, y, z, x0, y1, z0, seed);
            n1 = GradientNoise(x, y, z, x1, y1, z0, seed);
            double ix1 = NoiseMath.LinearInterpolate(n0, n1, xs);
            double iy0 = NoiseMath.LinearInterpolate(ix0, ix1, ys);

            n0  = GradientNoise(x, y, z, x0, y0, z1, seed);
            n1  = GradientNoise(x, y, z, x1, y0, z1, seed);
            ix0 = NoiseMath.LinearInterpolate(n0, n1, xs);
            n0  = GradientNoise(x, y, z, x0, y1, z1, seed);
            n1  = GradientNoise(x, y, z, x1, y1, z1, seed);
            ix1 = NoiseMath.LinearInterpolate(n0, n1, xs);
            double iy1 = NoiseMath.LinearInterpolate(ix0, ix1, ys);

            return(NoiseMath.LinearInterpolate(iy0, iy1, zs));
        }
Exemple #4
0
        public override double GetValue(double x, double y, double z)
        {
            if (ModuleA == null)
            {
                throw new InvalidOperationException("ModuleA cannot be null");
            }
            if (ModuleB == null)
            {
                throw new InvalidOperationException("ModuleB cannot be null");
            }
            if (ControlModule == null)
            {
                throw new InvalidOperationException("Control cannot be null");
            }

            double controlValue = ControlModule.GetValue(x, y, z);

            if (edgeFalloff > 0.0)
            {
                if (controlValue < (LowerBound - edgeFalloff))
                {
                    // The output value from the control module is below the selector
                    // threshold; return the output value from the first source module.
                    return(ModuleA.GetValue(x, y, z));
                }
                double alpha;
                if (controlValue < (LowerBound + edgeFalloff))
                {
                    // The output value from the control module is near the lower end of the
                    // selector threshold and within the smooth curve. Interpolate between
                    // the output values from the first and second source modules.
                    double lowerCurve = (LowerBound - edgeFalloff);
                    double upperCurve = (LowerBound + edgeFalloff);
                    alpha = NoiseMath.SCurve3((controlValue - lowerCurve) / (upperCurve - lowerCurve));
                    return(NoiseMath.LinearInterpolate(ModuleA.GetValue(x, y, z), ModuleB.GetValue(x, y, z), alpha));
                }
                if (controlValue < (UpperBound - edgeFalloff))
                {
                    // The output value from the control module is within the selector
                    // threshold; return the output value from the second source module.
                    return(ModuleB.GetValue(x, y, z));
                }
                if (controlValue < (UpperBound + edgeFalloff))
                {
                    // The output value from the control module is near the upper end of the
                    // selector threshold and within the smooth curve. Interpolate between
                    // the output values from the first and second source modules.
                    double lowerCurve = (UpperBound - edgeFalloff);
                    double upperCurve = (UpperBound + edgeFalloff);
                    alpha = NoiseMath.SCurve3((controlValue - lowerCurve) / (upperCurve - lowerCurve));
                    return(NoiseMath.LinearInterpolate(ModuleB.GetValue(x, y, z), ModuleA.GetValue(x, y, z), alpha));
                }
                // Output value from the control module is above the selector threshold;
                // return the output value from the first source module.
                return(ModuleA.GetValue(x, y, z));
            }
            if (controlValue < LowerBound || controlValue > UpperBound)
            {
                return(ModuleA.GetValue(x, y, z));
            }
            return(ModuleB.GetValue(x, y, z));
        }