/// <summary> /// Method that returns a Turbulence noise with the speficied parameters that has a customizable filter. /// </summary> /// <param name="period">Size of the noise.</param> /// <param name="steps">Detail of the noise</param> /// <param name="seed">The seed used</param> /// <returns>A SumNoise with several noises.</returns> public static Noise TurbulenceNoise(float period, int steps, int seed) { SumNoise sumNoise = new SumNoise(); Random rdm = new Random(seed); BasicGradientNoise noise; float[] weights = new float[steps]; weights[0] = 0.5f; float weightSum = 0.5f; for (int i = 1; i < steps; i++) { weights[i] = weights[i - 1] * 0.5f; weightSum += weights[i]; } noiseSumFunction simpleSum = (float a, float b) => a + b; noiseSumFunction lastFunc = (float a, float b) => ((a + b) - 0.5f) * 2f; //float weight; for (int i = 0; i < steps; i++) { noise = new BasicGradientNoise(rdm.Next(), period); float weight = weights[i] + (weights[i] / weightSum) * (1f - weightSum); noise.filter = (float value, Vector2 position) => Math.Abs(value) * weight; //noise.filter = (float value, Vector2 position) => (Math.Abs(value) - 0.5f)*2f*weight; if (i != steps - 1) { sumNoise.addNoise(noise, (float a, float b) => a + b); } else { sumNoise.addNoise(noise, (float a, float b) => ((a + b) - 0.5f) * 2f); } period *= 0.5f; } //sumNoise.filter = (float a, Vector2 b) => { return (a + 1f) / 2f; }; sumNoise.filter = (float a, Vector2 b) => a; return(sumNoise); }
public static Noise MixedNoise(float period, int BasicRandomNoiseSteps, int BasicGradientNoiseSteps, int seed) { SumNoise sumNoise = new SumNoise(); Random rdm = new Random(seed); Noise noise; int steps = BasicRandomNoiseSteps + BasicGradientNoiseSteps; float[] weights = new float[steps]; weights[0] = 0.5f; float weightSum = 0.5f; for (int i = 1; i < steps; i++) { weights[i] = weights[i - 1] * 0.5f; weightSum += weights[i]; } for (int i = 0; i < steps; i++) { float weight = weights[i] + (weights[i] / weightSum) * (1f - weightSum); if (i < BasicRandomNoiseSteps) { noise = new BasicRandomNoise(rdm.Next(), period); noise.filter = (float value, Vector2 position) => value * weight; } else { noise = new BasicGradientNoise(rdm.Next(), period); noise.filter = (float value, Vector2 position) => (Math.Abs(value) - 0.5f) * 2f * weight; } sumNoise.addNoise(noise, (float a, float b) => a + b); period *= 0.5f; } return(sumNoise); }
public static Noise RegularNoise(float period, int steps, int seed) { if (steps == 0) { throw new Exception("Can't create a noise regular noise with 0 steps"); } SumNoise sumNoise = new SumNoise(); Random rdm = new Random(seed); BasicGradientNoise noise; float[] weights = new float[steps]; weights[0] = 0.5f; float weightSum = 0.5f; for (int i = 1; i < steps; i++) { weights[i] = weights[i - 1] * 0.5f; weightSum += weights[i]; } //float weight; noiseSumFunction simpleSum = (float a, float b) => { return(a + b); }; for (int j = 0; j < steps; j++) { noise = new BasicGradientNoise(rdm.Next(), period); float weight = weights[j] / weightSum; noise.filter = (float value, Vector2 position) => { return(value * (weight)); }; sumNoise.addNoise(noise, simpleSum); period *= 0.5f; } return(sumNoise); }