我遇到了问题,我试图在1000x1000
图像的区域进行绘制,并且空白空间public static void FloodFillBorderFromOriginalTexture(this Texture2D aTex, Texture2D refTex, int aX, int aY, Color aFillColor, Color aBorderColor)
{
// Get the dimensions
int w = aTex.width;
int h = aTex.height;
// Get the colors
var colors = aTex.GetPixels32(); // <-- Gets the colors from the texture
var refColors = refTex.GetPixels32();
byte[] checkedPixels = new byte[colors.Length];
// Assign the reference color the border color to look for
Color refCol = aBorderColor;
// Get the list of nodes to loop through as going along
Queue<Point> nodes = new Queue<Point>();
nodes.Enqueue(new Point(aX, aY));
while (nodes.Count > 0)
{
// Get the point
Point current = nodes.Dequeue();
// Loop through to the right
for (int i = current.x; i < w; i++)
{
// If we checked this pixel or its the border, skip it
if (checkedPixels[i + current.y * w] > 0 || isBorder(colors[i + current.y * w], refCol))
break;
// Need to get the grayscale and multiply by it
colors[i + current.y * w] = (Color32)((Color)refColors[i + current.y * w] * aFillColor); // <-- *** IMPORTANT! ***
// Set the pixel as checked
checkedPixels[i + current.y * w] = 1;
// If we are less than height
if (current.y + 1 < h)
{
// Check if the upper has not been colored and is not the border
if (checkedPixels[i + current.y * w + w] == 0 && isNotBorder(colors[i + current.y * w + w], refCol))
// Add it to the stack
nodes.Enqueue(new Point(i, current.y + 1));
}
// If we are going down and not at the bottom
if (current.y - 1 >= 0)
{
// Check if it is not the border and has not been checked yet
if (checkedPixels[i + current.y * w - w] == 0 && isNotBorder(colors[i + current.y * w - w], refCol))
// add it to the stack
nodes.Enqueue(new Point(i, current.y - 1));
}
}
// Loop through vertically
for (int i = current.x - 1; i >= 0; i--)
{
// Check if it has been colored and is border, break
if (checkedPixels[i + current.y * w] > 0 || isBorder(colors[i + current.y * w], refCol))
break;
// Grayscale it
colors[i + current.y * w] = (Color32)((Color)refColors[i + current.y * w] * aFillColor); // <-- *** IMPORTANT! ***
// Set as colored
checkedPixels[i + current.y * w] = 1;
if (current.y + 1 < h)
{
// Check the next move
if (checkedPixels[i + current.y * w + w] == 0 && isNotBorder(colors[i + current.y * w + w], refCol))
nodes.Enqueue(new Point(i, current.y + 1));
}
if (current.y - 1 >= 0)
{
// Check the next move
if (checkedPixels[i + current.y * w - w] == 0 && isNotBorder(colors[i + current.y * w - w], refCol))
nodes.Enqueue(new Point(i, current.y - 1));
}
}
}
// Set the pixels to the modifyable texture
aTex.SetPixels32(colors);
}
private static bool isNotBorder(Color color, Color borderColor)
{
return color.grayscale > 0.5f;
}
private static bool isBorder(Color color, Color borderColor)
{
return color.grayscale <= 0.5f;
}
周围的最大区域在PC上占用大约1秒钟。由于我的目标是移动设备,因此这是不可接受的。
这是我目前的代码,是否有人可以提出一种方法来加快速度?
class Food(Model):
__tablename__ = 'food'
id = db.Column(db.Integer, primary_key=True)
short_desc = db.Column(db.String(80))
sci_name = db.Column(db.String(250))