public void UpdateChunk(Chunk chunk) { for (int i = 0; i < VertexGridSize; i++) { } // Clear the vertices, triangles and vertex count. vertices.Clear(); triangles.Clear(); vertexCount = 0; activeChunk = chunk; VoxPos position = new VoxPos(); for (position.X = 0; position.X < Chunk.Size; position.X++) { for (position.Z = 0; position.Z < Chunk.Size; position.Z++) { for (position.Y = 0; position.Y < Chunk.Size; position.Y++) { March(position); } } } Vector3[] verts = new Vector3[vertices.Count]; uint[] tris = new uint[triangles.Count]; // Set the new mesh on the chunk. // chunk.SetMesh(vertices.ToArray(), triangles.ToArray()); }
private int getVertex(VoxPos position) { int index = position.X + position.Y * LookupSize + position.Z * LookupSize * LookupSize; // If the vert does not exist, add it. if (vertLookup[index] < 0) { vertLookup[index] = verticesCount++; vertices.Push(new Chunk.Vertex(position)); } return(vertLookup[index]); }
public float GetAt(VoxPos pos) { Vector2 position = new Vector2(pos.X, pos.Z) * WorldScale; float height = NoiseFunction.GetAt(position, persistance, lacunarity, octaves) * HeightScale; if (height > pos.Z) { return(1f); } else { return(0f); } }
int vertexIndex(VoxPos position) { // Check that the position is within the range of the vertex grid. if ( position.X >= VertexGridSize || position.X < 0 || position.Y >= VertexGridSize || position.Y < 0 || position.Z >= VertexGridSize || position.Z < 0 ) { Log.Error(this, $"Vertex position is out of range {position}"); } return(position.X + position.Y * VertexGridSize + position.Z * VertexGridSize * VertexGridSize); }
bool isSolid(VoxPos position) { // return false; // If we are not on the surface of a chunk just quickly get the value. if (position.X >= 0 && position.X < Chunk.Size && position.Y >= 0 && position.Y < Chunk.Size && position.Z >= 0 && position.Z < Chunk.Size) { return(activeChunk.GetSolid(position)); } return(true); }
public VoxPos[] GetNeighbors(Chunk chunk) { Stack <VoxPos> neighbors = new Stack <VoxPos>(); for (int i = 0; i < 6; i++) { VoxPos pos = chunk.Position + VoxPos.AllDirections[i]; if (loadedChunks.ContainsKey(pos)) { neighbors.Push(pos); } } return(neighbors.ToArray()); }
public void SetSolid(VoxPos position, bool value, bool autoRebuild) { if (!autoRebuild) { Solid[position.X + position.Y * Chunk.Size + position.Z * Chunk.Size * Chunk.Size] = value; } else { bool oldVal = GetSolid(position); Solid[position.X + position.Y * Chunk.Size + position.Z * Chunk.Size * Chunk.Size] = value; if (oldVal != value) { IsDirty = true; } } }
public Chunk(VoxPos position, IVoxelProvider provider, ChunkManager manager) { Position = position; if (MeshMaker == null) { MeshMaker = new MeshMaker(); } var values = provider.GetChunk(Position); for (int i = 0; i < Solid.Length; i++) { Solid[i] = values[i] > 0.5f; } this.manager = manager; }
void March(VoxPos pos) { // Left face. if (!isSolid(pos + VoxPos.Left)) { triangle(pos, 1, 5, 4); triangle(pos, 1, 4, 0); } // Right face. if (!isSolid(pos + VoxPos.Right)) { // triangle(pos, 3, 7, 6); // triangle(pos, 3, 6, 2); } // Top face. if (!isSolid(pos + VoxPos.Up)) { // triangle(pos, 6, 7, 4); // triangle(pos, 6, 4, 5); } // Bottom face. if (!isSolid(pos + VoxPos.Down)) { // triangle(pos, 3, 2, 1); // triangle(pos, 3, 1, 0); } // Front face. if (!isSolid(pos + VoxPos.Forward)) { // triangle(pos, 4, 7, 3); // triangle(pos, 4, 3, 0); } // Back face. if (!isSolid(pos + VoxPos.Backwards)) { // triangle(pos, 6, 5, 1); // triangle(pos, 6, 1, 2); } }
void CreateImage(int width, int height, string filename) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height); VoxPos pos = new VoxPos(); float h = float.MinValue, l = float.MaxValue; int octaves = 6; float persistance = 0.5f; float lacunarity = 3f; float scale = GetScale(persistance, lacunarity, octaves); for (pos.X = 0; pos.X < width; pos.X++) { for (pos.Y = 0; pos.Y < width; pos.Y++) { Vector2 noisePos = new Vector2(pos.X, pos.Y) * 0.04f; float noise = GetAt(noisePos, persistance, lacunarity, octaves) * scale; if (noise > h) { h = noise; } else if (noise < l) { l = noise; } byte value = (byte)(noise * byte.MaxValue); bmp.SetPixel(pos.X, pos.Y, System.Drawing.Color.FromArgb(value, value, value)); } } Log.Debug(this, $"min {l} max {h}"); bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Png); }
public float[] GetChunk(VoxPos chunkPos) { var values = new float[Chunk.Size * Chunk.Size * Chunk.Size]; var internalPos = new VoxPos(); for (internalPos.X = 0; internalPos.X < Chunk.Size; internalPos.X++) { for (internalPos.Z = 0; internalPos.Z < Chunk.Size; internalPos.Z++) { Vector2 position = new Vector2(internalPos.X + (chunkPos.X * Chunk.Size), internalPos.Z + (chunkPos.Z * Chunk.Size)) * WorldScale; float height = NoiseFunction.GetAt(position, persistance, lacunarity, octaves); for (internalPos.Y = 0; internalPos.Y < height; internalPos.Y++) { VoxPos pos = internalPos + (chunkPos * Chunk.Size); values[pos.X + pos.Y * Chunk.Size + pos.Z * Chunk.Size * Chunk.Size] = 1f; } } } return(values); }
public bool GetSolid(VoxPos position) { return(Solid[position.X + position.Y * Chunk.Size + position.Z * Chunk.Size * Chunk.Size]); }
public bool TryGetChunk(VoxPos position, out Chunk chunk) => loadedChunks.TryGetValue(position, out chunk);
public Chunk GetChunk(VoxPos position) => loadedChunks.GetValueOrDefault(position);
// public void Cube(VoxPos pos, Faces visiableFaces) { // if(visiableFaces.HasFlag(Faces.NegX)) { // triangle(pos, 1, 5, 4); // triangle(pos, 1, 4, 0); // } // if(visiableFaces.HasFlag(Faces.PosX)) { // triangle(pos, 3, 7, 6); // triangle(pos, 3, 6, 2); // } // if(visiableFaces.HasFlag(Faces.NegY)){ // triangle(pos, 3, 2, 1); // triangle(pos, 3, 1, 0); // } // if(visiableFaces.HasFlag(Faces.PosY)) { // triangle(pos, 6, 7, 4); // triangle(pos, 6, 4, 5); // } // if(visiableFaces.HasFlag(Faces.NegZ)) { // triangle(pos, 6, 5, 1); // triangle(pos, 6, 1, 2); // } // if(visiableFaces.HasFlag(Faces.PosZ)) { // triangle(pos, 4, 7, 3); // triangle(pos, 4, 3, 0); // } // } private void triangle(VoxPos position, int a, int b, int c) { triangles.Push(getVertex(position + corner[a])); triangles.Push(getVertex(position + corner[b])); triangles.Push(getVertex(position + corner[c])); }