198 lines
7.0 KiB
C#
198 lines
7.0 KiB
C#
/*
|
|
* 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 Meta.XR.Util;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace Oculus.Interaction.Input
|
|
{
|
|
|
|
[Feature(Feature.Interaction)]
|
|
public class AnimatedHandOVR : MonoBehaviour,
|
|
IDeltaTimeConsumer
|
|
{
|
|
public enum AllowThumbUp
|
|
{
|
|
Always,
|
|
GripRequired,
|
|
TriggerAndGripRequired,
|
|
}
|
|
|
|
[SerializeField]
|
|
private OVRInput.Controller _controller = OVRInput.Controller.None;
|
|
[SerializeField]
|
|
private Animator _animator = null;
|
|
|
|
[SerializeField]
|
|
[Tooltip("Indicates the input needed in order to perform a thumbs-up when the fist is closed")]
|
|
private AllowThumbUp _allowThumbUp = AllowThumbUp.TriggerAndGripRequired;
|
|
public AllowThumbUp AllowThumbUpMode
|
|
{
|
|
get => _allowThumbUp;
|
|
set => _allowThumbUp = value;
|
|
}
|
|
|
|
|
|
[Header("Animation Speed")]
|
|
[SerializeField]
|
|
[FormerlySerializedAs("_animFlexhGain")]
|
|
[Tooltip("Speed of the index flex animation")]
|
|
private float _animFlexGain = 35;
|
|
public float AnimFlexGain
|
|
{
|
|
get => _animFlexGain;
|
|
set => _animFlexGain = value;
|
|
}
|
|
|
|
[SerializeField]
|
|
[Tooltip("Speed of the pinch animation")]
|
|
private float _animPinchGain = 35;
|
|
public float AnimPinchGain
|
|
{
|
|
get => _animPinchGain;
|
|
set => _animPinchGain = value;
|
|
}
|
|
|
|
[SerializeField]
|
|
[Tooltip("Speed of the point, slide and thumbs up animation")]
|
|
private float _animPointAndThumbsUpGain = 20;
|
|
public float AnimPointAndThumbsUpGain
|
|
{
|
|
get => _animPointAndThumbsUpGain;
|
|
set => _animPointAndThumbsUpGain = value;
|
|
}
|
|
|
|
public Func<float> DeltaTimeProvider
|
|
{
|
|
get; set;
|
|
} = () => Time.deltaTime;
|
|
|
|
private const string ANIM_LAYER_NAME_POINT = "Point Layer";
|
|
private const string ANIM_LAYER_NAME_THUMB = "Thumb Layer";
|
|
private const string ANIM_PARAM_NAME_FLEX = "Flex";
|
|
private const string ANIM_PARAM_NAME_PINCH = "Pinch";
|
|
private const string ANIM_PARAM_NAME_INDEX_SLIDE = "IndexSlide";
|
|
|
|
private const float TRIGGER_MAX = 0.95f;
|
|
|
|
private int _animLayerIndexThumb = -1;
|
|
private int _animLayerIndexPoint = -1;
|
|
private int _animParamIndexFlex = Animator.StringToHash(ANIM_PARAM_NAME_FLEX);
|
|
private int _animParamPinch = Animator.StringToHash(ANIM_PARAM_NAME_PINCH);
|
|
private int _animParamIndexSlide = Animator.StringToHash(ANIM_PARAM_NAME_INDEX_SLIDE);
|
|
|
|
private bool _isGivingThumbsUp = false;
|
|
private float _pointBlend = 0.0f;
|
|
private float _slideBlend = 0.0f;
|
|
|
|
private float _thumbsUpBlend = 0.0f;
|
|
private float _pointTarget = 0.0f;
|
|
private float _slideTarget = 0.0f;
|
|
|
|
private float _animFlex = 0;
|
|
private float _animPinch = 0;
|
|
|
|
private Func<float> _deltaTimeProvider = () => Time.deltaTime;
|
|
public void SetDeltaTimeProvider(Func<float> deltaTimeProvider)
|
|
{
|
|
_deltaTimeProvider = deltaTimeProvider;
|
|
}
|
|
|
|
protected virtual void Start()
|
|
{
|
|
_animLayerIndexPoint = _animator.GetLayerIndex(ANIM_LAYER_NAME_POINT);
|
|
_animLayerIndexThumb = _animator.GetLayerIndex(ANIM_LAYER_NAME_THUMB);
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
UpdateCapTouchStates();
|
|
|
|
_pointBlend = Mathf.Lerp(_pointBlend, _pointTarget, _animPointAndThumbsUpGain * _deltaTimeProvider());
|
|
_slideBlend = Mathf.Lerp(_slideBlend, _slideTarget, _animPointAndThumbsUpGain * _deltaTimeProvider());
|
|
_thumbsUpBlend = Mathf.Lerp(_thumbsUpBlend, _isGivingThumbsUp ? 1 : 0, _animPointAndThumbsUpGain * _deltaTimeProvider());
|
|
|
|
UpdateAnimStates();
|
|
}
|
|
|
|
private void UpdateCapTouchStates()
|
|
{
|
|
float indexCurl = OVRControllerUtility.GetIndexCurl(_controller);
|
|
float indexSlide = OVRControllerUtility.GetIndexSlide(_controller);
|
|
_pointTarget = 1 - indexCurl;
|
|
_slideTarget = indexSlide;
|
|
|
|
bool triggerThumbsUp = _allowThumbUp == AllowThumbUp.Always ||
|
|
(_allowThumbUp == AllowThumbUp.GripRequired
|
|
&& OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, _controller) >= TRIGGER_MAX) ||
|
|
(_allowThumbUp == AllowThumbUp.TriggerAndGripRequired
|
|
&& OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, _controller) >= TRIGGER_MAX
|
|
&& OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, _controller) >= TRIGGER_MAX);
|
|
|
|
_isGivingThumbsUp = triggerThumbsUp
|
|
&& !OVRInput.Get(OVRInput.NearTouch.PrimaryThumbButtons, _controller);
|
|
}
|
|
|
|
private void UpdateAnimStates()
|
|
{
|
|
// Flex
|
|
// blend between open hand and fully closed fist
|
|
float flex = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, _controller);
|
|
_animFlex = Mathf.Lerp(_animFlex, flex, _animFlexGain * DeltaTimeProvider());
|
|
_animator.SetFloat(_animParamIndexFlex, _animFlex);
|
|
|
|
// Pinch
|
|
float pinchAmount = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, _controller);
|
|
_animPinch = Mathf.Lerp(_animPinch, pinchAmount, _animPinchGain * DeltaTimeProvider());
|
|
_animator.SetFloat(_animParamPinch, _animPinch);
|
|
|
|
// Point
|
|
_animator.SetLayerWeight(_animLayerIndexPoint, _pointBlend);
|
|
_animator.SetFloat(_animParamIndexSlide, _slideBlend);
|
|
|
|
// Thumbs up
|
|
_animator.SetLayerWeight(_animLayerIndexThumb, _thumbsUpBlend);
|
|
}
|
|
|
|
|
|
#region Inject
|
|
|
|
public void InjectAllAnimatedHandOVR(OVRInput.Controller controller, Animator animator)
|
|
{
|
|
InjectController(controller);
|
|
InjectAnimator(animator);
|
|
}
|
|
|
|
public void InjectController(OVRInput.Controller controller)
|
|
{
|
|
_controller = controller;
|
|
}
|
|
|
|
public void InjectAnimator(Animator animator)
|
|
{
|
|
_animator = animator;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|