如何获得重新着色的网格百分比?

发布于 2025-01-20 10:32:40 字数 1823 浏览 2 评论 0原文

我正在尝试根据对象的三角形和顶点进行绘画。我已经完成了这些,但我想立即显示玩家/用户绘制了多少对象。因此,当达到一定的速度时,我就会结束游戏。但我不知道如何获得百分比。

预先感谢您的帮助

https://i.sstatic.net/mrXKt.jpg

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VertexPainterSec : MonoBehaviour
{
    [SerializeField] private MeshRenderer meshRenderer;
    [SerializeField] private Mesh mesh;
    [SerializeField] Vector3[] vertices;
    [SerializeField] Color[] colorArray;
    [SerializeField] public Text colorPercentage;

    private void Start()
    {
        mesh = transform.GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;

        colorArray = new Color[vertices.Length];
        for (int k = 0; k < vertices.Length; k++)
        {
            colorArray[k] = Color.white;
        }
        mesh.colors = colorArray;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out RaycastHit hit))
            {            

                int[] triangles = mesh.triangles;
                var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
                var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
                var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];

                colorArray[vertIndex1] = Color.red;
                colorArray[vertIndex2] = Color.red;
                colorArray[vertIndex3] = Color.red;

                mesh.colors = colorArray;
                colorPercentage.text = "Color Percantage";
            }
            else
            {
                Debug.Log("no hit");
            }
        }
    }
} 

I'm trying to paint according to the triangles and vertexes of the object. I've done these, but I want to instantly show how much of the object the player/user has painted. Thus, when it reaches a certain rate, I will end the game. But I don't know how to get the percentage rate.

thank you in advance for your help

https://i.sstatic.net/mrXKt.jpg

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VertexPainterSec : MonoBehaviour
{
    [SerializeField] private MeshRenderer meshRenderer;
    [SerializeField] private Mesh mesh;
    [SerializeField] Vector3[] vertices;
    [SerializeField] Color[] colorArray;
    [SerializeField] public Text colorPercentage;

    private void Start()
    {
        mesh = transform.GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;

        colorArray = new Color[vertices.Length];
        for (int k = 0; k < vertices.Length; k++)
        {
            colorArray[k] = Color.white;
        }
        mesh.colors = colorArray;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out RaycastHit hit))
            {            

                int[] triangles = mesh.triangles;
                var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
                var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
                var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];

                colorArray[vertIndex1] = Color.red;
                colorArray[vertIndex2] = Color.red;
                colorArray[vertIndex3] = Color.red;

                mesh.colors = colorArray;
                colorPercentage.text = "Color Percantage";
            }
            else
            {
                Debug.Log("no hit");
            }
        }
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-01-27 10:32:40

您可以做到这两种方法:

第一个是运行一个函数来计算colorArray中的非白色颜色总数,因此

void CalculatePercentage()
        {
            int totalPainted = 0;
            foreach (var c in colorArray)
            {
                if (c != Color.White)
                {
                    totalPainted++;
                }
            }
            colorPercentage = "Color Percantage: %" + (totalPainted / colorArray) * 100;            
        }

它具有一些缺陷,因为它非常效率,因为您正在计数每一个缺点每次运行此阵列中的项目,只有在您的基本颜色为白色的情况下,

第二种方法涉及跟踪您绘制了多少个顶点,您必须稍微修改代码像这样:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VertexPainterSec : MonoBehaviour
{
    [SerializeField]
    private MeshRenderer meshRenderer;
    [SerializeField]
    private Mesh mesh;
    [SerializeField]
    Vector3[] vertices;
    [SerializeField]
    Color[] colorArray;
    [SerializeField]
    public Text colorPercentage;
    //This value will track how many vertices you've painted
    [SerializeField]
    private int paintedVertices;
    private void Start()
    {
        //Initialize the value
        paintedVertices = 0;
        mesh = transform.GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;
        colorArray = new Color[vertices.Length];
        for (int k = 0; k < vertices.Length; k++)
        {
            colorArray[k] = Color.white;
        }

        mesh.colors = colorArray;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                int[] triangles = mesh.triangles;
                var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
                var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
                var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];
                //Only paint and add to paintedVertices if the color is white
                //to avoid constantly adding to the paintedVertices value
                if (colorArray[vertIndex1] == Color.white)
                {
                    colorArray[vertIndex1] = Color.red;
                    paintedVertices++;
                }

                if (colorArray[vertIndex2] == Color.white)
                {
                    colorArray[vertIndex2] = Color.red;
                    paintedVertices++;
                }

                if (colorArray[vertIndex3] == Color.white)
                {
                    colorArray[vertIndex3] = Color.red;
                    paintedVertices++;
                }

                mesh.colors = colorArray;
                //Calculate the percentage by dividing the
                //paintedVertices value by the total number of vertices
                colorPercentage.text = "Color Percantage: %" + (paintedVertices / colorArray.Length) * 100;
            }
            else
            {
                Debug.Log("no hit");
            }
        }
    }
}

Two ways you could do this:

The first is by running a function that calculates the total number of non-white colors in colorArray like so

void CalculatePercentage()
        {
            int totalPainted = 0;
            foreach (var c in colorArray)
            {
                if (c != Color.White)
                {
                    totalPainted++;
                }
            }
            colorPercentage = "Color Percantage: %" + (totalPainted / colorArray) * 100;            
        }

This has some drawbacks though as its very inefficient since you're counting every item in the array every time you run this, and it would only work if your base color is white

The second method involves keeping track of how many vertices you've painted as you're going, you'd have to modify your code slightly like so:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VertexPainterSec : MonoBehaviour
{
    [SerializeField]
    private MeshRenderer meshRenderer;
    [SerializeField]
    private Mesh mesh;
    [SerializeField]
    Vector3[] vertices;
    [SerializeField]
    Color[] colorArray;
    [SerializeField]
    public Text colorPercentage;
    //This value will track how many vertices you've painted
    [SerializeField]
    private int paintedVertices;
    private void Start()
    {
        //Initialize the value
        paintedVertices = 0;
        mesh = transform.GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;
        colorArray = new Color[vertices.Length];
        for (int k = 0; k < vertices.Length; k++)
        {
            colorArray[k] = Color.white;
        }

        mesh.colors = colorArray;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                int[] triangles = mesh.triangles;
                var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
                var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
                var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];
                //Only paint and add to paintedVertices if the color is white
                //to avoid constantly adding to the paintedVertices value
                if (colorArray[vertIndex1] == Color.white)
                {
                    colorArray[vertIndex1] = Color.red;
                    paintedVertices++;
                }

                if (colorArray[vertIndex2] == Color.white)
                {
                    colorArray[vertIndex2] = Color.red;
                    paintedVertices++;
                }

                if (colorArray[vertIndex3] == Color.white)
                {
                    colorArray[vertIndex3] = Color.red;
                    paintedVertices++;
                }

                mesh.colors = colorArray;
                //Calculate the percentage by dividing the
                //paintedVertices value by the total number of vertices
                colorPercentage.text = "Color Percantage: %" + (paintedVertices / colorArray.Length) * 100;
            }
            else
            {
                Debug.Log("no hit");
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文