void OnVREvent(VREvent e) { // only respond to tracker move events if ((e.ContainsStringField("EventType")) && (e.GetStringData("EventType") == "TrackerMove")) { string trackerName = e.Name.Remove(e.Name.IndexOf("_Move"), 5); if (!trackersToIgnore.Contains(trackerName)) { float[] data = e.GetFloatArrayData("Transform"); Matrix4x4 m = VRConvert.ToMatrix4x4(data); Vector3 pos = m.GetTranslation(); Quaternion rot = m.GetRotation(); if (!cursors.ContainsKey(trackerName)) { GameObject newCursorObj = Instantiate(cursorPrefab); TextMesh label = newCursorObj.GetComponentInChildren <TextMesh>(); if (label != null) { label.text = trackerName; } cursors[trackerName] = newCursorObj; } GameObject cursorObj = cursors[trackerName]; cursorObj.transform.position = pos; cursorObj.transform.rotation = rot; } } }
private void ProcessCallbacks(VREvent e) { // These "unfiltered" callbacks are called for every event for (int ucb = 0; ucb < genericCallbacksUnfiltered.Count; ucb++) { genericCallbacksUnfiltered[ucb].Invoke(e); } // These callbacks are passed the enitre event data structure, which can really contain anything. if (genericCallbacks.ContainsKey(e.Name)) { for (int cb = 0; cb < genericCallbacks[e.Name].Count; cb++) { genericCallbacks[e.Name][cb].Invoke(e); } } // For these callbacks, we know the type of data, so we pull it out of the dataindex here to // make it a bit easier for the user. if ((analogCallbacks.ContainsKey(e.Name)) && (e.ContainsStringField("EventType")) && (e.GetStringData("EventType") == "AnalogUpdate")) { for (int cb = 0; cb < analogCallbacks[e.Name].Count; cb++) { analogCallbacks[e.Name][cb].Invoke(e.GetFloatData("AnalogValue")); } } if ((buttonDownCallbacks.ContainsKey(e.Name)) && (e.ContainsStringField("EventType")) && ((e.GetStringData("EventType") == "ButtonDown") || (e.GetStringData("EventType") == "ButtonTouch"))) { for (int cb = 0; cb < buttonDownCallbacks[e.Name].Count; cb++) { buttonDownCallbacks[e.Name][cb].Invoke(); } } if ((buttonUpCallbacks.ContainsKey(e.Name)) && (e.ContainsStringField("EventType")) && ((e.GetStringData("EventType") == "ButtonUp") || (e.GetStringData("EventType") == "ButtonUntouch"))) { for (int cb = 0; cb < buttonUpCallbacks[e.Name].Count; cb++) { buttonUpCallbacks[e.Name][cb].Invoke(); } } if ((cursorCallbacks.ContainsKey(e.Name)) && (e.ContainsStringField("EventType")) && (e.GetStringData("EventType") == "CursorMove")) { float[] pos = e.GetFloatArrayData("Position"); float[] npos = e.GetFloatArrayData("NormalizedPosition"); for (int cb = 0; cb < cursorCallbacks[e.Name].Count; cb++) { cursorCallbacks[e.Name][cb].Invoke(new Vector3(pos[0], pos[1], pos[2]), new Vector3(npos[0], npos[1], npos[2])); } } if ((trackerCallbacks.ContainsKey(e.Name)) && (e.ContainsStringField("EventType")) && (e.GetStringData("EventType") == "TrackerMove")) { float[] data = e.GetFloatArrayData("Transform"); Matrix4x4 m = VRConvert.ToMatrix4x4(data); Vector3 pos = m.GetTranslation(); Quaternion rot = m.GetRotation(); for (int cb = 0; cb < trackerCallbacks[e.Name].Count; cb++) { trackerCallbacks[e.Name][cb].Invoke(pos, rot); } } }
void OnVREvent(VREvent e) { if (e.Name == user1HeadTrackingEvent) { Matrix4x4 m = VRConvert.ToMatrix4x4(e.DataIndex.GetValueAsDoubleArray(matrix4x4DataField)); if (user_id == 1) { transform.position = m.GetTranslation(); transform.rotation = m.GetRotation(); } //Debug.Log("Head Position = " + m.GetTranslation().ToString()); //Debug.Log("Head Rotation = " + m.GetRotation().ToString()); //------------------ Avatar 1 movement ------------------------ // position Vector3 pos = m.GetTranslation(); user1Avatar.transform.position = new Vector3(pos.x, user1Avatar.transform.position.y, pos.z); // rotation Quaternion rot = m.GetRotation(); // 3rd column of a rotation matrix is the z-axis Vector3 z = new Vector3(m.GetColumn(2).x, m.GetColumn(2).y, m.GetColumn(2).z); // z-axis of the tracker projected onto a horizontal plane Vector3 zHorizontal = (z - Vector3.Dot(z, Vector3.up) * Vector3.up).normalized; // align the z axis of the avatar with zHorizontal Quaternion newRot = Quaternion.FromToRotation(new Vector3(0, 0, 1), zHorizontal); user1Avatar.transform.rotation = newRot; //------------------ Menu movement ------------------------ if (user_id == 1) { /* * GameObject terrain = GameObject.Find ("Terrain"); * MenuContainer.transform.position = new Vector3 (pos.x, terrain.transform.position.y, pos.z); * MenuContainer.transform.rotation = newRot; */ Text hPos = GameObject.Find("MinVRUnityClient/VRCameraPair/HeadPosition").GetComponent <Text> (); Text mPos = GameObject.Find("MinVRUnityClient/VRCameraPair/MenuPosition").GetComponent <Text> (); Text bPos = GameObject.Find("MinVRUnityClient/VRCameraPair/BrushPosition").GetComponent <Text> (); //MenuContainer.transform.position = new Vector3 (pos.x, pos.y - 50, pos.z); //MenuContainer.transform.rotation = newRot; hPos.text = "H pos: " + pos.ToString(); mPos.text = "m Pos: " + MenuContainer.transform.position.ToString(); bPos.text = "b Pos: " + brush1.transform.position.ToString(); } } else if (e.Name == user2HeadTrackingEvent) { Matrix4x4 m = VRConvert.ToMatrix4x4(e.DataIndex.GetValueAsDoubleArray(matrix4x4DataField)); if (user_id == 2) { transform.position = m.GetTranslation(); transform.rotation = m.GetRotation(); } //Debug.Log("Head Position = " + m.GetTranslation().ToString()); //Debug.Log("Head Rotation = " + m.GetRotation().ToString()); //------------------ Avatar 2 movement ------------------------ // position Vector3 pos = m.GetTranslation(); user2Avatar.transform.position = new Vector3(pos.x, user2Avatar.transform.position.y, pos.z); // rotation Quaternion rot = m.GetRotation(); // 3rd column of a rotation matrix is the z-axis Vector3 z = new Vector3(m.GetColumn(2).x, m.GetColumn(2).y, m.GetColumn(2).z); // z-axis of the tracker projected onto a horizontal plane Vector3 zHorizontal = (z - Vector3.Dot(z, Vector3.up) * Vector3.up).normalized; // align the z axis of the avatar with zHorizontal Quaternion newRot = Quaternion.FromToRotation(new Vector3(0, 0, 1), zHorizontal); user2Avatar.transform.rotation = newRot; //------------------ Menu movement ------------------------ if (user_id == 2) { Text hPos = GameObject.Find("MinVRUnityClient/VRCameraPair/HeadPosition").GetComponent <Text> (); Text mPos = GameObject.Find("MinVRUnityClient/VRCameraPair/MenuPosition").GetComponent <Text> (); Text bPos = GameObject.Find("MinVRUnityClient/VRCameraPair/BrushPosition").GetComponent <Text> (); //MenuContainer.transform.position = new Vector3 (pos.x, pos.y - 50, pos.z); //MenuContainer.transform.rotation = newRot; hPos.text = "H pos: " + pos.ToString(); mPos.text = "m Pos: " + MenuContainer.transform.position.ToString(); bPos.text = "b Pos: " + brush2.transform.position.ToString(); } } }