Initialer Upload neues Unity-Projekt
This commit is contained in:
@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Oculus.Interaction.Body.Input;
|
||||
|
||||
namespace Oculus.Interaction.Body.PoseDetection
|
||||
{
|
||||
/// <summary>
|
||||
/// Compares a user-provided set of joints between two Body Poses.
|
||||
/// </summary>
|
||||
public class BodyPoseComparerActiveState :
|
||||
MonoBehaviour, IActiveState
|
||||
{
|
||||
public struct BodyPoseComparerFeatureState
|
||||
{
|
||||
public readonly float Delta;
|
||||
|
||||
public readonly float MaxDelta;
|
||||
|
||||
public BodyPoseComparerFeatureState(float delta, float maxDelta)
|
||||
{
|
||||
Delta = delta;
|
||||
MaxDelta = maxDelta;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class JointComparerConfig
|
||||
{
|
||||
[Tooltip("The joint to compare from each Body Pose")]
|
||||
public BodyJointId Joint = BodyJointId.Body_Head;
|
||||
|
||||
[Min(0)]
|
||||
[Tooltip("The maximum angle that two joint rotations " +
|
||||
"can be from each other to be considered equal.")]
|
||||
public float MaxDelta = 30f;
|
||||
|
||||
[Tooltip("The width of the threshold when transitioning " +
|
||||
"states. " + nameof(Width) + " / 2 is added to " +
|
||||
nameof(MaxDelta) + " when leaving Active state, and " +
|
||||
"subtracted when entering.")]
|
||||
[Min(0)]
|
||||
public float Width = 4f;
|
||||
}
|
||||
|
||||
[Tooltip("The first body pose to compare.")]
|
||||
[SerializeField, Interface(typeof(IBodyPose))]
|
||||
private UnityEngine.Object _poseA;
|
||||
private IBodyPose PoseA;
|
||||
|
||||
[Tooltip("The second body pose to compare.")]
|
||||
[SerializeField, Interface(typeof(IBodyPose))]
|
||||
private UnityEngine.Object _poseB;
|
||||
private IBodyPose PoseB;
|
||||
|
||||
[SerializeField]
|
||||
private List<JointComparerConfig> _configs =
|
||||
new List<JointComparerConfig>()
|
||||
{
|
||||
new JointComparerConfig()
|
||||
};
|
||||
|
||||
[Tooltip("A new state must be maintaned for at least this " +
|
||||
"many seconds before the Active property changes.")]
|
||||
[SerializeField]
|
||||
private float _minTimeInState = 0.05f;
|
||||
|
||||
public float MinTimeInState
|
||||
{
|
||||
get => _minTimeInState;
|
||||
set => _minTimeInState = value;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<JointComparerConfig, BodyPoseComparerFeatureState> FeatureStates =>
|
||||
_featureStates;
|
||||
|
||||
private Dictionary<JointComparerConfig, BodyPoseComparerFeatureState> _featureStates =
|
||||
new Dictionary<JointComparerConfig, BodyPoseComparerFeatureState>();
|
||||
|
||||
private Func<float> _timeProvider;
|
||||
private bool _isActive;
|
||||
private bool _internalActive;
|
||||
private float _lastStateChangeTime;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
PoseA = _poseA as IBodyPose;
|
||||
PoseB = _poseB as IBodyPose;
|
||||
_timeProvider = () => Time.time;
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
this.AssertField(PoseA, nameof(PoseA));
|
||||
this.AssertField(PoseB, nameof(PoseB));
|
||||
}
|
||||
|
||||
public bool Active
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!isActiveAndEnabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasActive = _internalActive;
|
||||
_internalActive = true;
|
||||
foreach (var config in _configs)
|
||||
{
|
||||
float maxDelta = wasActive ?
|
||||
config.MaxDelta + config.Width / 2f :
|
||||
config.MaxDelta - config.Width / 2f;
|
||||
|
||||
bool withinDelta = GetJointDelta(config.Joint, out float delta) &&
|
||||
Mathf.Abs(delta) <= maxDelta;
|
||||
_featureStates[config] = new BodyPoseComparerFeatureState(delta, maxDelta);
|
||||
_internalActive &= withinDelta;
|
||||
}
|
||||
float time = _timeProvider();
|
||||
if (wasActive != _internalActive)
|
||||
{
|
||||
_lastStateChangeTime = time;
|
||||
}
|
||||
if (time - _lastStateChangeTime >= _minTimeInState)
|
||||
{
|
||||
_isActive = _internalActive;
|
||||
}
|
||||
return _isActive;
|
||||
}
|
||||
}
|
||||
|
||||
private bool GetJointDelta(BodyJointId joint, out float delta)
|
||||
{
|
||||
if (!PoseA.GetJointPoseLocal(joint, out Pose localA) ||
|
||||
!PoseB.GetJointPoseLocal(joint, out Pose localB))
|
||||
{
|
||||
delta = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
delta = Quaternion.Angle(localA.rotation, localB.rotation);
|
||||
return true;
|
||||
}
|
||||
|
||||
#region Inject
|
||||
public void InjectAllBodyPoseComparerActiveState(
|
||||
IBodyPose poseA, IBodyPose poseB,
|
||||
IEnumerable<JointComparerConfig> configs)
|
||||
{
|
||||
InjectPoseA(poseA);
|
||||
InjectPoseB(poseB);
|
||||
InjectJoints(configs);
|
||||
}
|
||||
|
||||
public void InjectPoseA(IBodyPose poseA)
|
||||
{
|
||||
_poseA = poseA as UnityEngine.Object;
|
||||
PoseA = poseA;
|
||||
}
|
||||
|
||||
public void InjectPoseB(IBodyPose poseB)
|
||||
{
|
||||
_poseB = poseB as UnityEngine.Object;
|
||||
PoseB = poseB;
|
||||
}
|
||||
|
||||
public void InjectJoints(IEnumerable<JointComparerConfig> configs)
|
||||
{
|
||||
_configs = new List<JointComparerConfig>(configs);
|
||||
}
|
||||
|
||||
public void InjectOptionalTimeProvider(Func<float> timeProvider)
|
||||
{
|
||||
_timeProvider = timeProvider;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ebf7b9e4cfb7d2e4aa5a2dccb5ee7d0f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using Oculus.Interaction.Body.Input;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Oculus.Interaction.Body.PoseDetection
|
||||
{
|
||||
public class BodyPoseComparerActiveStateDebugVisual : MonoBehaviour
|
||||
{
|
||||
[Tooltip("The PoseComparer to debug.")]
|
||||
[SerializeField]
|
||||
private BodyPoseComparerActiveState _bodyPoseComparer;
|
||||
|
||||
[Tooltip("Gizmos will be drawn at joint positions of this body pose.")]
|
||||
[SerializeField, Interface(typeof(IBodyPose))]
|
||||
private UnityEngine.Object _bodyPose;
|
||||
private IBodyPose BodyPose;
|
||||
|
||||
[Tooltip("The root transform of the body skeleton. Debug " +
|
||||
"gizmos will be drawn in the local space of this transform.")]
|
||||
[SerializeField]
|
||||
private Transform _root;
|
||||
|
||||
[Tooltip("The radius of the debug spheres.")]
|
||||
[SerializeField, Delayed]
|
||||
private float _radius = 0.1f;
|
||||
|
||||
public float Radius
|
||||
{
|
||||
get => _radius;
|
||||
set => _radius = value;
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
BodyPose = _bodyPose as IBodyPose;
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
this.AssertField(_bodyPoseComparer, nameof(_bodyPoseComparer));
|
||||
this.AssertField(BodyPose, nameof(BodyPose));
|
||||
this.AssertField(_root, nameof(BodyPose));
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
DrawJointSpheres();
|
||||
}
|
||||
|
||||
private void DrawJointSpheres()
|
||||
{
|
||||
var featureStates = _bodyPoseComparer.FeatureStates;
|
||||
foreach (var kvp in featureStates)
|
||||
{
|
||||
BodyJointId joint = kvp.Key.Joint;
|
||||
var state = kvp.Value;
|
||||
if (BodyPose.GetJointPoseFromRoot(joint, out Pose pose))
|
||||
{
|
||||
Vector3 jointPos = _root.TransformPoint(pose.position);
|
||||
|
||||
Color color;
|
||||
if (state.Delta <= state.MaxDelta)
|
||||
{
|
||||
color = Color.green;
|
||||
}
|
||||
else if (state.MaxDelta > 0)
|
||||
{
|
||||
float amt = (state.Delta / state.MaxDelta) / 2f;
|
||||
color = Color.Lerp(Color.yellow, Color.red, amt);
|
||||
}
|
||||
else
|
||||
{
|
||||
color = Color.red;
|
||||
}
|
||||
|
||||
DebugGizmos.LineWidth = _radius / 2f;
|
||||
DebugGizmos.Color = color;
|
||||
DebugGizmos.DrawPoint(jointPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllBodyPoseComparerActiveStateDebugVisual(
|
||||
BodyPoseComparerActiveState bodyPoseComparer,
|
||||
IBodyPose bodyPose, Transform root)
|
||||
{
|
||||
InjectBodyPoseComparer(bodyPoseComparer);
|
||||
InjectBodyPose(bodyPose);
|
||||
InjectRootTransform(root);
|
||||
}
|
||||
|
||||
public void InjectRootTransform(Transform root)
|
||||
{
|
||||
_root = root;
|
||||
}
|
||||
|
||||
public void InjectBodyPoseComparer(BodyPoseComparerActiveState bodyPoseComparer)
|
||||
{
|
||||
_bodyPoseComparer = bodyPoseComparer;
|
||||
}
|
||||
|
||||
public void InjectBodyPose(IBodyPose bodyPose)
|
||||
{
|
||||
_bodyPose = bodyPose as UnityEngine.Object;
|
||||
BodyPose = bodyPose;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9faf130193937b348be91e09c530a810
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Oculus.Interaction.Body.Input;
|
||||
using System;
|
||||
using Oculus.Interaction.Collections;
|
||||
|
||||
namespace Oculus.Interaction.Body.PoseDetection
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Oculus/Interaction/SDK/Pose Detection/Body Pose")]
|
||||
public class BodyPoseData : ScriptableObject,
|
||||
IBodyPose, ISerializationCallbackReceiver
|
||||
{
|
||||
[System.Serializable]
|
||||
private struct JointData
|
||||
{
|
||||
public BodyJointId JointId;
|
||||
public BodyJointId ParentId;
|
||||
public Pose PoseFromRoot;
|
||||
public Pose LocalPose;
|
||||
}
|
||||
|
||||
private class Mapping : ISkeletonMapping
|
||||
{
|
||||
public EnumerableHashSet<BodyJointId> Joints =
|
||||
new EnumerableHashSet<BodyJointId>();
|
||||
|
||||
public Dictionary<BodyJointId, BodyJointId> JointToParent =
|
||||
new Dictionary<BodyJointId, BodyJointId>();
|
||||
|
||||
IEnumerableHashSet<BodyJointId> ISkeletonMapping.Joints => Joints;
|
||||
|
||||
bool ISkeletonMapping.TryGetParentJointId(BodyJointId jointId, out BodyJointId parent) =>
|
||||
JointToParent.TryGetValue(jointId, out parent);
|
||||
}
|
||||
|
||||
public event Action WhenBodyPoseUpdated = delegate { };
|
||||
|
||||
[SerializeField, HideInInspector]
|
||||
private List<JointData> _jointData = new List<JointData>();
|
||||
|
||||
private Dictionary<BodyJointId, Pose> _posesFromRoot =
|
||||
new Dictionary<BodyJointId, Pose>();
|
||||
|
||||
private Dictionary<BodyJointId, Pose> _localPoses =
|
||||
new Dictionary<BodyJointId, Pose>();
|
||||
|
||||
private Mapping _mapping = new Mapping();
|
||||
|
||||
public bool GetJointPoseFromRoot(BodyJointId bodyJointId, out Pose pose) =>
|
||||
_posesFromRoot.TryGetValue(bodyJointId, out pose);
|
||||
|
||||
public bool GetJointPoseLocal(BodyJointId bodyJointId, out Pose pose) =>
|
||||
_localPoses.TryGetValue(bodyJointId, out pose);
|
||||
|
||||
public ISkeletonMapping SkeletonMapping => _mapping;
|
||||
|
||||
public void SetBodyPose(IBody body)
|
||||
{
|
||||
_jointData.Clear();
|
||||
foreach (var joint in body.SkeletonMapping.Joints)
|
||||
{
|
||||
if (body.GetJointPoseLocal(joint, out Pose local) &&
|
||||
body.GetJointPoseFromRoot(joint, out Pose fromRoot) &&
|
||||
body.SkeletonMapping.TryGetParentJointId(joint, out BodyJointId parent))
|
||||
{
|
||||
_jointData.Add(new JointData()
|
||||
{
|
||||
JointId = joint,
|
||||
ParentId = parent,
|
||||
PoseFromRoot = fromRoot,
|
||||
LocalPose = local,
|
||||
});
|
||||
}
|
||||
}
|
||||
Rebuild();
|
||||
WhenBodyPoseUpdated.Invoke();
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Rebuild();
|
||||
}
|
||||
|
||||
private void Rebuild()
|
||||
{
|
||||
_localPoses.Clear();
|
||||
_posesFromRoot.Clear();
|
||||
_mapping.Joints.Clear();
|
||||
_mapping.JointToParent.Clear();
|
||||
|
||||
for (int i = 0; i < _jointData.Count; ++i)
|
||||
{
|
||||
_localPoses[_jointData[i].JointId] =
|
||||
_jointData[i].LocalPose;
|
||||
_posesFromRoot[_jointData[i].JointId] =
|
||||
_jointData[i].PoseFromRoot;
|
||||
_mapping.Joints.Add(
|
||||
_jointData[i].JointId);
|
||||
_mapping.JointToParent.Add(
|
||||
_jointData[i].JointId, _jointData[i].ParentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55ca4a17febb5bb4084ecb9e8cd230cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using Oculus.Interaction.Body.Input;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace Oculus.Interaction.Body.PoseDetection
|
||||
{
|
||||
public class BodyPoseDebugGizmos : SkeletonDebugGizmos
|
||||
{
|
||||
[SerializeField, Interface(typeof(IBodyPose))]
|
||||
private UnityEngine.Object _bodyPose;
|
||||
private IBodyPose BodyPose;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
BodyPose = _bodyPose as IBodyPose;
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
Assert.IsNotNull(BodyPose);
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
foreach (BodyJointId joint in BodyPose.SkeletonMapping.Joints)
|
||||
{
|
||||
Draw(joint, GetVisibilityFlags());
|
||||
}
|
||||
}
|
||||
|
||||
private VisibilityFlags GetVisibilityFlags()
|
||||
{
|
||||
VisibilityFlags modifiedFlags = Visibility;
|
||||
if (HasNegativeScale)
|
||||
{
|
||||
modifiedFlags &= ~VisibilityFlags.Axes;
|
||||
}
|
||||
return modifiedFlags;
|
||||
}
|
||||
|
||||
protected override bool TryGetWorldJointPose(BodyJointId jointId, out Pose pose)
|
||||
{
|
||||
if (BodyPose.GetJointPoseFromRoot(jointId, out pose))
|
||||
{
|
||||
pose.position = transform.TransformPoint(pose.position);
|
||||
pose.rotation = transform.rotation * pose.rotation;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override bool TryGetParentJointId(BodyJointId jointId, out BodyJointId parent)
|
||||
{
|
||||
return BodyPose.SkeletonMapping.TryGetParentJointId(jointId, out parent);
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllBodyJointDebugGizmos(IBodyPose bodyPose)
|
||||
{
|
||||
InjectBodyPose(bodyPose);
|
||||
}
|
||||
|
||||
public void InjectBodyPose(IBodyPose bodyPose)
|
||||
{
|
||||
_bodyPose = bodyPose as UnityEngine.Object;
|
||||
BodyPose = bodyPose;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: feb0f50b1a0593647b5de2d2b20029a2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Oculus.Interaction.Body.Input;
|
||||
|
||||
namespace Oculus.Interaction.Body.PoseDetection
|
||||
{
|
||||
public interface IBodyPose
|
||||
{
|
||||
/// <summary>
|
||||
/// Called each time the body pose is updated with new data
|
||||
/// </summary>
|
||||
event Action WhenBodyPoseUpdated;
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of the skeleton
|
||||
/// </summary>
|
||||
ISkeletonMapping SkeletonMapping { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to return the pose of the requested body joint,
|
||||
/// in local space relative to its parent joint.
|
||||
/// </summary>
|
||||
bool GetJointPoseLocal(BodyJointId bodyJointId, out Pose pose);
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to return the pose of the requested body joint relative
|
||||
/// to the root joint <see cref="BodyJointId.Body_Root"/>.
|
||||
/// </summary>
|
||||
bool GetJointPoseFromRoot(BodyJointId bodyJointId, out Pose pose);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2eac04d5eb0c554bbbdcdcb2df831c2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Oculus.Interaction.Body.Input;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Oculus.Interaction.Body.PoseDetection
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes an <see cref="IBodyPose"/> from an <see cref="IBody"/>
|
||||
/// </summary>
|
||||
public class PoseFromBody : MonoBehaviour, IBodyPose
|
||||
{
|
||||
public event Action WhenBodyPoseUpdated = delegate { };
|
||||
|
||||
[Tooltip("The IBodyPose will be derived from this IBody.")]
|
||||
[SerializeField, Interface(typeof(IBody))]
|
||||
private UnityEngine.Object _body;
|
||||
private IBody Body;
|
||||
|
||||
[Tooltip("If true, this component will track the provided IBody as " +
|
||||
"its data is updated. If false, you must call " +
|
||||
nameof(UpdatePose) + " to update joint data.")]
|
||||
[SerializeField]
|
||||
private bool _autoUpdate = true;
|
||||
|
||||
/// <summary>
|
||||
/// If true, this component will track the provided IBody as
|
||||
/// its data is updated. If false, you must call
|
||||
/// <see cref="UpdatePose"/> to update joint data.
|
||||
/// </summary>
|
||||
public bool AutoUpdate
|
||||
{
|
||||
get => _autoUpdate;
|
||||
set => _autoUpdate = value;
|
||||
}
|
||||
|
||||
protected bool _started = false;
|
||||
|
||||
private Dictionary<BodyJointId, Pose> _jointPosesLocal;
|
||||
private Dictionary<BodyJointId, Pose> _jointPosesFromRoot;
|
||||
|
||||
public ISkeletonMapping SkeletonMapping => Body.SkeletonMapping;
|
||||
|
||||
public bool GetJointPoseLocal(BodyJointId bodyJointId, out Pose pose) =>
|
||||
_jointPosesLocal.TryGetValue(bodyJointId, out pose);
|
||||
|
||||
public bool GetJointPoseFromRoot(BodyJointId bodyJointId, out Pose pose) =>
|
||||
_jointPosesFromRoot.TryGetValue(bodyJointId, out pose);
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
_jointPosesLocal = new Dictionary<BodyJointId, Pose>();
|
||||
_jointPosesFromRoot = new Dictionary<BodyJointId, Pose>();
|
||||
Body = _body as IBody;
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
this.BeginStart(ref _started);
|
||||
this.AssertField(Body, nameof(Body));
|
||||
this.EndStart(ref _started);
|
||||
}
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (_started)
|
||||
{
|
||||
Body.WhenBodyUpdated += Body_WhenBodyUpdated;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
if (_started)
|
||||
{
|
||||
Body.WhenBodyUpdated -= Body_WhenBodyUpdated;
|
||||
}
|
||||
}
|
||||
|
||||
private void Body_WhenBodyUpdated()
|
||||
{
|
||||
if (_autoUpdate)
|
||||
{
|
||||
UpdatePose();
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdatePose()
|
||||
{
|
||||
_jointPosesLocal.Clear();
|
||||
_jointPosesFromRoot.Clear();
|
||||
|
||||
foreach (var joint in Body.SkeletonMapping.Joints)
|
||||
{
|
||||
if (Body.GetJointPoseLocal(joint,
|
||||
out Pose localPose))
|
||||
{
|
||||
_jointPosesLocal[joint] = localPose;
|
||||
}
|
||||
if (Body.GetJointPoseFromRoot(joint,
|
||||
out Pose poseFromRoot))
|
||||
{
|
||||
_jointPosesFromRoot[joint] = poseFromRoot;
|
||||
}
|
||||
}
|
||||
|
||||
WhenBodyPoseUpdated.Invoke();
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllPoseFromBody(IBody body)
|
||||
{
|
||||
InjectBody(body);
|
||||
}
|
||||
|
||||
public void InjectBody(IBody body)
|
||||
{
|
||||
_body = body as UnityEngine.Object;
|
||||
Body = body;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2be7e1a3256ca942865ee433ce9e3e9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user