public List <Vec3i> RebuildNetwork(Vec3i pos_init, VoxelWire old_network) { if (!old_network.GotVoxelAtPos(pos_init)) { return(new List <Vec3i>()); } List <Vec3i> to_explore = new List <Vec3i>(); List <Vec3i> explored = new List <Vec3i>(); to_explore.Add(pos_init); while (to_explore.Any()) { Vec3i current_pos = to_explore.Last(); foreach (BlockFacing facing in BlockFacing.ALLFACES) { Vec3i offset_pos = current_pos.AddCopy(facing); if (old_network.GotVoxelAtPos(offset_pos) && !explored.Contains(offset_pos)) { to_explore.Add(offset_pos); } } to_explore.Remove(current_pos); explored.Add(current_pos); } //At the end, the explored list contains connected voxels return(explored); }
public bool OnAdd(Vec3i voxelPos) { //We first test if the voxel is already contained in a network foreach (VoxelWire net in networks.Values) { if (net.GotVoxelAtPos(voxelPos)) { return(false); } } //We explore adjacent voxels, looking for a network //if a network is found, we take the id. if another network is found with different id, we merge the networks. VoxelWire current_net = null; foreach (BlockFacing face in BlockFacing.ALLFACES) { Vec3i pos2 = voxelPos.AddCopy(face); VoxelWire net = GetWireAtPos(pos2); if (net != null) { if (current_net == null) { net.AddVoxel(voxelPos); current_net = net; } else if (current_net != null && net.id != current_net.id) { current_net.MergeWith(net); networks.Remove(net.id); } else if (net.id == current_net.id) { net.AddVoxel(voxelPos); } } } if (current_net == null) { //We create a new network int new_id = getNewId(); VoxelWire network = new VoxelWire(new_id); network.AddVoxel(voxelPos); networks.Add(new_id, network); } hasChanged = true; return(true); }
private MeshData GetNetworkMesh(VoxelWire net, MeshData singleVoxelMesh) { MeshData voxelMeshOffset = singleVoxelMesh.Clone(); networkStates[net.id] = net.state; MeshData mesh = new MeshData(24, 36, false); byte[] color = getColor(net.id).Append(new byte[] { 255 }); foreach (Vec3i vec in net.getVoxelPos()) { float px = vec.X / 16f; float py = vec.Y / 16f; float pz = vec.Z / 16f; for (int i = 0; i < singleVoxelMesh.xyz.Length; i += 3) { voxelMeshOffset.xyz[i] = px + singleVoxelMesh.xyz[i]; voxelMeshOffset.xyz[i + 1] = py + singleVoxelMesh.xyz[i + 1]; voxelMeshOffset.xyz[i + 2] = pz + singleVoxelMesh.xyz[i + 2]; //voxelMeshOffset.Rgba[i / 3 * 4] = color[0]; //voxelMeshOffset.Rgba[i / 3 * 4 + 1] = color[1]; //voxelMeshOffset.Rgba[i / 3 * 4 + 2] = color[2]; //voxelMeshOffset.Rgba[i / 3 * 4 + 3] = color[3]; } float offsetX = ((((vec.X + 4 * vec.Y) % 16f / 16f)) * 32f) / capi.BlockTextureAtlas.Size.Width; float offsetY = (pz * 32f) / capi.BlockTextureAtlas.Size.Height; for (int i = 0; i < singleVoxelMesh.Uv.Length; i += 2) { voxelMeshOffset.Uv[i] = singleVoxelMesh.Uv[i] + offsetX; voxelMeshOffset.Uv[i + 1] = singleVoxelMesh.Uv[i + 1] + offsetY; } mesh.AddMeshData(voxelMeshOffset); } return(mesh); }
public void MergeWith(VoxelWire net) { voxelpositions.AddRange(net.voxelpositions); }