private void GenerateMesh(VoxelGrid grid) { this.EnqueueWorkerJob(() => { //Thread.Sleep(750); var voxels = this.grid.Data; var vertices = new List <VertexPositionTextureNormal>(voxels.XLength * voxels.YLength * voxels.ZLength); var indices = new List <ushort>(voxels.XLength * voxels.YLength * voxels.ZLength); var imageSize = new Vector2(MaterialInstance.ImageWidth, MaterialInstance.ImageHeight); //MeshGenerator.GenerateMesh(voxels, (voxel, side, quad) => //{ // var type = _types[voxel.BlockType]; // var textureOffset = GetTexCoords(type, voxel.Orientation, side); // indices.Add((ushort)(vertices.Count + 0)); // indices.Add((ushort)(vertices.Count + 1)); // indices.Add((ushort)(vertices.Count + 3)); // indices.Add((ushort)(vertices.Count + 1)); // indices.Add((ushort)(vertices.Count + 2)); // indices.Add((ushort)(vertices.Count + 3)); // vertices.Add(new VertexPositionTextureNormal(quad.A, (textureOffset + new Vector2(0, 128)) / imageSize, quad.Normal)); // vertices.Add(new VertexPositionTextureNormal(quad.B, (textureOffset + new Vector2(0, 0)) / imageSize, quad.Normal)); // vertices.Add(new VertexPositionTextureNormal(quad.C, (textureOffset + new Vector2(128, 0)) / imageSize, quad.Normal)); // vertices.Add(new VertexPositionTextureNormal(quad.D, (textureOffset + new Vector2(128, 128)) / imageSize, quad.Normal)); //}); GreedyMeshGenerator.GenerateMesh(voxels, (typeNum, orientation, side, quad, size) => { var type = _types[typeNum]; var textureOffset = GetTexCoords(type, orientation, side); indices.Add((ushort)(vertices.Count + 0)); indices.Add((ushort)(vertices.Count + 1)); indices.Add((ushort)(vertices.Count + 3)); indices.Add((ushort)(vertices.Count + 1)); indices.Add((ushort)(vertices.Count + 2)); indices.Add((ushort)(vertices.Count + 3)); vertices.Add(new VertexPositionTextureNormal(quad.A, (textureOffset + new Vector2(0, 128)) / imageSize, quad.Normal)); vertices.Add(new VertexPositionTextureNormal(quad.B, (textureOffset + new Vector2(0, 0)) / imageSize, quad.Normal)); vertices.Add(new VertexPositionTextureNormal(quad.C, (textureOffset + new Vector2(128, 0)) / imageSize, quad.Normal)); vertices.Add(new VertexPositionTextureNormal(quad.D, (textureOffset + new Vector2(128, 128)) / imageSize, quad.Normal)); }); UpdateMesh(vertices.ToArray(), indices.ToArray()); }); }
public void Initialize(GraphicsDevice device, CommandList commandList, RenderableInitialize initialize) { grid = GameObject.GetComponent <VoxelGrid>(); grid.VoxelsChanged += GenerateMesh; GenerateMesh(grid); }
public static void Check(int flatIndex, VoxelGrid voxels, T checker) { var voxelIndex = voxels.AsCoordinate(flatIndex); var xInc = 1; var yInc = voxels.GridSize; var zInc = voxels.GridSize * voxels.GridSize; if (voxelIndex.X > 0) { checker.Check(flatIndex - xInc, voxels); } else { var otherGrid = voxels.NeighborGrids[(int)VoxelSide.WEST]; if (otherGrid != null) { checker.Check(otherGrid.AsFlatIndex(voxels.GridSize - 1, voxelIndex.Y, voxelIndex.Z), otherGrid); } } if (voxelIndex.X < voxels.GridSize - 1) { checker.Check(flatIndex + xInc, voxels); } else { var otherGrid = voxels.NeighborGrids[(int)VoxelSide.EAST]; if (otherGrid != null) { checker.Check(otherGrid.AsFlatIndex(0, voxelIndex.Y, voxelIndex.Z), otherGrid); } } if (voxelIndex.Y > 0) { checker.CheckBelow(flatIndex - yInc, voxels); } else { var otherGrid = voxels.NeighborGrids[(int)VoxelSide.BOTTOM]; if (otherGrid != null) { checker.CheckBelow(otherGrid.AsFlatIndex(voxelIndex.X, voxels.GridSize - 1, voxelIndex.Z), otherGrid); } } if (voxelIndex.Y < voxels.GridSize - 1) { checker.Check(flatIndex + yInc, voxels); } else { var otherGrid = voxels.NeighborGrids[(int)VoxelSide.TOP]; if (otherGrid != null) { checker.Check(otherGrid.AsFlatIndex(voxelIndex.X, 0, voxelIndex.Z), otherGrid); } } if (voxelIndex.Z > 0) { checker.Check(flatIndex - zInc, voxels); } else { var otherGrid = voxels.NeighborGrids[(int)VoxelSide.NORTH]; if (otherGrid != null) { checker.Check(otherGrid.AsFlatIndex(voxelIndex.X, voxelIndex.Y, voxels.GridSize - 1), otherGrid); } } if (voxelIndex.Z < voxels.GridSize - 1) { checker.Check(flatIndex + zInc, voxels); } else { var otherGrid = voxels.NeighborGrids[(int)VoxelSide.SOUTH]; if (otherGrid != null) { checker.Check(otherGrid.AsFlatIndex(voxelIndex.X, voxelIndex.Y, 0), otherGrid); } } }