From 2d396814059c5c82bcda48d4f1b2949fbfbc2088 Mon Sep 17 00:00:00 2001 From: Floris Date: Mon, 4 Mar 2019 22:14:54 +0100 Subject: [PATCH 1/5] Add softbody node editing --- Scripts/Editor/BSoftBodyEditor.cs | 65 ++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/Scripts/Editor/BSoftBodyEditor.cs b/Scripts/Editor/BSoftBodyEditor.cs index 39a7a70..b71b91b 100644 --- a/Scripts/Editor/BSoftBodyEditor.cs +++ b/Scripts/Editor/BSoftBodyEditor.cs @@ -1,4 +1,5 @@ -using UnityEditor; +using BulletSharp.SoftBody; +using UnityEditor; using UnityEngine; @@ -222,5 +223,67 @@ void DrawCustomMeshSettingsOptions() EditorGUILayout.Space(); } + private int _currentSelectedNode = -1; + + private void OnSceneGUI() + { + + SoftBody softBody = bSoftBodyTarget.GetCollisionObject() as SoftBody; + MeshFilter meshFilter = bSoftBodyTarget.GetComponent(); + Transform trans = bSoftBodyTarget.transform; + Vector3[] vertices = meshFilter.sharedMesh.vertices; + for (int i = 0; i < vertices.Length; i++) + { +// Vector3 position = trans.TransformPoint(vertices[i]); +// if(Handles.Button(position, Quaternion.identity, 1, 1, Handles.SphereHandleCap)) +// { +// Debug.Log(position); +// } + } + + if (softBody == null) + { + Handles.BeginGUI(); + GUILayout.Label("Viewing nodes only supported during run-time."); + Handles.EndGUI(); + return; + } + + bool clickedNode = false; + AlignedNodeArray nodes = softBody.Nodes; + for (int i = 0; i < nodes.Count; i++) + { + if (_currentSelectedNode == i) + { + float mass = softBody.GetMass(i); + Handles.BeginGUI(); + EditorGUI.BeginChangeCheck(); + 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 + { + Handles.color = Color.white; + } + BulletSharp.Math.Vector3 position = nodes[i].Position; + if(Handles.Button(position.ToUnity(), Quaternion.identity, 1, 1, Handles.SphereHandleCap)) + { + clickedNode = true; + _currentSelectedNode = i; + } + } + + if (!clickedNode && Input.GetMouseButton(0)) + { + _currentSelectedNode = -1; + } + } } } \ No newline at end of file From f5b2b5723bc682f409f397ff95a2b284b7f3710d Mon Sep 17 00:00:00 2001 From: Floris Date: Mon, 4 Mar 2019 22:49:48 +0100 Subject: [PATCH 2/5] Dynamic scale --- Scripts/Editor/BSoftBodyEditor.cs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Scripts/Editor/BSoftBodyEditor.cs b/Scripts/Editor/BSoftBodyEditor.cs index b71b91b..498e029 100644 --- a/Scripts/Editor/BSoftBodyEditor.cs +++ b/Scripts/Editor/BSoftBodyEditor.cs @@ -251,13 +251,23 @@ private void OnSceneGUI() 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) { - float mass = softBody.GetMass(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()) { @@ -270,10 +280,21 @@ private void OnSceneGUI() } else { - Handles.color = Color.white; + Color usedColor; + if (mass > 0) + { + usedColor = Color.Lerp(Color.white, Color.gray, mass / highestMass); + } + else + { + usedColor = Color.blue; + } + + Handles.color = usedColor; } - BulletSharp.Math.Vector3 position = nodes[i].Position; - if(Handles.Button(position.ToUnity(), Quaternion.identity, 1, 1, Handles.SphereHandleCap)) + 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; From 508afee26c4001eabcb130f3162816c0eed4fd5d Mon Sep 17 00:00:00 2001 From: Floris Date: Thu, 14 Mar 2019 20:15:10 +0100 Subject: [PATCH 3/5] Cleanup --- Scripts/Editor/BSoftBodyEditor.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Scripts/Editor/BSoftBodyEditor.cs b/Scripts/Editor/BSoftBodyEditor.cs index 498e029..cde21e0 100644 --- a/Scripts/Editor/BSoftBodyEditor.cs +++ b/Scripts/Editor/BSoftBodyEditor.cs @@ -230,16 +230,8 @@ private void OnSceneGUI() SoftBody softBody = bSoftBodyTarget.GetCollisionObject() as SoftBody; MeshFilter meshFilter = bSoftBodyTarget.GetComponent(); - Transform trans = bSoftBodyTarget.transform; +// Transform trans = bSoftBodyTarget.transform; Vector3[] vertices = meshFilter.sharedMesh.vertices; - for (int i = 0; i < vertices.Length; i++) - { -// Vector3 position = trans.TransformPoint(vertices[i]); -// if(Handles.Button(position, Quaternion.identity, 1, 1, Handles.SphereHandleCap)) -// { -// Debug.Log(position); -// } - } if (softBody == null) { From 2cef568b64caaafc1c898f6757265f4a96794993 Mon Sep 17 00:00:00 2001 From: Floris Date: Fri, 22 Mar 2019 13:06:37 +0100 Subject: [PATCH 4/5] Remove vertices --- Scripts/Editor/BSoftBodyEditor.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Scripts/Editor/BSoftBodyEditor.cs b/Scripts/Editor/BSoftBodyEditor.cs index cde21e0..b5f2742 100644 --- a/Scripts/Editor/BSoftBodyEditor.cs +++ b/Scripts/Editor/BSoftBodyEditor.cs @@ -230,8 +230,6 @@ private void OnSceneGUI() SoftBody softBody = bSoftBodyTarget.GetCollisionObject() as SoftBody; MeshFilter meshFilter = bSoftBodyTarget.GetComponent(); -// Transform trans = bSoftBodyTarget.transform; - Vector3[] vertices = meshFilter.sharedMesh.vertices; if (softBody == null) { From 39eff081c8d9de1785f873be6ccbbefb4eaf2bac Mon Sep 17 00:00:00 2001 From: Floris Date: Fri, 5 Apr 2019 17:43:33 +0200 Subject: [PATCH 5/5] Improve performance when not playing --- Scripts/Editor/BSoftBodyEditor.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Scripts/Editor/BSoftBodyEditor.cs b/Scripts/Editor/BSoftBodyEditor.cs index b5f2742..e969a76 100644 --- a/Scripts/Editor/BSoftBodyEditor.cs +++ b/Scripts/Editor/BSoftBodyEditor.cs @@ -227,14 +227,20 @@ void DrawCustomMeshSettingsOptions() 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; - MeshFilter meshFilter = bSoftBodyTarget.GetComponent(); if (softBody == null) { Handles.BeginGUI(); - GUILayout.Label("Viewing nodes only supported during run-time."); + GUILayout.Label("Seems like the CollisionObject has not been generated."); Handles.EndGUI(); return; }