-
Notifications
You must be signed in to change notification settings - Fork 14
/
MeshTransferToolEditor.cs
177 lines (148 loc) · 6.21 KB
/
MeshTransferToolEditor.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR
class MeshTransferToolEditor : EditorWindow
{
public static GameObject Armature;
public static List<SkinnedMeshRenderer> Mesh_Renderers = new List<SkinnedMeshRenderer>();
public static Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
private int meshNum = 0;
[MenuItem("GameObject/TransferMeshes", false, 0)]
public static void contextMenu(MenuCommand menuCommand)
{
Mesh_Renderers = new List<SkinnedMeshRenderer>();
foreach (var gameObject in Selection.gameObjects)
{
if (gameObject.GetComponent<SkinnedMeshRenderer>() == true)
{
Mesh_Renderers.Add(gameObject.GetComponent<SkinnedMeshRenderer>());
Debug.Log("MESH: " + gameObject.name);
}
else
{
Armature = gameObject.GetComponentsInChildren<Transform>()[1].gameObject;
Debug.Log("ROOT: " + Armature.name);
}
}
MeshRemap();
}
[MenuItem("Cascadian/MeshTransferTool")]
static void Init()
{
// Get existing open window or if none, make a new one:
MeshTransferToolEditor window = (MeshTransferToolEditor)EditorWindow.GetWindow(typeof(MeshTransferToolEditor));
window.Show();
}
// Put in a map every bones until there is no more children
static void GetBones(Transform pBone)
{
foreach (Transform bone in pBone){
if (bone.name.Equals("Armature")) { continue; } // skip sub-avatar in main avatar
boneMap[bone.gameObject.name] = bone;
GetBones(bone);
}
}
//https://answers.unity.com/questions/44355/shared-skeleton-and-animation-state.html
static void BoneRemap(SkinnedMeshRenderer Mesh)
{
Transform[] newBonesList = new Transform[Mesh.bones.Length];
for (int j = 0; j < Mesh.bones.Length; ++j)
{
Transform tempBone = Mesh.bones[j];
if (tempBone == null) { continue; }
GameObject bone = tempBone.gameObject;
if (!boneMap.TryGetValue(bone.name, out newBonesList[j])) // Check to see if bone exists in bone map
{
if (boneMap.TryGetValue(bone.transform.parent.name, out Transform pBone)) // try to find the parent reference in the target armature
{
//add the new bones to the target armature
var components = bone.GetComponents(typeof(Component));
foreach (var component in components)
{
//Debug.Log(component.GetComponent(component));
}
bone.transform.position += pBone.position - bone.transform.parent.position;
bone.transform.SetParent(pBone);
GetBones(pBone.transform);
boneMap.TryGetValue(bone.name, out newBonesList[j]);
}
else
{
Debug.Log("Unable to map bone \"" + bone.name + "\" to target skeleton.");
}
}
}
Mesh.bones = newBonesList;
}
static void TransferMeshes(SkinnedMeshRenderer Mesh)
{
Mesh.gameObject.transform.SetParent(Armature.gameObject.transform.parent);
Mesh.rootBone = Armature.gameObject.transform.parent.gameObject.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Hips);
}
public void OnGUI()
{
GUILayout.Label("Remap Meshes", EditorStyles.largeLabel);
GUILayout.Space(10f);
GUILayout.Label("Armature From Target Skeleton:", EditorStyles.boldLabel);
Armature = (GameObject)EditorGUILayout.ObjectField(Armature, typeof(GameObject), true, GUILayout.Height(25f));
GUILayout.Space(20f);
{ // Meshes
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Meshes to Transfer", EditorStyles.boldLabel);
GUIStyle customButton = new GUIStyle("button");
customButton.fontSize = 20;
if (GUILayout.Button("-", customButton, GUILayout.Width(25f), GUILayout.Height(25f)))
{
if (meshNum <= 0) {return;}
meshNum--;
Mesh_Renderers.RemoveAt(Mesh_Renderers.Count - 1);
}
if (GUILayout.Button("+", customButton, GUILayout.Width(25f), GUILayout.Height(25f)))
{
meshNum++;
Mesh_Renderers.Add(null);
}
EditorGUILayout.EndHorizontal();
for (int i = 0; i < meshNum; i++)
{
Mesh_Renderers[i] = (SkinnedMeshRenderer)EditorGUILayout.ObjectField(Mesh_Renderers[i], typeof(SkinnedMeshRenderer), true, GUILayout.Height(30f));
}
}
GUILayout.Space(10f);
if (GUILayout.Button("Remap Meshes"))
{
MeshRemap();
}
}
private static void MeshRemap()
{
if (Armature.gameObject.transform.parent.gameObject.GetComponent<Animator>() == null)
{
Debug.LogError("Base Armature not Humanoid. Please set rig type to humanoid to proceed.");
}
if (Armature.name.Equals("Armature")) //the gameobject must be the armature of target avatar
{
GetBones(Armature.transform);
for (int i = 0; i < Mesh_Renderers.Count; i++)
{
SkinnedMeshRenderer Mesh = Mesh_Renderers[i];
if (Mesh == null)
{
continue;
}
if (PrefabUtility.GetPrefabInstanceStatus(Mesh.transform.parent.gameObject) == PrefabInstanceStatus.Connected) //unpack prefab to move mesh and bones to the target avatar
{
PrefabUtility.UnpackPrefabInstance(Mesh.transform.parent.gameObject, unpackMode: PrefabUnpackMode.Completely, action: InteractionMode.AutomatedAction);
}
BoneRemap(Mesh);
TransferMeshes(Mesh);
}
}
else
{
Debug.LogError("Please select the \"Armature\" of target skeleton.");
}
}
}
#endif