Advertisement
Dadido3

Heightmap C#

Oct 7th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.74 KB | None | 0 0
  1.     public IEnumerator MapGen_Standart(object[] parms) {
  2.         int Chunk_X = (int)parms[0];
  3.         int Chunk_Y = (int)parms[1];
  4.  
  5.         TileMap[Chunk_X, Chunk_Y].Chunk = new TileStructure[ChunkSize, ChunkSize, Map_Height];
  6.  
  7.         for (int Z = 0; Z < Map_Height; Z++) {
  8.             for (int X = 0; X < ChunkSize; X++) {
  9.                 for (int Y = 0; Y < ChunkSize; Y++) {
  10.                     TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 0;
  11.                 }
  12.             }
  13.         }
  14.  
  15.         float Height_Solid = 0;
  16.         float Height_Water = 3;
  17.         float Height_Sand = 4;
  18.         float Height_Grass = 8;
  19.        
  20.         float[,] Heightmap = Heightmap_Square(Chunk_X, Chunk_Y, Seed, 3);
  21.         float[,] Smoothmap = Heightmap_Square(Chunk_X, Chunk_Y, Seed, 4);
  22.         float[,] Roughtmap = Heightmap_Square(Chunk_X+1, Chunk_Y+1, Seed+1, 3);
  23.         float[,] Offsetmap = Heightmap_Square(Chunk_X+1, Chunk_Y+1, Seed+2, 4);
  24.  
  25.         for (int X = 0; X < ChunkSize; X++) {
  26.             for (int Y = 0; Y < ChunkSize; Y++) {
  27.                 //float Offset = (Mathf.Cos(Offsetmap[X, Y]*12+2)-1+2*Offsetmap[X, Y]*6)*3f;
  28.                 float Height = Heightmap[X ,Y] * Smoothmap[X, Y] * 32; //Mathf.Floor(Offset + Heightmap[X, Y]*Roughtmap[X, Y]*20);
  29.                 float Stone_Height = Mathf.Floor(Height - (0.6f-Offsetmap[X, Y]*Roughtmap[X, Y])*50);  
  30.                 float Max_Height = Mathf.Max(0, Height, Height_Water, Stone_Height); //EDIT THIS
  31.  
  32.                 for (int Z = 0; Z < Max_Height; Z++) {
  33.                     //DebugText.text = "Max height: " + Max_Height + ", X: " + X + ", Y: " + Y + ", Z: " + Z + "\n" + DebugText.text;
  34.                     if (Z <= Height_Solid) {
  35.                         TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
  36.                     } else if (Z <= Height_Water) {
  37.                         if (Z <= Stone_Height) {
  38.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
  39.                         } else if (Z < Height) {
  40.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
  41.                         } else if (Z == Height) {
  42.                             if (Z < Height_Water - 3) {
  43.                                 TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
  44.                             } else {
  45.                                 TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 4;
  46.                             }
  47.                         } else {
  48.                             if (Z == Max_Height) {
  49.                                 TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 5;
  50.                             } else {
  51.                                 TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 5;
  52.                             }
  53.                         }
  54.                     } else if (Z <= Height_Sand) {
  55.                         if (Z <= Stone_Height) {
  56.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
  57.                         } else if (Z+1 < Height) {
  58.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
  59.                         } else {
  60.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 4;                                                                                 
  61.                         }
  62.                     } else if (Z <= Height_Grass-Roughtmap[X, Y]*15) {
  63.                         if (Z <= Stone_Height) {
  64.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
  65.                         } else if (Z < Height) {
  66.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
  67.                         } else {
  68.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 2;
  69.                         }
  70.                     } else {
  71.                         if (Z < Stone_Height) {
  72.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
  73.                         } else if (Z == Stone_Height) {
  74.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 6;
  75.                         } else if (Z < Height) {
  76.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
  77.                         } else {
  78.                             TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 6;
  79.                         }
  80.                     }
  81.                 }
  82.                 yield return null;
  83.             }
  84.         }
  85.  
  86.         TileMap[Chunk_X, Chunk_Y].generated = true;
  87.         object[] parms2 = new object[2]{Chunk_X, Chunk_Y};
  88.         StartCoroutine ("CreateChunk", parms2);
  89.         DebugText.text = "\nMath for Chunk[" + Chunk_X + "," + Chunk_Y + "] done, queuing render." + DebugText.text;
  90.     }
  91.  
  92.  
  93.     public float Random_By_Coordinate(int input)
  94.     {
  95.         Random.seed = input;
  96.         return Random.Range(0f, 1f);
  97.     }
  98.  
  99.     public float[,] Heightmap_Square(int Chunk_X, int Chunk_Y, int seed, int Iterations)
  100.     {
  101.         // Fixed Chunk_Size
  102.         int Chunk_Size = ChunkSize;
  103.        
  104.         // Calculate the Size to accomplish the wanted Chunk_Size with the given Iterations
  105.         int Size = Chunk_Size / (int)Mathf.Pow(2, Iterations);
  106.        
  107.         //int Size = 2; // Start with a 2x2 heightmap
  108.         //int Iterations = 4; // Size*2^Iterations gives a chunksize of 32 tiles
  109.        
  110.         //int Chunk_Size = Size * (int)Mathf.Pow(2, Iterations);
  111.        
  112.         float[,] Heightmap = new float[Chunk_Size+1, Chunk_Size+1];
  113.        
  114.         // #### Write array of (Size+1)² random elements ####
  115.         for (int ix = 0; ix <= Size; ix++){
  116.             int Real_X = Chunk_X*Chunk_Size + ix * (Chunk_Size / Size);
  117.             for (int iy = 0; iy <= Size; iy++){
  118.                 int Real_Y = Chunk_Y*Chunk_Size + iy * (Chunk_Size / Size);
  119.                 Heightmap[ix, iy] = Random_By_Coordinate(seed + Real_X + Real_Y*100000);
  120.             }
  121.         }
  122.        
  123.         // #### Do the iterations ####
  124.         for (int i = 0; i < Iterations; i++){
  125.  
  126.             // #### Resize the array ####
  127.             for (int ix = Size; ix >= 0; ix--){
  128.                 for (int iy = Size; iy >= 0; iy--){
  129.                     Heightmap[ix*2, iy*2] = Heightmap[ix, iy];
  130.                 }
  131.             }
  132.             Size *= 2;
  133.            
  134.             // #### The diamond step ####
  135.             for (int ix = 1; ix <= Size; ix+=2){
  136.                 for (int iy = 1; iy <= Size; iy+=2){
  137.                     Heightmap[ix, iy] = (Heightmap[ix+1, iy+1] + Heightmap[ix-1, iy+1] + Heightmap[ix+1, iy-1] + Heightmap[ix-1, iy-1]) / 4f;
  138.                     // #### Add some noise to it
  139.                     //Heightmap[ix, iy] += ((float)rand.Next() / 2147483648) - 0.5f;
  140.                     // Don't add noise, doesn't work well with infinite maps.
  141.                 }
  142.             }
  143.            
  144.             // #### The square step ####
  145.             for (int ix = 0; ix <= Size; ix+=2){
  146.                 for (int iy = 1; iy <= Size; iy+=2){
  147.                     Heightmap[ix, iy] = (Heightmap[ix, iy-1] + Heightmap[ix, iy+1]) / 2f;
  148.                 }
  149.             }
  150.             for (int ix = 1; ix <= Size; ix+=2){
  151.                 for (int iy = 0; iy <= Size; iy+=2){
  152.                     Heightmap[ix, iy] = (Heightmap[ix-1, iy] + Heightmap[ix+1, iy]) / 2f;
  153.                 }
  154.             }
  155.            
  156.         }
  157.        
  158.         // #### The size of the Heightmap is (Chunk_Size+1)²   ####
  159.         // #### Resize array to Chunk_Size x Chunk_Size                 ####
  160.         //Array.Resize(ref Heightmap, new int[] {Chunk_Size, Chunk_Size});
  161.        
  162.         return Heightmap;
  163.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement