public override float[,,] getValues(float[,,] values, Noise3D.noiseSumFunction sumFunc, Point3 samples, Vector3 origin, Vector3 area) { values = new float[samples.X, samples.Y,samples.Z]; for (int i = 0; i < noises.Count; i++) { noises[i].getValues(values, sumFunctions[i], samples, origin, area); } Vector3 position = new Vector3(); Vector3 step = new Vector3(area.X / (samples.X - 1), area.Y / (samples.Y - 1), area.Z / (samples.Z - 1)); for(int i=0;i<values.GetLength(0);i++) for(int j=0;j<values.GetLength(1);j++) for (int k = 0; k < values.GetLength(2); k++) { position.X = i * step.X + origin.X; position.Y = j * step.Y + origin.Y; position.Z = k * step.Z + origin.Z; values[i, j, k] = filter(values[i, j, k], position); } return values; }
public LoopableNoise3D(int seed, float period, Point3 periodLoop) { this.seed = seed; this.period = period; this.periodLoop = periodLoop; Random rdm = new Random(seed); gradientVectors = new Vector3[periodLoop.X, periodLoop.Y, periodLoop.Z]; for(int i=0;i<gradientVectors.GetLength(0);i++) for(int j=0;j<gradientVectors.GetLength(1);j++) for (int k = 0; k < gradientVectors.GetLength(2); k++) { gradientVectors[i, j, k] = randVect(rdm); } }
public override float[,,] getValues(float[,,] values, Noise3D.noiseSumFunction sumFunc, Point3 samples, Vector3 origin, Vector3 area) { origin *= 1f / period; area *= 1f / period; float difX, difY,difZ; Vector3 difVector = new Vector3(); Vector3 step = new Vector3(area.X / (samples.X - 1), area.Y / (samples.Y - 1), area.Z / (samples.Z - 1)); Vector3 pos = new Vector3(); int cubeX,cubeY,cubeZ; Vector3 gradientVector; float lll, llu, lul, luu, ull, ulu, uul, uuu; float ll, lu, ul, uu; float l,u; float value; for(int i=0;i<samples.X;i++) for(int j=0;j<samples.Y;j++) for (int k = 0; k < samples.Z; k++) { pos.X = origin.X + i * step.X; pos.Y = origin.Y + j * step.Y; pos.Z = origin.Z + k * step.Z; cubeX = (int)Math.Floor(pos.X); cubeY = (int)Math.Floor(pos.Y); cubeZ = (int)Math.Floor(pos.Z); difX = pos.X - (float)cubeX; difY = pos.Y - (float)cubeY; difZ = pos.Z - (float)cubeZ; pos *= period; //lll llu luu lul uul uuu ulu ull //lll difVector.X = difX; difVector.Y = difY; difVector.Z = difZ; gradientVector = getGradientVector(cubeX, cubeY, cubeZ); Vector3.Dot(ref gradientVector, ref difVector, out lll); //llu difVector.Z = difZ - 1f; gradientVector = getGradientVector(cubeX, cubeY, cubeZ + 1); Vector3.Dot(ref gradientVector, ref difVector, out llu); //luu difVector.Y = difY - 1f; gradientVector = getGradientVector(cubeX, cubeY + 1, cubeZ + 1); Vector3.Dot(ref gradientVector, ref difVector, out luu); //lul difVector.Z = difZ; gradientVector = getGradientVector(cubeX, cubeY + 1, cubeZ); Vector3.Dot(ref gradientVector, ref difVector, out lul); //uul difVector.X = difX - 1f; gradientVector = getGradientVector(cubeX + 1, cubeY + 1, cubeZ); Vector3.Dot(ref gradientVector, ref difVector, out uul); //uuu difVector.Z = difZ - 1f; gradientVector = getGradientVector(cubeX + 1, cubeY + 1, cubeZ + 1); Vector3.Dot(ref gradientVector, ref difVector, out uuu); //ulu difVector.Y = difY; gradientVector = getGradientVector(cubeX + 1, cubeY, cubeZ + 1); Vector3.Dot(ref gradientVector, ref difVector, out ulu); //ull difVector.Z = difZ; gradientVector = getGradientVector(cubeX + 1, cubeY, cubeZ); Vector3.Dot(ref gradientVector, ref difVector, out ull); ll = NGInterp(lll, ull, difX); lu = NGInterp(llu, ulu, difX); ul = NGInterp(lul, uul, difX); uu = NGInterp(luu, uuu, difX); l = NGInterp(ll, ul, difY); u = NGInterp(lu, uu, difY); value = NGInterp(l, u, difZ); value = (float)Math.Cos((value + 1) * Math.PI / 2); values[i, j, k] = sumFunc(values[i, j, k], filter(value, pos)); } return values; }
public float[,,] getValues(Point3 samples, Vector3 origin, Vector3 area) { float[,,] values = new float[samples.X, samples.Y,samples.Z]; return getValues(values, OverrideSum, samples, origin, area); }
public abstract float[,,] getValues(float[,,] values, noiseSumFunction sumFunc, Point3 samples, Vector3 origin, Vector3 area);
/// <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; }
public static Noise3D RegularNoise(float period, int steps, Point3 periodLoop, int seed) { if (steps == 0) throw new Exception("Can't create a noise regular noise with 0 steps"); 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]; } //float weight; Noise3D.noiseSumFunction simpleSum = (float a, float b) => { return a + b; }; for (int j = 0; j < steps; j++) { noise = new LoopableNoise3D(rdm.Next(), period, periodLoop); float weight = weights[j] / weightSum; noise.filter = (float value, Vector3 position) => { return value * (weight); }; sumNoise.addNoise(noise, simpleSum); period *= 0.5f; periodLoop.X *= 2; periodLoop.Y *= 2; periodLoop.Z *= 2; } return sumNoise; }