protected void CreateVerticies(Color color) { m_vertices = new ExtendedVertex[m_relativeTransforms.Count * 6]; // Calculate the position of the vertices on the top face. Vector3 topLeftFront = new Vector3(-0.5f * Width, 0.5f * Height, 0.0f); Vector3 topLeftBack = new Vector3(-0.5f * Width, 0.5f * Height, 0.0f); Vector3 topRightFront = new Vector3(0.5f * Width, 0.5f * Height, 0.0f); Vector3 topRightBack = new Vector3(0.5f * Width, 0.5f * Height, 0.0f); // Calculate the position of the vertices on the bottom face. Vector3 btmLeftFront = new Vector3(-0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmLeftBack = new Vector3(-0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmRightFront = new Vector3(0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmRightBack = new Vector3(0.5f * Width, -0.5f * Height, 0.0f); for (int i = 0; i < m_relativeTransforms.Count; i++) { // Add the vertices for the FRONT face. m_vertices[i * 6 + 0] = new ExtendedVertex(Vector3.Transform(topLeftFront, m_relativeTransforms[i]), m_tints[i], UVs.m_uvs[0], UVs.Bounds); m_vertices[i * 6 + 1] = new ExtendedVertex(Vector3.Transform(btmLeftFront, m_relativeTransforms[i]), m_tints[i], UVs.m_uvs[1], UVs.Bounds); m_vertices[i * 6 + 2] = new ExtendedVertex(Vector3.Transform(topRightFront, m_relativeTransforms[i]), m_tints[i], UVs.m_uvs[2], UVs.Bounds); m_vertices[i * 6 + 3] = new ExtendedVertex(Vector3.Transform(btmLeftFront, m_relativeTransforms[i]), m_tints[i], UVs.m_uvs[3], UVs.Bounds); m_vertices[i * 6 + 4] = new ExtendedVertex(Vector3.Transform(btmRightFront, m_relativeTransforms[i]), m_tints[i], UVs.m_uvs[4], UVs.Bounds); m_vertices[i * 6 + 5] = new ExtendedVertex(Vector3.Transform(topRightFront, m_relativeTransforms[i]), m_tints[i], UVs.m_uvs[5], UVs.Bounds); } //Console.Out.WriteLine("There are {0}", m_relativeTransforms.Count); }
private void CreateVerticies(Color color) { Vertices = new ExtendedVertex[RelativeTransforms.Count * 4]; Vector3 topLeftFront = new Vector3(-0.5f * Width, 0.5f * Height, 0.0f); Vector3 topRightFront = new Vector3(0.5f * Width, 0.5f * Height, 0.0f); Vector3 btmLeftFront = new Vector3(-0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmRightFront = new Vector3(0.5f * Width, -0.5f * Height, 0.0f); short[] indices = new short[RelativeTransforms.Count * 6]; for(int i = 0; i < RelativeTransforms.Count; i++) { int vertOffset = i*4; Vertices[vertOffset + 0] = new ExtendedVertex(Vector3.Transform(topLeftFront, RelativeTransforms[i]), Tints[i], UVs.UVs[0], UVs.Bounds); Vertices[vertOffset + 1] = new ExtendedVertex(Vector3.Transform(btmLeftFront, RelativeTransforms[i]), Tints[i], UVs.UVs[1], UVs.Bounds); Vertices[vertOffset + 2] = new ExtendedVertex(Vector3.Transform(topRightFront, RelativeTransforms[i]), Tints[i], UVs.UVs[2], UVs.Bounds); Vertices[vertOffset + 3] = new ExtendedVertex(Vector3.Transform(btmRightFront, RelativeTransforms[i]), Tints[i], UVs.UVs[3], UVs.Bounds); int indOffset = i*6; indices[indOffset + 0] = (short)(vertOffset + 1); indices[indOffset + 1] = (short)(vertOffset + 0); indices[indOffset + 2] = (short)(vertOffset + 2); indices[indOffset + 3] = (short)(vertOffset + 1); indices[indOffset + 4] = (short)(vertOffset + 2); indices[indOffset + 5] = (short)(vertOffset + 3); } IndexBuffer = new IndexBuffer(GameState.Game.GraphicsDevice, typeof(short), RelativeTransforms.Count * 6, BufferUsage.None); IndexBuffer.SetData(indices); }
protected void CreateVerticies(Color color) { m_vertices = new ExtendedVertex[NUM_VERTICES]; // Calculate the position of the vertices on the top face. Vector3 topLeftFront = new Vector3(-0.5f * Width, 0.5f * Height, 0.0f); Vector3 topLeftBack = new Vector3(-0.5f * Width, 0.5f * Height, 0.0f); Vector3 topRightFront = new Vector3(0.5f * Width, 0.5f * Height, 0.0f); Vector3 topRightBack = new Vector3(0.5f * Width, 0.5f * Height, 0.0f); // Calculate the position of the vertices on the bottom face. Vector3 btmLeftFront = new Vector3(-0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmLeftBack = new Vector3(-0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmRightFront = new Vector3(0.5f * Width, -0.5f * Height, 0.0f); Vector3 btmRightBack = new Vector3(0.5f * Width, -0.5f * Height, 0.0f); // Add the vertices for the FRONT face. m_vertices[0] = new ExtendedVertex(topLeftFront, color, UVs.m_uvs[0], UVs.Bounds); m_vertices[1] = new ExtendedVertex(btmLeftFront, color, UVs.m_uvs[1], UVs.Bounds); m_vertices[2] = new ExtendedVertex(topRightFront, color, UVs.m_uvs[2], UVs.Bounds); m_vertices[3] = new ExtendedVertex(btmLeftFront, color, UVs.m_uvs[3], UVs.Bounds); m_vertices[4] = new ExtendedVertex(btmRightFront, color, UVs.m_uvs[4], UVs.Bounds); m_vertices[5] = new ExtendedVertex(topRightFront, color, UVs.m_uvs[5], UVs.Bounds); }
public void InitializeFromChunk(VoxelChunk chunk, GraphicsDevice graphics) { if (chunk == null) { return; } rebuildMutex.WaitOne(); if(isRebuilding) { rebuildMutex.ReleaseMutex(); return; } isRebuilding = true; rebuildMutex.ReleaseMutex(); accumulatedVertices.Clear(); accumulatedIndices.Clear(); faceExists.Clear(); drawFace.Clear(); Voxel v = chunk.MakeVoxel(0, 0, 0); Voxel voxelOnFace = chunk.MakeVoxel(0, 0, 0); Voxel[] manhattanNeighbors = new Voxel[4]; for(int x = 0; x < chunk.SizeX; x++) { for(int y = 0; y < Math.Min(chunk.Manager.ChunkData.MaxViewingLevel + 1, chunk.SizeY); y++) { for(int z = 0; z < chunk.SizeZ; z++) { v.GridPosition = new Vector3(x, y, z); if((v.IsExplored && v.IsEmpty) || !v.IsVisible) { continue; } BoxPrimitive primitive = VoxelLibrary.GetPrimitive(v.Type); if (v.IsExplored && primitive == null) continue; else if (!v.IsExplored) { primitive = VoxelLibrary.GetPrimitive("Bedrock"); } BoxPrimitive.BoxTextureCoords uvs = primitive.UVs; if (v.Type.HasTransitionTextures) { uvs = v.ComputeTransitionTexture(manhattanNeighbors); } Voxel worldVoxel = new Voxel(); for(int i = 0; i < 6; i++) { BoxFace face = (BoxFace) i; Vector3 delta = FaceDeltas[face]; faceExists[face] = chunk.IsCellValid(x + (int) delta.X, y + (int) delta.Y, z + (int) delta.Z); drawFace[face] = true; if(faceExists[face]) { voxelOnFace.GridPosition = new Vector3(x + (int) delta.X, y + (int) delta.Y, z + (int) delta.Z); drawFace[face] = voxelOnFace.IsEmpty || !voxelOnFace.IsVisible || (voxelOnFace.Type.CanRamp && voxelOnFace.RampType != RampType.None && IsSideFace(face) && ShouldDrawFace(face, voxelOnFace.RampType, v.RampType)); } else { bool success = chunk.Manager.ChunkData.GetNonNullVoxelAtWorldLocation(new Vector3(x + (int) delta.X, y + (int) delta.Y, z + (int) delta.Z) + chunk.Origin, ref worldVoxel); drawFace[face] = !success || worldVoxel.IsEmpty || !worldVoxel.IsVisible || (worldVoxel.Type.CanRamp && worldVoxel.RampType != RampType.None && IsSideFace(face) && ShouldDrawFace(face, worldVoxel.RampType, v.RampType)); } } for(int i = 0; i < 6; i++) { BoxFace face = (BoxFace) i; if(!drawFace[face]) { continue; } int faceIndex = 0; int faceCount = 0; int vertexIndex = 0; int vertexCount = 0; primitive.GetFace(face, uvs, out faceIndex, out faceCount, out vertexIndex, out vertexCount); Vector2 texScale = uvs.Scales[i]; int indexOffset = accumulatedVertices.Count; for (int vertOffset = 0; vertOffset < vertexCount; vertOffset++) { ExtendedVertex vert = primitive.Vertices[vertOffset + vertexIndex]; VoxelVertex bestKey = VoxelChunk.GetNearestDelta(vert.Position); Color color = v.Chunk.Data.GetColor(x, y, z, bestKey); Vector3 offset = Vector3.Zero; Vector2 texOffset = Vector2.Zero; if(v.Type.CanRamp && ShouldRamp(bestKey, v.RampType)) { offset = new Vector3(0, -v.Type.RampSize, 0); if(face != BoxFace.Top && face != BoxFace.Bottom) { texOffset = new Vector2(0, v.Type.RampSize * (texScale.Y)); } } ExtendedVertex newVertex = new ExtendedVertex((vert.Position + v.Position + VertexNoise.GetNoiseVectorFromRepeatingTexture(vert.Position + v.Position) + offset), color, uvs.Uvs[vertOffset + vertexIndex] + texOffset, uvs.Bounds[faceIndex / 6]); accumulatedVertices.Add(newVertex); } for (int idx = faceIndex; idx < faceCount + faceIndex; idx++) { int vertexOffset = primitive.Indices[idx]; accumulatedIndices.Add((short)(indexOffset + (vertexOffset - primitive.Indices[faceIndex]))); } } } } } Vertices = new ExtendedVertex[accumulatedVertices.Count]; accumulatedVertices.CopyTo(Vertices); IndexBuffer = new IndexBuffer(graphics, typeof(short), accumulatedIndices.Count, BufferUsage.WriteOnly); IndexBuffer.SetData(accumulatedIndices.ToArray()); ResetBuffer(graphics); isRebuilding = false; //chunk.PrimitiveMutex.WaitOne(); chunk.NewPrimitive = this; chunk.NewPrimitiveReceived = true; //chunk.PrimitiveMutex.ReleaseMutex(); }
protected void CreateVerticies() { m_vertices = new ExtendedVertex[NUM_VERTICES]; // Calculate the position of the vertices on the top face. Vector3 topLeftFront = new Vector3(0.0f, Height, 0.0f); Vector3 topLeftBack = new Vector3(0.0f, Height , Depth); Vector3 topRightFront = new Vector3(Width , Height, 0.0f); Vector3 topRightBack = new Vector3(Width, Height , Depth); ; // Calculate the position of the vertices on the bottom face. Vector3 btmLeftFront = new Vector3(0.0f, 0.0f, 0.0f); Vector3 btmLeftBack = new Vector3(0.0f, 0.0f, Depth); Vector3 btmRightFront = new Vector3(Width, 0.0f, 0.0f); Vector3 btmRightBack = new Vector3(Width, 0.0f, Depth ); // Normal vectors for each face (needed for lighting / display) /* Vector3 normalFront = new Vector3(0.0f, 0.0f, 1.0f) * SideLength; Vector3 normalBack = new Vector3(0.0f, 0.0f, -1.0f) * SideLength; Vector3 normalTop = new Vector3(0.0f, 1.0f, 0.0f) * SideLength; Vector3 normalBottom = new Vector3(0.0f, -1.0f, 0.0f) * SideLength; Vector3 normalLeft = new Vector3(-1.0f, 0.0f, 0.0f) * SideLength; Vector3 normalRight = new Vector3(1.0f, 0.0f, 0.0f) * SideLength; */ // Add the vertices for the FRONT face. m_vertices[0] = new ExtendedVertex(topLeftFront, Color.White, UVs.m_uvs[0], UVs.Bounds[0]); m_vertices[1] = new ExtendedVertex(btmLeftFront, Color.White, UVs.m_uvs[1], UVs.Bounds[0]); m_vertices[2] = new ExtendedVertex(topRightFront, Color.White, UVs.m_uvs[2], UVs.Bounds[0]); m_vertices[3] = new ExtendedVertex(btmLeftFront, Color.White, UVs.m_uvs[3], UVs.Bounds[0]); m_vertices[4] = new ExtendedVertex(btmRightFront, Color.White, UVs.m_uvs[4], UVs.Bounds[0]); m_vertices[5] = new ExtendedVertex(topRightFront, Color.White, UVs.m_uvs[5], UVs.Bounds[0]); // Add the vertices for the BACK face. m_vertices[6] = new ExtendedVertex(topLeftBack, Color.White, UVs.m_uvs[6], UVs.Bounds[1]); m_vertices[7] = new ExtendedVertex(topRightBack, Color.White, UVs.m_uvs[7], UVs.Bounds[1]); m_vertices[8] = new ExtendedVertex(btmLeftBack, Color.White, UVs.m_uvs[8], UVs.Bounds[1]); m_vertices[9] = new ExtendedVertex(btmLeftBack, Color.White, UVs.m_uvs[9], UVs.Bounds[1]); m_vertices[10] = new ExtendedVertex(topRightBack, Color.White, UVs.m_uvs[10], UVs.Bounds[1]); m_vertices[11] = new ExtendedVertex(btmRightBack, Color.White, UVs.m_uvs[11], UVs.Bounds[1]); // Add the vertices for the TOP face. m_vertices[12] = new ExtendedVertex(topLeftFront, Color.White, UVs.m_uvs[12], UVs.Bounds[2]); m_vertices[13] = new ExtendedVertex(topRightBack, Color.White, UVs.m_uvs[13], UVs.Bounds[2]); m_vertices[14] = new ExtendedVertex(topLeftBack, Color.White, UVs.m_uvs[14], UVs.Bounds[2]); m_vertices[15] = new ExtendedVertex(topLeftFront, Color.White, UVs.m_uvs[15], UVs.Bounds[2]); m_vertices[16] = new ExtendedVertex(topRightFront, Color.White, UVs.m_uvs[16], UVs.Bounds[2]); m_vertices[17] = new ExtendedVertex(topRightBack, Color.White, UVs.m_uvs[17], UVs.Bounds[2]); // Add the vertices for the BOTTOM face. m_vertices[18] = new ExtendedVertex(btmLeftFront, Color.White, UVs.m_uvs[18], UVs.Bounds[3]); m_vertices[19] = new ExtendedVertex(btmLeftBack, Color.White, UVs.m_uvs[19], UVs.Bounds[3]); m_vertices[20] = new ExtendedVertex(btmRightBack, Color.White, UVs.m_uvs[20], UVs.Bounds[3]); m_vertices[21] = new ExtendedVertex(btmLeftFront, Color.White, UVs.m_uvs[21], UVs.Bounds[3]); m_vertices[22] = new ExtendedVertex(btmRightBack, Color.White, UVs.m_uvs[22], UVs.Bounds[3]); m_vertices[23] = new ExtendedVertex(btmRightFront, Color.White, UVs.m_uvs[23], UVs.Bounds[3]); // Add the vertices for the LEFT face. m_vertices[24] = new ExtendedVertex(topLeftFront, Color.White, UVs.m_uvs[24], UVs.Bounds[4]); m_vertices[25] = new ExtendedVertex(btmLeftBack, Color.White, UVs.m_uvs[25], UVs.Bounds[4]); m_vertices[26] = new ExtendedVertex(btmLeftFront, Color.White, UVs.m_uvs[26], UVs.Bounds[4]); m_vertices[27] = new ExtendedVertex(topLeftBack, Color.White, UVs.m_uvs[27], UVs.Bounds[4]); m_vertices[28] = new ExtendedVertex(btmLeftBack, Color.White, UVs.m_uvs[28], UVs.Bounds[4]); m_vertices[29] = new ExtendedVertex(topLeftFront, Color.White, UVs.m_uvs[29], UVs.Bounds[4]); // Add the vertices for the RIGHT face. m_vertices[30] = new ExtendedVertex(topRightFront, Color.White, UVs.m_uvs[30], UVs.Bounds[5]); m_vertices[31] = new ExtendedVertex(btmRightFront, Color.White, UVs.m_uvs[31], UVs.Bounds[5]); m_vertices[32] = new ExtendedVertex(btmRightBack, Color.White, UVs.m_uvs[32], UVs.Bounds[5]); m_vertices[33] = new ExtendedVertex(topRightBack, Color.White, UVs.m_uvs[33], UVs.Bounds[5]); m_vertices[34] = new ExtendedVertex(topRightFront, Color.White, UVs.m_uvs[34], UVs.Bounds[5]); m_vertices[35] = new ExtendedVertex(btmRightBack, Color.White, UVs.m_uvs[35], UVs.Bounds[5]); }
private void CreateVerticies() { Indices = new short[36]; Vertices = new ExtendedVertex[NumVertices]; // Calculate the position of the vertices on the top face. Vector3 topLeftFront = new Vector3(0.0f, Height, 0.0f); Vector3 topLeftBack = new Vector3(0.0f, Height, Depth); Vector3 topRightFront = new Vector3(Width, Height, 0.0f); Vector3 topRightBack = new Vector3(Width, Height, Depth); // Calculate the position of the vertices on the bottom face. Vector3 btmLeftFront = new Vector3(0.0f, 0.0f, 0.0f); Vector3 btmLeftBack = new Vector3(0.0f, 0.0f, Depth); Vector3 btmRightFront = new Vector3(Width, 0.0f, 0.0f); Vector3 btmRightBack = new Vector3(Width, 0.0f, Depth); // Normal vectors for each face (needed for lighting / display) // Add the vertices for the FRONT face. Vertices[0] = new ExtendedVertex(topLeftFront, Color.White, UVs.Uvs[0], UVs.Bounds[0]); Vertices[1] = new ExtendedVertex(btmLeftFront, Color.White, UVs.Uvs[1], UVs.Bounds[0]); Vertices[2] = new ExtendedVertex(topRightFront, Color.White, UVs.Uvs[2], UVs.Bounds[0]); Vertices[3] = new ExtendedVertex(btmRightFront, Color.White, UVs.Uvs[3], UVs.Bounds[0]); Indices[0] = 0; Indices[1] = 1; Indices[2] = 2; Indices[3] = 2; Indices[4] = 1; Indices[5] = 3; // Add the vertices for the BACK face. Vertices[4] = new ExtendedVertex(topLeftBack, Color.White, UVs.Uvs[4], UVs.Bounds[1]); Vertices[5] = new ExtendedVertex(topRightBack, Color.White, UVs.Uvs[5], UVs.Bounds[1]); Vertices[6] = new ExtendedVertex(btmLeftBack, Color.White, UVs.Uvs[6], UVs.Bounds[1]); Vertices[7] = new ExtendedVertex(btmRightBack, Color.White, UVs.Uvs[7], UVs.Bounds[1]); Indices[6] = 4; Indices[7] = 5; Indices[8] = 6; Indices[9] = 7; Indices[10] = 6; Indices[11] = 5; // Add the vertices for the TOP face. Vertices[8] = new ExtendedVertex(topLeftFront, Color.White, UVs.Uvs[8], UVs.Bounds[2]); Vertices[9] = new ExtendedVertex(topRightBack, Color.White, UVs.Uvs[9], UVs.Bounds[2]); Vertices[10] = new ExtendedVertex(topLeftBack, Color.White, UVs.Uvs[10], UVs.Bounds[2]); Vertices[11] = new ExtendedVertex(topRightFront, Color.White, UVs.Uvs[11], UVs.Bounds[2]); Indices[12] = 8; Indices[13] = 9; Indices[14] = 10; Indices[15] = 9; Indices[16] = 8; Indices[17] = 11; // Add the vertices for the BOTTOM face. Vertices[12] = new ExtendedVertex(btmLeftFront, Color.White, UVs.Uvs[12], UVs.Bounds[3]); Vertices[13] = new ExtendedVertex(btmLeftBack, Color.White, UVs.Uvs[13], UVs.Bounds[3]); Vertices[14] = new ExtendedVertex(btmRightBack, Color.White, UVs.Uvs[14], UVs.Bounds[3]); Vertices[15] = new ExtendedVertex(btmRightFront, Color.White, UVs.Uvs[15], UVs.Bounds[3]); Indices[18] = 12; Indices[19] = 13; Indices[20] = 14; Indices[21] = 15; Indices[22] = 12; Indices[23] = 14; // Add the vertices for the LEFT face. Vertices[16] = new ExtendedVertex(topLeftFront, Color.White, UVs.Uvs[16], UVs.Bounds[4]); Vertices[17] = new ExtendedVertex(btmLeftBack, Color.White, UVs.Uvs[17], UVs.Bounds[4]); Vertices[18] = new ExtendedVertex(btmLeftFront, Color.White, UVs.Uvs[18], UVs.Bounds[4]); Vertices[19] = new ExtendedVertex(topLeftBack, Color.White, UVs.Uvs[19], UVs.Bounds[4]); Indices[24] = 16; Indices[25] = 17; Indices[26] = 18; Indices[27] = 17; Indices[28] = 16; Indices[29] = 19; // Add the vertices for the RIGHT face. Vertices[20] = new ExtendedVertex(topRightFront, Color.White, UVs.Uvs[20], UVs.Bounds[5]); Vertices[21] = new ExtendedVertex(btmRightFront, Color.White, UVs.Uvs[21], UVs.Bounds[5]); Vertices[22] = new ExtendedVertex(btmRightBack, Color.White, UVs.Uvs[22], UVs.Bounds[5]); Vertices[23] = new ExtendedVertex(topRightBack, Color.White, UVs.Uvs[23], UVs.Bounds[5]); Indices[30] = 20; Indices[31] = 21; Indices[32] = 22; Indices[33] = 23; Indices[34] = 20; Indices[35] = 22; IndexBuffer = new IndexBuffer(GameState.Game.GraphicsDevice, typeof(short), Indices.Length, BufferUsage.WriteOnly); IndexBuffer.SetData(Indices); }
public void InitializeFromChunk(VoxelChunk chunk, GraphicsDevice graphics) { //chunk.PrimitiveMutex.WaitOne(); if(!chunk.IsVisible || IsBuilding) { // chunk.PrimitiveMutex.ReleaseMutex(); return; } IsBuilding = true; //chunk.PrimitiveMutex.ReleaseMutex(); accumulatedVertices.Clear(); faceExists.Clear(); drawFace.Clear(); int[,,] totalDepth = new int[chunk.SizeX, chunk.SizeY, chunk.SizeZ]; for(int x = 0; x < chunk.SizeX; x++) { for(int z = 0; z < chunk.SizeZ; z++) { bool drynessEncountered = false; int previousSum = 0; for(int y = 0; y < chunk.SizeY; y++) { WaterCell cell = chunk.Data.Water[chunk.Data.IndexAt(x, y, z)]; byte waterLevel = cell.WaterLevel; if(cell.Type != LiqType) { waterLevel = 0; } if(drynessEncountered) { if(waterLevel > 0) { drynessEncountered = false; previousSum += waterLevel; totalDepth[x, y, z] = previousSum; } } else { if(waterLevel > 0) { previousSum += waterLevel; totalDepth[x, y, z] = previousSum; } else { drynessEncountered = true; previousSum = 0; totalDepth[x, y, z] = 0; } } } } } int maxY = chunk.SizeY; if(chunk.Manager.ChunkData.Slice == ChunkManager.SliceMode.Y) { maxY = (int) Math.Min(chunk.Manager.ChunkData.MaxViewingLevel + 1, chunk.SizeY); } Voxel myVoxel = chunk.MakeVoxel(0, 0, 0); Voxel vox = chunk.MakeVoxel(0, 0, 0); for(int x = 0; x < chunk.SizeX; x++) { for(int y = 0; y < maxY; y++) { for(int z = 0; z < chunk.SizeZ; z++) { int index = chunk.Data.IndexAt(x, y, z); if(chunk.Data.Water[index].WaterLevel > 0 && chunk.Data.Water[index].Type == LiqType) { bool isTop = false; myVoxel.GridPosition = new Vector3(x, y, z); for(int i = 0; i < 6; i++) { BoxFace face = (BoxFace) i; if(face == BoxFace.Bottom) { continue; } Vector3 delta = faceDeltas[face]; bool success = chunk.Manager.ChunkData.GetVoxel(chunk, new Vector3(x + (int)delta.X, y + (int)delta.Y, z + (int)delta.Z) + chunk.Origin, ref vox); if(success) { if(face == BoxFace.Top) { if(vox.WaterLevel == 0 || y == (int) chunk.Manager.ChunkData.MaxViewingLevel) { drawFace[face] = true; } else { drawFace[face] = false; } } else { if(vox.WaterLevel == 0 && vox.IsEmpty) { drawFace[face] = true; } else { drawFace[face] = false; } bool gotVox = chunk.Manager.ChunkData.GetVoxel(chunk, new Vector3(x, y + 1, z) + chunk.Origin, ref vox); isTop = !gotVox || vox.IsEmpty || vox.WaterLevel == 0; } } else { drawFace[face] = true; } if(!drawFace[face]) { continue; } IEnumerable<ExtendedVertex> vertices = CreateWaterFace(myVoxel, face, chunk, x, y, z, totalDepth[x, y, z], isTop); foreach(ExtendedVertex newVertex in vertices.Select(vertex => new ExtendedVertex(vertex.Position + VertexNoise.GetRandomNoiseVector(vertex.Position), vertex.Color, vertex.TextureCoordinate, vertex.TextureBounds))) { accumulatedVertices.Add(newVertex); } } } } } } try { ExtendedVertex[] vertex = new ExtendedVertex[accumulatedVertices.Count]; for(int i = 0; i < accumulatedVertices.Count; i++) { vertex[i] = accumulatedVertices[i]; } Vertices = vertex; chunk.PrimitiveMutex.WaitOne(); ResetBuffer(graphics); chunk.PrimitiveMutex.ReleaseMutex(); } catch(System.Threading.AbandonedMutexException e) { Console.Error.WriteLine(e.Message); } IsBuilding = false; }
private static IEnumerable<ExtendedVertex> CreateWaterFace(Voxel voxel, BoxFace face, VoxelChunk chunk, int x, int y, int z, int totalDepth, bool top) { List<ExtendedVertex> toReturn = new List<ExtendedVertex>(); int idx = 0; int c = 0; int vertOffset = 0; int numVerts = 0; m_canconicalPrimitive.GetFace(face, m_canconicalPrimitive.UVs, out idx, out c, out vertOffset, out numVerts); for (int i = idx; i < idx + c; i++) { toReturn.Add(m_canconicalPrimitive.Vertices[m_canconicalPrimitive.Indices[i]]); } Vector3 origin = chunk.Origin + new Vector3(x, y, z); List<Voxel> neighborsVertex = new List<Voxel>(); for(int i = 0; i < toReturn.Count; i ++) { VoxelVertex currentVertex = VoxelChunk.GetNearestDelta(toReturn[i].Position); chunk.GetNeighborsVertex(currentVertex, voxel, neighborsVertex); int index = chunk.Data.IndexAt(x, y, z); float averageWaterLevel = chunk.Data.Water[index].WaterLevel; float count = 1.0f; float emptyNeighbors = 0.0f; foreach(byte level in neighborsVertex.Select(vox => vox.WaterLevel)) { averageWaterLevel += level; count++; if(level < 1) { emptyNeighbors++; } } averageWaterLevel = averageWaterLevel / count; float averageWaterHeight = (float) averageWaterLevel / 255.0f; float puddleness = 0; Vector2 uv; float foaminess = emptyNeighbors / count; if(foaminess <= 0.5f) { foaminess = 0.0f; } if(totalDepth < 5) { foaminess = 0.75f; puddleness = 0; uv = new Vector2((toReturn[i].Position.X + origin.X) / 80.0f, (toReturn[i].Position.Z + origin.Z) / 80.0f); } else { uv = new Vector2((toReturn[i].Position.X + origin.X) / 80.0f, (toReturn[i].Position.Z + origin.Z) / 80.0f); } Vector4 bounds = new Vector4(0, 0, 1, 1); if(chunk.Data.Water[index].IsFalling || !top) { averageWaterHeight = 1.0f; } if(face == BoxFace.Top) { toReturn[i] = new ExtendedVertex(toReturn[i].Position + origin + new Vector3(0, (averageWaterHeight * 0.4f - 1.0f), 0), new Color(foaminess, puddleness, (float) totalDepth / 512.0f, 1.0f), uv, bounds); } else { Vector3 offset = Vector3.Zero; switch(face) { case BoxFace.Back: case BoxFace.Front: uv = new Vector2((Math.Abs(toReturn[i].Position.X + origin.X) / 80.0f), (Math.Abs(toReturn[i].Position.Y + origin.Y) / 80.0f)); foaminess = 1.0f; offset = new Vector3(0, -0.5f, 0); break; case BoxFace.Right: case BoxFace.Left: uv = new Vector2((Math.Abs(toReturn[i].Position.Z + origin.Z) / 80.0f), (Math.Abs(toReturn[i].Position.Y + origin.Y) / 80.0f)); foaminess = 1.0f; offset = new Vector3(0, -0.5f, 0); break; case BoxFace.Top: offset = new Vector3(0, -0.5f, 0); break; } toReturn[i] = new ExtendedVertex(toReturn[i].Position + origin + offset, new Color(foaminess, 0.0f, 1.0f, 1.0f), uv, bounds); } } return toReturn; }