c# - Unity3d : How to calculate the surface area of different colors in a colored mesh

标签 c# unity-game-engine shader

我在运行时创建了一个具有特殊共享的网格。共享具有以下属性:
1.第一次上色时颜色是蓝色
2.第二次上色时颜色为黄色
3.第三次着色时颜色为红色
4. 当着色第四次或更多时,颜色变为绿色

以下是彩色区域的示例:

enter image description here

在上图中,绿色区域有很多层的混搭。我无法通过对网格中的三角形求和来计算面积,因为有很多重叠。

编辑!我正在用更多信息更新问题
网格是使用以下代码生成的(这不是完整的代码,但足以大致了解网格的生成方式)

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/

相关文章:

c# - MVVM 按钮无法按下

c# - 如何创建本地用户组(在 C# 中)

browser - Blender 与 Unity

安卓 OpenGL : GLbyte LoadShader conversion

xna - 是否可以从着色器代码*检索*整数数据?

C# Epplus 在桌面应用程序中将字节文件保存为 XLS

c# - 从 64 位 c# WinForms 应用程序调用第三方 32 位 dll 时的事件处理

javascript - 需要帮助剖析和重新创建基于 PastryKit 的完美滚动缓动

unity-game-engine - 游戏对象的Unity网络代码无法连接主机

XNA:如何使用着色器写入纹理