private void AddHeadTrackerEvent(ref List <VREvent> eventList) { if (Input.GetKey("up")) { headTrackerPos += 0.1f * Camera.main.transform.forward; } else if (Input.GetKey("down")) { headTrackerPos -= 0.1f * Camera.main.transform.forward; } else if (Input.GetKey("left")) { headTrackerRot *= Quaternion.AngleAxis(-1.0f, new Vector3(0f, 1f, 0f)); } else if (Input.GetKey("right")) { headTrackerRot *= Quaternion.AngleAxis(1.0f, new Vector3(0f, 1f, 0f)); } Matrix4x4 m3 = Matrix4x4.TRS(headTrackerPos, headTrackerRot, Vector3.one); float[] d3 = VRConvert.ToFloatArray(m3); VREvent e = new VREvent(fakeHeadTrackerEvent); e.AddData("EventType", "TrackerMove"); e.AddData("Transform", d3); eventList.Add(e); }
// Update is called once per frame void Update() { if (threadingMode == ThreadingMode.SINGLE_THREADED) { // update the trackers only once per frame if (lastTrackerUpdateFrame != Time.frameCount) { UpdateTrackers(); lastTrackerUpdateFrame = Time.frameCount; } trackerData = (TrackerData)Marshal.PtrToStructure(trackerDataPointer, typeof(TrackerData)); } else { // update the trackers only once per frame if (lastTrackerUpdateFrame != Time.frameCount) { UpdateTrackersMultiThreaded(); lastTrackerUpdateFrame = Time.frameCount; } LockTrackerData(trackerDataPointer); trackerData = (TrackerData)Marshal.PtrToStructure(trackerDataPointer, typeof(TrackerData)); UnlockTrackerData(trackerDataPointer); } Vector3 transformPosition = transform.position; Quaternion transformRotation = transform.rotation; this.updateTransformFromTrackerData(ref transformPosition, ref transformRotation); // if a custom origin has been specified // then update the transform coordinate space if (origin != null) { if (trackingMode != TrackingMode.POSITION_ONLY) transformRotation = origin.rotation * transformRotation; else transformRotation = origin.rotation; if (trackingMode != TrackingMode.ORIENTATION_ONLY) transformPosition = origin.position + origin.rotation * transformPosition; else transformPosition = origin.position; } Matrix4x4 m3 = Matrix4x4.TRS(transformPosition, transformRotation, Vector3.one); float[] d3 = VRConvert.ToFloatArray(m3); VREvent e = new VREvent(eventName); e.AddData("EventType", "TrackerMove"); e.AddData("Transform", d3); pendingEvents.Add(e); if (applyUpdatesToGameObject) { transform.position = transformPosition; transform.rotation = transformRotation; } }
private void AddHeadTrackerEvent(ref List <VREvent> eventList) { // if (Input.GetKey(KeyCode.LeftShift)) // { // if (Input.GetKey(KeyCode.DownArrow)) // { // headTrackerRot *= Quaternion.AngleAxis(-1.0f, new Vector3(1f, 0f, 0f)); // } // else if (Input.GetKey(KeyCode.UpArrow)) // { // headTrackerRot *= Quaternion.AngleAxis(1.0f, new Vector3(1f, 0f, 0f)); // } // } // else if (Input.GetKey(KeyCode.UpArrow)) { headTrackerPos += 0.1f * Camera.main.transform.forward; } else if (Input.GetKey(KeyCode.DownArrow)) { headTrackerPos -= 0.1f * Camera.main.transform.forward; } else if (Input.GetKey(KeyCode.LeftArrow)) { headTrackerRot *= Quaternion.AngleAxis(-1.0f, new Vector3(0f, 1f, 0f)); } else if (Input.GetKey(KeyCode.RightArrow)) { headTrackerRot *= Quaternion.AngleAxis(1.0f, new Vector3(0f, 1f, 0f)); } headTrackerRot.Normalize(); Matrix4x4 m3 = Matrix4x4.TRS(headTrackerPos, headTrackerRot, Vector3.one); float[] d3 = VRConvert.ToFloatArray(m3); VREvent e = new VREvent(fakeHeadTrackerEvent); e.AddData("EventType", "TrackerMove"); e.AddData("Transform", d3); eventList.Add(e); }
public void AddUnityInputEvents(ref List <VREvent> eventList) { // Convery Unity Mouse Button events to VREvent ButtonUp/Down events if (vrDevice.unityMouseBtnsToVREvents) { if (Input.GetMouseButtonDown(0)) { VREvent e = new VREvent("MouseBtnLeft_Down"); e.AddData("EventType", "ButtonDown"); eventList.Add(e); } if (Input.GetMouseButtonDown(2)) { VREvent e = new VREvent("MouseBtnMiddle_Down"); e.AddData("EventType", "ButtonDown"); eventList.Add(e); } if (Input.GetMouseButtonDown(1)) { VREvent e = new VREvent("MouseBtnRight_Down"); e.AddData("EventType", "ButtonDown"); eventList.Add(e); } if (Input.GetMouseButtonUp(0)) { VREvent e = new VREvent("MouseBtnLeft_Up"); e.AddData("EventType", "ButtonUp"); eventList.Add(e); } if (Input.GetMouseButtonUp(2)) { VREvent e = new VREvent("MouseBtnMiddle_Up"); e.AddData("EventType", "ButtonUp"); eventList.Add(e); } if (Input.GetMouseButtonUp(1)) { VREvent e = new VREvent("MouseBtnRight_Up"); e.AddData("EventType", "ButtonUp"); eventList.Add(e); } } // Convert Unity mouse move events to VREvent Cursor events if (vrDevice.unityMouseMovesToVREvents) { Vector3 pos = Input.mousePosition; if (pos != lastMousePos) { Vector3 npos = new Vector3(pos[0] / Screen.width, pos[1] / Screen.height, 0.0f); VREvent e = new VREvent("Mouse_Move"); e.AddData("EventType", "CursorMove"); e.AddData("Position", VRConvert.ToFloatArray(pos)); e.AddData("NormalizedPosition", VRConvert.ToFloatArray(npos)); eventList.Add(e); lastMousePos = pos; } } // Convert Unity key up/down events to VREvent ButtonUp/Down events for (int i = 0; i < vrDevice.unityKeysToVREvents.Count; i++) { if (Input.GetKeyDown(vrDevice.unityKeysToVREvents[i])) { VREvent e = new VREvent("Kbd" + vrDevice.unityKeysToVREvents[i] + "_Down"); e.AddData("EventType", "ButtonDown"); eventList.Add(e); } if (Input.GetKeyDown(vrDevice.unityKeysToVREvents[i])) { VREvent e = new VREvent("Kbd" + vrDevice.unityKeysToVREvents[i] + "_Up"); e.AddData("EventType", "ButtonUp"); eventList.Add(e); } } // Convert Unity Axis values to VREvent AnalogUpdate events for (int i = 0; i < vrDevice.unityAxesToVREvents.Count; i++) { float current = Input.GetAxis(vrDevice.unityAxesToVREvents[i]); bool update = false; if (axesStates.ContainsKey(vrDevice.unityAxesToVREvents[i])) { float last = axesStates[vrDevice.unityAxesToVREvents[i]]; if (current != last) { axesStates[vrDevice.unityAxesToVREvents[i]] = current; update = true; } } else { axesStates.Add(vrDevice.unityAxesToVREvents[i], current); update = true; } if (update) { VREvent e = new VREvent(vrDevice.unityAxesToVREvents[i] + "_Update"); e.AddData("EventType", "AnalogUpdate"); e.AddData("AnalogValue", current); eventList.Add(e); } } // TODO: Convert other Unity inputs as needed (touch, accelerometer, etc.) }
private void AddTrackerEvents(ref List <VREvent> eventList) { float x = Input.mousePosition.x; float y = Input.mousePosition.y; // first time through if (float.IsNaN(lastx)) { lastx = x; lasty = y; return; } if (Input.GetKeyDown("1")) { curTracker = 0; } else if (Input.GetKeyDown("2")) { curTracker = 1; } if (Input.GetKey("x")) { float angle = 0.1f * (x - lastx); if (curTracker == 0) { tracker1Rot = Quaternion.AngleAxis(angle, new Vector3(1f, 0f, 0f)) * tracker1Rot; } else if (curTracker == 1) { tracker2Rot = Quaternion.AngleAxis(angle, new Vector3(1f, 0f, 0f)) * tracker2Rot; } } else if (Input.GetKey("y")) { float angle = 0.1f * (x - lastx); if (curTracker == 0) { tracker1Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 1f, 0f)) * tracker1Rot; } else if (curTracker == 1) { tracker2Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 1f, 0f)) * tracker2Rot; } } else if (Input.GetKey("z")) { float angle = 0.1f * (x - lastx); if (curTracker == 0) { tracker1Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 0f, 1f)) * tracker1Rot; } else if (curTracker == 1) { tracker2Rot = Quaternion.AngleAxis(angle, new Vector3(0f, 0f, 1f)) * tracker2Rot; } } else if (Input.GetKey("left shift")) { float depth = 0.005f * (y - lasty); if (curTracker == 0) { tracker1Pos += depth * Camera.main.transform.forward; } else if (curTracker == 1) { tracker2Pos += depth * Camera.main.transform.forward; } } else { Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y, 0f)); Plane p = new Plane(); float dist = 0.0f; if (curTracker == 0) { p.SetNormalAndPosition(-Camera.main.transform.forward, tracker1Pos); if (p.Raycast(ray, out dist)) { tracker1Pos = ray.GetPoint(dist); } } else if (curTracker == 1) { p.SetNormalAndPosition(-Camera.main.transform.forward, tracker2Pos); if (p.Raycast(ray, out dist)) { tracker2Pos = ray.GetPoint(dist); } } } // for fake traker 1 Matrix4x4 m1 = Matrix4x4.TRS(tracker1Pos, tracker1Rot, Vector3.one); float[] d1 = VRConvert.ToFloatArray(m1); VREvent e1 = new VREvent(fakeTracker1Event); e1.AddData("EventType", "TrackerMove"); e1.AddData("Transform", d1); eventList.Add(e1); // for fake traker 2 Matrix4x4 m2 = Matrix4x4.TRS(tracker2Pos, tracker2Rot, Vector3.one); float[] d2 = VRConvert.ToFloatArray(m2); VREvent e2 = new VREvent(fakeTracker2Event); e2.AddData("EventType", "TrackerMove"); e2.AddData("Transform", d2); eventList.Add(e2); // this.lastx = x; this.lasty = y; }