열차 목업의 내부 확인용 프로젝트
smchoi
2024-07-31 9fe33c1ae076b0f6501619388e6b4cc872b76f80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
 
public static class GameObjectExtension
{
    /// <summary>
    /// 복제하여 서브매쉬 별로 게임오브젝트 만들기
    /// </summary>
    /// <param name="go">게임 오브젝트</param>
    /// <param name="meshCol">메쉬 콜라이더 사용여부</param>
    /// <returns>만들어진 서브매쉬 게임오브젝트</returns>
    public static GameObject[] CloneSubMeshAll(this GameObject go, bool meshCol)
    {
        MeshFilter meshFilter = go.GetComponent<MeshFilter>();
        if (meshFilter == null) return null;
 
        Mesh mesh = meshFilter.sharedMesh;
        if (mesh == null) return null;
 
        MeshRenderer meshRenderer = go.GetComponent<MeshRenderer>();
 
        int subMeshCount = mesh.subMeshCount;
        GameObject[] gos = new GameObject[subMeshCount];
        for (int i = 0; i < subMeshCount; i++)
        {
            GameObject newGo = new GameObject(string.Format("{0}_{1}", go.name, i));
            newGo.transform.SetParent(go.transform.parent);
            newGo.transform.localPosition = go.transform.localPosition;
            newGo.transform.localEulerAngles = go.transform.localEulerAngles;
            newGo.transform.localScale = go.transform.localScale;
 
            MeshFilter mf = newGo.AddComponent<MeshFilter>();
            mf.sharedMesh = mesh.CloneSubmesh(i);
            mf.sharedMesh.name = string.Format("{0}_{1}", mesh.name, i);
 
            Debug.Log(string.Format("{0}: {1}", mf.sharedMesh.name, mf.sharedMesh.vertices.Length));
 
            MeshRenderer mr = newGo.AddComponent<MeshRenderer>();
            if (meshRenderer.sharedMaterials.Length > i)
            {
                mr.sharedMaterial = meshRenderer.sharedMaterials[i];
            }
            if (meshCol)
            {
                newGo.AddComponent<MeshCollider>();
            }
 
            gos[i] = newGo;
        }
 
        return gos;
    }
 
    /// <summary>
    /// Mesh Vertex 개수 가져오기
    /// </summary>
    /// <param name="go"></param>
    /// <returns></returns>
    public static int GetVertexCount(this GameObject go)
    {
        if (go != null)
        {
            MeshFilter mf = go.GetComponent<MeshFilter>();
            if (mf != null)
            {
                Mesh mesh = mf.sharedMesh;
                if (mesh != null)
                {
                    return mesh.vertexCount;
                }
            }
        }
        return 0;
    }
 
    public static int GetMeshCount(this GameObject go)
    {
        if (go != null)
        {
            MeshFilter mf = go.GetComponent<MeshFilter>();
            if (mf != null)
            {
                Mesh mesh = mf.sharedMesh;
                if (mesh != null)
                {
                    return mesh.triangles.Length;
                }
            }
        }
        return 0;
    }
 
    /// <summary>
    /// 메트리얼 개수대로 게임오브젝트 분리하기
    /// </summary>
    /// <param name="go">게임오브젝트</param>
    /// <param name="useCollider">MeshCollider 사용여부 </param>
    /// <param name="removeNoMaterialObject">split 과정에서 발생하는 Material  없는 오브젝트 삭제 여부</param>
    /// <returns>분리된 오브젝트 배열</returns>
    public static GameObject[] SplitSubMesh(this GameObject go, bool useCollider, bool removeNoMaterialObject)
    {
        GameObject[] objs = go.CloneSubMeshAll(useCollider);
 
        if (removeNoMaterialObject)
        {
            if (objs != null)
            {
                for (int i = 0; i < objs.Length; i++)
                {
                    MeshRenderer meshRenderer = objs[i].GetComponent<MeshRenderer>();
                    if (meshRenderer.sharedMaterial == null)
                    {
                        GameObject.DestroyImmediate(objs[i]);
                    }
                }
            }
        }
        return objs;
    }
}