public void CreateChunk(Chunk chunk) { var cornerPos = (BlockPos)chunk.Pos; for (int x = 0; x < GeometryConstants.ChunkSize; ++x) { long wx = cornerPos.X + x; for (int z = 0; z < GeometryConstants.ChunkSize; ++z) { long wz = cornerPos.Z + z; var dirtHeight = (long)(_dirtNoise[wx, wz] * 2.5 - 8); var mountainHeight = (long)(_mountainNoise[wx, wz] * 8 + _mountainDetailNoise[wx, wz] * 3 - 10); for (int y = 0; y < GeometryConstants.ChunkSize; ++y) { long wy = cornerPos.Y + y; if (wy < mountainHeight) { chunk[x, y, z] = 2; // Rock } else if (wy < dirtHeight) { chunk[x, y, z] = 1; // Dirt } } } } chunk.IsCreated = true; }
public Chunk GetChunk(ChunkPos chunkPos) { lock (_mutex) { Chunk chunk; if (!_chunkMap.TryGetValue(chunkPos, out chunk)) { chunk = new Chunk(this, chunkPos); _chunkMap[chunkPos] = chunk; Task.Run(() => LoadOrGenerate(chunk)); } return chunk; } }
private void RenderChunk(TriangleBuffer buffer, Chunk chunk) { var offset = chunk.Pos - new BlockPos(0, 0, 0); float xOffset = offset.X + 0.5f; float yOffset = offset.Y + 0.5f; float zOffset = offset.Z + 0.5f; for (int y = GeometryConstants.ChunkSize - 1; y >= 0; --y) { for (int x = 0; x < GeometryConstants.ChunkSize; ++x) { for (int z = 0; z < GeometryConstants.ChunkSize; ++z) { ushort material = chunk[x, y, z]; if (material != 0) { var textureEntry = _textureAtlas.GetTextureEntry(material); float x1 = x + xOffset, y1 = y + yOffset, z1 = z + zOffset; if (x == 0 || chunk[x - 1, y, z] == 0) DrawBlockLeft(buffer, textureEntry, x1, y1, z1, 0); if (x == GeometryConstants.ChunkSize - 1 || chunk[x + 1, y, z] == 0) DrawBlockRight(buffer, textureEntry, x1, y1, z1, 0); if (y == 0 || chunk[x, y - 1, z] == 0) DrawBlockBottom(buffer, textureEntry, x1, y1, z1, 0); if (y == GeometryConstants.ChunkSize - 1 || chunk[x, y + 1, z] == 0) DrawBlockTop(buffer, textureEntry, x1, y1, z1, 0); if (z == 0 || chunk[x, y, z - 1] == 0) DrawBlockBack(buffer, textureEntry, x1, y1, z1, 0); if (z == GeometryConstants.ChunkSize - 1 || chunk[x, y, z + 1] == 0) DrawBlockFront(buffer, textureEntry, x1, y1, z1, 0); } } } } }
private async void LoadOrGenerate(Chunk chunk) { var savedValueTask = Universe?.SaveFile?.ReadAsync(StorageKey.Get("ChunkData", chunk.Pos)); StorageValue savedValue = savedValueTask == null ? null : await savedValueTask; if (savedValue != null) { var chunkData = savedValue.Deserialize<ChunkData>(); if (chunkData != null) { chunk.PasteChunkData(savedValue.Deserialize<ChunkData>()); } } else if (Generator != null) { Generator.CreateChunk(chunk); } }