Skip to content

Instantly share code, notes, and snippets.

@arun02139
Created March 14, 2018 15:00
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save arun02139/1c9e15391ff2857c49294c11208a6a93 to your computer and use it in GitHub Desktop.
using UnityEngine;
using PlusOne.SDK;
using PlusOne.SDK.Input.Interaction;
using UnityEngine.XR.WSA.Input;
using System.Collections.Generic;
public class WinMRIKSetup : MonoBehaviour
{
public Transform dummyRoot = null;
public bool HeadIKOn = false;
Transform _camTransform = null;
Dictionary<uint, WSAInputHandler.ControllerState> _controllers;
Transform _headIKTarget = null;
Transform _rHandIKTarget = null;
Transform _lHandIKTarget = null;
void Awake()
{
_controllers = new Dictionary<uint, WSAInputHandler.ControllerState>();
// rather than hook-up IK targets in the Editor, let's do it in code
GetTargets();
}
void Start()
{
_camTransform = Camera.main.transform;
}
void OnEnable ()
{
InteractionManager.InteractionSourceDetected += InteractionSourceDetected;
InteractionManager.InteractionSourceLost += InteractionSourceLost;
InteractionManager.InteractionSourceUpdated += InteractionSourceUpdated;
}
void OnDisable()
{
InteractionManager.InteractionSourceDetected -= InteractionSourceDetected;
InteractionManager.InteractionSourceLost -= InteractionSourceLost;
InteractionManager.InteractionSourceUpdated -= InteractionSourceUpdated;
}
void InteractionSourceDetected(InteractionSourceDetectedEventArgs obj)
{
//Debug.LogFormat("{0} {1} Detected", obj.state.source.handedness, obj.state.source.kind);
if (obj.state.source.kind == InteractionSourceKind.Controller && !_controllers.ContainsKey(obj.state.source.id))
{
_controllers.Add(obj.state.source.id, new WSAInputHandler.ControllerState { Handedness = obj.state.source.handedness });
}
}
void InteractionSourceLost(InteractionSourceLostEventArgs obj)
{
//Debug.LogFormat("{0} {1} Lost", obj.state.source.handedness, obj.state.source.kind);
_controllers.Remove(obj.state.source.id);
}
void InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
{
WSAInputHandler.ControllerState controllerState;
if (_controllers.TryGetValue(obj.state.source.id, out controllerState))
{
obj.state.sourcePose.TryGetPosition(out controllerState.GripPosition, InteractionSourceNode.Grip);
obj.state.sourcePose.TryGetRotation(out controllerState.GripRotation, InteractionSourceNode.Grip);
if(obj.state.source.handedness == InteractionSourceHandedness.Left)
{
_lHandIKTarget.position = controllerState.GripPosition;
_lHandIKTarget.rotation = controllerState.GripRotation;
}
if (obj.state.source.handedness == InteractionSourceHandedness.Right)
{
_rHandIKTarget.position = controllerState.GripPosition;
_rHandIKTarget.rotation = controllerState.GripRotation;
}
}
}
void LateUpdate()
{
if(_camTransform != null)
{
if (dummyRoot != null)
{
var forwardVecXZ = Vector3.ProjectOnPlane(_camTransform.forward, Vector3.up);
var forwardQuat = Quaternion.LookRotation(forwardVecXZ, Vector3.up);
dummyRoot.rotation = forwardQuat;
//ConsoleProDebug.Watch("forwardXZ", forwardVecXZ.ToString("#.##"));
}
if(HeadIKOn)
{
_headIKTarget.position = _camTransform.position;
_headIKTarget.rotation = _camTransform.rotation;
}
}
}
void GetTargets()
{
// head target
GameObject go = gameObject.FindRecursive("Head Target");
if (go == null)
{
Debug.Log("WinMRIKSetup.GetTargets: head target not found, aborting");
return;
}
_headIKTarget = go.transform;
// left-hand target
go = gameObject.FindRecursive("Left Hand Target");
if (go == null)
{
Debug.Log("WinMRIKSetup.GetTargets: left-hand target not found, aborting");
return;
}
_lHandIKTarget = go.transform;
// right-hand target
go = gameObject.FindRecursive("Right Hand Target");
if (go == null)
{
Debug.Log("WinMRIKSetup.GetTargets: right-hand target not found, aborting");
return;
}
_rHandIKTarget = go.transform;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment