Skip to content

Instantly share code, notes, and snippets.

@youten
Created January 9, 2018 10:08
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 youten/ecf0c37d377dc8eb4bbe6534dcd93bc9 to your computer and use it in GitHub Desktop.
Save youten/ecf0c37d377dc8eb4bbe6534dcd93bc9 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ref. Unity+HTC Vive開発メモ - フレームシンセシス https://framesynthesis.jp/tech/unity/htcvive/
// Viveコントローラのタッチパッドを左上・右上・左下・右下の4分割で使い分ける(上下左右はAPI上難しいので象限の方が妥当)
// 左上[(予約)][(予約)]右上
// 左下[ グー ][ チョキ ]右下
public class HandController : MonoBehaviour {
public GameObject targetMikoko;
public bool isRight = true; // 右手かどうか(false時左手)
private MecanimHandController mecanimHandController;
private bool isTouchpadLeftDownPressing = false;
private bool isTouchpadRightDownPressing = false;
void Start() {
mecanimHandController = targetMikoko.GetComponent<MecanimHandController> ();
}
void Update() {
SteamVR_Controller.Device device = SteamVR_Controller.Input ((int)GetComponent<SteamVR_TrackedObject> ().index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Touchpad)) {
if (isTouchPadLeftDownTouched (device)) {
isTouchpadLeftDownPressing = true;
updateHand ();
}
if (isTouchPadRightDownTouched (device)) {
isTouchpadRightDownPressing = true;
updateHand ();
}
} else if (device.GetPressUp (SteamVR_Controller.ButtonMask.Touchpad)) {
isTouchpadLeftDownPressing = false;
isTouchpadRightDownPressing = false;
updateHand ();
}
}
void updateHand() {
if (isTouchpadLeftDownPressing) {
if (isRight) {
mecanimHandController.rightRock ();
} else { // left
mecanimHandController.leftRock ();
}
} else if (isTouchpadRightDownPressing) {
if (isRight) {
mecanimHandController.rightScissors ();
} else { // left
mecanimHandController.leftScissors ();
}
} else {
if (isRight) {
mecanimHandController.rightPaper ();
} else { // left
mecanimHandController.leftPaper ();
}
}
}
bool isTouchPadLeftDownTouched(SteamVR_Controller.Device device) {
var pos = device.GetAxis();
if (pos.x < 0 && pos.y < 0) {
return true;
}
return false;
}
bool isTouchPadRightDownTouched(SteamVR_Controller.Device device) {
var pos = device.GetAxis();
if (0 < pos.x && pos.y < 0) {
return true;
}
return false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Mecanim Hand(Fingers) control by localRotation updating
public class MecanimHandController : MonoBehaviour {
private Animator animator_;
private Quaternion[] leftHands = new Quaternion[15];
private Quaternion[] rightHands = new Quaternion[15];
private Transform leftThumbProximal() { return animator_.GetBoneTransform (HumanBodyBones.LeftThumbProximal); }
private Transform leftThumbIntermediate() { return animator_.GetBoneTransform (HumanBodyBones.LeftThumbProximal); }
private Transform leftThumbDistal() { return animator_.GetBoneTransform (HumanBodyBones.LeftThumbProximal); }
private Transform leftIndexProximal() { return animator_.GetBoneTransform(HumanBodyBones.LeftIndexProximal); }
private Transform leftIndexIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.LeftIndexIntermediate); }
private Transform leftIndexDistal() { return animator_.GetBoneTransform(HumanBodyBones.LeftIndexDistal); }
private Transform leftMiddleProximal() { return animator_.GetBoneTransform(HumanBodyBones.LeftMiddleProximal); }
private Transform leftMiddleIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.LeftMiddleIntermediate); }
private Transform leftMiddleDistal() { return animator_.GetBoneTransform(HumanBodyBones.LeftMiddleDistal); }
private Transform leftRingProximal() { return animator_.GetBoneTransform(HumanBodyBones.LeftRingProximal); }
private Transform leftRingIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.LeftRingIntermediate); }
private Transform leftRingDistal() { return animator_.GetBoneTransform(HumanBodyBones.LeftRingDistal); }
private Transform leftLittleProximal() { return animator_.GetBoneTransform(HumanBodyBones.LeftLittleProximal); }
private Transform leftLittleIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.LeftLittleIntermediate); }
private Transform leftLittleDistal() { return animator_.GetBoneTransform(HumanBodyBones.LeftLittleDistal); }
private Transform rightThumbProximal() { return animator_.GetBoneTransform (HumanBodyBones.RightThumbProximal); }
private Transform rightThumbIntermediate() { return animator_.GetBoneTransform (HumanBodyBones.RightThumbProximal); }
private Transform rightThumbDistal() { return animator_.GetBoneTransform (HumanBodyBones.RightThumbProximal); }
private Transform rightIndexProximal() { return animator_.GetBoneTransform(HumanBodyBones.RightIndexProximal); }
private Transform rightIndexIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.RightIndexIntermediate); }
private Transform rightIndexDistal() { return animator_.GetBoneTransform(HumanBodyBones.RightIndexDistal); }
private Transform rightMiddleProximal() { return animator_.GetBoneTransform(HumanBodyBones.RightMiddleProximal); }
private Transform rightMiddleIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.RightMiddleIntermediate); }
private Transform rightMiddleDistal() { return animator_.GetBoneTransform(HumanBodyBones.RightMiddleDistal); }
private Transform rightRingProximal() { return animator_.GetBoneTransform(HumanBodyBones.RightRingProximal); }
private Transform rightRingIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.RightRingIntermediate); }
private Transform rightRingDistal() { return animator_.GetBoneTransform(HumanBodyBones.RightRingDistal); }
private Transform rightLittleProximal() { return animator_.GetBoneTransform(HumanBodyBones.RightLittleProximal); }
private Transform rightLittleIntermediate() { return animator_.GetBoneTransform(HumanBodyBones.RightLittleIntermediate); }
private Transform rightLittleDistal() { return animator_.GetBoneTransform(HumanBodyBones.RightLittleDistal); }
// Rock - Paper - Scissors
public void leftRock() {
resetLeft ();
// Rock(Gu-)
leftThumbProximal().Rotate (new Vector3(-40, 0, -30));
leftIndexProximal().Rotate (new Vector3(-90, 0, 0));
leftIndexIntermediate().Rotate (new Vector3(-90, 0, 0));
leftIndexDistal().Rotate (new Vector3 (-90, 0, 0));
leftMiddleProximal().Rotate (new Vector3(-90, 0, 0));
leftMiddleIntermediate().Rotate (new Vector3(-90, 0, 0));
leftMiddleDistal().Rotate (new Vector3 (-90, 0, 0));
leftRingProximal().Rotate (new Vector3(-90, 0, 0));
leftRingIntermediate().Rotate (new Vector3(-90, 0, 0));
leftRingDistal().Rotate (new Vector3 (-90, 0, 0));
leftLittleProximal().Rotate (new Vector3(-90, 0, 0));
leftLittleIntermediate().Rotate (new Vector3(-90, 0, 0));
leftLittleDistal().Rotate (new Vector3 (-90, 0, 0));
}
public void leftPaper() {
resetLeft ();
// Paper(Pa-)
leftThumbIntermediate().Rotate (new Vector3(10, 0, 10));
leftThumbDistal().Rotate (new Vector3(0, 0, 10));
leftIndexProximal().Rotate (new Vector3 (0, 0, 10));
leftMiddleProximal().Rotate (new Vector3 (0, 0, 4));
leftRingProximal().Rotate (new Vector3 (0, 0, -4));
leftLittleProximal().Rotate (new Vector3 (0, 0, -10));
}
public void leftScissors() {
resetLeft ();
// Scissors(Choki)
leftThumbProximal().Rotate (new Vector3(-50, 0, -30));
leftIndexProximal().Rotate (new Vector3(0, 0, 10));
leftIndexIntermediate().Rotate (new Vector3(20, 0, 0));
leftIndexDistal().Rotate (new Vector3 (5, 0, 0));
leftMiddleProximal().Rotate (new Vector3 (0, 0, -5));
leftMiddleIntermediate().Rotate (new Vector3(20, 0, 0));
leftMiddleDistal().Rotate (new Vector3 (5, 0, 0));
leftRingProximal().Rotate (new Vector3(-90, 0, 0));
leftRingIntermediate().Rotate (new Vector3(-90, 0, 0));
leftRingDistal().Rotate (new Vector3 (-90, 0, 0));
leftLittleProximal().Rotate (new Vector3(-90, 0, 0));
leftLittleIntermediate().Rotate (new Vector3(-90, 0, 0));
leftLittleDistal().Rotate (new Vector3 (-90, 0, 0));
}
public void rightRock() {
resetRight ();
// Rock(Gu-)
rightThumbProximal().Rotate (new Vector3(-40, 0, 30));
rightIndexProximal().Rotate (new Vector3(-90, 0, 0));
rightIndexIntermediate().Rotate (new Vector3(-90, 0, 0));
rightIndexDistal().Rotate (new Vector3 (-90, 0, 0));
rightMiddleProximal().Rotate (new Vector3(-90, 0, 0));
rightMiddleIntermediate().Rotate (new Vector3(-90, 0, 0));
rightMiddleDistal().Rotate (new Vector3 (-90, 0, 0));
rightRingProximal().Rotate (new Vector3(-90, 0, 0));
rightRingIntermediate().Rotate (new Vector3(-90, 0, 0));
rightRingDistal().Rotate (new Vector3 (-90, 0, 0));
rightLittleProximal().Rotate (new Vector3(-90, 0, 0));
rightLittleIntermediate().Rotate (new Vector3(-90, 0, 0));
rightLittleDistal().Rotate (new Vector3 (-90, 0, 0));
}
public void rightPaper() {
resetRight ();
// Paper(Pa-)
rightThumbIntermediate().Rotate (new Vector3(10, 0, -10));
rightThumbDistal().Rotate (new Vector3(0, 0, -10));
rightIndexProximal().Rotate (new Vector3 (0, 0, -10));
rightMiddleProximal().Rotate (new Vector3 (0, 0, -4));
rightRingProximal().Rotate (new Vector3 (0, 0, 4));
rightLittleProximal().Rotate (new Vector3 (0, 0, 10));
}
public void rightScissors() {
resetRight ();
// Scissors(Choki)
rightThumbProximal().Rotate (new Vector3(-50, 0, 30));
rightIndexProximal().Rotate (new Vector3(0, 0, -10));
rightIndexIntermediate().Rotate (new Vector3(20, 0, 0));
rightIndexDistal().Rotate (new Vector3 (5, 0, 0));
rightMiddleProximal().Rotate (new Vector3 (0, 0, 5));
rightMiddleIntermediate().Rotate (new Vector3(20, 0, 0));
rightMiddleDistal().Rotate (new Vector3 (5, 0, 0));
rightRingProximal().Rotate (new Vector3(-90, 0, 0));
rightRingIntermediate().Rotate (new Vector3(-90, 0, 0));
rightRingDistal().Rotate (new Vector3 (-90, 0, 0));
rightLittleProximal().Rotate (new Vector3(-90, 0, 0));
rightLittleIntermediate().Rotate (new Vector3(-90, 0, 0));
rightLittleDistal().Rotate (new Vector3 (-90, 0, 0));
}
void Awake () {
animator_ = GetComponent<Animator>();
// save fingers's transform
leftHands [0] = leftThumbProximal().localRotation;
leftHands [1] = leftThumbIntermediate ().localRotation;
leftHands [2] = leftThumbDistal().localRotation;
leftHands [3] = leftIndexProximal().localRotation;
leftHands [4] = leftIndexIntermediate().localRotation;
leftHands [5] = leftIndexDistal().localRotation;
leftHands [6] = leftMiddleProximal().localRotation;
leftHands [7] = leftMiddleIntermediate().localRotation;
leftHands [8] = leftMiddleDistal().localRotation;
leftHands [9] = leftRingProximal().localRotation;
leftHands [10] = leftRingIntermediate().localRotation;
leftHands [11] = leftRingDistal().localRotation;
leftHands [12] = leftLittleProximal().localRotation;
leftHands [13] = leftLittleIntermediate().localRotation;
leftHands [14] = leftLittleDistal().localRotation;
rightHands [0] = rightThumbProximal().localRotation;
rightHands [1] = rightThumbIntermediate ().localRotation;
rightHands [2] = rightThumbDistal().localRotation;
rightHands [3] = rightIndexProximal().localRotation;
rightHands [4] = rightIndexIntermediate().localRotation;
rightHands [5] = rightIndexDistal().localRotation;
rightHands [6] = rightMiddleProximal().localRotation;
rightHands [7] = rightMiddleIntermediate().localRotation;
rightHands [8] = rightMiddleDistal().localRotation;
rightHands [9] = rightRingProximal().localRotation;
rightHands [10] = rightRingIntermediate().localRotation;
rightHands [11] = rightRingDistal().localRotation;
rightHands [12] = rightLittleProximal().localRotation;
rightHands [13] = rightLittleIntermediate().localRotation;
rightHands [14] = rightLittleDistal().localRotation;
}
void Start() {
}
void Update () {
}
void resetLeft () {
// restore fingers's transform
leftThumbProximal ().localRotation = leftHands [0];
leftThumbIntermediate ().localRotation = leftHands [1];
leftThumbDistal ().localRotation = leftHands [2];
leftIndexProximal ().localRotation = leftHands [3];
leftIndexIntermediate ().localRotation = leftHands [4];
leftIndexDistal ().localRotation = leftHands [5];
leftMiddleProximal ().localRotation = leftHands [6];
leftMiddleIntermediate ().localRotation = leftHands [7];
leftMiddleDistal ().localRotation = leftHands [8];
leftRingProximal ().localRotation = leftHands [9];
leftRingIntermediate ().localRotation = leftHands [10];
leftRingDistal ().localRotation = leftHands [11];
leftLittleProximal ().localRotation = leftHands [12];
leftLittleIntermediate ().localRotation = leftHands [13];
leftLittleDistal ().localRotation = leftHands [14];
}
void resetRight () {
// restore fingers's transform
rightThumbProximal().localRotation = rightHands[0];
rightThumbIntermediate().localRotation = rightHands [1];
rightThumbDistal().localRotation = rightHands [2];
rightIndexProximal().localRotation = rightHands [3];
rightIndexIntermediate().localRotation = rightHands [4];
rightIndexDistal().localRotation = rightHands [5];
rightMiddleProximal().localRotation = rightHands [6];
rightMiddleIntermediate().localRotation = rightHands [7];
rightMiddleDistal().localRotation = rightHands [8];
rightRingProximal().localRotation = rightHands [9];
rightRingIntermediate().localRotation = rightHands [10];
rightRingDistal().localRotation = rightHands [11];
rightLittleProximal().localRotation = rightHands [12];
rightLittleIntermediate().localRotation = rightHands [13];
rightLittleDistal().localRotation = rightHands [14];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment