2023-02-06【Gpt】niubility的一次使用 su

2023-02-05  本文已影响0人  持刀的要迟到了
using UnityEngine;

public class UnpackSubmesh : MonoBehaviour
{
    public int submeshIndex = 0;

    private void Start()
    {
        MeshFilter meshFilter = GetComponent<MeshFilter>();
        Mesh mesh = meshFilter.mesh;
        int[] triangles = mesh.GetTriangles(submeshIndex);

        for (int i = 0; i < triangles.Length; i += 3)
        {
            Vector3[] vertices = new Vector3[3];
            Vector3[] normals = new Vector3[3];
            Vector2[] uv = new Vector2[3];

            for (int j = 0; j < 3; j++)
            {
                int index = triangles[i + j];
                vertices[j] = mesh.vertices[index];
                normals[j] = mesh.normals[index];
                uv[j] = mesh.uv[index];
            }

            GameObject triangleMesh = new GameObject("Triangle Mesh");
            triangleMesh.transform.parent = transform;
            triangleMesh.AddComponent<MeshFilter>().mesh = CreateMesh(vertices, normals, uv);
            triangleMesh.AddComponent<MeshRenderer>().material = meshFilter.GetComponent<MeshRenderer>().materials[submeshIndex];
        }

        meshFilter.mesh = new Mesh();
    }

    private Mesh CreateMesh(Vector3[] vertices, Vector3[] normals, Vector2[] uv)
    {
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.normals = normals;
        mesh.uv = uv;
        mesh.triangles = new int[] { 0, 1, 2 };
        return mesh;
    }
}

上一篇下一篇

猜你喜欢

热点阅读