Advertisement
Guest User

Untitled

a guest
Dec 20th, 2012
1,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using Microsoft.Xna.Framework.Net;
  12. using Microsoft.Xna.Framework.Storage;
  13. using System.IO;
  14.  
  15. namespace TileTest
  16. {
  17.     static class NoiseGenerator
  18.     {
  19.         public static int Seed { get; private set; }
  20.  
  21.         public static int Octaves { get; set; }
  22.  
  23.         public static double Amplitude { get; set; }
  24.  
  25.         public static double Persistence { get; set; }
  26.  
  27.         public static double Frequency { get; set; }
  28.  
  29.         static NoiseGenerator()
  30.         {
  31.             Random r = new Random();
  32.             //LOOOL
  33.             NoiseGenerator.Seed = r.Next(Int32.MaxValue);
  34.             NoiseGenerator.Octaves = 6;
  35.             NoiseGenerator.Amplitude = 12;
  36.             NoiseGenerator.Frequency = 0.045;
  37.             NoiseGenerator.Persistence = 0.3;
  38.         }
  39.         public static void generateNoise(int iOctaves, int iAmplitude, double iFrequency, double iPersistence)
  40.         {
  41.             Random r = new Random();
  42.             //LOOOL
  43.             NoiseGenerator.Seed = r.Next(Int32.MaxValue);
  44.             NoiseGenerator.Octaves = iOctaves;
  45.             NoiseGenerator.Amplitude = iAmplitude;
  46.             NoiseGenerator.Frequency = iFrequency;
  47.             NoiseGenerator.Persistence = iPersistence;
  48.         }
  49.  
  50.         public static double Noise(int x, int y)
  51.         {
  52.             //returns -1 to 1
  53.             double total = 0.0;
  54.             double freq = NoiseGenerator.Frequency, amp = NoiseGenerator.Amplitude;
  55.             for (int i = 0; i < NoiseGenerator.Octaves; ++i)
  56.             {
  57.                 total = total + NoiseGenerator.Smooth(x * freq, y * freq) * amp;
  58.                 freq *= 2;
  59.                 amp *= NoiseGenerator.Persistence;
  60.             }
  61.             if (total < -2.4) total = -2.4;
  62.             else if (total > 2.4) total = 2.4;
  63.  
  64.             return (total / 2.4);
  65.         }
  66.  
  67.         public static double NoiseGeneration(int x, int y)
  68.         {
  69.             int n = x + y * 57;
  70.             n = (n << 13) ^ n;
  71.  
  72.             return (1.0 - ((n * (n * n * 15731 + 789221) + NoiseGenerator.Seed) & 0x7fffffff) / 1073741824.0);
  73.         }
  74.  
  75.         private static double Interpolate(double x, double y, double a)
  76.         {
  77.             double value = (1 - Math.Cos(a * Math.PI)) * 0.5;
  78.             return x * (1 - value) + y * value;
  79.         }
  80.  
  81.         private static double Smooth(double x, double y)
  82.         {
  83.             double n1 = NoiseGeneration((int)x, (int)y);
  84.             double n2 = NoiseGeneration((int)x + 1, (int)y);
  85.             double n3 = NoiseGeneration((int)x, (int)y + 1);
  86.             double n4 = NoiseGeneration((int)x + 1, (int)y + 1);
  87.  
  88.             double i1 = Interpolate(n1, n2, x - (int)x);
  89.             double i2 = Interpolate(n3, n4, x - (int)x);
  90.  
  91.             return Interpolate(i1, i2, y - (int)y);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement