美文网首页
2023-02-06【Gpt】niubility的一次使用 su

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

作者: 持刀的要迟到了 | 来源:发表于2023-02-05 17:10 被阅读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;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:2023-02-06【Gpt】niubility的一次使用 su

          本文链接:https://www.haomeiwen.com/subject/cmczhdtx.html