public override void GenerateChunk(Bounds area, long seed, IVoxelDataSource <VoxelData> dataSource) { int xvol = (int)area.size.x; int yvol = (int)area.size.y; int zvol = (int)area.size.z; int xoff = (int)area.min.x; int yoff = (int)area.min.y; int zoff = (int)area.min.z; Block grass = Game.BlockRegistry["Grass"]; Block dirt = Game.BlockRegistry["Dirt"]; Block stone = Game.BlockRegistry["Stone"]; for (int xx = 0; xx < xvol; xx++) { for (int zz = 0; zz < zvol; zz++) { int landHeight = (int)System.Math.Floor(Perlin.GetValue(xx + xoff, 0, zz + zoff) * HeightHalfDiff + HeightHalfDiff + MinHeight); int dirtHeight = (int)System.Math.Floor((Hash(xx + xoff, zz + zoff) >> 8 & 0xf) / 15f * 3) + 1; for (int yy = 0; yy < yvol; yy++) { if (yoff == 0 && yy == 0) { // We don't want to fall through the world now do we? dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)stone.Id); } else if (yoff + yy > landHeight) { continue; } if (yoff + yy == landHeight) { dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)grass.Id); } else if (yoff + yy >= landHeight - dirtHeight) { dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)dirt.Id); } else { dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)stone.Id); } } } } }
public override void GenerateChunk(Bounds area, long seed, IVoxelDataSource <VoxelData> dataSource) { var xvol = (int)area.size.x; var zvol = (int)area.size.z; var xoff = (int)area.min.x; var zoff = (int)area.min.z; for (var xx = 0; xx < xvol; xx++) { for (var yy = 0; yy < zvol; yy++) { for (var d = 0; d <= Height; d++) { dataSource.Set(xx + xoff, d, yy + zoff, new VoxelData() { Material = (byte)ToGenerate.Id }); } } } }
public void GenerateMesh(IVoxelDataSource<VoxelData> data, Bounds cellSize, out SimpleMesh mesh) { mesh = new SimpleMesh(); mesh.RenderMaterial = Game.BlockRegistry.AtlasMaterial; int xvol = (int)cellSize.size.x; int yvol = (int)cellSize.size.y; int zvol = (int)cellSize.size.y; int xoff = (int)cellSize.min.x; int yoff = (int)cellSize.min.y; int zoff = (int)cellSize.min.z; //Determine how many distinct blocks we have in this chunk //VoxelData[, ,] chunkData = data.SampleSpace(cellSize); for (int x = 0; x < xvol; x++) { for (int y = 0; y < yvol; y++) { for (int z = 0; z < zvol; z++) { int xx = xoff + x; int yy = yoff + y; int zz = zoff + z; int id = data.Sample(xx, yy, zz); if (id <= 0) continue; Vector3 vec = new Vector3(x, y, z); //Add Back Face if (data.Sample(xx, yy, zz + 1) <= 0) { AddMesh(ref mesh, 1, vec, id); } //Add Front Face if (data.Sample(xx, yy, zz - 1) <= 0) { AddMesh(ref mesh, 0, vec, id); } //Add Top Face if (data.Sample(xx, yy + 1, zz) <= 0) { AddMesh(ref mesh, 4, vec, id); } //Add Bottom Face if (data.Sample(xx, yy - 1, zz) <= 0) { AddMesh(ref mesh, 5, vec, id); } //Add Left Face if (data.Sample(xx + 1, yy, zz) <= 0) { AddMesh(ref mesh, 3, vec, id); } //Add Right Face if (data.Sample(xx - 1, yy, zz) <= 0) { AddMesh(ref mesh, 2, vec, id); } } } } }
public void GenerateMesh(IVoxelDataSource <VoxelData> data, Bounds cellSize, out SimpleMesh mesh) { mesh = new SimpleMesh(); mesh.RenderMaterial = Game.BlockRegistry.AtlasMaterial; int xvol = (int)cellSize.size.x; int yvol = (int)cellSize.size.y; int zvol = (int)cellSize.size.y; int xoff = (int)cellSize.min.x; int yoff = (int)cellSize.min.y; int zoff = (int)cellSize.min.z; //Determine how many distinct blocks we have in this chunk //VoxelData[, ,] chunkData = data.SampleSpace(cellSize); for (int x = 0; x < xvol; x++) { for (int y = 0; y < yvol; y++) { for (int z = 0; z < zvol; z++) { int xx = xoff + x; int yy = yoff + y; int zz = zoff + z; int id = data.Sample(xx, yy, zz); if (id <= 0) { continue; } Vector3 vec = new Vector3(x, y, z); //Add Back Face if (data.Sample(xx, yy, zz + 1) <= 0) { AddMesh(ref mesh, 1, vec, id); } //Add Front Face if (data.Sample(xx, yy, zz - 1) <= 0) { AddMesh(ref mesh, 0, vec, id); } //Add Top Face if (data.Sample(xx, yy + 1, zz) <= 0) { AddMesh(ref mesh, 4, vec, id); } //Add Bottom Face if (data.Sample(xx, yy - 1, zz) <= 0) { AddMesh(ref mesh, 5, vec, id); } //Add Left Face if (data.Sample(xx + 1, yy, zz) <= 0) { AddMesh(ref mesh, 3, vec, id); } //Add Right Face if (data.Sample(xx - 1, yy, zz) <= 0) { AddMesh(ref mesh, 2, vec, id); } } } } }
public void Awake() { this.tag = "World"; dataSource = new InMemoryDataSource<VoxelData>(); Debug.Log("Awake called on world, datasource set to: " + dataSource.ToString()); }
public void Awake() { this.tag = "World"; dataSource = new InMemoryDataSource <VoxelData>(); Debug.Log("Awake called on world, datasource set to: " + dataSource.ToString()); }
public void GenerateMesh(IVoxelDataSource <VoxelData> source, Bounds bounds, out SimpleMesh mesh) { mesh = new SimpleMesh { RenderMaterial = Chunk.mcMaterial }; // Create a new bounds object for the volume to work with Bounds volume = new Bounds(); volume.SetMinMax(bounds.min, bounds.max + new Vector3(1, 1, 1)); VoxelData[, ,] data = source.SampleSpace(volume); int index; IEnumerable <VoxelData> distinctElements = data.Cast <VoxelData>().Where(v => v.Data > 0).Distinct(); int numTypes = distinctElements.Count(); Dictionary <int, int> indices = new Dictionary <int, int>(); for (int i = 0; i < numTypes; i++) { int blockID = distinctElements.First().Material; SimpleMesh submesh = new SimpleMesh { RenderMaterial = Game.BlockRegistry[blockID].Data.RenderMaterial }; mesh.Submeshes.Add(submesh); indices[blockID] = i; distinctElements = distinctElements.Skip(1); } SimpleMesh s = new SimpleMesh { RenderMaterial = Game.BlockRegistry[1].Data.RenderMaterial }; mesh.Submeshes.Add(s); int[] idxs = new int[numTypes + 1]; for (int x = 0; x < volume.size.x - 1; x++) { for (int y = 0; y < volume.size.y - 1; y++) { for (int z = 0; z < volume.size.z - 1; z++) { int cubeIndex = 0; if (data[x, y, z] != 0) { cubeIndex |= 16; } if (data[x + 1, y, z] != 0) { cubeIndex |= 32; } if (data[x, y + 1, z] != 0) { cubeIndex |= 1; } if (data[x + 1, y + 1, z] != 0) { cubeIndex |= 2; } if (data[x, y, z + 1] != 0) { cubeIndex |= 128; } if (data[x + 1, y, z + 1] != 0) { cubeIndex |= 64; } if (data[x, y + 1, z + 1] != 0) { cubeIndex |= 8; } if (data[x + 1, y + 1, z + 1] != 0) { cubeIndex |= 4; } int inx = data[x, y, z]; if (inx <= 0) { int dx = (cubeIndex & 32) == 32 ? 1 : 0; int dy = (cubeIndex & 1) == 1 ? 1 : 0; int dz = (cubeIndex & 128) == 128 ? 1 : 0; inx = data[x + dx, y + dy, z + dz]; } SimpleMesh m = mesh.Submeshes[numTypes]; int l = numTypes; if (inx > 0) { m = mesh.Submeshes[indices[inx]]; l = indices[inx]; } int edgeVal = GenericTable.MC_Edges[cubeIndex]; if (edgeVal == 0) { continue; } Vector3[] points = new Vector3[12]; if ((edgeVal & 1) > 0) { points[0] = new Vector3(x + 0.5f, y + 1, z); } if ((edgeVal & 2) > 0) { points[1] = new Vector3(x + 1, y + 1, z + 0.5f); } if ((edgeVal & 4) > 0) { points[2] = new Vector3(x + 0.5f, y + 1, z + 1); } if ((edgeVal & 8) > 0) { points[3] = new Vector3(x, y + 1, z + 0.5f); } if ((edgeVal & 16) > 0) { points[4] = new Vector3(x + 0.5f, y, z); } if ((edgeVal & 32) > 0) { points[5] = new Vector3(x + 1, y, z + 0.5f); } if ((edgeVal & 64) > 0) { points[6] = new Vector3(x + 0.5f, y, z + 1); } if ((edgeVal & 128) > 0) { points[7] = new Vector3(x, y, z + 0.5f); } if ((edgeVal & 256) > 0) { points[8] = new Vector3(x, y + 0.5f, z); } if ((edgeVal & 512) > 0) { points[9] = new Vector3(x + 1, y + 0.5f, z); } if ((edgeVal & 1024) > 0) { points[10] = new Vector3(x + 1, y + 0.5f, z + 1); } if ((edgeVal & 2048) > 0) { points[11] = new Vector3(x, y + 0.5f, z + 1); } short[] tris = GenericTable.MC_Triangles[cubeIndex]; for (int i = 0; tris[i] != -1; i += 3) { Vector2 uv1 = new Vector2(points[tris[i]].x, points[tris[i]].z); Vector2 uv2 = new Vector2(points[tris[i + 2]].x, points[tris[i + 2]].z); Vector2 uv3 = new Vector2(points[tris[i + 1]].x, points[tris[i + 1]].z); index = idxs[l]; m.Vertices.Add(points[tris[i]]); m.Vertices.Add(points[tris[i + 2]]); m.Vertices.Add(points[tris[i + 1]]); m.UV1.Add(uv1); m.UV1.Add(uv2); m.UV1.Add(uv3); m.Triangles.Add(index); m.Triangles.Add(index + 1); m.Triangles.Add(index + 2); idxs[l] += 3; } } } } }
public abstract void GenerateChunk(Bounds area, long seed, IVoxelDataSource <T> dataSource);