-
Notifications
You must be signed in to change notification settings - Fork 0
/
TobiiTest.cs
78 lines (61 loc) · 2.47 KB
/
TobiiTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class TobiiTest : MonoBehaviour
{
TobiiResearch.TobiiResearchEyeTrackers eyeTrackers;
TobiiResearch.EyeTracker eyeTracker;
public GameObject leftEye;
public GameObject rightEye;
public Camera camera;
public float left_gazeX;
public float left_gazeY;
public float right_gazeX;
public float right_gazeY;
public Vector3 worldPoint;
public float objectDistance = 2;
// Start is called before the first frame update
void Start()
{
eyeTrackers = new TobiiResearch.TobiiResearchEyeTrackers {};
_ = TobiiResearch.FindEyeTrackers(ref eyeTrackers);
print(eyeTrackers.count);
if(eyeTrackers.count.ToUInt64() == 0) return;
eyeTracker = TobiiResearch.GetEyeTracker(ref eyeTrackers, 0);
string name = TobiiResearch.GetDeviceName(ref eyeTracker);
print(name);
//TobiiResearch.FreeEyeTrackers(ref eyeTrackers);
TobiiResearch.SubscribeGaze(ref eyeTracker, TobiiResearch.GazeCallback);
print("Subscribed?\n");
}
// Update is called once per frame
void Update()
{
left_gazeX = (TobiiResearch.leftEye.x);
left_gazeY = (TobiiResearch.leftEye.y);
right_gazeX = (TobiiResearch.rightEye.x);
right_gazeY = (TobiiResearch.rightEye.y);
int numValid = 0;
if(!float.IsNaN(left_gazeX)) numValid++;
if(!float.IsNaN(right_gazeX)) numValid++;
float avg_x = ((float.IsNaN(left_gazeX) ? 0 : left_gazeX) + (float.IsNaN(right_gazeX) ? 0 : right_gazeX)) / numValid;
float avg_y = ((float.IsNaN(left_gazeX) ? 0 : left_gazeY) + (float.IsNaN(right_gazeX) ? 0 : right_gazeY)) / numValid;
int ScreenW = Screen.width;
int ScreenH = Screen.height;
int ScreenX = (int)(ScreenW * avg_x);
int ScreenY = (int)(ScreenH - ScreenH * avg_y);
Vector3 screenPoint;
screenPoint.x = ScreenX;
screenPoint.y = ScreenY;
screenPoint.z = objectDistance;
worldPoint = camera.ScreenToWorldPoint(screenPoint, camera.stereoActiveEye);
leftEye.transform.position = new Vector3(worldPoint.x, worldPoint.y, worldPoint.z);
}
void OnApplicationQuit(){
TobiiResearch.UnsubscribeGaze(ref eyeTracker, TobiiResearch.GazeCallback);
print("Unsubscribe");
TobiiResearch.FreeEyeTrackers(ref eyeTrackers);
print("Free");
}
}