我一直在努力使用 Unity 的Texture2D
类和遮罩组件创建一个自由形式的裁剪工具。到目前为止,我已经创建了一个系统,用户可以使用该系统在透明图像上绘制自由形状。我需要一种算法,我可以用它来识别和填充绘制区域内的部分。
到目前为止,我已经实施了以下方法
- 我使用这个逻辑来确定一个点是否在绘制的形状之间,但这不适用于自由绘制的形状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++;
}
}
}
}
- 我不是只检查一条线,而是检查了该点上方和该点之外的点,但它仍然不适用于所有情况
// 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);
}
}
即使我理解为什么这些方法都不起作用,但我想不出一个正确的算法