1

我一直在努力使用 Unity 的Texture2D类和遮罩组件创建一个自由形式的裁剪工具。到目前为止,我已经创建了一个系统,用户可以使用该系统在透明图像上绘制自由形状。我需要一种算法,我可以用它来识别和填充绘制区域内的部分。

到目前为止,我已经实施了以下方法

  1. 我使用这个逻辑来确定一个点是否在绘制的形状之间,但这不适用于自由绘制的形状https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-多边形内/
int count;
        int a = 0;

        for (int x = 0; x < image_width; x++)
        {
            count = 0;
            for (int y = 0; y < image_height-1; y++)
            {
                if (drawingLayerTexture.GetPixel(x, y) == Color.red && drawingLayerTexture.GetPixel(x, y + 1) != Color.red)
                {
                    count++;
                }
            }

            if(drawingLayerTexture.GetPixel(x,image_height)==Color.red)
            {
                count++;
            }

            if (count > 1)
            {
                count = 0;
                for (int y = 0; y < image_height-1; y++)
                {
                    if (drawingLayerTexture.GetPixel(x, y) == Color.red && drawingLayerTexture.GetPixel(x, y + 1) != Color.red)
                    {
                        count++;
                    }
                    if (count % 2 == 1)
                    {
                        drawingLayerTexture.SetPixel(x, y, Color.white);
                        a++;
                    }
                }
            }
        }
  1. 我不是只检查一条线,而是检查了该点上方和该点之外的点,但它仍然不适用于所有情况
// Red is the color of the freeform shape drawn by the user, White is the region which is inside the shape

for(int y=0;y<image_height;y++)
        {
            for(int x=0;x<image_width;x++)
            {
                if (drawingLayerTexture.GetPixel(x, y) == Color.red)
                    drawingLayerTexture.SetPixel(x, y, Color.white);
                else if (x == 0 || y == 0)
                    continue;
                else if ((drawingLayerTexture.GetPixel(x - 1, y) == Color.white) && (drawingLayerTexture.GetPixel(x, y - 1) == Color.white))
                    drawingLayerTexture.SetPixel(x, y, Color.white);
            }
        }

绘制的形状(红线)

识别的区域(不正确)

即使我理解为什么这些方法都不起作用,但我想不出一个正确的算法

4

1 回答 1

0

您可以使用纯色(无抗锯齿)制作轮廓,然后从每个像素向外绘制四条假想线,并且仅当它们都与轮廓相交时才填充它们。据我所知,它应该始终适用于封闭的形状。这是非常低效的,但这是一个好的开始。

于 2020-07-15T18:00:21.580 回答