22using System . Collections . Generic ;
33using System . Linq ;
44using UnityEngine ;
5+ using UnityEditor ;
56using GK ;
7+
68public class SoftbodyGenerator : MonoBehaviour
79{
810 private MeshFilter originalMeshFilter ;
@@ -17,14 +19,133 @@ public class SoftbodyGenerator : MonoBehaviour
1719 private List < GameObject > phyisicedVertexes ;
1820 private new Dictionary < int , int > vertexDictunery ;
1921 /** public variable to controll softbody **/
20- public bool runOptimizedVersion = true ;
21- public float collissionSurfaceOffset = 0.1f ;
22- public float softness = 1f ;
23- public float damp = .2f ;
24- public float mass = 1f ;
25- public bool debugMode = false ;
26- public float physicsRoughness = 0 ;
27- private GameObject centerOfMasObj = null ;
22+ public bool runOptimizedVersion = false ;
23+ public float _collissionSurfaceOffset = 0.1f ;
24+ public float collissionSurfaceOffset
25+ {
26+ get
27+ {
28+ return _collissionSurfaceOffset ;
29+ }
30+ set
31+ {
32+ _collissionSurfaceOffset = value ;
33+ if ( phyisicedVertexes != null )
34+ foreach ( var gObject in phyisicedVertexes )
35+ gObject . GetComponent < SphereCollider > ( ) . radius = _collissionSurfaceOffset ;
36+ }
37+ }
38+
39+ public SoftJointLimitSpring springlimit ;
40+ public float _softness = 1f ;
41+ public float softness
42+ {
43+ get
44+ {
45+ return _softness ;
46+ }
47+ set
48+ {
49+ _softness = value ;
50+ if ( phyisicedVertexes != null )
51+ foreach ( var gObject in phyisicedVertexes )
52+ gObject . GetComponent < SpringJoint > ( ) . spring = _softness ;
53+
54+ springlimit . spring = _softness ;
55+ }
56+ }
57+ public float _damp = .2f ;
58+ public float damp
59+ {
60+ get
61+ {
62+ return _damp ;
63+ }
64+ set
65+ {
66+ _damp = value ;
67+ if ( phyisicedVertexes != null )
68+ foreach ( var gObject in phyisicedVertexes )
69+ gObject . GetComponent < SpringJoint > ( ) . damper = _damp ;
70+
71+ springlimit . damper = _damp ;
72+ }
73+ }
74+ public float _mass = 1f ;
75+ public float mass
76+ {
77+ get
78+ {
79+ return _mass ;
80+ }
81+ set
82+ {
83+ _mass = value ;
84+ if ( phyisicedVertexes != null )
85+ foreach ( var gObject in phyisicedVertexes )
86+ gObject . GetComponent < Rigidbody > ( ) . mass = _mass ;
87+ }
88+ }
89+
90+ private bool _debugMode = false ;
91+ public bool debugMode
92+ {
93+ get
94+ {
95+ return _debugMode ;
96+ }
97+ set
98+ {
99+ _debugMode = value ;
100+ if ( _debugMode == false )
101+ {
102+ if ( phyisicedVertexes != null )
103+ foreach ( var gObject in phyisicedVertexes )
104+ gObject . hideFlags = HideFlags . HideAndDontSave ;
105+ if ( centerOfMasObj != null )
106+ centerOfMasObj . hideFlags = HideFlags . HideAndDontSave ;
107+ } else {
108+ if ( phyisicedVertexes != null )
109+ foreach ( var gObject in phyisicedVertexes )
110+ gObject . hideFlags = HideFlags . None ;
111+ if ( centerOfMasObj != null )
112+ centerOfMasObj . hideFlags = HideFlags . None ;
113+ }
114+
115+ }
116+ }
117+
118+
119+ private float _physicsRoughness = 4 ;
120+ public float physicsRoughness {
121+ get {
122+ return _physicsRoughness ;
123+ }
124+ set {
125+ _physicsRoughness = value ;
126+ if ( phyisicedVertexes != null )
127+ foreach ( var gObject in phyisicedVertexes )
128+ gObject . GetComponent < Rigidbody > ( ) . drag = physicsRoughness ;
129+ }
130+ }
131+ private bool _gravity = true ;
132+ public bool gravity
133+ {
134+ get
135+ {
136+ return _gravity ;
137+ }
138+ set
139+ {
140+ _gravity = value ;
141+ if ( phyisicedVertexes != null )
142+ foreach ( var gObject in phyisicedVertexes )
143+ gObject . GetComponent < Rigidbody > ( ) . useGravity = _gravity ;
144+ if ( centerOfMasObj != null )
145+ centerOfMasObj . GetComponent < Rigidbody > ( ) . useGravity = _gravity ;
146+ }
147+ }
148+ public GameObject centerOfMasObj = null ;
28149 private void Awake ( )
29150 {
30151
@@ -41,6 +162,7 @@ private void Awake()
41162 originalMeshFilter . mesh . GetNormals ( writableNormals ) ;
42163 writableTris = originalMeshFilter . mesh . triangles ;
43164
165+
44166
45167 var localToWorld = transform . localToWorldMatrix ;
46168 for ( int i = 0 ; i < writableVertices . Count ; ++ i )
@@ -61,6 +183,7 @@ private void Awake()
61183 }
62184
63185 writableMesh = new Mesh ( ) ;
186+ writableMesh . MarkDynamic ( ) ;
64187 writableMesh . SetVertices ( writableVertices ) ;
65188 writableMesh . SetNormals ( writableNormals ) ;
66189 writableMesh . triangles = writableTris ;
@@ -109,8 +232,8 @@ private void Awake()
109232 _tempRigidBody . drag = physicsRoughness ;
110233 //_tempRigidBody.useGravity = false;
111234
112- if ( debugMode )
113- _tempObj . AddComponent < DebugColorGameObject > ( ) . Color = Random . ColorHSV ( ) ;
235+
236+ _tempObj . AddComponent < DebugColorGameObject > ( ) . Color = Random . ColorHSV ( ) ;
114237
115238
116239 phyisicedVertexes . Add ( _tempObj ) ;
@@ -147,18 +270,27 @@ private void Awake()
147270
148271
149272
150-
273+ // extract Lines from quad of mesh
151274 List < Vector2Int > tempListOfSprings = new List < Vector2Int > ( ) ;
152-
275+ bool isFirstTrisOfQuad = true ;
153276 for ( int i = 0 ; i < writableTris . Length ; i += 3 )
154277 {
155278 int index0 = vertexDictunery [ writableTris [ i ] ] ;
156279 int index1 = vertexDictunery [ writableTris [ i + 1 ] ] ;
157280 int index2 = vertexDictunery [ writableTris [ i + 2 ] ] ;
158-
159- tempListOfSprings . Add ( new Vector2Int ( index0 , index1 ) ) ;
160- //tempListOfSprings.Add(new Vector2Int(index1, index2));
161- tempListOfSprings . Add ( new Vector2Int ( index2 , index0 ) ) ;
281+
282+ tempListOfSprings . Add ( new Vector2Int ( index1 , index2 ) ) ;
283+ // this System convert Tris To Quad
284+ if ( isFirstTrisOfQuad )
285+ {
286+ tempListOfSprings . Add ( new Vector2Int ( index0 , index1 ) ) ;
287+ isFirstTrisOfQuad = false ;
288+ }
289+ else
290+ {
291+ tempListOfSprings . Add ( new Vector2Int ( index2 , index0 ) ) ;
292+ isFirstTrisOfQuad = true ;
293+ }
162294 }
163295
164296
@@ -216,9 +348,8 @@ private void Awake()
216348
217349 //thisBodyJoint.
218350
219- SoftJointLimitSpring springlimit = new SoftJointLimitSpring ( ) ;
220- springlimit . damper = 0.3f ;
221- springlimit . spring = 1f ;
351+ springlimit . damper = damp ;
352+ springlimit . spring = softness ;
222353
223354 thisBodyJoint . swingLimitSpring = springlimit ;
224355 thisBodyJoint . twistLimitSpring = springlimit ;
@@ -228,24 +359,29 @@ private void Awake()
228359
229360
230361 }
362+
231363 // Decelare Center of mass variable
232364 foreach ( var jointIndex in phyisicedVertexes )
233365 {
234- var destinationBodyJoint = jointIndex . AddComponent < FixedJoint > ( ) ;
366+ var destinationBodyJoint = jointIndex . AddComponent < SpringJoint > ( ) ;
235367
236368 float distanceToCenterOfmass = Vector3 . Distance (
237369 centerOfMasObj . transform . localPosition
238370 , destinationBodyJoint . transform . localPosition
239371 ) ;
240372
241373 destinationBodyJoint . connectedBody = centerOfMasObj . GetComponent < Rigidbody > ( ) ;
242-
243- destinationBodyJoint . massScale = 0.001f ;
244- destinationBodyJoint . connectedMassScale = 0.001f ;
374+ destinationBodyJoint . spring = softness ;
375+ destinationBodyJoint . damper = damp ;
376+
377+ //destinationBodyJoint.massScale = 0.001f;
378+ //destinationBodyJoint.connectedMassScale = 0.001f;
379+
245380 if ( ! runOptimizedVersion )
246381 destinationBodyJoint . enableCollision = true ;
247382
248383 }
384+
249385
250386 }
251387 List < Vector2Int > noDupesListOfSprings = new List < Vector2Int > ( ) ;
@@ -280,7 +416,7 @@ public void Update()
280416 }
281417 originalMeshFilter . mesh . vertices = tempVertexes ;
282418 originalMeshFilter . mesh . RecalculateBounds ( ) ;
283- originalMeshFilter . mesh . RecalculateTangents ( ) ;
419+ // originalMeshFilter.mesh.RecalculateTangents();
284420 originalMeshFilter . mesh . RecalculateNormals ( ) ;
285421 }
286422
@@ -289,4 +425,23 @@ public void Update()
289425public class DebugColorGameObject : MonoBehaviour
290426{
291427 public Color Color { get ; set ; }
428+ }
429+
430+ [ CustomEditor ( typeof ( SoftbodyGenerator ) ) ]
431+ public class LookAtPointEditor : Editor
432+ {
433+ public override void OnInspectorGUI ( )
434+ {
435+ SoftbodyGenerator softbody = target as SoftbodyGenerator ;
436+
437+ softbody . debugMode = EditorGUILayout . Toggle ( "#Debug mod" , softbody . debugMode ) ;
438+ EditorGUILayout . Space ( ) ;
439+
440+ softbody . gravity = EditorGUILayout . Toggle ( "Gravity" , softbody . gravity ) ;
441+ softbody . mass = EditorGUILayout . FloatField ( "Mass(KG)" , softbody . mass ) ;
442+ softbody . physicsRoughness = EditorGUILayout . FloatField ( "Drag (roughness)" , softbody . physicsRoughness ) ;
443+ softbody . softness = EditorGUILayout . FloatField ( "Softbody hardness" , softbody . softness ) ;
444+ softbody . damp = EditorGUILayout . FloatField ( "Softbody damper" , softbody . damp ) ;
445+
446+ }
292447}
0 commit comments