相关疑难解决方法(0)

Unity - 单击时如何使用网格三角形索引重新着色三角形?

我想知道如何在单击它们时在 Unity 中的网格中重新着色单个三角形。我下面的代码允许我检测点击在 3D 空间中的网格表面上的位置。从那里我尝试从网格中获取三角形索引并将颜色数组中的相应索引设置为红色。

但是,我可以看到我单击的位置(绿色立方体在图像中转换到的位置)甚至不接近三角形在网格上重新着色的位置。是否需要进行某种索引或坐标转换才能为光线投射/点击与网格碰撞的确切三角形着色?

我正在为图像中的球体使用标准的无光照着色器,有时会出现索引越界错误,这表明我在尝试将颜色数组设置为红色时超出了颜色数组的长度。

对此的任何帮助或见解将不胜感激。谢谢!

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

public class MyRayDraw : MonoBehaviour
{
    public GameObject cube;
    private MeshRenderer meshRenderer;
    Mesh mesh;
    Vector3[] vertices;
    Color[] colorArray;

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

        // create new colors array where the colors will be created
        colorArray = new Color[vertices.Length];
        for (int k = 0; k < vertices.Length; k++)
        {
            colorArray[k] = Color.white;
        }
        mesh.colors = colorArray;
    }

    void Update()
    {        
        if (Input.GetMouseButtonDown(0)) …
Run Code Online (Sandbox Code Playgroud)

unity-game-engine

2
推荐指数
1
解决办法
1677
查看次数

标签 统计

unity-game-engine ×1