public static void clear() { _image = null; _gather = null; _processValue = 0; _pixelQueue.Clear(); _stand = null; }
// 获取像素集集合 private static TextureGather GetGather(int id) { if (_gather == null) { _gather = new TextureGather(); _gather.id = id; } return(_gather); }
public static Bitmap ToImage(TextureGather gather) { int width = gather.width; int height = gather.height; Bitmap bitmap = new Bitmap(width + 1, height + 1); foreach (var item in gather.pixels) { Position position = gather.GetPixelPosition(item.Key); Color pixel = item.Value; bitmap.SetPixel(position.x, position.y, pixel); } return(bitmap); }
// 划分像素值,并将八方向的像素放入队列准备划分 private static void SetGather(int x, int y, int id) { Color color = _image.GetPixel(x, y); TextureGather textureGather = GetGather(id); textureGather.AddPixel(new Position(x, y), color); PushToQueue(x + 1, y); PushToQueue(x - 1, y); PushToQueue(x, y + 1); PushToQueue(x, y - 1); PushToQueue(x + 1, y + 1); PushToQueue(x - 1, y - 1); PushToQueue(x + 1, y - 1); PushToQueue(x - 1, y + 1); }
// 划分图片 public static void SliceTexture() { int gatherCount = 0; bool isLucency = true; for (int i = 0; i < _image.Width; i++) { for (int j = 0; j < _image.Height; j++) { _processValue++; // Console.WriteLine(_processValue); Color pixel = _image.GetPixel(i, j); // 根据找到的第一个非透时像素点开始划分像素值 if (isLucency) { if (_cutOff < pixel.A && !_stand[i, j]) { // 开始对该像素进行划分 PushToQueue(i, j); SliceTextureImpl(gatherCount); // 将得到的像素值分保存起来 if (gatherCount == 79) { Console.WriteLine("pause"); } Bitmap image = BitmapHelper.ToImage(_gather); BitmapHelper.SaveImage(string.Format("{0}/Texture_{1}.png", _savePath, _gather.id), image); _gather = null; gatherCount++; isLucency = false; } } else // 划分像素集后,找到第一个透明像素点,并使状态恢复到划分前 { if (_cutOff > pixel.A) { isLucency = true; } } _stand[i, j] = true; } } }