Skip to content
Draft
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion Scripts/Editor/BSoftBodyEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEditor;
using BulletSharp.SoftBody;
using UnityEditor;
using UnityEngine;


Expand Down Expand Up @@ -222,5 +223,80 @@ void DrawCustomMeshSettingsOptions()
EditorGUILayout.Space();
}

private int _currentSelectedNode = -1;

private void OnSceneGUI()
{

SoftBody softBody = bSoftBodyTarget.GetCollisionObject() as SoftBody;
MeshFilter meshFilter = bSoftBodyTarget.GetComponent<MeshFilter>();
// Transform trans = bSoftBodyTarget.transform;
Vector3[] vertices = meshFilter.sharedMesh.vertices;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks cool!

Can the vertices of the shared mesh be cached? Getting the vertices every editor frame update will generate a fair bit of garbage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's definitely a good idea, but I am actually not using the vertices at all! I was thinking about using them, but I'm using the nodes instead.

Do you have any suggestions on other properties that should be available in the editor view?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there some stiffness properties that could be edited this way? That would make adjusting the properties very intuitive.


if (softBody == null)
{
Handles.BeginGUI();
GUILayout.Label("Viewing nodes only supported during run-time.");
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;
}
}
}
}