Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- namespace FirzLib.MapSystem
- {
- class HeightmapChunk
- {
- Vector2 minPos, maxPos;
- int indexCount;
- int indexOffset;
- const int MinChunkSize = 10;
- bool bottomChunk;
- HeightmapChunk[] childChunks;
- public HeightmapChunk(Vector2 minPos, Vector2 maxPos, Vector2 mapSize, int offset, ref int[] indices)
- {
- this.minPos = minPos;
- this.maxPos = maxPos;
- indexOffset = offset;
- Vector2 ChunkSize = (maxPos-minPos);
- if (ChunkSize.X > 2 * MinChunkSize && ChunkSize.Y > 2 * MinChunkSize)
- {
- childChunks = new HeightmapChunk[4];
- childChunks[0] = new HeightmapChunk(minPos, minPos+ChunkSize/2.0f,mapSize,offset, ref indices);
- 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);
- 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);
- childChunks[3] = new HeightmapChunk(minPos + ChunkSize / 2.0f, maxPos, mapSize, childChunks[2].indexOffset + childChunks[2].indexCount, ref indices);
- indexCount = childChunks[0].indexCount + childChunks[1].indexCount + childChunks[2].indexCount + childChunks[3].indexCount;
- bottomChunk = false;
- }
- else
- {
- int count = 0;
- int sizeX=(int)mapSize.X;
- for(int x=(int)minPos.X; x<maxPos.X; x++)
- for (int y = (int)minPos.Y; y < maxPos.Y; y++)
- {
- indices[offset + count] = x + y * sizeX;
- indices[offset + count + 1] = x + 1 + y * sizeX;
- indices[offset + count + 2] = x + 1 + (y+1) * sizeX;
- indices[offset + count + 3] = indices[offset + count];
- indices[offset + count + 4] = indices[offset + count + 2];
- indices[offset + count + 5] = x + (y+1) * sizeX;
- count+=6;
- }
- indexCount = count-6;
- bottomChunk = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement