private void addPoseTrackerDistance(int trackerNum) { for (int po_i = 0; po_i < latestPoses.Length; po_i++) { TrackedDevicePose_t trackedDevicePose = latestPoses[po_i]; SteamVR_TrackedObject.EIndex eIndex = (SteamVR_TrackedObject.EIndex)po_i; if (trackedDevicePose.bDeviceIsConnected && trackedDevicePose.bPoseIsValid && eIndex != SteamVR_TrackedObject.EIndex.Hmd) { var pose = new SteamVR_Utils.RigidTransform(trackedDevicePose.mDeviceToAbsoluteTracking); Vector3 trackedObjectOrigin = trackedObjects[trackerNum].gameObject.transform.position; float dist = Vector3.Distance(pose.pos, trackedObjectOrigin); if (!distances[trackerNum].ContainsKey(eIndex)) { distances[trackerNum].Add(eIndex, new List <float>() { dist }); } else { distances[trackerNum][eIndex].Add(dist); } } } }
bool IsTracked_Steam() { if (string.IsNullOrEmpty(context.Data.trackedByDevice)) { return(false); } try { if (trackedByIndexSteam.ToString() != context.Data.trackedByDevice) { trackedByIndexSteam = (SteamVR_TrackedObject.EIndex)System.Enum.Parse(typeof(SteamVR_TrackedObject.EIndex), context.Data.trackedByDevice); } if (trackedByIndexSteam == SteamVR_TrackedObject.EIndex.None || (int)trackedByIndexSteam >= Valve.VR.OpenVR.k_unMaxTrackedDeviceCount) { return(false); } if (!SteamVR.instance.hmd.IsTrackedDeviceConnected((uint)trackedByIndexSteam)) { return(false); } return(true); } catch (System.Exception) { return(false); } }
/// <summary> /// 匹配对应的设备号,完成手柄模型的设置 /// </summary> /// <param name="device"></param> /// <param name="index"></param> void DeviceActive(SteamVR_TrackedObject device, uint index) { SteamVR_TrackedObject.EIndex eIndex = (SteamVR_TrackedObject.EIndex)Enum.Parse(typeof(SteamVR_TrackedObject.EIndex), "Device" + index); device.index = eIndex; device.GetComponentInChildren <SteamVR_RenderModel>().index = eIndex; device.gameObject.SetActive(true); }
bool IsTracked_Steam() { if (string.IsNullOrEmpty(context.Data.trackedByDevice)) { return(false); } try { if (trackedByIndexSteam.ToString() != context.Data.trackedByDevice) { trackedByIndexSteam = (SteamVR_TrackedObject.EIndex)System.Enum.Parse(typeof(SteamVR_TrackedObject.EIndex), context.Data.trackedByDevice); } if (trackedByIndexSteam == SteamVR_TrackedObject.EIndex.None || (int)trackedByIndexSteam >= Valve.VR.OpenVR.k_unMaxTrackedDeviceCount) { return(false); } SteamVR_Controller.Device dev = SteamVR_Controller.Input((int)trackedByIndexSteam); return(dev.valid && dev.hasTracking); } catch (System.Exception) { return(false); } }
public void SetDeviceIndex(int index) { this.index = (SteamVR_TrackedObject.EIndex)index; this.modelOverride = string.Empty; if (base.enabled) { this.UpdateModel(); } }
public void SetDeviceIndex(int index) { this.index = (SteamVR_TrackedObject.EIndex)index; modelOverride = ""; if (enabled) { UpdateModel(); } }
// Use this for initialization void Start() { SteamVR_TrackedObject.EIndex index = GetComponentInParent <SteamVR_TrackedObject>().index; controller = new SteamVR_Controller.Device((uint)index); lineRenderer = GetComponent <LineRenderer>(); charge = transform.FindChild("Charge").gameObject; chargeOriginScale = charge.transform.localScale.y; }
private void CreateTrackedObjects() { // Get list of Tracked device IDs List <SteamVR_TrackedObject.EIndex> AvailableEIndecies = new List <SteamVR_TrackedObject.EIndex>(); List <SteamVR_TrackedObject.EIndex> AllocatedEIndecies = new List <SteamVR_TrackedObject.EIndex>(); foreach (Dictionary <SteamVR_TrackedObject.EIndex, List <float> > trackedObjectDistances in distances) { AvailableEIndecies = AvailableEIndecies.Union(trackedObjectDistances.Keys).ToList(); } for (int to_i = 0; to_i < _trackedObjects.Count; to_i++) { Dictionary <SteamVR_TrackedObject.EIndex, List <float> > trackedObjectDistances = distances[to_i]; float min = -1; SteamVR_TrackedObject.EIndex allocatedEIndex = SteamVR_TrackedObject.EIndex.None; foreach (var eIndex in AvailableEIndecies) { float mean = trackedObjectDistances[eIndex].Sum() / trackedObjectDistances[eIndex].Count; if (min == -1) { min = mean; allocatedEIndex = eIndex; } else if (mean < min) { min = mean; allocatedEIndex = eIndex; } } if (AllocatedEIndecies.Contains(allocatedEIndex)) { Debug.LogWarning("Tracker " + allocatedEIndex + " allocated twice"); } else { AllocatedEIndecies.Add(allocatedEIndex); } steamVR_TrackedObject = _trackedObjects[to_i].gameObject.AddComponent <SteamVR_TrackedObject>(); steamVR_TrackedObject.SetDeviceIndex((int)allocatedEIndex); } }
// Update is called once per frame void Update() { if (Input.GetKeyUp(KeyCode.F1)) { if (select_controller != Controller_Type.NONE) { SteamVR_TrackedObject.EIndex index = controller[(int)select_controller].index; controller[(int)select_controller].index = controller[(int)Controller_Type.RIGHT].index; controller[(int)Controller_Type.RIGHT].index = index; select_controller = Controller_Type.NONE; } else { select_controller = Controller_Type.RIGHT; } } if (Input.GetKeyUp(KeyCode.F2)) { if (select_controller != Controller_Type.NONE) { SteamVR_TrackedObject.EIndex index = controller[(int)select_controller].index; controller[(int)select_controller].index = controller[(int)Controller_Type.LEFT].index; controller[(int)Controller_Type.LEFT].index = index; select_controller = Controller_Type.NONE; } else { select_controller = Controller_Type.LEFT; } } if (Input.GetKeyUp(KeyCode.F3)) { if (select_controller != Controller_Type.NONE) { SteamVR_TrackedObject.EIndex index = controller[(int)select_controller].index; controller[(int)select_controller].index = controller[(int)Controller_Type.MAT].index; controller[(int)Controller_Type.MAT].index = index; select_controller = Controller_Type.NONE; } else { select_controller = Controller_Type.MAT; } } }
protected override void OnUpdate() { if (_index != Tracking.index) { // Update device _index = Tracking.index; _input = SteamVR_Controller.Input((int)_index); _inputScheme.Device = _input; } ProcessLock(); if (CanAcquireFocus()) { UpdateLogic(); } }
/// <summary> /// Updates the selected device. /// </summary> /// <param name="selectedDevice"></param> public void UpdatedSelectedDevice(int selectedDevice) { this.selectedDevice = (SteamVR_TrackedObject.EIndex)selectedDevice; }
private void Start() { _currentViveIndex = GetComponent <SteamVR_TrackedObject>().index; }
SteamVR_Controller.Device controller;// = new SteamVR_Controller.Device(3); // Use this for initialization void Start() { SteamVR_TrackedObject.EIndex index = GetComponentInParent <SteamVR_TrackedObject>().index; controller = new SteamVR_Controller.Device((uint)index); }
bool GetTrackedTransform_Steam(out Vector3 pos, out Quaternion rot) { pos = Vector3.zero; rot = Quaternion.identity; if (string.IsNullOrEmpty(context.Data.trackedByDeviceId)) { lastTrackingDeviceId = null; if (!string.IsNullOrEmpty(context.Data.trackedByDevice)) { SteamVR_TrackedObject.EIndex deviceIndex = (SteamVR_TrackedObject.EIndex)System.Enum.Parse(typeof(SteamVR_TrackedObject.EIndex), context.Data.trackedByDevice); context.Data.trackedByDeviceId = VRInfo.GetDeviceSerial((uint)deviceIndex); } else { return(false); } } try { if (lastTrackingDeviceId != context.Data.trackedByDeviceId) { if (trackedObject == null) { var proxy = new GameObject("Tracking Proxy"); proxy.transform.parent = transform; trackedObject = proxy.AddComponent <SteamVR_TrackedObject>(); } lastTrackingDeviceId = null; for (int i = 0; i < Valve.VR.OpenVR.k_unMaxTrackedDeviceCount; i++) { string deviceId = VRInfo.GetDeviceSerial((uint)i); if (deviceId == context.Data.trackedByDeviceId) { trackedObject.SetDeviceIndex(i); trackedObject.enabled = false; //Reset component so trackedObject.isValid will be helpful again trackedObject.enabled = true; lastTrackingDeviceId = context.Data.trackedByDeviceId; break; } } newPosesAppliedAction.enabled = true; isTrackedDeviceReady = false; return(false); } if (!isTrackedDeviceReady || string.IsNullOrEmpty(lastTrackingDeviceId) || trackedObject == null || !trackedObject.isValid) { return(false); } pos = trackedObject.transform.localPosition; rot = trackedObject.transform.localRotation; return(true); } catch (System.Exception) { return(false); } }
private void OnNewPoses(TrackedDevicePose_t[] poses) { SteamVR_TrackedObject.EIndex trackerIndex = (SteamVR_TrackedObject.EIndex)SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.First, ETrackedDeviceClass.GenericTracker); SteamVR_TrackedObject[] index = GameObject.FindObjectsOfType <SteamVR_TrackedObject> (); if (trackerIndex == SteamVR_TrackedObject.EIndex.None) { // Debug.Log ("Couldn't find tracker.Please turn on the tracker."); } foreach (SteamVR_TrackedObject obj in index) { if (obj.index == trackerIndex) { tracker = obj.gameObject; } } // SteamVR.instance.hmd.GetDeviceToAbsoluteTrackingPose() if (mainCam == null || backupCam == null) { Debug.Log("VR Cam is null"); } //****For Testing purpose****** if (!lostTrack) //Check whether hmd pose is valid (hmd is trackable) // if(poses[0].bPoseIsValid) { hmdTracking = true; aligned = false; //headCamera.transform.SetParent (mainSteamVRObject.transform); if (backupCamera.activeSelf) { backupCamera.GetComponent <Camera> ().depth = -1; headCamera.GetComponent <Camera> ().depth = 1; backupCamera.GetComponent <Camera> ().enabled = true; backupCamera.SetActive(true); headCamera.gameObject.SetActive(false); backupCamera.tag = "BackupCamera"; mainCam.enabled = false; } prevCamRot = headCamera.transform.rotation; prevCamPos = headCamera.transform.position; Vector3 v1 = Vector3.zero - prevCamPos; Vector3 unitVector = v1 / v1.magnitude; Vector3 v2 = new Vector3(-unitVector.x, 0f, -unitVector.z); alignVector = 4f * v2; Debug.DrawLine(Vector3.zero, backupCamParent.transform.position, Color.red); Debug.DrawLine(Vector3.zero, alignVector, Color.green); Debug.DrawLine(backupCamParent.transform.position, alignVector, Color.blue); if (Input.GetKeyDown(KeyCode.Space) && synthesizer.isBroadcasterStarted) { Quaternion gyro = synthesizer.GetRotation(); Quaternion gyroRot = new Quaternion(gyro.y, -gyro.z, -gyro.x, gyro.w); // Quaternion onlyRot = new Quaternion (0f, -gyro.z, 0f, gyro.w); Quaternion onlyRot = new Quaternion(0f, backupCamera.transform.rotation.z, 0f, backupCamera.transform.rotation.w); initialRot = onlyRot; Vector3 compass = synthesizer.GetCompass(); compassHeading = compass.x; Debug.Log("Initial Rotation : " + initialRot + " ~ Compass Heading : " + compassHeading); } } else { Vector3 acc = synthesizer.GetAcceleration(); Quaternion gyro = synthesizer.GetRotation(); Vector3 compass = synthesizer.GetCompass(); GetRotatingDirection(); hmdTracking = false; backupCamera.SetActive(true); if (headCamera.activeSelf) { backupCamera.GetComponent <Camera> ().depth = 1; headCamera.GetComponent <Camera> ().depth = -1; backupCamera.GetComponent <Camera> ().enabled = true; headCamera.gameObject.SetActive(false); } // tracker.transform.position = Vector3.Lerp (tracker.transform.position, prevCamPos, 1.0f); if (useGyroscope) { Quaternion newRot = new Quaternion(); // newRot = new Quaternion(gyro.y, -gyro.z, -gyro.x, gyro.w) * Quaternion.Euler(euler); tracker.transform.rotation = Quaternion.Lerp(tracker.transform.rotation, newRot, 0.1f); } else { Quaternion newRot = new Quaternion(acc.x, acc.y, -acc.z, 0.4f); tracker.transform.rotation = Quaternion.Lerp(tracker.transform.rotation, newRot, 0.1f); } //Use intermediary (parent) to compensate misaligned camera localposition & localrotation Transform parent = backupCamParent.transform.GetChild(0); Quaternion compensate = Quaternion.Inverse(backupCam.transform.localRotation); parent.transform.localRotation = Quaternion.Lerp(parent.transform.localRotation, compensate, 0.1f); parent.transform.localPosition = -backupCam.transform.localPosition; // Quaternion r = tracker.transform.rotation * Quaternion.Inverse (prevCamRot) ; Quaternion r = tracker.transform.rotation; backupCamParent.transform.rotation = Quaternion.Lerp(backupCamParent.transform.rotation, r, 0.1f); if (!aligned) { // AlignCamera (); aligned = true; Quaternion onlyRot = new Quaternion(0f, -gyro.z, 0f, gyro.w); // Quaternion onlyRot = new Quaternion(0f, -backupCamera.transform.rotation.z, 0f, backupCamera.transform.rotation.w); float angle = Quaternion.Angle(initialRot, onlyRot); if (compass.x - compassHeading < 0f) { //anti-clockwise if (angle > 0f) { angle = -angle; } } else { //clockwise if (angle < 0f) { angle = -angle; } } environment.transform.rotation = Quaternion.AngleAxis(-angle, environment.transform.up); mainSteamVRObject.transform.rotation = Quaternion.AngleAxis(-angle, mainSteamVRObject.transform.up); mainSteamVRObject.transform.position = GameObject.Find("CameraRig Marker 1").transform.position; Debug.Log("Angle : " + (-angle)); } Debug.DrawRay(initialEnvironmentPosition, Vector3.forward * 10f + initialEnvironmentPosition, Color.red); Debug.DrawRay(environment.transform.position, environment.transform.forward * 10f, Color.blue); // Vector3 newPos = backupCam.transform.position; // newPos.y = height; // backupCamParent.transform.position = newPos; } }
private void Update() { if (Time.unscaledTime < next_time) { return; } if (video == null) { UnloadVideo(); return; } int frame_index = (int)((Time.unscaledTime - start_time) / video.time_step); if (frame_index >= video.frames.Length) { UnloadVideo(); return; } while (previous_frame_index < frame_index) { VideoFrame finished_frame = video.frames[++previous_frame_index]; if (finished_frame.destroy_meshes != null) { foreach (var mesh_id in finished_frame.destroy_meshes) { DestroyImmediate(meshes[mesh_id]); meshes[mesh_id] = null; } } } VideoFrame frame = video.frames[frame_index]; next_time = start_time + (frame_index + 1) * video.time_step; while (actors.Count < frame.go.Length) { actors.Add(new Actor { vgo_index = -1, tr = Instantiate(baseActor, this.transform) }); } while (actors.Count > frame.go.Length) { DestroyImmediate(actors.Pop().tr.gameObject); } int j = 0; foreach (var vgo_index in frame.go) { var actor = actors[j++]; if (actor.vgo_index == vgo_index) { continue; /* no updates here */ } Transform tr = actor.tr; VideoGameObject vgo = video.gameobjects[vgo_index]; ExpandToTransform(tr, vgo.tr); tr.GetComponent <MeshFilter>().sharedMesh = LoadMesh(vgo.mesh); Material[] mat = new Material[vgo.mats.Length]; for (int i = 0; i < mat.Length; i++) { mat[i] = LoadMaterial(vgo.mats[i]); } tr.GetComponent <MeshRenderer>().sharedMaterials = mat; actor.vgo_index = vgo_index; } foreach (var ctrl in BaroqueUI.Baroque.GetControllers()) { var trobj = ctrl.GetComponent <SteamVR_TrackedObject>(); if (trobj.index != SteamVR_TrackedObject.EIndex.None) { ctrl_index = trobj.index; } } for (int k = 0; k < 3; k++) { if ((k + 1) * 10 <= frame.head_ctrls.Length) { ExpandToTransform(head_ctrls[k].transform, frame.head_ctrls.Skip(k * 10).Take(10).ToArray()); if (!head_ctrls[k].gameObject.activeSelf) { head_ctrls[k].index = k == 0 ? SteamVR_TrackedObject.EIndex.Hmd : ctrl_index; head_ctrls[k].gameObject.SetActive(true); head_ctrls[k].enabled = true; head_ctrls[k].UpdateModel(); } var rend = head_ctrls[k].GetComponent <MeshRenderer>(); if (rend != null) { rend.sharedMaterial = otherControllersMaterial; } } else { head_ctrls[k].gameObject.SetActive(false); } } }