public static void ChunkFillUpdate(Chunk chunk, Voxel voxel) { switch (chunk.fillType) { case Chunk.FillType.Empty: if (voxel.IsSolid()) { chunk.fillType = Chunk.FillType.Mixed; } break; case Chunk.FillType.Solid: if (!voxel.IsSolid()) { chunk.fillType = Chunk.FillType.Mixed; } break; case Chunk.FillType.Null: chunk.fillType = voxel.IsSolid() ? Chunk.FillType.Solid : Chunk.FillType.Empty; break; } }
private static Mesh BasicMesh() { TerrainGeneratorBase terrainGenerator = chunk.voxelEngineManager.terrainGenerator; int index = -1; for (int x = 0; x < Chunk.SIZE; x++) { for (int y = 0; y < Chunk.SIZE; y++) { for (int z = 0; z < Chunk.SIZE; z++) { Voxel voxel = chunk.voxelData[++index]; Voxel left = GetAdjVoxelLeft(index, x); Voxel down = GetAdjVoxelDown(index, y); Voxel back = GetAdjVoxelBack(index, z); if (voxel.IsSolid()) { Color32 color = new Color32(); bool colorInit = false; // Left if (!left.IsSolid()) { colorInit = true; color = terrainGenerator.DensityColor(voxel); quads.Add(new Quad( new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), new Vector3(x - 0.5f, y + 0.5f, z + 0.5f), new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), color, Direction.Left)); } // Down if (!down.IsSolid()) { if (!colorInit) { colorInit = true; color = terrainGenerator.DensityColor(voxel); } quads.Add(new Quad( new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), new Vector3(x + 0.5f, y - 0.5f, z + 0.5f), new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), color, Direction.Down)); } // Back if (!back.IsSolid()) { if (!colorInit) { color = terrainGenerator.DensityColor(voxel); } quads.Add(new Quad( new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), new Vector3(x + 0.5f, y + 0.5f, z - 0.5f), new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), color, Direction.Back)); } } else // Voxel not solid { // Left if (left.IsSolid()) { quads.Add(new Quad( new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), new Vector3(x - 0.5f, y + 0.5f, z + 0.5f), new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), terrainGenerator.DensityColor(left), Direction.Right)); } // Down if (down.IsSolid()) { quads.Add(new Quad( new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), new Vector3(x + 0.5f, y - 0.5f, z + 0.5f), new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), terrainGenerator.DensityColor(down), Direction.Up)); } // Back if (back.IsSolid()) { quads.Add(new Quad( new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), new Vector3(x + 0.5f, y + 0.5f, z - 0.5f), new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), terrainGenerator.DensityColor(back), Direction.Forward)); } } } } } if (quads.Count == 0) { return(null); } Vector3[] verts = new Vector3[quads.Count * 4]; Vector3[] normals = new Vector3[quads.Count * 4]; //Vector2[] uvs = new Vector2[quads.Count * 4]; Color32[] colors = new Color32[quads.Count * 4]; int[] tris = new int[quads.Count * 6]; int vertIndex = 0; int triIndex = 0; foreach (Quad quad in quads) { tris[triIndex++] = vertIndex; tris[triIndex++] = vertIndex + 1; tris[triIndex++] = vertIndex + 2; tris[triIndex++] = vertIndex; tris[triIndex++] = vertIndex + 2; tris[triIndex++] = vertIndex + 3; colors[vertIndex] = quad.color; normals[vertIndex] = directionNormals[(int)quad.direction]; verts[vertIndex++] = quad.v0; colors[vertIndex] = quad.color; normals[vertIndex] = directionNormals[(int)quad.direction]; verts[vertIndex++] = quad.v1; colors[vertIndex] = quad.color; normals[vertIndex] = directionNormals[(int)quad.direction]; verts[vertIndex++] = quad.v2; colors[vertIndex] = quad.color; normals[vertIndex] = directionNormals[(int)quad.direction]; verts[vertIndex++] = quad.v3; } Mesh mesh = new Mesh { vertices = verts, normals = normals, triangles = tris, colors32 = colors }; return(mesh); }