Created
February 6, 2016 19:08
-
-
Save Albert/806e3dff3f1b595c924c to your computer and use it in GitHub Desktop.
Vive, Spatial Computing, Lab notes feb 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class InteractionController : MonoBehaviour { | |
private List<Vector3> focalPoints = new List<Vector3> (); | |
private List<Vector3> hoverPoints = new List<Vector3> (); | |
private GameObject ixdLayer; | |
private int controllerIndex; | |
private bool isPointing = true; | |
private GameObject subject; | |
// Use this for initialization | |
void Start () { | |
SteamVR_TrackedObject indexFinder = GetComponent<SteamVR_TrackedObject>(); | |
// can't figure out the right way to do this | |
if (indexFinder.index.ToString ().Equals ("Device1")) { | |
controllerIndex = 1; | |
} else if (indexFinder.index.ToString ().Equals ("Device2")) { | |
controllerIndex = 2; | |
} else if (indexFinder.index.ToString ().Equals ("Device3")) { | |
controllerIndex = 3; | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
focalPoints.Clear (); | |
hoverPoints.Clear (); | |
if (isPointing) { | |
RaycastHit[] hits = Physics.RaycastAll (transform.position, transform.forward, Mathf.Infinity); | |
Vector3 hitPoint = Vector3.zero; | |
bool isHitThisFrame = false; | |
foreach (RaycastHit hit in hits) { | |
if (hit.collider.gameObject.Equals (subject)) { | |
hitPoint = hit.point; | |
isHitThisFrame = true; | |
} | |
} | |
if (isHitThisFrame) { | |
hoverPoints.Add (hitPoint); | |
if (SteamVR_Controller.Input (controllerIndex).GetPress (SteamVR_Controller.ButtonMask.Trigger)) { | |
focalPoints.Add (hitPoint); | |
} | |
} | |
} else { | |
hoverPoints.Add (transform.position); | |
if (SteamVR_Controller.Input (controllerIndex).GetPress (SteamVR_Controller.ButtonMask.Trigger)) { | |
focalPoints.Add (transform.position); | |
} | |
} | |
if (SteamVR_Controller.Input (controllerIndex).GetPressDown (SteamVR_Controller.ButtonMask.Grip)) { | |
isPointing = (isPointing) ? false : true; | |
} | |
} | |
public List<Vector3> getFocalPoints() { | |
return focalPoints; | |
} | |
public List<Vector3> getHoverPoints() { | |
return hoverPoints; | |
} | |
public void setSubject(GameObject obj) { | |
subject = obj; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class InteractionManager : MonoBehaviour { | |
private GameObject focalPointsContainer; | |
private GameObject transformer; | |
private List<Vector3> focalPoints = new List<Vector3>(); | |
private List<Vector3> hoverPoints = new List<Vector3>(); | |
public List<GameObject> controllers; | |
public List<GameObject> pips; | |
public GameObject subject; | |
public GameObject pip; | |
private Transform subjectTransform; | |
private Transform previousParent; | |
// Use this for initialization | |
void Start () { | |
focalPointsContainer = new GameObject("focalPoints"); | |
transformer = new GameObject("transformer"); | |
focalPointsContainer.transform.parent = transform; | |
transformer.transform.parent = transform; | |
subjectTransform = subject.transform; | |
if (subjectTransform.parent != null) { | |
previousParent = subjectTransform.parent.gameObject.transform; | |
} | |
foreach (GameObject controller in controllers) { | |
InteractionController ixdc = controller.GetComponent<InteractionController> (); | |
ixdc.setSubject (subject); | |
} | |
for(int i = 0; i < 6; i++) { | |
GameObject newPip = Instantiate(pip,Vector3.zero, Quaternion.identity) as GameObject; | |
newPip.transform.parent = focalPointsContainer.transform; | |
pips.Add(newPip); | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
int lastFocalPointCount = focalPoints.Count; | |
focalPoints.Clear (); | |
hoverPoints.Clear (); | |
foreach (GameObject controller in controllers) { | |
InteractionController ixdc = controller.GetComponent<InteractionController> (); | |
focalPoints.AddRange (ixdc.getFocalPoints ()); | |
hoverPoints.AddRange (ixdc.getHoverPoints ()); | |
} | |
for (int i = 0; i < 6; i++) { | |
if (i < focalPoints.Count) { | |
pips [i].SetActive (true); | |
pips [i].GetComponent<PipController> ().SetClickDown (true); | |
pips [i].transform.position = focalPoints [i]; | |
} else if (i < focalPoints.Count + hoverPoints.Count) { | |
pips [i].SetActive (true); | |
pips [i].GetComponent<PipController> ().SetClickDown (false); | |
pips [i].transform.position = hoverPoints [i - focalPoints.Count]; | |
} else { | |
pips [i].SetActive (false); | |
} | |
} | |
if (lastFocalPointCount == focalPoints.Count && focalPoints.Count > 0) { | |
subjectTransform.SetParent (transformer.transform); | |
} else { | |
subjectTransform.SetParent (previousParent); | |
} | |
// transform | |
if (focalPoints.Count > 0) { | |
Vector3 averagePoint = new Vector3(); | |
foreach (Vector3 point in focalPoints) { | |
averagePoint += point; | |
} | |
averagePoint /= focalPoints.Count; | |
transformer.transform.position = averagePoint; | |
} | |
// scale | |
if (focalPoints.Count > 1) { | |
float averageDistance = 0; | |
foreach (Vector3 pointA in focalPoints) { | |
foreach (Vector3 pointB in focalPoints) { | |
if (pointA != pointB) { | |
averageDistance += (pointA - pointB).magnitude; | |
} | |
} | |
} | |
averageDistance /= focalPoints.Count; | |
transformer.transform.localScale = new Vector3 (averageDistance, averageDistance, averageDistance); | |
} | |
if (focalPoints.Count == 2) { | |
Quaternion currentRotation = Quaternion.LookRotation (focalPoints[0] - focalPoints[1]); | |
transformer.transform.rotation = currentRotation; | |
} | |
} | |
public void addFocalPoints(List<Vector3> focalPointsToAdd) { | |
focalPoints.AddRange (focalPointsToAdd); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class PipController : MonoBehaviour { | |
private bool isClickDown = false; | |
private Renderer thisRenderer; | |
// Use this for initialization | |
void Start () { | |
thisRenderer = GetComponent<Renderer> (); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (isClickDown) { | |
thisRenderer.material.color = Color.red; | |
} else { | |
thisRenderer.material.color = Color.green; | |
} | |
} | |
public void SetClickDown(bool active) { | |
isClickDown = active; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment