Skip to content

Commit

Permalink
Add Runtime Editing Value of softBody Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsanwwe committed Jan 15, 2024
1 parent 0a639da commit 005f918
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 31 deletions.
9 changes: 2 additions & 7 deletions Assets/ProSoftBody/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ MonoBehaviour:
damp: 0.2
mass: 1
debugMode: 0
physicsRoughness: 0
--- !u!1 &435069491
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -606,13 +605,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d0c94ea3a1b6c9444841d7b0af4c0aed, type: 3}
m_Name:
m_EditorClassIdentifier:
runOptimizedVersion: 1
runOptimizedVersion: 0
collissionSurfaceOffset: 0.1
softness: 1000
damp: 100
mass: 1
debugMode: 0
physicsRoughness: 0
--- !u!1 &963194225
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -796,8 +794,7 @@ MonoBehaviour:
softness: 0.8
damp: 0.2
mass: 1
debugMode: 0
physicsRoughness: 0
debugMode: 1
--- !u!1 &1206348761
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1068,7 +1065,6 @@ MonoBehaviour:
damp: 0.4
mass: 1
debugMode: 0
physicsRoughness: 0
--- !u!23 &1756869007
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1431,4 +1427,3 @@ MonoBehaviour:
damp: 0.05
mass: 0.1
debugMode: 0
physicsRoughness: 5
203 changes: 179 additions & 24 deletions Assets/ProSoftBody/SoftbodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using GK;

public class SoftbodyGenerator : MonoBehaviour
{
private MeshFilter originalMeshFilter;
Expand All @@ -17,14 +19,133 @@ public class SoftbodyGenerator : MonoBehaviour
private List<GameObject> phyisicedVertexes;
private new Dictionary<int, int> vertexDictunery;
/** public variable to controll softbody **/
public bool runOptimizedVersion = true;
public float collissionSurfaceOffset = 0.1f;
public float softness = 1f;
public float damp = .2f;
public float mass = 1f;
public bool debugMode = false;
public float physicsRoughness = 0;
private GameObject centerOfMasObj = null;
public bool runOptimizedVersion = false;
public float _collissionSurfaceOffset = 0.1f;
public float collissionSurfaceOffset
{
get
{
return _collissionSurfaceOffset;
}
set
{
_collissionSurfaceOffset = value;
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.GetComponent<SphereCollider>().radius = _collissionSurfaceOffset;
}
}

public SoftJointLimitSpring springlimit;
public float _softness = 1f;
public float softness
{
get
{
return _softness;
}
set
{
_softness = value;
if(phyisicedVertexes!=null)
foreach (var gObject in phyisicedVertexes)
gObject.GetComponent<SpringJoint>().spring = _softness;

springlimit.spring = _softness;
}
}
public float _damp = .2f;
public float damp
{
get
{
return _damp;
}
set
{
_damp = value;
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.GetComponent<SpringJoint>().damper = _damp;

springlimit.damper = _damp;
}
}
public float _mass = 1f;
public float mass
{
get
{
return _mass;
}
set
{
_mass = value;
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.GetComponent<Rigidbody>().mass = _mass;
}
}

private bool _debugMode = false;
public bool debugMode
{
get
{
return _debugMode;
}
set
{
_debugMode = value;
if (_debugMode == false)
{
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.hideFlags = HideFlags.HideAndDontSave;
if (centerOfMasObj != null)
centerOfMasObj.hideFlags = HideFlags.HideAndDontSave;
} else {
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.hideFlags = HideFlags.None;
if(centerOfMasObj!=null)
centerOfMasObj.hideFlags = HideFlags.None;
}

}
}


private float _physicsRoughness = 4;
public float physicsRoughness {
get {
return _physicsRoughness;
}
set {
_physicsRoughness = value;
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.GetComponent<Rigidbody>().drag = physicsRoughness;
}
}
private bool _gravity = true;
public bool gravity
{
get
{
return _gravity;
}
set
{
_gravity = value;
if (phyisicedVertexes != null)
foreach (var gObject in phyisicedVertexes)
gObject.GetComponent<Rigidbody>().useGravity = _gravity;
if (centerOfMasObj != null)
centerOfMasObj.GetComponent<Rigidbody>().useGravity = _gravity;
}
}
public GameObject centerOfMasObj = null;
private void Awake()
{

Expand All @@ -41,6 +162,7 @@ private void Awake()
originalMeshFilter.mesh.GetNormals(writableNormals);
writableTris = originalMeshFilter.mesh.triangles;



var localToWorld = transform.localToWorldMatrix;
for (int i = 0; i < writableVertices.Count; ++i)
Expand All @@ -61,6 +183,7 @@ private void Awake()
}

writableMesh = new Mesh();
writableMesh.MarkDynamic();
writableMesh.SetVertices(writableVertices);
writableMesh.SetNormals(writableNormals);
writableMesh.triangles = writableTris;
Expand Down Expand Up @@ -109,8 +232,8 @@ private void Awake()
_tempRigidBody.drag = physicsRoughness;
//_tempRigidBody.useGravity = false;

if(debugMode)
_tempObj.AddComponent<DebugColorGameObject>().Color = Random.ColorHSV();

_tempObj.AddComponent<DebugColorGameObject>().Color = Random.ColorHSV();


phyisicedVertexes.Add(_tempObj);
Expand Down Expand Up @@ -147,18 +270,27 @@ private void Awake()




// extract Lines from quad of mesh
List<Vector2Int> tempListOfSprings = new List<Vector2Int>();

bool isFirstTrisOfQuad = true;
for (int i=0;i<writableTris.Length;i+=3)
{
int index0 = vertexDictunery[writableTris[i]];
int index1 = vertexDictunery[writableTris[i+1]];
int index2 = vertexDictunery[writableTris[i+2]];

tempListOfSprings.Add(new Vector2Int(index0, index1));
//tempListOfSprings.Add(new Vector2Int(index1, index2));
tempListOfSprings.Add(new Vector2Int(index2, index0));

tempListOfSprings.Add(new Vector2Int(index1, index2));
// this System convert Tris To Quad
if (isFirstTrisOfQuad)
{
tempListOfSprings.Add(new Vector2Int(index0, index1));
isFirstTrisOfQuad = false;
}
else
{
tempListOfSprings.Add(new Vector2Int(index2, index0));
isFirstTrisOfQuad = true;
}
}


Expand Down Expand Up @@ -216,9 +348,8 @@ private void Awake()

//thisBodyJoint.

SoftJointLimitSpring springlimit = new SoftJointLimitSpring();
springlimit.damper = 0.3f;
springlimit.spring = 1f;
springlimit.damper = damp;
springlimit.spring = softness;

thisBodyJoint.swingLimitSpring = springlimit;
thisBodyJoint.twistLimitSpring = springlimit;
Expand All @@ -228,24 +359,29 @@ private void Awake()


}

// Decelare Center of mass variable
foreach (var jointIndex in phyisicedVertexes)
{
var destinationBodyJoint = jointIndex.AddComponent<FixedJoint>();
var destinationBodyJoint = jointIndex.AddComponent<SpringJoint>();

float distanceToCenterOfmass = Vector3.Distance(
centerOfMasObj.transform.localPosition
, destinationBodyJoint.transform.localPosition
);

destinationBodyJoint.connectedBody = centerOfMasObj.GetComponent<Rigidbody>();

destinationBodyJoint.massScale = 0.001f;
destinationBodyJoint.connectedMassScale = 0.001f;
destinationBodyJoint.spring = softness;
destinationBodyJoint.damper = damp;

//destinationBodyJoint.massScale = 0.001f;
//destinationBodyJoint.connectedMassScale = 0.001f;

if (!runOptimizedVersion)
destinationBodyJoint.enableCollision = true;

}


}
List<Vector2Int> noDupesListOfSprings = new List<Vector2Int>();
Expand Down Expand Up @@ -280,7 +416,7 @@ public void Update()
}
originalMeshFilter.mesh.vertices = tempVertexes;
originalMeshFilter.mesh.RecalculateBounds();
originalMeshFilter.mesh.RecalculateTangents();
//originalMeshFilter.mesh.RecalculateTangents();
originalMeshFilter.mesh.RecalculateNormals();
}

Expand All @@ -289,4 +425,23 @@ public void Update()
public class DebugColorGameObject : MonoBehaviour
{
public Color Color { get; set; }
}

[CustomEditor(typeof(SoftbodyGenerator))]
public class LookAtPointEditor : Editor
{
public override void OnInspectorGUI()
{
SoftbodyGenerator softbody = target as SoftbodyGenerator;

softbody.debugMode = EditorGUILayout.Toggle("#Debug mod", softbody.debugMode);
EditorGUILayout.Space();

softbody.gravity = EditorGUILayout.Toggle("Gravity", softbody.gravity);
softbody.mass = EditorGUILayout.FloatField("Mass(KG)", softbody.mass);
softbody.physicsRoughness = EditorGUILayout.FloatField("Drag (roughness)", softbody.physicsRoughness);
softbody.softness = EditorGUILayout.FloatField("Softbody hardness", softbody.softness);
softbody.damp = EditorGUILayout.FloatField("Softbody damper", softbody.damp);

}
}

0 comments on commit 005f918

Please sign in to comment.