AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GazeableColorPicker.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
5 using UnityEngine;
6 using UnityEngine.Events;
7 
8 namespace HoloToolkit.Examples.ColorPicker
9 {
10  public class GazeableColorPicker : MonoBehaviour, IFocusable, IInputClickHandler
11  {
12  public Renderer rendererComponent;
13 
14  [System.Serializable]
15  public class PickedColorCallback : UnityEvent<Color> { }
16 
17  public PickedColorCallback OnGazedColor = new PickedColorCallback();
18  public PickedColorCallback OnPickedColor = new PickedColorCallback();
19 
20  private bool gazing = false;
21 
22  private void Update()
23  {
24  if (gazing == false) return;
25  UpdatePickedColor(OnGazedColor);
26  }
27 
28  private void UpdatePickedColor(PickedColorCallback cb)
29  {
30  RaycastHit hit = GazeManager.Instance.HitInfo;
31 
32  if (hit.transform.gameObject != rendererComponent.gameObject) { return; }
33 
34  var texture = (Texture2D)rendererComponent.material.mainTexture;
35 
36  Vector2 pixelUV = hit.textureCoord;
37  pixelUV.x *= texture.width;
38  pixelUV.y *= texture.height;
39 
40  Color col = texture.GetPixel((int)pixelUV.x, (int)pixelUV.y);
41  cb.Invoke(col);
42  }
43 
44  public void OnFocusEnter()
45  {
46  gazing = true;
47  }
48 
49  public void OnFocusExit()
50  {
51  gazing = false;
52  }
53 
54  public void OnInputClicked(InputClickedEventData eventData)
55  {
56  UpdatePickedColor(OnPickedColor);
57  }
58  }
59 }
The gaze manager manages everything related to a gaze ray that can interact with other objects...
Definition: GazeManager.cs:13
Interface to implement to react to simple click input.
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
Interface to implement to react to focus enter/exit.
Definition: IFocusable.cs:11
Describes an input event that involves a tap.
void OnInputClicked(InputClickedEventData eventData)