Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace FirzLib.MapSystem
  8. {
  9.     class HeightmapChunk
  10.     {
  11.         Vector2 minPos, maxPos;
  12.         int indexCount;
  13.         int indexOffset;
  14.         const int MinChunkSize = 10;
  15.         bool bottomChunk;
  16.         HeightmapChunk[] childChunks;
  17.  
  18.         public HeightmapChunk(Vector2 minPos, Vector2 maxPos, Vector2 mapSize, int offset, ref int[] indices)
  19.         {
  20.             this.minPos = minPos;
  21.             this.maxPos = maxPos;
  22.             indexOffset = offset;
  23.             Vector2 ChunkSize = (maxPos-minPos);
  24.             if (ChunkSize.X > 2 * MinChunkSize && ChunkSize.Y > 2 * MinChunkSize)
  25.             {
  26.                 childChunks = new HeightmapChunk[4];
  27.                 childChunks[0] = new HeightmapChunk(minPos, minPos+ChunkSize/2.0f,mapSize,offset, ref indices);
  28.                 childChunks[1] = new HeightmapChunk(minPos + Vector2.UnitX * ChunkSize.X / 2.0f, maxPos - Vector2.UnitY * ChunkSize.Y / 2.0f, mapSize, offset + childChunks[0].indexCount, ref indices);
  29.                 childChunks[2] = new HeightmapChunk(minPos + Vector2.UnitY * ChunkSize.Y / 2.0f, maxPos - Vector2.UnitX * ChunkSize.X / 2.0f, mapSize, childChunks[1].indexOffset + childChunks[1].indexCount, ref indices);
  30.                 childChunks[3] = new HeightmapChunk(minPos + ChunkSize / 2.0f, maxPos, mapSize, childChunks[2].indexOffset + childChunks[2].indexCount, ref indices);
  31.                 indexCount = childChunks[0].indexCount + childChunks[1].indexCount + childChunks[2].indexCount + childChunks[3].indexCount;
  32.                 bottomChunk = false;
  33.             }
  34.             else
  35.             {
  36.                 int count = 0;
  37.                 int sizeX=(int)mapSize.X;
  38.                 for(int x=(int)minPos.X; x<maxPos.X; x++)
  39.                     for (int y = (int)minPos.Y; y < maxPos.Y; y++)
  40.                     {
  41.                         indices[offset + count] = x + y * sizeX;
  42.                         indices[offset + count + 1] = x + 1 + y * sizeX;
  43.                         indices[offset + count + 2] = x + 1 + (y+1) * sizeX;
  44.                         indices[offset + count + 3] = indices[offset + count];
  45.                         indices[offset + count + 4] = indices[offset + count + 2];
  46.                         indices[offset + count + 5] = x + (y+1) * sizeX;
  47.                         count+=6;
  48.                     }
  49.                 indexCount = count-6;
  50.                 bottomChunk = true;
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement