Skip to content

Instantly share code, notes, and snippets.

@wakagomo
Created February 28, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakagomo/40b4e3a8def7b9b31c9fbb2183572a92 to your computer and use it in GitHub Desktop.
Save wakagomo/40b4e3a8def7b9b31c9fbb2183572a92 to your computer and use it in GitHub Desktop.
/******************************************************************************
* Copyright (c) 2019 wakagomo *
* *
* This source code is released under the MIT License. *
* http://opensource.org/licenses/mit-license.php *
******************************************************************************/
using UnityEngine;
public class Finger : MonoBehaviour
{
public Transform Bone1;
public Transform Bone2;
public Transform Bone3;
public Transform Bone4;
}
/******************************************************************************
* Copyright (c) 2019 wakagomo *
* *
* This source code is released under the MIT License. *
* http://opensource.org/licenses/mit-license.php *
******************************************************************************/
using UnityEngine;
public class Hand : MonoBehaviour
{
public Transform Wirst;
public Finger Thumb;
public Finger Index;
public Finger Middle;
public Finger Ring;
public Finger Pinky;
}
/******************************************************************************
* Copyright (c) 2019 wakagomo *
* *
* This source code is released under the MIT License. *
* http://opensource.org/licenses/mit-license.php *
******************************************************************************/
using UnityEngine;
/// <summary>
/// 手のトラッキングのトレース
/// </summary>
public class LeapMotion : MonoBehaviour
{
#region Offset Definition
/// <summary>
/// Quaternionの差分の保持
/// </summary>
private class Offset
{
/// <summary>
/// 対象モデルのQuaternion
/// </summary>
private Quaternion Model;
/// <summary>
/// 参照モデルのQuaternion
/// </summary>
private Quaternion Leap;
/// <summary>
/// Quaternionの差分を保持し、参照モデルのQuaternionの変化を対象モデルへと伝搬する
/// </summary>
/// <param name="model">対象モデル</param>
/// <param name="leap">参照モデル</param>
public Offset(Quaternion model, Quaternion leap)
{
Model = model;
Leap = Quaternion.Inverse(leap);
}
/// <summary>
/// オフセットの適用
/// </summary>
/// <param name="currentLeap">現在の参照モデルのQuaternion</param>
/// <returns>オフセットを適用した対象モデルのQuaternion</returns>
public Quaternion ApplyOffset(Quaternion currentLeap)
{
return currentLeap * Leap * Model;
}
}
/// <summary>
/// 指のオフセット
/// </summary>
private class FingerOffset
{
private Offset Bone1;
private Offset Bone2;
private Offset Bone3;
private Offset Bone4;
public FingerOffset(Finger model, Finger leap)
{
if (model.Bone1 != null && leap.Bone1 != null)
{
Bone1 = new Offset(model.Bone1.transform.rotation, leap.Bone1.transform.rotation);
}
Bone2 = new Offset(model.Bone2.transform.rotation, leap.Bone2.transform.rotation);
Bone3 = new Offset(model.Bone3.transform.rotation, leap.Bone3.transform.rotation);
Bone4 = new Offset(model.Bone4.transform.rotation, leap.Bone4.transform.rotation);
}
public void ApplyOffset(Finger model, Finger leap)
{
if (Bone1 != null)
{
model.Bone1.rotation = Bone1.ApplyOffset(leap.Bone1.rotation);
}
model.Bone2.rotation = Bone2.ApplyOffset(leap.Bone2.rotation);
model.Bone3.rotation = Bone3.ApplyOffset(leap.Bone3.rotation);
model.Bone4.rotation = Bone4.ApplyOffset(leap.Bone4.rotation);
}
}
private class HandOffset
{
private FingerOffset Thumb;
private FingerOffset Index;
private FingerOffset Middle;
private FingerOffset Ring;
private FingerOffset Pinky;
private Offset Wirst;
private Vector3 OffsetWirst;
public HandOffset(Hand model, Hand leap)
{
Thumb = new FingerOffset(model.Thumb, leap.Thumb);
Index = new FingerOffset(model.Index, leap.Index);
Middle = new FingerOffset(model.Middle, leap.Middle);
Ring = new FingerOffset(model.Ring, leap.Ring);
Pinky = new FingerOffset(model.Pinky, leap.Pinky);
Wirst = new Offset(model.Wirst.rotation, leap.Wirst.rotation);
OffsetWirst = model.Wirst.position - leap.Wirst.position;
}
public void ApplyOffset(Hand model, Hand leap)
{
Thumb.ApplyOffset(model.Thumb, leap.Thumb);
Index.ApplyOffset(model.Index, leap.Index);
Middle.ApplyOffset(model.Middle, leap.Middle);
Ring.ApplyOffset(model.Ring, leap.Ring);
Pinky.ApplyOffset(model.Pinky, leap.Pinky);
model.Wirst.rotation = Wirst.ApplyOffset(leap.Wirst.rotation);
model.Wirst.position = leap.Wirst.position + OffsetWirst;
}
}
#endregion
[SerializeField]
private Hand HandLeft;
[SerializeField]
private Hand HandLeapLeft;
[SerializeField]
private Hand HandRight;
[SerializeField]
private Hand HandLeapRight;
private HandOffset HandOffsetLeft;
private HandOffset HandOffsetRight;
private void Awake()
{
HandOffsetLeft = new HandOffset(HandLeft, HandLeapLeft);
HandOffsetRight = new HandOffset(HandRight, HandLeapRight);
GameObject obj;
obj = new GameObject("LeftHandOffset");
obj.transform.parent = gameObject.transform;
obj = new GameObject("RightHandOffset");
obj.transform.parent = gameObject.transform;
}
private void Update()
{
HandOffsetLeft.ApplyOffset(HandLeft, HandLeapLeft);
HandOffsetRight.ApplyOffset(HandRight, HandLeapRight);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment