检测网格上图像大小与网格大小的不同方法

Different ways to detect size of image on mesh versus size of mesh

我正在创建一个益智游戏,它可以生成带有 2D 网格的随机大小的碎片。图像包含透明部分,有时一块是完全透明的。我需要检测一块透明的百分比。我发现这样做的一种方法是逐像素进行。我将我的解决方案发布到此 HERE。但是,这个过程在加载过程中增加了几秒钟,我想避免这种情况,我正在寻找其他想法

我考虑过使用 MeshCollider 的选择轮廓以某种方式获得可以与网格的表面积进行比较的表面积,但我发现的所有内容都是在使用专用着色器渲染轮廓时。有没有人有解决这个问题的想法?

.

1) 我想您可以将 PolygonCollider2D 添加到您的 sprite 中,并使用其 Path 来绘制轮廓和计算表面积。但是不确定这是否会更快。

PolygonCollider2D.GetPath:

A path is a cyclic sequence of line segments between points that define the outline of the Collider

检查 PolygonCollider2D.GetTotalPointCount 或路径长度可能足以确定精灵是否 'empty'。

Sprite.vertices, Sprite.triangles 也可能有帮助。

2) 您还可以提高第一种方法的性能:

  • 不像现在那样调用 GetPixel,而是使用 GetPixels 或 GetPixels32 并在一个 for 循环中遍历数组。

Using GetPixels can be faster than calling GetPixel repeatedly, especially for large textures. In addition, GetPixels can access individual mipmap levels. For most textures, even faster is to use GetPixels32 which returns low precision color data without costly integer-to-float conversions.

  • 只检查每 2 个或第 n 个像素,因为它应该足够好以进行近似
  • 限制类型转换的数量