-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathMirrorSpringBoneWindow.cs
More file actions
executable file
·342 lines (293 loc) · 15.1 KB
/
MirrorSpringBoneWindow.cs
File metadata and controls
executable file
·342 lines (293 loc) · 15.1 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using Unity.Animations.SpringBones.GameObjectExtensions;
using UnityEditor.Localization.Editor;
namespace Unity.Animations.SpringBones
{
public class MirrorSpringBoneWindow : EditorWindow
{
private static class Styles
{
public static readonly string editorWindowTitle = L10n.Tr("SpringBone Mirror");
public static readonly GUIContent labelDoMirror = new GUIContent(L10n.Tr("Do Mirror"));
public static readonly GUIContent labelGetFromSelection = new GUIContent(L10n.Tr("Get from selection"));
public static readonly GUIContent labelSelectAllCopyDst = new GUIContent(L10n.Tr("Select all copy destination"));
public static readonly GUIContent labelSelectAllCopySrc = new GUIContent(L10n.Tr("Select all copy source"));
public static readonly GUIContent labelSelectAll = new GUIContent(L10n.Tr("Select All"));
public static readonly GUIContent labelConfigXbZero = new GUIContent(L10n.Tr("Set bone where X > 0"));
public static readonly GUIContent labelConfigXlZero = new GUIContent(L10n.Tr("Set bone where X < 0"));
public static readonly GUIContent labelSrc = new GUIContent(L10n.Tr("Source"));
public static readonly GUIContent labelDst = new GUIContent(L10n.Tr("→ Destination"));
}
public static void ShowWindow()
{
var window = GetWindow<MirrorSpringBoneWindow>(Styles.editorWindowTitle);
window.Show();
window.OnShow();
}
// private
private enum Axis { X, Y, Z }
private class BoneEntry
{
public BoneEntry(SpringBone newSourceBone, SpringBone newTargetBone)
{
sourceBone = newSourceBone;
targetBone = newTargetBone;
}
public SpringBone sourceBone;
public SpringBone targetBone;
public void ShowEntryUI(Rect rect)
{
var halfWidth = 0.5f * rect.width;
var sourceRect = new Rect(rect.x, rect.y, halfWidth, rect.height);
sourceBone = (SpringBone)EditorGUI.ObjectField(sourceRect, sourceBone, typeof(SpringBone), true);
var targetRect = new Rect(rect.x + halfWidth, rect.y, halfWidth, rect.height);
targetBone = (SpringBone)EditorGUI.ObjectField(targetRect, targetBone, typeof(SpringBone), true);
}
}
private const float Spacing = 8f;
private const float RowHeight = 30f;
private Axis mirrorAxis = Axis.X;
private List<BoneEntry> boneEntries;
private Vector2 scrollPosition;
private void OnGUI()
{
SpringBoneGUIStyles.ReacquireStyles();
if (boneEntries == null) { AcquireBonesFromSelection(); }
var uiRect = new Rect(Spacing, Spacing, position.width - Spacing * 2f, RowHeight);
uiRect = ShowUtilityButtons(uiRect);
uiRect = ShowBoneList(uiRect);
if (GUI.Button(uiRect, Styles.labelDoMirror, SpringBoneGUIStyles.ButtonStyle))
{
PerformMirror();
}
}
private Rect ShowUtilityButtons(Rect uiRect)
{
var buttonOffset = uiRect.height + Spacing;
if (GUI.Button(uiRect, Styles.labelGetFromSelection, SpringBoneGUIStyles.ButtonStyle))
{
AcquireBonesFromSelection();
}
uiRect.y += buttonOffset;
var halfRectWidth = 0.5f * (uiRect.width - Spacing);
var halfButtonRect = new Rect(uiRect.x, uiRect.y, halfRectWidth, uiRect.height);
if (GUI.Button(halfButtonRect, Styles.labelConfigXlZero, SpringBoneGUIStyles.ButtonStyle))
{
AcquireSourceBonesOnSideOfAxis(true);
}
halfButtonRect.x += halfRectWidth + Spacing;
if (GUI.Button(halfButtonRect, Styles.labelConfigXbZero, SpringBoneGUIStyles.ButtonStyle))
{
AcquireSourceBonesOnSideOfAxis(false);
}
uiRect.y += buttonOffset;
halfButtonRect.x = uiRect.x;
halfButtonRect.y = uiRect.y;
if (GUI.Button(halfButtonRect, Styles.labelSelectAllCopySrc, SpringBoneGUIStyles.ButtonStyle))
{
var sourceBones = boneEntries.Select(entry => entry.sourceBone).Where(bone => bone != null);
if (sourceBones.Any()) { Selection.objects = sourceBones.Select(bone => bone.gameObject).ToArray(); }
}
halfButtonRect.x += halfRectWidth + Spacing;
if (GUI.Button(halfButtonRect, Styles.labelSelectAllCopyDst, SpringBoneGUIStyles.ButtonStyle))
{
var targetBones = boneEntries.Select(entry => entry.targetBone).Where(bone => bone != null);
if (targetBones.Any()) { Selection.objects = targetBones.Select(bone => bone.gameObject).ToArray(); }
}
uiRect.y += buttonOffset;
if (GUI.Button(uiRect, Styles.labelSelectAll, SpringBoneGUIStyles.ButtonStyle))
{
var bonesToSelect = new List<SpringBone>();
bonesToSelect.AddRange(boneEntries.Select(entry => entry.sourceBone).Where(bone => bone != null));
bonesToSelect.AddRange(boneEntries.Select(entry => entry.targetBone).Where(bone => bone != null));
if (bonesToSelect.Any()) { Selection.objects = bonesToSelect.Select(bone => bone.gameObject).ToArray(); }
}
uiRect.y += buttonOffset;
return uiRect;
}
private Rect ShowBoneList(Rect uiRect)
{
var listBoxBottom = position.height - (Spacing * 2f + RowHeight);
var headerRowRect = new Rect(uiRect.x, uiRect.y, uiRect.width * 0.5f, uiRect.height);
GUI.Label(headerRowRect, Styles.labelSrc, SpringBoneGUIStyles.LabelStyle);
headerRowRect.x += headerRowRect.width;
GUI.Label(headerRowRect, Styles.labelDst, SpringBoneGUIStyles.LabelStyle);
uiRect.y += uiRect.height;
const float ScrollbarWidth = 20f;
const float EntryHeight = 24f;
var listBoxRect = new Rect(uiRect.x, uiRect.y, uiRect.width, listBoxBottom - uiRect.y);
var entryCount = boneEntries.Count;
var listContentsRect = new Rect(0f, 0f, uiRect.width - ScrollbarWidth, entryCount * EntryHeight);
scrollPosition = GUI.BeginScrollView(listBoxRect, scrollPosition, listContentsRect);
var entryRect = new Rect(0f, 0f, listContentsRect.width, EntryHeight);
for (int entryIndex = 0; entryIndex < entryCount; entryIndex++)
{
boneEntries[entryIndex].ShowEntryUI(entryRect);
entryRect.y += entryRect.height;
}
GUI.EndScrollView();
uiRect.y = listBoxBottom + Spacing;
return uiRect;
}
private static Vector3 MirrorPosition(Vector3 originalPosition, Axis mirrorAxis)
{
var targetPosition = originalPosition;
switch (mirrorAxis)
{
case Axis.X: targetPosition.x = -targetPosition.x; break;
case Axis.Y: targetPosition.y = -targetPosition.y; break;
case Axis.Z: targetPosition.z = -targetPosition.z; break;
}
return targetPosition;
}
private static T FindMirroredComponent<T>(GameObject rootObject, T sourceObject, Axis mirrorAxis, float threshold)
where T : Component
{
if (rootObject == null
|| sourceObject == null)
{
return null;
}
var possibleTargets = rootObject.GetComponentsInChildren<T>(true);
var targetPosition = MirrorPosition(sourceObject.transform.position, mirrorAxis);
var squaredThreshold = threshold * threshold;
return possibleTargets
.Where(item => (item.transform.position - targetPosition).sqrMagnitude <= squaredThreshold)
.FirstOrDefault();
}
private static IEnumerable<T> FindMirroredComponents<T>
(
GameObject rootObject,
IEnumerable<T> sourceObjects,
Axis mirrorAxis,
float threshold = 0.001f
) where T : Component
{
return sourceObjects.Select(item => FindMirroredComponent(rootObject, item, mirrorAxis, threshold));
}
private static SpringBone FindMirroredSpringBone(SpringBone sourceBone, Axis mirrorAxis, float threshold = 0.001f)
{
var manager = sourceBone.GetComponentInParent<SpringManager>();
return FindMirroredComponent(manager.gameObject, sourceBone, mirrorAxis, threshold);
}
private void AcquireBonesFromSelection()
{
if (boneEntries == null) { boneEntries = new List<BoneEntry>(); }
var selectedBones = Selection.gameObjects
.SelectMany(gameObject => gameObject.GetComponents<SpringBone>());
var newBoneEntries = selectedBones
.Select(bone => new BoneEntry(bone, FindMirroredSpringBone(bone, mirrorAxis)));
foreach (var entry in newBoneEntries.Where(entry => entry.sourceBone == entry.targetBone))
{
entry.targetBone = null;
}
boneEntries = newBoneEntries.ToList();
}
private void AcquireSourceBonesOnSideOfAxis(bool getLessThanZero)
{
if (boneEntries == null) { boneEntries = new List<BoneEntry>(); }
// Is there a better way of defining this?
System.Func<SpringBone, bool> boneMatches = item => false;
if (getLessThanZero)
{
switch (mirrorAxis)
{
case Axis.X: boneMatches = item => item.transform.position.x < 0f; break;
case Axis.Y: boneMatches = item => item.transform.position.y < 0f; break;
case Axis.Z: boneMatches = item => item.transform.position.z < 0f; break;
}
}
else
{
switch (mirrorAxis)
{
case Axis.X: boneMatches = item => item.transform.position.x > 0f; break;
case Axis.Y: boneMatches = item => item.transform.position.y > 0f; break;
case Axis.Z: boneMatches = item => item.transform.position.z > 0f; break;
}
}
var allSpringBones = GameObjectUtil.FindComponentsOfType<SpringBone>();
var selectedBones = allSpringBones.Where(boneMatches);
var newBoneEntries = selectedBones
.Select(bone => new BoneEntry(bone, FindMirroredSpringBone(bone, mirrorAxis)))
.Where(entry => entry.targetBone != null
&& entry.targetBone != entry.sourceBone);
boneEntries = newBoneEntries.ToList();
}
private void MirrorPivot(SpringBone sourceBone, SpringBone targetBone)
{
var sourcePivot = sourceBone.pivotNode;
var targetPivot = targetBone.pivotNode;
var targetPosition = MirrorPosition(sourcePivot.position, mirrorAxis);
var pivotDirection = -sourcePivot.right;
pivotDirection = MirrorPosition(pivotDirection, mirrorAxis);
var upDirection = MirrorPosition(sourcePivot.forward, mirrorAxis);
targetPivot.position = targetPosition;
targetPivot.LookAt(targetPosition + pivotDirection, upDirection);
targetPivot.Rotate(-90f, 90f, 0f, Space.Self);
}
private void PerformMirror()
{
var mirrorItems = boneEntries.Where(
item => item.sourceBone != null
&& item.targetBone != null
&& item.sourceBone != item.targetBone);
var undoItems = mirrorItems.Select(item => (Component)item.targetBone).ToList();
var allSkinBones = GameObjectUtil.FindComponentsOfType<SkinnedMeshRenderer>()
.SelectMany(renderer => renderer.bones)
.Distinct()
.ToArray();
var editablePivots = mirrorItems
.Select(item => item.targetBone.pivotNode)
.Where(pivotNode => pivotNode != null
&& SpringBoneSetup.IsPivotProbablySafeToDestroy(pivotNode, allSkinBones))
.ToArray();
undoItems.AddRange(editablePivots);
Undo.RecordObjects(undoItems.ToArray(), "Mirror SpringBones");
foreach (var mirrorItem in mirrorItems)
{
var sourceBone = mirrorItem.sourceBone;
var targetBone = mirrorItem.targetBone;
var rootManager = targetBone.GetComponentInParent<SpringManager>();
if (rootManager == null) { continue; }
targetBone.stiffnessForce = sourceBone.stiffnessForce;
targetBone.dragForce = sourceBone.dragForce;
targetBone.springForce = sourceBone.springForce;
targetBone.windInfluence = sourceBone.windInfluence;
if (editablePivots.Contains(targetBone.pivotNode))
{
MirrorPivot(sourceBone, targetBone);
}
targetBone.angularStiffness = sourceBone.angularStiffness;
sourceBone.yAngleLimits.CopyTo(targetBone.yAngleLimits);
sourceBone.zAngleLimits.CopyTo(targetBone.zAngleLimits);
targetBone.yAngleLimits.min = -sourceBone.yAngleLimits.max;
targetBone.yAngleLimits.max = -sourceBone.yAngleLimits.min;
targetBone.lengthLimitTargets = FindMirroredComponents(
rootManager.gameObject, sourceBone.lengthLimitTargets, mirrorAxis)
.Where(item => item != null)
.ToArray();
targetBone.radius = sourceBone.radius;
targetBone.sphereColliders = FindMirroredComponents(
rootManager.gameObject, sourceBone.sphereColliders, mirrorAxis)
.Where(item => item != null)
.ToArray();
targetBone.capsuleColliders = FindMirroredComponents(
rootManager.gameObject, sourceBone.capsuleColliders, mirrorAxis)
.Where(item => item != null)
.ToArray();
targetBone.panelColliders = FindMirroredComponents(
rootManager.gameObject, sourceBone.panelColliders, mirrorAxis)
.Where(item => item != null)
.ToArray();
}
}
private void OnShow()
{
AcquireBonesFromSelection();
}
}
}