我在运行时创建了一个具有特殊共享的网格。共享具有以下属性:
1.第一次上色时颜色是蓝色
2.第二次上色时颜色为黄色
3.第三次着色时颜色为红色
4. 当着色第四次或更多时,颜色变为绿色
以下是彩色区域的示例:
在上图中,绿色区域有很多层的混搭。我无法通过对网格中的三角形求和来计算面积,因为有很多重叠。
编辑!我正在用更多信息更新问题
网格是使用以下代码生成的(这不是完整的代码,但足以大致了解网格的生成方式)
for (int i = start; i<colorUntil; i++)
{
parent.position = currentPosition;
Vector3 relativePos = points[i + 1] - points[i];
if (relativePos.magnitude > 0.5)
{
parent.rotation = Quaternion.LookRotation(relativePos);
}
currentPosition = points[i];
Vector3 offset = parent.right * width / 2f;
vertices.Add(currentPosition - offset);
vertices.Add(currentPosition + offset);
uvs.Add(new Vector2(0, 1));
uvs.Add(new Vector2(1, 1));
if (vertices.Count< 4)
{
return;
}
int c = vertices.Count;
triangles.Add(c - 1);
triangles.Add(c - 2);
triangles.Add(c - 3);
triangles.Add(c - 3);
triangles.Add(c - 2);
triangles.Add(c - 4);
Vector3[] v = new Vector3[c];
Vector2[] uv = new Vector2[c];
int[] t = new int[triangles.Count];
vertices.CopyTo(v, 0);
uvs.CopyTo(uv, 0);
triangles.CopyTo(t, 0);
mesh.vertices = v;
mesh.triangles = t;
mesh.uv = uv;
}
现在我必须计算每种颜色的面积。给定上面的网格,是否可以计算每种颜色的面积?欢迎任何建议。
编辑2!
显然,没有办法使用混搭信息来计算彩色区域(至少根据我的研究)。我如何寻找一种创造性的方式来实现我想要的目标。我欢迎并感谢任何建议。
最佳答案
一个简单的脚本,用于截取屏幕截图然后计算绿色像素。
将脚本放在渲染相机上,将m_TakeScreenshot
设置为true,它将在当前帧中工作。
public class ScreenshotCamera : MonoBehaviour
{
public bool m_TakeScreenshot;
public Texture2D m_OutputTexture;
void OnPostRender()
{
if (m_TakeScreenshot)
{
m_TakeScreenshot = false;
//Take screenshot
if (m_OutputTexture == null)
m_OutputTexture = new Texture2D(Screen.width, Screen.height);
m_OutputTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
m_OutputTexture.Apply();
//Get all pixels, count green pixels
var pixels = m_OutputTexture.GetPixels(0, 0, m_OutputTexture.width, m_OutputTexture.height);
int greenPixels = 0, otherPixels = 0;
foreach (var color in pixels)
{
//green
if (color.g > 0.8f && color.r < 0.1f && color.b < 0.1f)
greenPixels++;
//not black
else if (color.r > 0.1f || color.g > 0.1f || color.b > 0.1f)
otherPixels++;
}
}
}
}
关于c# - Unity3d : How to calculate the surface area of different colors in a colored mesh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57304751/