public void GetGazeData() { VarjoEyeTracking.GazeData eyeTrackingData = VarjoEyeTracking.GetGaze(); _eyeGazeData.Timestamp = 1e-6f * eyeTrackingData.captureTime; _eyeGazeData.isRayValid = eyeTrackingData.status == VarjoEyeTracking.GazeStatus.Valid; _eyeGazeData.Origin = eyeTrackingData.gaze.origin; _eyeGazeData.Direction = eyeTrackingData.gaze.forward; float dist = eyeTrackingData.focusDistance; _eyeGazeData.Distance = dist; _eyeGazeData.isDistanceValid = 0.01f < dist && dist < 2f; }
void Update() { // Request gaze calibration if (Input.GetKeyDown(calibrationRequestKey)) { VarjoEyeTracking.RequestGazeCalibration(gazeCalibrationMode); } // Set output filter type if (Input.GetKeyDown(setOutputFilterTypeKey)) { VarjoEyeTracking.SetGazeOutputFilterType(gazeOutputFilterType); Debug.Log("Gaze output filter type is now: " + VarjoEyeTracking.GetGazeOutputFilterType()); } // Check if gaze is allowed if (Input.GetKeyDown(checkGazeAllowed)) { Debug.Log("Gaze allowed: " + VarjoEyeTracking.IsGazeAllowed()); } // Check if gaze is calibrated if (Input.GetKeyDown(checkGazeCalibrated)) { Debug.Log("Gaze calibrated: " + VarjoEyeTracking.IsGazeCalibrated()); } // Toggle gaze target visibility if (Input.GetKeyDown(toggleGazeTarget)) { gazeTarget.GetComponentInChildren <MeshRenderer>().enabled = !gazeTarget.GetComponentInChildren <MeshRenderer>().enabled; } // Get gaze data if gaze is allowed and calibrated if (VarjoEyeTracking.IsGazeAllowed() && VarjoEyeTracking.IsGazeCalibrated()) { //Get device if not valid if (!device.isValid) { GetDevice(); } // Show gaze target gazeTarget.SetActive(true); if (gazeDataSource == GazeDataSource.InputSubsystem) { // Get data for eye positions, rotations and the fixation point if (device.TryGetFeatureValue(CommonUsages.eyesData, out eyes)) { if (eyes.TryGetLeftEyePosition(out leftEyePosition)) { leftEyeTransform.localPosition = leftEyePosition; } if (eyes.TryGetLeftEyeRotation(out leftEyeRotation)) { leftEyeTransform.localRotation = leftEyeRotation; } if (eyes.TryGetRightEyePosition(out rightEyePosition)) { rightEyeTransform.localPosition = rightEyePosition; } if (eyes.TryGetRightEyeRotation(out rightEyeRotation)) { rightEyeTransform.localRotation = rightEyeRotation; } if (eyes.TryGetFixationPoint(out fixationPoint)) { fixationPointTransform.localPosition = fixationPoint; } } // Set raycast origin point to VR camera position rayOrigin = xrCamera.transform.position; // Direction from VR camera towards fixation point direction = (fixationPointTransform.position - xrCamera.transform.position).normalized; } else { gazeData = VarjoEyeTracking.GetGaze(); if (gazeData.status != VarjoEyeTracking.GazeStatus.Invalid) { // GazeRay vectors are relative to the HMD pose so they need to be transformed to world space if (gazeData.leftStatus != VarjoEyeTracking.GazeEyeStatus.Invalid) { leftEyeTransform.position = xrCamera.transform.TransformPoint(gazeData.left.origin); leftEyeTransform.rotation = Quaternion.LookRotation(xrCamera.transform.TransformDirection(gazeData.left.forward)); } if (gazeData.rightStatus != VarjoEyeTracking.GazeEyeStatus.Invalid) { rightEyeTransform.position = xrCamera.transform.TransformPoint(gazeData.right.origin); rightEyeTransform.rotation = Quaternion.LookRotation(xrCamera.transform.TransformDirection(gazeData.right.forward)); } // Set gaze origin as raycast origin rayOrigin = xrCamera.transform.TransformPoint(gazeData.gaze.origin); // Set gaze direction as raycast direction direction = xrCamera.transform.TransformDirection(gazeData.gaze.forward); // Fixation point can be calculated using ray origin, direction and focus distance fixationPointTransform.position = rayOrigin + direction * gazeData.focusDistance; } } } // Raycast to world from VR Camera position towards fixation point if (Physics.SphereCast(rayOrigin, gazeRadius, direction, out hit)) { // Put target on gaze raycast position with offset towards user gazeTarget.transform.position = hit.point - direction * targetOffset; // Make gaze target point towards user gazeTarget.transform.LookAt(rayOrigin, Vector3.up); // Scale gazetarget with distance so it apperas to be always same size distance = hit.distance; gazeTarget.transform.localScale = Vector3.one * distance; // Prefer layers or tags to identify looked objects in your application // This is done here using GetComponent for the sake of clarity as an example RotateWithGaze rotateWithGaze = hit.collider.gameObject.GetComponent <RotateWithGaze>(); if (rotateWithGaze != null) { rotateWithGaze.RayHit(); } // Alternative way to check if you hit object with tag if (hit.transform.CompareTag("FreeRotating")) { AddForceAtHitPosition(); } } else { // If gaze ray didn't hit anything, the gaze target is shown at fixed distance gazeTarget.transform.position = rayOrigin + direction * floatingGazeTargetDistance; gazeTarget.transform.LookAt(rayOrigin, Vector3.up); gazeTarget.transform.localScale = Vector3.one * floatingGazeTargetDistance; } if (Input.GetKeyDown(loggingToggleKey)) { if (!logging) { StartLogging(); } else { StopLogging(); } return; } if (logging) { int dataCount = VarjoEyeTracking.GetGazeList(out dataSinceLastUpdate); foreach (var data in dataSinceLastUpdate) { LogGazeData(data); } } }