Tanoda
SimpleInteractionGlow.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity;
11using System.Collections;
12using System.Collections.Generic;
13using UnityEngine;
14
20[AddComponentMenu("")]
21[RequireComponent(typeof(InteractionBehaviour))]
22public class SimpleInteractionGlow : MonoBehaviour {
23
24 [Tooltip("If enabled, the object will lerp to its hoverColor when a hand is nearby.")]
25 public bool useHover = true;
26
27 [Tooltip("If enabled, the object will use its primaryHoverColor when the primary hover of an InteractionHand.")]
28 public bool usePrimaryHover = false;
29
30 [Header("InteractionBehaviour Colors")]
31 public Color defaultColor = Color.Lerp(Color.black, Color.white, 0.1F);
33 public Color hoverColor = Color.Lerp(Color.black, Color.white, 0.7F);
34 public Color primaryHoverColor = Color.Lerp(Color.black, Color.white, 0.8F);
35
36 [Header("InteractionButton Colors")]
37 [Tooltip("This color only applies if the object is an InteractionButton or InteractionSlider.")]
38 public Color pressedColor = Color.white;
39
40 private Material _material;
41
42 private InteractionBehaviour _intObj;
43
44 void Start() {
45 _intObj = GetComponent<InteractionBehaviour>();
46
47 Renderer renderer = GetComponent<Renderer>();
48 if (renderer == null) {
49 renderer = GetComponentInChildren<Renderer>();
50 }
51 if (renderer != null) {
52 _material = renderer.material;
53 }
54 }
55
56 void Update() {
57 if (_material != null) {
58
59 // The target color for the Interaction object will be determined by various simple state checks.
60 Color targetColor = defaultColor;
61
62 // "Primary hover" is a special kind of hover state that an InteractionBehaviour can
63 // only have if an InteractionHand's thumb, index, or middle finger is closer to it
64 // than any other interaction object.
65 if (_intObj.isPrimaryHovered && usePrimaryHover) {
66 targetColor = primaryHoverColor;
67 }
68 else {
69 // Of course, any number of objects can be hovered by any number of InteractionHands.
70 // InteractionBehaviour provides an API for accessing various interaction-related
71 // state information such as the closest hand that is hovering nearby, if the object
72 // is hovered at all.
73 if (_intObj.isHovered && useHover) {
74 float glow = _intObj.closestHoveringControllerDistance.Map(0F, 0.2F, 1F, 0.0F);
75 targetColor = Color.Lerp(defaultColor, hoverColor, glow);
76 }
77 }
78
79 if (_intObj.isSuspended) {
80 // If the object is held by only one hand and that holding hand stops tracking, the
81 // object is "suspended." InteractionBehaviour provides suspension callbacks if you'd
82 // like the object to, for example, disappear, when the object is suspended.
83 // Alternatively you can check "isSuspended" at any time.
84 targetColor = suspendedColor;
85 }
86
87 // We can also check the depressed-or-not-depressed state of InteractionButton objects
88 // and assign them a unique color in that case.
89 if (_intObj is InteractionButton && (_intObj as InteractionButton).isPressed) {
90 targetColor = pressedColor;
91 }
92
93 // Lerp actual material color to the target color.
94 _material.color = Color.Lerp(_material.color, targetColor, 30F * Time.deltaTime);
95 }
96 }
97
98}
UnityEngine.Color Color
Definition: TestScript.cs:32
InteractionBehaviours are components that enable GameObjects to interact with interaction controllers...
bool isPrimaryHovered
Gets whether this object is the primary hover for any interaction controller.
bool isSuspended
Gets whether the object is currently suspended. An object is "suspended" if it is currently grasped b...
bool isHovered
Gets whether any interaction controller is nearby.
float closestHoveringControllerDistance
Gets the distance from this object to the palm of the closest hand to this object,...
A physics-enabled button. Activated by physically pressing the button, with events for press and unpr...
This simple script changes the color of an InteractionBehaviour as a function of its distance to the ...