Last active
December 29, 2022 03:13
-
-
Save ProGM/22a615b812c5a9f1183d43b536d14a42 to your computer and use it in GitHub Desktop.
A simple Flood Fill implementation for Unity3D C# for video games. https://elfgames.com/2016/12/14/identify-unwanted-maze-solutions-using-flood-fill-with-unity3d/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2022 Piero Dotti, Elf Games | |
// | |
// Permission is hereby granted, free of charge, to any person | |
// obtaining a copy of this software and associated documentation | |
// files (the "Software"), to deal in the Software without | |
// restriction, including without limitation the rights to use, | |
// copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the | |
// Software is furnished to do so, subject to the following | |
// conditions: | |
// The above copyright notice and this permission notice shall be | |
// included in all copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
// OTHER DEALINGS IN THE SOFTWARE. | |
using UnityEngine; | |
using System.Collections.Generic; | |
// Flood Fill implementation for Unity3D C# | |
// Used in: https://www.elfgamesworks.com/2016/12/14/identify-unwanted-maze-solutions-using-flood-fill-with-unity3d/ | |
public static class ImageUtils | |
{ | |
public struct Point { | |
public int x; | |
public int y; | |
public Point(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
public static void FloodFill(Texture2D readTexture, Texture2D writeTexture, Color sourceColor, float tollerance, int x, int y) { | |
var targetColor = Color.red; | |
var q = new Queue<Point> (readTexture.width * readTexture.height); | |
q.Enqueue (new Point (x, y)); | |
int iterations = 0; | |
var width = readTexture.width; | |
var height = readTexture.height; | |
while (q.Count > 0) { | |
var point = q.Dequeue (); | |
var x1 = point.x; | |
var y1 = point.y; | |
if (q.Count > width * height) { | |
throw new System.Exception ("The algorithm is probably looping. Queue size: " + q.Count); | |
} | |
if (writeTexture.GetPixel (x1, y1) == targetColor) { | |
continue; | |
} | |
writeTexture.SetPixel (x1, y1, targetColor); | |
var newPoint = new Point (x1 + 1, y1); | |
if (CheckValidity (readTexture, readTexture.width, readTexture.height, newPoint, sourceColor, tollerance)) | |
q.Enqueue (newPoint); | |
newPoint = new Point (x1 - 1, y1); | |
if (CheckValidity (readTexture, readTexture.width, readTexture.height, newPoint, sourceColor, tollerance)) | |
q.Enqueue (newPoint); | |
newPoint = new Point (x1, y1 + 1); | |
if (CheckValidity (readTexture, readTexture.width, readTexture.height, newPoint, sourceColor, tollerance)) | |
q.Enqueue (newPoint); | |
newPoint = new Point (x1, y1 - 1); | |
if (CheckValidity (readTexture, readTexture.width, readTexture.height, newPoint, sourceColor, tollerance)) | |
q.Enqueue (newPoint); | |
iterations++; | |
} | |
} | |
static bool CheckValidity(Texture2D texture, int width, int height, Point p, Color sourceColor, float tollerance) { | |
if (p.x < 0 || p.x >= width) { | |
return false; | |
} | |
if (p.y < 0 || p.y >= height) { | |
return false; | |
} | |
var color = texture.GetPixel(p.x, p.y); | |
var distance = Mathf.Abs (color.r - sourceColor.r) + Mathf.Abs (color.g - sourceColor.g) + Mathf.Abs (color.b - sourceColor.b); | |
return distance <= tollerance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks very useful! It currently doesn't appear to have a license associated with it. I would like to use and build on the code, but only if it has an open-source license. Would you please specify its license? Thanks!