How can I fill empty and closed areas with the flood fill algorithm?
I want the floor to fill with the color I choose when the object creates any shape with space in the middle. It should automatically fill the gaps between the lines\boxes. How can I do that?
This is my flood fill code:
public class FloodFill : MonoBehaviour
{
[SerializeField] private float fillDelay = 0.01f;
private GridManager gridManager;
void Start()
{
gridManager = this.GetComponent<GridManager>();
}
public IEnumerator Flood(int x, int y, Color oldColor, Color newColor)
{
WaitForSeconds wait = new WaitForSeconds(fillDelay);
if (x >= 0 && x < gridManager.xSize && y >= 0 && y < gridManager.ySize)
{
yield return wait;
if (gridManager.grid[x, y].GetComponent<SpriteRenderer>().color == oldColor)
{
gridManager.grid[x, y].GetComponent<SpriteRenderer>().color = newColor;
StartCoroutine(Flood(x + 1, y, oldColor, newColor));
StartCoroutine(Flood(x - 1, y, oldColor, newColor));
StartCoroutine(Flood(x, y + 1, oldColor, newColor));
StartCoroutine(Flood(x, y - 1, oldColor, newColor));
}
}
}
}
and this is fill for just borders:
if (hit.collider != null)
{
mov = false;
if (Mathf.RoundToInt(this.transform.position.x) >= gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) >= gridManager.ySize / 2)
{
if (currentDirection == up)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x + 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
}
if (currentDirection == right)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y + 1), Color.white, Color.green));
}
}
if (Mathf.RoundToInt(this.transform.position.x) <= gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) <= gridManager.ySize / 2)
{
if (currentDirection == down)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x - 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
}
if (currentDirection == left)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y - 1), Color.white, Color.green));
}
}
if (Mathf.RoundToInt(this.transform.position.x) < gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) > gridManager.ySize / 2)
{
if (currentDirection == up)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x - 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
}
if (currentDirection == left)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y + 1), Color.white, Color.green));
}
}
if (Mathf.RoundToInt(this.transform.position.x) > gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) < gridManager.ySize / 2)
{
if (currentDirection == down)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x - 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
}
if (currentDirection == right)
{
StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y - 1), Color.white, Color.green));
}
}
}
else
{
mov = true;
}
screenshot-6.png
(6.4 kB)
screenshot-5.png
(5.5 kB)
Comment