这个算法的介绍基本可以找到 放个维基的https://zh.wikipedia.org/wiki/Flood_fill
主要实现代码
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Click(Input.mousePosition);
}
}
private void Click(Vector2 mousePosition)
{
if (RectTransformUtility.RectangleContainsScreenPoint(rawImage.rectTransform, mousePosition) &&
RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform,
mousePosition, null, out Vector2 localPoint))
{
int x = (int)localPoint.x + tex.width / 2;
int y = (int)localPoint.y + tex.height / 2;
FloodFill(x, y, tex.GetPixel(x, y));
}
}
private void FloodFill(int x, int y, Color ClickColor)
{
if (ColorCompare(ClickColor, setColor))
{
return;
}
InitData();
for (int i = 0; i < tex.width; i++)
{
bChecked[i] = false;
}
points.Enqueue(new Vector2Int(x, y));
while (points.Count > 0)
{
var point = points.Dequeue();
if (!(point.x < 0 || point.y < 0 || point.x >= tex.width || point.y >= tex.height))
{
if (!bChecked[point.y * tex.width + point.x] && ColorCompare(ClickColor, tex.GetPixel(point.x,point.y)))
{
bChecked[point.y * tex.width + point.x] = true;
tex.SetPixel(point.x, point.y, setColor);
points.Enqueue(new Vector2Int(point.x - 1, point.y + 1));
points.Enqueue(new Vector2Int(point.x - 1, point.y));
points.Enqueue(new Vector2Int(point.x - 1, point.y - 1));
points.Enqueue(new Vector2Int(point.x, point.y + 1));
points.Enqueue(new Vector2Int(point.x, point.y - 1));
points.Enqueue(new Vector2Int(point.x + 1, point.y + 1));
points.Enqueue(new Vector2Int(point.x + 1, point.y));
points.Enqueue(new Vector2Int(point.x + 1, point.y - 1));
}
}
}
tex.Apply();
}
有需要的下一个 [email protected]:Laptter/Entertianment.git