-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBSoftBodyEditor.cs
More file actions
306 lines (254 loc) · 12.6 KB
/
BSoftBodyEditor.cs
File metadata and controls
306 lines (254 loc) · 12.6 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
using BulletSharp.SoftBody;
using UnityEditor;
using UnityEngine;
namespace BulletUnity
{
[CustomEditor(typeof(BSoftBody), true)]
public class BSoftBodyEditor : Editor
{
public BSoftBody bSoftBodyTarget;
SerializedProperty softBodySettings;
//BulletSharp.SoftBody.Collisions collisions; //bitmask field for collisions
GUIContent gcSoftBodySettings = new GUIContent("SoftBodySettings");
//GUIContent gcSoftBodyMeshSettings = new GUIContent("SoftBodyMeshSettings");
//used to hide specific serialized properties in the editor
//Hide softBody Settings until we want to display it, also script name
private static readonly string[] hideMe = new string[] { "_softBodySettings", "m_Script"};
const string collisionTooltip = "Collisions flags\n" +
"SDF_RS Rigid versus soft mask.\n" +
"CL_RS: SDF based rigid vs soft.\n" +
"SVSmask: Cluster vs convex rigid vs soft.\n" +
"VF_SS: Rigid versus soft mask.\n" +
"CL_SS:Vertex vs face soft vs soft handling.\n" +
"CL_SELF: Cluster vs cluster soft vs soft handling.\n" +
"Default: Cluster soft body self collision.";
GUIContent gcCollisionTooltip = new GUIContent("Soft vs. Rigid Mask", collisionTooltip);
Color GUIBlue = new Color32(82, 140, 255, 255);
BAnyMeshSettingsForEditor inspectorMeshSettings;
public void OnEnable()
{
bSoftBodyTarget = (BSoftBody)target;
softBodySettings = serializedObject.FindProperty("_softBodySettings");
if (inspectorMeshSettings == null)
{
inspectorMeshSettings = new BAnyMeshSettingsForEditor();
}
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorHelpers.DrawLogoAndVersion();
if (bSoftBodyTarget is BSoftBodyWMesh)
{
DrawCustomMeshSettingsOptions();
}
DrawPropertiesExcluding(serializedObject, hideMe); //Draw settings after the default inspector
if (target is BSoftBodyPartOnSkinnedMesh)
{
BSoftBodyPartOnSkinnedMesh sb = (BSoftBodyPartOnSkinnedMesh)target;
if (EditorHelpers.InspectorButton("Bind Bones To Soft Body & Nodes To Anchors", 300, 15, GUIBlue))
{
sb.BindBonesToSoftBodyAndNodesToAnchors();
}
EditorGUILayout.HelpBox(sb.DescribeBonesAndAnchors(), MessageType.Info);
if (sb.SoftBodySettings.sBpresetSelect != SBSettingsPresets.ShapeMatching)
{
EditorGUILayout.HelpBox("For a soft body mesh the preset should probably be 'shape matching' or 'Volume'", MessageType.Warning);
}
}
EditorGUILayout.BeginHorizontal();
if (EditorHelpers.InspectorButton("Apply Preset", 100, 15, GUIBlue))
{
//SBSettingsPresets saveMe = bSoftBodyTarget.SoftBodySettings.sBpresetSelect;
bSoftBodyTarget.SoftBodySettings.ResetToSoftBodyPresets(bSoftBodyTarget.SoftBodySettings.sBpresetSelect);
//bSoftBodyTarget.SoftBodySettings.sBpresetSelect = saveMe;
}
bSoftBodyTarget.SoftBodySettings.sBpresetSelect = (SBSettingsPresets)EditorGUILayout.EnumPopup(bSoftBodyTarget.SoftBodySettings.sBpresetSelect);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
//bitmask field for collisions
bSoftBodyTarget.SoftBodySettings.config.Collisions = (BulletSharp.SoftBody.CollisionFlags) EditorGUILayout.EnumMaskField(gcCollisionTooltip, bSoftBodyTarget.SoftBodySettings.config.Collisions);
EditorGUILayout.PropertyField(softBodySettings, gcSoftBodySettings, true);
serializedObject.ApplyModifiedProperties();
if (GUI.changed) //Can apply settings on editor change
{
bSoftBodyTarget.BuildSoftBody();
}
serializedObject.ApplyModifiedProperties();
}
//Menu items here
#region MenuBulletForUnity
[MenuItem("BulletForUnity/BSoftBody/Rope")]
[MenuItem("GameObject/Create Other/BulletForUnity/BSoftBody/Rope")] //right click menu
static void CreateBSoftBodyRope()
{
Selection.activeObject = BSoftBodyRope.CreateNew(EditorHelpers.GetCameraRaycastPosition(), Quaternion.identity, false);
PostCreateObject();
}
[MenuItem("BulletForUnity/BSoftBody/BSoftBodyWMesh")]
[MenuItem("GameObject/Create Other/BulletForUnity/BSoftBody/BSoftBodyWMesh")] //right click menu
static void CreateBSoftWithMesh()
{
BAnyMeshSettings settings = new BAnyMeshSettings();
settings.meshType = PrimitiveMeshOptions.Bunny;
Selection.activeObject = BSoftBodyWMesh.CreateNew(EditorHelpers.GetCameraRaycastPosition(), Quaternion.identity, settings.Build(), true, SBSettingsPresets.ShapeMatching);
PostCreateObject();
}
/// <summary>
/// Stuff to do after creation like sort script order
/// </summary>
static void PostCreateObject()
{
BSoftBodyComponentOrderSorter.SortComponents((GameObject)Selection.activeObject); //order the scripts, looks nicer
}
#endregion
//Hackish method to get past Unity serialization
void DrawCustomMeshSettingsOptions()
{
EditorGUILayout.LabelField("Generate Custom Mesh?",EditorStyles.boldLabel);
//Get Instance
BAnyMeshSettingsForEditor bAny = inspectorMeshSettings; //BAnyMeshSettingsForEditor.Instance;
//Build it!
if (EditorHelpers.InspectorButton("Update Mesh", 100, 15, GUIBlue, "New/Change mesh"))
{
((BSoftBodyWMesh)bSoftBodyTarget).meshSettings.UserMesh = bAny.Build();
bSoftBodyTarget.BuildSoftBody();
}
bAny.imediateUpdate = EditorGUILayout.Toggle("Imediate Update", bAny.imediateUpdate);
//Select a mesh type
bAny.meshType = (PrimitiveMeshOptions)EditorGUILayout.EnumPopup("Mesh Type", bAny.meshType);
switch (bAny.meshType)
{
case PrimitiveMeshOptions.UserDefinedMesh:
bAny.userMesh = (Mesh) EditorGUILayout.ObjectField(bAny.userMesh, typeof(Mesh),false);
break;
case PrimitiveMeshOptions.Box:
bAny.extents = EditorGUILayout.Vector3Field("Extents", bAny.extents);
break;
case PrimitiveMeshOptions.Sphere:
bAny.radius = EditorGUILayout.FloatField("radius", bAny.radius);
bAny.numLongitudeLines = EditorGUILayout.IntField("numLongitudeLines", bAny.numLongitudeLines);
bAny.numLatitudeLines = EditorGUILayout.IntField("numLatitudeLines", bAny.numLatitudeLines);
break;
case PrimitiveMeshOptions.Cylinder:
bAny.height = EditorGUILayout.FloatField("height", bAny.height);
bAny.radius = EditorGUILayout.FloatField("radius", bAny.radius);
bAny.nbSides = EditorGUILayout.IntField("nbSides", bAny.nbSides);
break;
case PrimitiveMeshOptions.Cone:
bAny.height = EditorGUILayout.FloatField("height", bAny.height);
bAny.radius = EditorGUILayout.FloatField("radius", bAny.radius);
bAny.nbSides = EditorGUILayout.IntField("nbSides", bAny.nbSides);
break;
case PrimitiveMeshOptions.Pyramid:
bAny.height = EditorGUILayout.FloatField("height", bAny.height);
bAny.radius = EditorGUILayout.FloatField("radius", bAny.radius);
break;
case PrimitiveMeshOptions.Bunny:
break;
case PrimitiveMeshOptions.Plane:
bAny.length = EditorGUILayout.FloatField("length", bAny.length);
bAny.width = EditorGUILayout.FloatField("width", bAny.width);
bAny.resX = EditorGUILayout.IntField("resX", bAny.resX);
bAny.resZ = EditorGUILayout.IntField("resZ", bAny.resZ);
break;
default:
break;
}
//limit the fields [Range()] doesnt work
bAny.extents.x = Mathf.Clamp(bAny.extents.x, 0f, 10000f);
bAny.extents.y = Mathf.Clamp(bAny.extents.y, 0f, 10000f);
bAny.extents.z = Mathf.Clamp(bAny.extents.z, 0f, 10000f);
bAny.radius = Mathf.Clamp(bAny.radius, 0f, 10000f);
bAny.numLatitudeLines = Mathf.Clamp(bAny.numLatitudeLines, 2, 100);
bAny.numLongitudeLines = Mathf.Clamp(bAny.numLongitudeLines, 2, 100);
bAny.height = Mathf.Clamp(bAny.height, 0, 100);
bAny.nbSides = Mathf.Clamp(bAny.nbSides, 2, 100);
bAny.length = Mathf.Clamp(bAny.length, 0, 1000);
bAny.width = Mathf.Clamp(bAny.width, 0, 1000);
bAny.resX = Mathf.Clamp(bAny.resX, 2, 100);
bAny.resZ = Mathf.Clamp(bAny.resZ, 2, 100);
//AutoMagickally change settings is edited
if (GUI.changed && bAny.imediateUpdate) //Can apply settings on editor change
{
((BSoftBodyWMesh)bSoftBodyTarget).meshSettings.UserMesh = bAny.Build();
bSoftBodyTarget.BuildSoftBody();
}
EditorGUILayout.LabelField("Mesh Settings", EditorStyles.boldLabel);
EditorGUILayout.Space();
}
private int _currentSelectedNode = -1;
private void OnSceneGUI()
{
if (!EditorApplication.isPlaying)
{
Handles.BeginGUI();
GUILayout.Label("Viewing nodes only supported while playing.");
Handles.EndGUI();
return;
}
SoftBody softBody = bSoftBodyTarget.GetCollisionObject() as SoftBody;
if (softBody == null)
{
Handles.BeginGUI();
GUILayout.Label("Seems like the CollisionObject has not been generated.");
Handles.EndGUI();
return;
}
bool clickedNode = false;
AlignedNodeArray nodes = softBody.Nodes;
float highestMass = 0;
for (int i = 0; i < nodes.Count; i++)
{
float invMass = nodes[i].InverseMass;
if (invMass > 0 && 1 / invMass > highestMass)
{
highestMass = 1 / invMass;
}
}
for (int i = 0; i < nodes.Count; i++)
{
float mass = softBody.GetMass(i);
if (_currentSelectedNode == i)
{
Handles.BeginGUI();
EditorGUI.BeginChangeCheck();
EditorGUILayout.LabelField(string.Format("id: {0}", i));
mass = EditorGUILayout.Slider("Mass",mass, 0, Mathf.Clamp(mass * 10, 1, 100), GUILayout.Width(500));
if (EditorGUI.EndChangeCheck())
{
softBody.SetMass(i, mass);
nodes[i].Velocity = BulletSharp.Math.Vector3.Zero;
nodes[i].Force = BulletSharp.Math.Vector3.Zero;
}
Handles.EndGUI();
Handles.color = Color.green;
}
else
{
Color usedColor;
if (mass > 0)
{
usedColor = Color.Lerp(Color.white, Color.gray, mass / highestMass);
}
else
{
usedColor = Color.blue;
}
Handles.color = usedColor;
}
Vector3 position = nodes[i].Position.ToUnity();
float size = HandleUtility.GetHandleSize(position) * 0.5f;
if(Handles.Button(position, Quaternion.identity, size, size , Handles.SphereHandleCap))
{
clickedNode = true;
_currentSelectedNode = i;
}
}
if (!clickedNode && Input.GetMouseButton(0))
{
_currentSelectedNode = -1;
}
}
}
}