diff --git a/Unity/OpenSeeIKTarget.cs b/Unity/OpenSeeIKTarget.cs new file mode 100644 index 0000000..b8f1d2a --- /dev/null +++ b/Unity/OpenSeeIKTarget.cs @@ -0,0 +1,46 @@ +using UnityEngine; + +public class OpenSeeIKTarget : MonoBehaviour +{ + public OpenSee.OpenSee openSee; + public bool calibrate = true; + public int faceId = 0; + public float translationScale = 0.3f; + public float smooth = 0.2f; + double updated = 0.0f; + Quaternion dR; + Vector3 dT; + + void Update() + { + var openSeeData = openSee.trackingData; + if (openSeeData == null || openSeeData.Length < 1) + return; + int idx = -1; + int i = 0; + foreach (var item in openSeeData) { + if (item.id == faceId) + idx = i; + i++; + } + if (idx < 0) + return; + if (openSeeData[idx].time > updated) { + updated = openSeeData[idx].time; + } else { + return; + } + + if (calibrate) { + calibrate = false; + dR = Quaternion.Euler(-openSeeData[idx].rotation.x, openSeeData[idx].rotation.y, -openSeeData[0].rotation.z); + dR = Quaternion.Inverse(dR); + dT = openSeeData[idx].translation; + } + + Vector3 v = openSeeData[idx].translation - dT; + v.z = -v.z; + transform.localPosition = Vector3.Lerp(transform.localPosition, v * translationScale, 1f - smooth); + transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(-openSeeData[idx].rotation.x, openSeeData[idx].rotation.y, -openSeeData[idx].rotation.z) * dR, 1f - smooth); + } +} diff --git a/Unity/OpenSeeIKTarget.cs.meta b/Unity/OpenSeeIKTarget.cs.meta new file mode 100644 index 0000000..2507d88 --- /dev/null +++ b/Unity/OpenSeeIKTarget.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 16cac0ed1df366e4cbb82f6c6a93ef1a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: