Skip to content

Commit

Permalink
chore: refactoring (delete unnecessary code)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjae11 committed Dec 18, 2024
1 parent 1685206 commit 3f790a4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 97 deletions.
2 changes: 0 additions & 2 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/PlayerMover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class PlayerMover : MonoBehaviour
private SoftbodyGenerator _softbody;
private Player _player;

private SoftbodyGenerator _softbody;

private float _moveSpeed;
private float _jumpForce;
private float _rotationSpeed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SWPPT3.SoftbodyPhysics
{
public class RubberJump : MonoBehaviour
public class Particle : MonoBehaviour
{
private Rigidbody _rb;

Expand Down
101 changes: 7 additions & 94 deletions Assets/SWPPT3/Scripts/SoftbodyPhysics/SoftbodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public bool IsRubberJump
private NativeArray<int> _optToOriDic; // opt -> ori 를 위한 dictionary
private NativeArray<Vector3> _oriPositions;

private List<Vector3> _oriJointAnchorsList;
private Vector3[] _oriJointAnchorArray;
private NativeArray<Vector3> _bufferJointAnchors;
// private List<Vector3> _oriJointAnchorsList;
// private Vector3[] _oriJointAnchorArray;
// private NativeArray<Vector3> _bufferJointAnchors;

private List<ConfigurableJoint> _configurableJointList;
private ConfigurableJoint[] _configurableJointsArray;

private List<(int, int)> _jointsDict;
private NativeArray<(int,int)> _jointsDictNa;
// private List<(int, int)> _jointsDict;
// private NativeArray<(int,int)> _jointsDictNa;

// NativeArray for Job system

Expand Down Expand Up @@ -270,8 +270,6 @@ public void TriggerExit(Collider other)
private GameObject centerOfMasObj = null;
private Rigidbody _rbOfCenter = null;

private PhysicMaterial _physicsMaterial = null;

private bool _isJumpKey;

public bool IsJumpKey
Expand All @@ -288,21 +286,10 @@ private void Awake()
Damp = _script.Damp;
RubberJump = _script.RubberJump;

// CollissionSurfaceOffset = _script.CollissionSurfaceOffset;

_oriJointAnchorsList = new List<Vector3>();
_configurableJointList = new List<ConfigurableJoint>();
_jointsDict = new List<(int, int)>();

_isJumpKey = false;

_physicsMaterial = new PhysicMaterial();
_physicsMaterial.bounciness = 0f;
_physicsMaterial.dynamicFriction = 0f;
_physicsMaterial.staticFriction = 0f;

_physicsMaterial.bounceCombine = PhysicMaterialCombine.Maximum;

WritableVertices = new List<Vector3>();
WritableNormals = new List<Vector3>();
_phyisicedVertexes = new List<GameObject>();
Expand Down Expand Up @@ -370,7 +357,6 @@ private void Awake()
// add collider to each of vertex ( sphere collider )
var sphereColider = _tempObj.AddComponent<SphereCollider>() as SphereCollider;
sphereColider.radius = CollissionSurfaceOffset;
sphereColider.material = _physicsMaterial;

// add current collider to Collider list ;
_sphereColliderList.Add(sphereColider);
Expand All @@ -382,10 +368,11 @@ private void Awake()
_tempRigidBody.drag = PhysicsRoughness;
_tempRigidBody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;

var rubberJump = _tempObj.AddComponent<RubberJump>();
var rubberJump = _tempObj.AddComponent<Particle>();

OnRubberJump += rubberJump.SetActive;
rubberJump._softbody = this;
rubberJump.jumpForce = _script.RubberJump;

_rigidbodyList.Add(_tempRigidBody);

Expand All @@ -409,10 +396,6 @@ private void Awake()
rootRB.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
_rigidbodyList.Add(rootRB);

// var rootSC = gameObject.AddComponent<SphereCollider>();
// rootSC.radius = CollissionSurfaceOffset;
// _sphereColliderList.Add(rootSC);

_sphereColliderArray = _sphereColliderList.ToArray();
_rigidbodyArray = _rigidbodyList.ToArray();

Expand Down Expand Up @@ -489,10 +472,6 @@ private void Awake()
var destinationBody = _phyisicedVertexes[jointIndex.y].GetComponent<Rigidbody>();
float distanceBetween = Vector3.Distance(thisGameObject.transform.position, destinationBody.transform.position);

_jointsDict.Add((jointIndex.x, jointIndex.y));



// configure current spring joint
thisBodyJoint.connectedBody = destinationBody;

Expand All @@ -507,7 +486,6 @@ private void Awake()

thisBodyJoint.linearLimitSpring = Springlimit;

_oriJointAnchorsList.Add(thisBodyJoint.connectedAnchor);
_configurableJointList.Add(thisBodyJoint);
}

Expand All @@ -530,17 +508,10 @@ private void Awake()

destinationBodyJoint.massScale = 0.001f;

_jointsDict.Add((i,_rigidbodyList.Count - 1));
_oriJointAnchorsList.Add(jointIndex.transform.localPosition - _rbOfCenter.transform.localPosition);
_configurableJointList.Add(destinationBodyJoint);
}

_oriJointAnchorArray = _oriJointAnchorsList.ToArray();
_bufferJointAnchors = new NativeArray<Vector3>(_oriJointAnchorArray.Length, Allocator.Persistent);

_configurableJointsArray = _configurableJointList.ToArray();

_jointsDictNa = new NativeArray<(int, int)>(_jointsDict.ToArray(), Allocator.Persistent);
}

public void FixJoint()
Expand Down Expand Up @@ -686,23 +657,7 @@ public void Update()

public void FixedUpdate()
{
if (IsSlime)
{
var getVertexLocalPositionJob = new GetVertexLocalPositionJob
{
Buffer = _optVerticesBuffer,
};
var getVertexLocalPositionHandle = getVertexLocalPositionJob.Schedule(_optVerticesTransform);

var getConnectedAnchorJob = new GetConnectedAnchorJob
{
AnchorBuffer = _bufferJointAnchors,
JointDict = _jointsDictNa,
LocalPositions = _optVerticesBuffer
};
var getConnectedAnchorHandle = getConnectedAnchorJob.Schedule(_jointsDictNa.Length,16, getVertexLocalPositionHandle);
getConnectedAnchorHandle.Complete();
}
}

public void OnDestroy()
Expand All @@ -711,42 +666,10 @@ public void OnDestroy()
if(_optVerticesTransform.isCreated) _optVerticesTransform.Dispose();
if(_optToOriDic.IsCreated) _optToOriDic.Dispose();
if(_oriPositions.IsCreated) _oriPositions.Dispose();
if(_jointsDictNa.IsCreated) _jointsDictNa.Dispose();
if(_bufferJointAnchors.IsCreated) _bufferJointAnchors.Dispose();
}
}

/// <summary>
///
/// LocalPosition buffer에 transformacess에 있는 local position을 대입
/// </summary>
[BurstCompile]
public struct GetVertexLocalPositionJob : IJobParallelForTransform
{
public NativeArray<Vector3> Buffer;

public void Execute(int index, TransformAccess transform)
{
Buffer[index] = transform.localPosition;
}
}
[BurstCompile]
public struct GetConnectedAnchorJob : IJobParallelFor
{
[ReadOnly] public NativeArray<Vector3> LocalPositions;
[ReadOnly] public NativeArray<(int, int)> JointDict;
public NativeArray<Vector3> AnchorBuffer;


public void Execute(int index)
{
var du = JointDict[index];
var idx1 = du.Item1;
var idx2 = du.Item2;

AnchorBuffer[index] = LocalPositions[idx1] - LocalPositions[idx2];
}
}

/// <summary>
/// vertex를 optimize하면서 optimize된 vertex를 Original vertex에 대응하기 위한 job
Expand All @@ -770,14 +693,4 @@ 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);
// }
// }
}

0 comments on commit 3f790a4

Please sign in to comment.