public static double ProcessWorley(WorleySample ws, CellType cellType) { switch (cellType) { default: case CellType.F0: return(ws.F[0]); case CellType.F1: return(ws.F[1]); case CellType.F2: return(ws.F[2]); case CellType.F3: return(ws.F[3]); case CellType.F1subF0: return(ws.F[1] - ws.F[0]); case CellType.F2subF1: return(ws.F[2] - ws.F[1]); case CellType.F0plusF1div2: return(ws.F[0] + ws.F[1] / 2.0); case CellType.F0multF1: return(ws.F[0] * ws.F[1]); case CellType.Crackle: double n = 10 * ws.F[1] - ws.F[0]; if (n < 1.0) { n = 1.0; } return(n); } }
/// <summary> /// Generates a fractal version of 3D worley noise /// </summary> /// <param name="v">The input coordinate.</param> /// <param name="octaves">Each octave adds a sample of higher frequency and lower amplitude noise to the total result.</param> /// <param name="frequency">The starting frequency of the noise.</param> /// <param name="cellType">The way to combine the worley distance results</param> /// <param name="dfunc">The distance function to use when determining distance to nearest cells</param> /// <param name="amplitude">The starting amplitude of the noise.</param> /// <param name="persistence">Specifies the amplitude multiplier for successive octaves (usually between 0 and 1). Controls the roughness of the noise.</param> /// <param name="lacunarity">Specifies the frequency multiplier for successive octaves (usually greater than 1).</param> /// <returns>A float between -1 and 1.</returns> public static float FractalWorley(Vector3 v, int octaves, float frequency, CellType cellType, DistanceFunction dfunc, double amplitude = 1.0f, float persistence = 0.5f, float lacunarity = 2.0f) { double total = 0.0; uint maxOrder = maxOrderByCellType[(int)cellType]; for (int i = 0; i < octaves; ++i) { WorleySample ws = Worley3(v.x * frequency, v.y * frequency, v.z * frequency, maxOrder, dfunc); double n = ProcessWorley(ws, cellType); total += n * amplitude; amplitude *= persistence; frequency *= lacunarity; } return((float)total); }
public static float FractalWorley(Vector3 v, int octaves, float frequency, CellType cellType, DistanceFunction dfunc, float persistence = 0.5f, float lacunarity = 2.0f) { double total = 0.0; double amplitude = 1.0; uint maxOrder = maxOrderByCellType[(int)cellType]; for (int i = 0; i < octaves; ++i) { WorleySample ws = Worley3(v.x * frequency, v.y * frequency, v.z * frequency, maxOrder, dfunc); double n; switch (cellType) { default: case CellType.F0: n = ws.F[0]; break; case CellType.F1: n = ws.F[1]; break; case CellType.F2: n = ws.F[2]; break; case CellType.F3: n = ws.F[3]; break; case CellType.F1subF0: n = ws.F[1] - ws.F[0]; break; case CellType.F2subF1: n = ws.F[2] - ws.F[1]; break; case CellType.F0plusF1div2: n = ws.F[0] + ws.F[1] / 2.0; break; case CellType.F0multF1: n = ws.F[0] * ws.F[1]; break; case CellType.Crackle: n = 10 * ws.F[1] - ws.F[0]; if (n < 1.0) { n = 1.0; } break; } total += n * amplitude; amplitude *= persistence; frequency *= lacunarity; } return((float)total); }