Repository navigation
Expand file tree
/
Copy pathGazeInputManager.cs
More file actions
90 lines (75 loc) · 2.79 KB
/
Copy pathGazeInputManager.cs
File metadata and controls
90 lines (75 loc) · 2.79 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AsagiHandyScripts
{
public class GazeInputManager : MonoBehaviour
{
public class GazeInputEventArgs : EventArgs
{
public GameObject target;
public Vector3 point;
public Vector3 normal;
public GazeInputEventArgs(GameObject _target, Vector3 _point, Vector3 _normal)
{
target = _target;
point = _point;
normal = _normal;
}
}
public event EventHandler<GazeInputEventArgs> GazeIn;
public event EventHandler<GazeInputEventArgs> GazeOut;
[SerializeField]
Transform eye;
[SerializeField]
LayerMask rayMask;
[SerializeField]
float rayRadius = 0.1f;
[SerializeField]
float rayMaxDistance = 3f;
[SerializeField]
bool isDebug = false;
GazeInputEventArgs gazeTarget = null;
public GazeInputEventArgs GazeTarget { get { return gazeTarget; } }
public float RayMaxDistance { get { return rayMaxDistance; } }
// Use this for initialization
void Start()
{
if (isDebug)
{
LineRenderer line = gameObject.AddComponent<LineRenderer>();
line.positionCount = 2;
line.startWidth = 0.05f;
line.endWidth = 0.05f;
line.useWorldSpace = false;
line.SetPositions(new Vector3[] { Vector3.down * 0.5f, Vector3.forward * rayMaxDistance });
}
}
// Update is called once per frame
void Update()
{
RaycastHit hitInfo = new RaycastHit();
if (Physics.SphereCast(eye.position, rayRadius, eye.forward, out hitInfo, rayMaxDistance, rayMask.value))
{
GazeInputEventArgs newArgs = new GazeInputEventArgs(hitInfo.collider.gameObject, hitInfo.point, hitInfo.normal);
if (gazeTarget != null && gazeTarget.target.GetInstanceID() != hitInfo.collider.gameObject.GetInstanceID() && GazeOut != null)
if(GazeOut != null)
GazeOut(this, gazeTarget);
if (gazeTarget == null || gazeTarget.target.GetInstanceID() != hitInfo.collider.gameObject.GetInstanceID())
if (GazeIn != null)
GazeIn(this, newArgs);
gazeTarget = newArgs;
}
else
{
if (gazeTarget != null)
{
if (GazeOut != null)
GazeOut(this, gazeTarget);
gazeTarget = null;
}
}
}
}
}