private void ReadData(Sprite sprite) { for (int y = _yMax - 1; y >= _yMin; y--) { for (int x = _xMin; x < _xMax; x++) { if (sprite.texture.GetPixel(x, y).a > 0.0f) { _pixels[x, y] = new PixelDataHigh(x, y, _size); var stripe = new MeshStripeHigh(_pixels[x, y]); x++; while (sprite.texture.GetPixel(x, y).a > 0.0f && x < _xMax) { _pixels[x, y] = new PixelDataHigh(x, y, _size); stripe.AddPixelToTheStripe(_pixels[x, y]); x++; } bool any = false; foreach (var s in _stripes) { if (s.MergeableToTheBottom(stripe)) { any = true; break; } } if (!any) { _stripes.Add(stripe); } } } } for (int y = _yMax - 1; y >= _yMin; y--) { for (int x = _xMin; x < _xMax; x++) { if (_pixels[x, y] == null) { continue; } _pixels[x, y].TopCovered = y != _yMax - 1 && _pixels[x, y + 1] != null; _pixels[x, y].RightCovered = x != _xMax - 1 && _pixels[x + 1, y] != null; _pixels[x, y].LeftCovered = x != _xMin && _pixels[x - 1, y] != null; _pixels[x, y].BottomCovered = y != _yMin && _pixels[x, y - 1] != null; } } }
private void MergeStripeTotheBottom(MeshStripeHigh stripeHigh) { _bottomLeftPixel = stripeHigh._bottomLeftPixel; _bottomRightPixel = stripeHigh._bottomRightPixel; }
public MeshStripeHigh(PixelDataHigh startPixel) { _topLeftPixel = _bottomLeftPixel = _topRightPixel = _bottomRightPixel = startPixel; }
public void AddPixelToTheStripe(PixelDataHigh pixel) { pixel.LeftCovered = true; _topRightPixel = _bottomRightPixel = pixel; }
private void CoverPixelSides(PixelDataHigh p) { var pixel = p; var tr = new int[6]; if (!pixel.TopCovered) { int x = pixel.Position.X; while (x >= _xMin && _pixels[x, pixel.Position.Y] != null && !_pixels[x, pixel.Position.Y].TopCovered) { pixel = _pixels[x, pixel.Position.Y]; x--; } var coord = pixel.VertPosition(Corner.TopLeft); tr[5] = _vertMatrix[coord.X, coord.Y].X; tr[2] = tr[3] = _vertMatrix[coord.X, coord.Y].Y; var endPixel = pixel; x = pixel.Position.X; while (x < _xMax && _pixels[x, pixel.Position.Y] != null && !_pixels[x, pixel.Position.Y].TopCovered) { endPixel = _pixels[x, pixel.Position.Y]; endPixel.TopCovered = true; x++; } coord = endPixel.VertPosition(Corner.TopRight); tr[1] = tr[4] = _vertMatrix[coord.X, coord.Y].X; tr[0] = _vertMatrix[coord.X, coord.Y].Y; } else if (!pixel.RightCovered) { int y = pixel.Position.Y; while (y < _yMax && _pixels[pixel.Position.X, y] != null && !_pixels[pixel.Position.X, y].RightCovered) { pixel = _pixels[pixel.Position.X, y]; y++; } var coord = pixel.VertPosition(Corner.TopRight); tr[5] = _vertMatrix[coord.X, coord.Y].X; tr[2] = tr[3] = _vertMatrix[coord.X, coord.Y].Y; var endPixel = pixel; y = pixel.Position.Y; while (y >= _yMin && _pixels[pixel.Position.X, y] != null && !_pixels[pixel.Position.X, y].RightCovered) { endPixel = _pixels[pixel.Position.X, y]; endPixel.RightCovered = true; y--; } coord = endPixel.VertPosition(Corner.BottomRight); tr[1] = tr[4] = _vertMatrix[coord.X, coord.Y].X; tr[0] = _vertMatrix[coord.X, coord.Y].Y; } else if (!pixel.BottomCovered) { int x = pixel.Position.X; while (x >= _xMin && _pixels[x, pixel.Position.Y] != null && !_pixels[x, pixel.Position.Y].BottomCovered) { pixel = _pixels[x, pixel.Position.Y]; x--; } var coord = pixel.VertPosition(Corner.BottomLeft); tr[0] = _vertMatrix[coord.X, coord.Y].X; tr[2] = tr[3] = _vertMatrix[coord.X, coord.Y].Y; var endPixel = pixel; x = pixel.Position.X; while (x < _xMax && _pixels[x, pixel.Position.Y] != null && !_pixels[x, pixel.Position.Y].BottomCovered) { endPixel = _pixels[x, pixel.Position.Y]; endPixel.BottomCovered = true; x++; } coord = endPixel.VertPosition(Corner.BottomRight); tr[1] = tr[4] = _vertMatrix[coord.X, coord.Y].X; tr[5] = _vertMatrix[coord.X, coord.Y].Y; } else if (!pixel.LeftCovered) { int y = pixel.Position.Y; while (y < _yMax && _pixels[pixel.Position.X, y] != null && !_pixels[pixel.Position.X, y].LeftCovered) { pixel = _pixels[pixel.Position.X, y]; y++; } var coord = pixel.VertPosition(Corner.TopLeft); tr[0] = _vertMatrix[coord.X, coord.Y].X; tr[2] = tr[3] = _vertMatrix[coord.X, coord.Y].Y; var endPixel = pixel; y = pixel.Position.Y; while (y >= _yMin && _pixels[pixel.Position.X, y] != null && !_pixels[pixel.Position.X, y].LeftCovered) { endPixel = _pixels[pixel.Position.X, y]; endPixel.LeftCovered = true; y--; } coord = endPixel.VertPosition(Corner.BottomLeft); tr[1] = tr[4] = _vertMatrix[coord.X, coord.Y].X; tr[5] = _vertMatrix[coord.X, coord.Y].Y; } _triangles.AddRange(tr); }