Browse Source

jointdrive controller added

audio
Cailean Finn 9 months ago
parent
commit
63e2ce1871
  1. 6
      Assets/5_Scenes/DreconDemo.unity
  2. 14
      Assets/5_Scenes/DreconDemo_Profiles/Camera Mound-Top.asset
  3. 40
      Assets/8_Scripts/2_General/Body.cs
  4. 152
      Assets/8_Scripts/2_General/JointDriveController.cs
  5. 11
      Assets/8_Scripts/2_General/JointDriveController.cs.meta
  6. 1276
      Assets/9_Models/Humanoid/SMPLX/SMPLXcfj.prefab
  7. 2
      Assets/9_Models/Humanoid/SMPLX/SMPLXcfj.prefab.meta
  8. 2
      Assets/ML-Agents/Timers/DreconDemo_timers.json

6
Assets/5_Scenes/DreconDemo.unity

@ -1380,7 +1380,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: eb69c2a4007c792419505438a2761ba7, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Rigidbody: {fileID: 6293844864974396996, guid: 49035d8f1718d354cac6d547795a0cb4,
m_Rigidbody: {fileID: 6293844864974396996, guid: 7fe3405e35529f240b5beac3990ab3d3,
type: 3}
m_SpawnTime: 0.2
m_DefaultPoolCapacity: 25
@ -1761,7 +1761,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 882491594}
serializedVersion: 2
m_LocalRotation: {x: 0.52918464, y: 0.000000026320038, z: -0.000000016414909, w: 0.8485067}
m_LocalRotation: {x: 0.52918464, y: -0.000000026320045, z: 0.000000016414912, w: 0.8485067}
m_LocalPosition: {x: 18.113062, y: 9.285504, z: -0.5535152}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -3816,7 +3816,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1955078056}
serializedVersion: 2
m_LocalRotation: {x: -0.0000005639385, y: 0.7132504, z: -0.7009093, w: -0.0000005738679}
m_LocalRotation: {x: 0.000000563939, y: 0.7132504, z: -0.7009093, w: 0.0000005738685}
m_LocalPosition: {x: 18.113062, y: 10.981609, z: -0.6314695}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0

14
Assets/5_Scenes/DreconDemo_Profiles/Camera Mound-Top.asset

@ -30,7 +30,7 @@ MonoBehaviour:
active: 1
quality:
m_OverrideState: 1
m_Value: 0
m_Value: 1
focusMode:
m_OverrideState: 1
m_Value: 2
@ -54,22 +54,22 @@ MonoBehaviour:
m_Value: 2.5
m_NearSampleCount:
m_OverrideState: 1
m_Value: 3
m_Value: 5
m_NearMaxBlur:
m_OverrideState: 1
m_Value: 2
m_Value: 4
m_FarSampleCount:
m_OverrideState: 1
m_Value: 4
m_Value: 7
m_FarMaxBlur:
m_OverrideState: 1
m_Value: 5
m_Value: 8
m_Resolution:
m_OverrideState: 1
m_Value: 4
m_Value: 2
m_HighQualityFiltering:
m_OverrideState: 1
m_Value: 0
m_Value: 1
m_PhysicallyBased:
m_OverrideState: 1
m_Value: 0

40
Assets/8_Scripts/2_General/Body.cs

@ -9,8 +9,48 @@ public class Body : MonoBehaviour
public Transform m_Pelvis;
public int m_DespawnTimer;
public bool m_JDActive;
[Header("Body Parts")]
public Transform m_Head;
public Transform m_Spine;
public Transform m_Left_Thigh;
public Transform m_Left_Leg;
public Transform m_Left_Foot;
public Transform m_Right_Thigh;
public Transform m_Right_Leg;
public Transform m_Right_Foot;
public Transform m_Left_Arm;
public Transform m_Left_Forearm;
public Transform m_Right_Arm;
public Transform m_Right_Forearm;
private JointDriveController m_JDController;
private void OnEnable()
{
if(m_JDController == null && m_JDActive) {
m_JDController = this.GetComponent<JointDriveController>();
m_JDController.SetupBodyPart(m_Head);
m_JDController.SetupBodyPart(m_Spine);
m_JDController.SetupBodyPart(m_Left_Thigh);
m_JDController.SetupBodyPart(m_Left_Leg);
m_JDController.SetupBodyPart(m_Left_Foot);
m_JDController.SetupBodyPart(m_Right_Thigh);
m_JDController.SetupBodyPart(m_Right_Leg);
m_JDController.SetupBodyPart(m_Right_Foot);
m_JDController.SetupBodyPart(m_Left_Arm);
m_JDController.SetupBodyPart(m_Left_Forearm);
m_JDController.SetupBodyPart(m_Right_Arm);
m_JDController.SetupBodyPart(m_Right_Forearm);
}
StartCoroutine(DespawnTimer());
}

152
Assets/8_Scripts/2_General/JointDriveController.cs

@ -0,0 +1,152 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class BodyPart
{
[Header("Body Part Info")] [Space(10)] public ConfigurableJoint joint;
public Rigidbody rb;
[HideInInspector] public Vector3 startingPos;
[HideInInspector] public Quaternion startingRot;
[HideInInspector] public JointDriveController thisJdController;
[Header("Current Joint Settings")]
[Space(10)]
public Vector3 currentEularJointRotation;
[HideInInspector] public float currentStrength;
public float currentXNormalizedRot;
public float currentYNormalizedRot;
public float currentZNormalizedRot;
[Header("Other Debug Info")]
[Space(10)]
public Vector3 currentJointForce;
public float currentJointForceSqrMag;
public Vector3 currentJointTorque;
public float currentJointTorqueSqrMag;
public AnimationCurve jointForceCurve = new AnimationCurve();
public AnimationCurve jointTorqueCurve = new AnimationCurve();
/// <summary>
/// Reset body part to initial configuration.
/// </summary>
public void Reset(BodyPart bp)
{
bp.rb.transform.position = bp.startingPos;
bp.rb.transform.rotation = bp.startingRot;
bp.rb.velocity = Vector3.zero;
bp.rb.angularVelocity = Vector3.zero;
}
/// <summary>
/// Apply torque according to defined goal `x, y, z` angle and force `strength`.
/// </summary>
public void SetJointTargetRotation(float x, float y, float z)
{
x = (x + 1f) * 0.5f;
y = (y + 1f) * 0.5f;
z = (z + 1f) * 0.5f;
var xRot = Mathf.Lerp(joint.lowAngularXLimit.limit, joint.highAngularXLimit.limit, x);
var yRot = Mathf.Lerp(-joint.angularYLimit.limit, joint.angularYLimit.limit, y);
var zRot = Mathf.Lerp(-joint.angularZLimit.limit, joint.angularZLimit.limit, z);
currentXNormalizedRot =
Mathf.InverseLerp(joint.lowAngularXLimit.limit, joint.highAngularXLimit.limit, xRot);
currentYNormalizedRot = Mathf.InverseLerp(-joint.angularYLimit.limit, joint.angularYLimit.limit, yRot);
currentZNormalizedRot = Mathf.InverseLerp(-joint.angularZLimit.limit, joint.angularZLimit.limit, zRot);
joint.targetRotation = Quaternion.Euler(xRot, yRot, zRot);
currentEularJointRotation = new Vector3(xRot, yRot, zRot);
}
public void SetJointStrength(float strength)
{
var rawVal = (strength + 1f) * 0.5f * thisJdController.maxJointForceLimit;
var jd = new JointDrive
{
positionSpring = thisJdController.maxJointSpring,
positionDamper = thisJdController.jointDampen,
maximumForce = rawVal
};
joint.slerpDrive = jd;
currentStrength = jd.maximumForce;
}
}
public class JointDriveController : MonoBehaviour
{
[Header("Joint Drive Settings")]
[Space(10)]
public float maxJointSpring;
public float jointDampen;
public float maxJointForceLimit;
[HideInInspector] public Dictionary<Transform, BodyPart> bodyPartsDict = new Dictionary<Transform, BodyPart>();
[HideInInspector] public List<BodyPart> bodyPartsList = new List<BodyPart>();
const float k_MaxAngularVelocity = 50.0f;
/// <summary>
/// Create BodyPart object and add it to dictionary.
/// </summary>
public void SetupBodyPart(Transform t)
{
var bp = new BodyPart
{
rb = t.GetComponent<Rigidbody>(),
joint = t.GetComponent<ConfigurableJoint>(),
startingPos = t.position,
startingRot = t.rotation
};
bp.rb.maxAngularVelocity = k_MaxAngularVelocity;
if (bp.joint)
{
var jd = new JointDrive
{
positionSpring = maxJointSpring,
positionDamper = jointDampen,
maximumForce = maxJointForceLimit
};
bp.joint.slerpDrive = jd;
}
bp.thisJdController = this;
bodyPartsDict.Add(t, bp);
bodyPartsList.Add(bp);
}
public void GetCurrentJointForces()
{
foreach (var bodyPart in bodyPartsDict.Values)
{
if (bodyPart.joint)
{
bodyPart.currentJointForce = bodyPart.joint.currentForce;
bodyPart.currentJointForceSqrMag = bodyPart.joint.currentForce.magnitude;
bodyPart.currentJointTorque = bodyPart.joint.currentTorque;
bodyPart.currentJointTorqueSqrMag = bodyPart.joint.currentTorque.magnitude;
if (Application.isEditor)
{
if (bodyPart.jointForceCurve.length > 1000)
{
bodyPart.jointForceCurve = new AnimationCurve();
}
if (bodyPart.jointTorqueCurve.length > 1000)
{
bodyPart.jointTorqueCurve = new AnimationCurve();
}
bodyPart.jointForceCurve.AddKey(Time.time, bodyPart.currentJointForceSqrMag);
bodyPart.jointTorqueCurve.AddKey(Time.time, bodyPart.currentJointTorqueSqrMag);
}
}
}
}
}

11
Assets/8_Scripts/2_General/JointDriveController.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e109baaf62426244eb24aec54decdf71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1276
Assets/9_Models/Humanoid/SMPLX/SMLPXModel_Loose_Joints.prefab → Assets/9_Models/Humanoid/SMPLX/SMPLXcfj.prefab

File diff suppressed because it is too large

2
Assets/9_Models/Humanoid/SMPLX/SMLPXModel_Loose_Joints.prefab.meta → Assets/9_Models/Humanoid/SMPLX/SMPLXcfj.prefab.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: feef188ff5146fc40805e1c2d1015a4f
guid: 7fe3405e35529f240b5beac3990ab3d3
PrefabImporter:
externalObjects: {}
userData:

2
Assets/ML-Agents/Timers/DreconDemo_timers.json

@ -1 +1 @@
{"count":1,"self":35.984704,"total":37.310626,"children":{"InitializeActuators":{"count":1,"self":0.001116,"total":0.001116,"children":null},"InitializeSensors":{"count":1,"self":0.002,"total":0.002,"children":null},"AgentSendState":{"count":1851,"self":0.0135468,"total":0.40610009999999996,"children":{"CollectObservations":{"count":926,"self":0.36748139999999996,"total":0.36748139999999996,"children":null},"WriteActionMask":{"count":926,"self":0.0012209,"total":0.0012209,"children":null},"RequestDecision":{"count":926,"self":0.023850999999999997,"total":0.023850999999999997,"children":null}}},"DecideAction":{"count":1851,"self":0.6228504,"total":0.6228504,"children":null},"AgentAct":{"count":1851,"self":0.29285659999999997,"total":0.29285659999999997,"children":null}},"gauges":{"DReCon-v0.CumulativeReward":{"count":1,"max":67.6433258,"min":67.6433258,"runningAverage":67.6433258,"value":67.6433258,"weightedAverage":67.6433258}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1712920398","unity_version":"2022.3.5f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.5f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\drecon-unity -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-cail -hubSessionId 3fbdf177-9b94-4cc1-adfd-c08277a8eede -accessToken odeT9cSeLBQY3FrA1yA0599oEegUBhCWJkhIJG2gUmk005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"DreconDemo","end_time_seconds":"1712920435"}}
{"count":1,"self":169.173824,"total":175.70866619999998,"children":{"InitializeActuators":{"count":1,"self":0.0017232999999999999,"total":0.0017232999999999999,"children":null},"InitializeSensors":{"count":1,"self":0.0015046,"total":0.0015046,"children":null},"AgentSendState":{"count":9952,"self":0.0856932,"total":2.6710059999999998,"children":{"CollectObservations":{"count":4976,"self":2.546122,"total":2.546122,"children":null},"WriteActionMask":{"count":4976,"self":0.0101377,"total":0.0101377,"children":null},"RequestDecision":{"count":4976,"self":0.0290531,"total":0.0290531,"children":null}}},"DecideAction":{"count":9952,"self":2.6619052,"total":2.6619051,"children":null},"AgentAct":{"count":9952,"self":1.1982021,"total":1.1982021,"children":null}},"gauges":{"DReCon-v0.CumulativeReward":{"count":2,"max":485.7487,"min":60.126297,"runningAverage":272.9375,"value":485.7487,"weightedAverage":166.531891}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1712925795","unity_version":"2022.3.5f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.5f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\drecon-unity -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-cail -hubSessionId 3fbdf177-9b94-4cc1-adfd-c08277a8eede -accessToken odeT9cSeLBQY3FrA1yA0599oEegUBhCWJkhIJG2gUmk005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"DreconDemo","end_time_seconds":"1712925970"}}
Loading…
Cancel
Save