如何获得重新着色的网格百分比?
我正在尝试根据对象的三角形和顶点进行绘画。我已经完成了这些,但我想立即显示玩家/用户绘制了多少对象。因此,当达到一定的速度时,我就会结束游戏。但我不知道如何获得百分比。
预先感谢您的帮助
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做到这两种方法:
第一个是运行一个函数来计算
colorArray中的非白色颜色总数
,因此它具有一些缺陷,因为它非常效率,因为您正在计数每一个缺点每次运行此阵列中的项目,只有在您的基本颜色为白色的情况下,
第二种方法涉及跟踪您绘制了多少个顶点,您必须稍微修改代码像这样:
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 soThis 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: