/** Generate the mesh for a specified chunk */ public MeshBuildInfo RenderChunk(Vector3i pos) { MeshBuildInfo meshInfo = new MeshBuildInfo(); ushort block; IRenderBlock renderer; for (int x = 0; x < CHUNK_SIZE; x++) { for (int y = 0; y < CHUNK_SIZE; y++) { for (int z = 0; z < CHUNK_SIZE; z++) { block = GetBlockAt(pos, x, y, z, 0); renderer = Block.GetInstance(block).renderer; if (renderer != null) { renderer.Render(meshInfo, this, pos, x, y, z); } } } } meshInfo.Build(); return(meshInfo); }
/** Generate the mesh for a specified column */ public MeshBuildInfo[] RenderColumn(Vector2i pos) { MeshBuildInfo[] meshes = new MeshBuildInfo[WORLD_HEIGHT]; for (int h = 0; h < WORLD_HEIGHT; h++) { Thread.Sleep(1); Vector3i chunkPos = new Vector3i(pos.x, h, pos.z); if (h * CHUNK_SIZE > GetMaxHeightAt(pos)) { // don't render if we're past the max height meshes[h] = new MeshBuildInfo(); meshes[h].Build(); } else { // render this chunk meshes[h] = RenderChunk(chunkPos); } } return(meshes); }
public ChunkUpdateTask(Vector3i pos, MeshBuildInfo mesh) { this.pos = pos; this.mesh = mesh; }
public void Render(MeshBuildInfo current, World world, Vector3i chunkPos, int x, int y, int z) { SingleMeshBuildInfo transparent = current.transparent; float height = amount * .85f; Vector3 center = new Vector3(.5f, height * .5f, .5f); Vector3 size = new Vector3(1, height, 1); ushort def = Block.DIRT; byte[] l = new byte[9]; Color blockColor = color; int xx, yy, zz; if (world.GetBlockAt(chunkPos, x, y + 1, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y + 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y + 1, z, 0); } CubeRenderHelper.CubeTop(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x, y - 1, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y - 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y - 1, z, 0); } CubeRenderHelper.CubeBottom(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x + 1, y, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x + 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x + 1, y, z, 0); } CubeRenderHelper.CubeEast(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x - 1, y, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x - 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x - 1, y, z, 0); } CubeRenderHelper.CubeWest(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x, y, z + 1, def) != block) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z + 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z + 1, 0); } CubeRenderHelper.CubeNorth(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x, y, z - 1, def) != block) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z - 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z - 1, 0); } CubeRenderHelper.CubeSouth(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } }
/** Render a chunk and queue it for update */ public void PerformRenderTask(ChunkRenderTask task) { MeshBuildInfo info = RenderChunk(task.pos); lock (updateQueue) updateQueue.Add(new ChunkUpdateTask(task.pos, info)); }
public void Render(MeshBuildInfo current, World world, Vector3i chunkPos, int x, int y, int z) { SingleMeshBuildInfo opaque = current.opaque; Vector3 center = Vector3.one / 2f; Vector3 size = Vector3.one; ushort def = Block.DIRT; byte[] l = new byte[9]; Color blockColor = color; int xx, yy, zz; if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y + 1, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y + 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y + 1, z, 0); } CubeRenderHelper.CubeTop(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y - 1, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y - 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y - 1, z, 0); } CubeRenderHelper.CubeBottom(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x + 1, y, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x + 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x + 1, y, z, 0); } CubeRenderHelper.CubeEast(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x - 1, y, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x - 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x - 1, y, z, 0); } CubeRenderHelper.CubeWest(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y, z + 1, def)).opaque) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z + 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z + 1, 0); } CubeRenderHelper.CubeNorth(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y, z - 1, def)).opaque) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z - 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z - 1, 0); } CubeRenderHelper.CubeSouth(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } }