@Lorandius

Как получить цвет определенной вершины меша?

У меня есть кастомный меш, и я хочу получить цвет отдельного треугольника. Я пытался использовать эти решения : https://stackoverflow.com/questions/45854076/set-c... , https://stackoverflow.com/questions/57405631/unity... . У меня нет текстуры на объекте, поэтому остается только способ с манипуляциями треугольниками и вершинами. Я пытался получить цвет таким образом :
using System;
using System.Collections;
using System.Collections.Generic;
 using UnityEngine;

 public class ColorFInd : MonoBehaviour
{
 public Camera camera;

private Color[] colorArray;
// Start is called before the first frame update

// Update is called once per frame
void Update()
{
    Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    RaycastHit[] hitInfos = Physics.RaycastAll(ray);
  //  Debug.Log(hitInfos[0].collider.GetComponent<MeshFilter>().mesh.triangles[hitInfos[0].triangleIndex]);
     Debug.Log(hitInfos[0].collider.GetComponent<MeshFilter>().mesh.colors.Length);
   
}
public static int GetSubMeshIndex(Mesh mesh, int triangleIndex)
    {
        if (mesh.isReadable == false)
        {
            Debug.LogError("You need to mark model's mesh as Read/Write Enabled in Import Settings.", mesh);
            return 0;
        }

        int triangleCounter = 0;
        for (int subMeshIndex = 0; subMeshIndex < mesh.subMeshCount; subMeshIndex++)
        {
            var indexCount = mesh.GetSubMesh(subMeshIndex).indexCount;
            triangleCounter += indexCount / 3;
            if (triangleIndex < triangleCounter)
            {
                return subMeshIndex;
            }
        }

        Debug.LogError(
            $"Failed to find triangle with index {triangleIndex} in mesh '{mesh.name}'. Total triangle count: {triangleCounter}",
            mesh);
        return 0;
    }
}

Но это не дало результата т.к массив цветов пустой
  • Вопрос задан
  • 68 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы