/// <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 Noise3D TurbulenceNoise(float period, int steps, Point3 periodLoop, int seed) { SumNoise3D sumNoise = new SumNoise3D(); Random rdm = new Random(seed); LoopableNoise3D 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 LoopableNoise3D(rdm.Next(), period, periodLoop); //float weight = weights[i] + (weights[i] / weightSum) * (1f - weightSum); float weight = weights[i] / weightSum; noise.filter = (float value, Vector3 position) => Math.Abs(value) * weight; //noise.filter = (float value, Vector2 position) => (Math.Abs(value) - 0.5f)*2f*weight; //o filtro do ultimo noise coloca os valores no intervalo [-1,1] 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; periodLoop.X *= 2; periodLoop.Y *= 2; periodLoop.Z *= 2; } //sumNoise.filter = (float a, Vector2 b) => { return (a + 1f) / 2f; }; sumNoise.filter = (float a, Vector3 b) => a; return(sumNoise); }
/// <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 override float[,] getValues(float[,] values, noiseSumFunction sumFunc, Point samples, Vector2 origin, Vector2 area) { float xStep = area.X / (samples.X - 1); float yStep = area.Y / (samples.Y - 1); Vector2 pos = new Vector2(); for (int i = 0; i < samples.X; i++) { for (int j = 0; j < samples.Y; j++) { pos.X = origin.X + i * xStep; pos.Y = origin.Y + j * yStep; values[i, j] += sumFunc(values[i, j], filter(0f, pos)); } } return(values); }
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); }
public void setNoiseSumFunction(int index, noiseSumFunction function) { sumFunctions[index] = function; }
public void addNoise(Noise3D noise, noiseSumFunction sumFunction) { noises.Add(noise); sumFunctions.Add(sumFunction); }
public abstract float[,,] getValues(float[,,] values, noiseSumFunction sumFunc, Point3 samples, Vector3 origin, Vector3 area);
public override float[,] getValues(float[,] values, noiseSumFunction sumFunc, Point samples, Vector2 origin, Vector2 area) { origin *= 1f / period; area *= 1f / period; if (values.GetLength(0) != samples.X || values.GetLength(1) != samples.Y) { throw new Exception("Invalid Array to sum To."); } Rectangle gradientSamples = new Rectangle(); gradientSamples.X = (int)Math.Floor(origin.X); gradientSamples.Y = (int)Math.Floor(origin.Y); gradientSamples.Width = (int)Math.Floor(origin.X + area.X) - gradientSamples.X + 1; gradientSamples.Height = (int)Math.Floor(origin.Y + area.Y) - gradientSamples.Y + 1; float[,] gradientVectors = new float[gradientSamples.Width + 1, gradientSamples.Height + 1]; for (int i = 0; i < gradientSamples.Width + 1; i++) { for (int j = 0; j < gradientSamples.Height + 1; j++) { gradientVectors[i, j] = getNodeValue(gradientSamples.X + i, gradientSamples.Y + j); } } float xDif, yDif; int p1, p2; float xStep = area.X / (samples.X - 2); float yStep = area.Y / (samples.Y - 2); float ll, lr, ur, ul, upper, lower; Vector2 pos = new Vector2(); double val; for (int i = 0; i < samples.X; i++) { for (int j = 0; j < samples.Y; j++) { pos.X = origin.X + i * xStep; pos.Y = origin.Y + j * yStep; p1 = (int)Math.Floor(pos.X); p2 = (int)Math.Floor(pos.Y); xDif = pos.X - (float)p1; yDif = pos.Y - (float)p2; p1 = p1 - gradientSamples.X; p2 = p2 - gradientSamples.Y; pos *= period; ll = gradientVectors[p1, p2]; lr = gradientVectors[p1 + 1, p2]; ur = gradientVectors[p1 + 1, p2 + 1]; ul = gradientVectors[p1, p2 + 1]; upper = NGInterp(ul, ur, xDif); lower = NGInterp(ll, lr, xDif); val = NGInterp(lower, upper, yDif); //val = Math.Cos((val + 1) * Math.PI / 2); //val = Math.Cos((val + 1) * Math.PI / 2); values[i, j] = sumFunc(values[i, j], filter((float)val, pos)); } } return(values); }