123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- //
- // Purpose: Highlights the controller when hovering over interactables
- //
- //=============================================================================
- using UnityEngine;
- using System.Collections;
- namespace Valve.VR.InteractionSystem
- {
- //-------------------------------------------------------------------------
- public class ControllerHoverHighlight : MonoBehaviour
- {
- public Material highLightMaterial;
- public bool fireHapticsOnHightlight = true;
- protected Hand hand;
- protected RenderModel renderModel;
- protected SteamVR_Events.Action renderModelLoadedAction;
- protected void Awake()
- {
- hand = GetComponentInParent<Hand>();
- }
- protected void OnHandInitialized(int deviceIndex)
- {
- GameObject renderModelGameObject = GameObject.Instantiate(hand.renderModelPrefab);
- renderModelGameObject.transform.parent = this.transform;
- renderModelGameObject.transform.localPosition = Vector3.zero;
- renderModelGameObject.transform.localRotation = Quaternion.identity;
- renderModelGameObject.transform.localScale = hand.renderModelPrefab.transform.localScale;
- renderModel = renderModelGameObject.GetComponent<RenderModel>();
- renderModel.SetInputSource(hand.handType);
- renderModel.OnHandInitialized(deviceIndex);
- renderModel.SetMaterial(highLightMaterial);
- hand.SetHoverRenderModel(renderModel);
- renderModel.onControllerLoaded += RenderModel_onControllerLoaded;
- renderModel.Hide();
- }
- private void RenderModel_onControllerLoaded()
- {
- renderModel.Hide();
- }
- //-------------------------------------------------
- protected void OnParentHandHoverBegin(Interactable other)
- {
- if (!this.isActiveAndEnabled)
- {
- return;
- }
- if (other.transform.parent != transform.parent)
- {
- ShowHighlight();
- }
- }
- //-------------------------------------------------
- private void OnParentHandHoverEnd(Interactable other)
- {
- HideHighlight();
- }
- //-------------------------------------------------
- private void OnParentHandInputFocusAcquired()
- {
- if (!this.isActiveAndEnabled)
- {
- return;
- }
- if (hand.hoveringInteractable && hand.hoveringInteractable.transform.parent != transform.parent)
- {
- ShowHighlight();
- }
- }
- //-------------------------------------------------
- private void OnParentHandInputFocusLost()
- {
- HideHighlight();
- }
- //-------------------------------------------------
- public void ShowHighlight()
- {
- if (renderModel == null)
- {
- return;
- }
- if (fireHapticsOnHightlight)
- {
- hand.TriggerHapticPulse(500);
- }
- renderModel.Show();
- }
- //-------------------------------------------------
- public void HideHighlight()
- {
- if (renderModel == null)
- {
- return;
- }
- if (fireHapticsOnHightlight)
- {
- hand.TriggerHapticPulse(300);
- }
- renderModel.Hide();
- }
- }
- }
|