ProximityLight.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace Rokid.UXR.Interaction {
  7. public class ProximityLight : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private PokeInteractor pokeInteractor;
  11. [SerializeField]
  12. private Material ligthMat;
  13. [SerializeField]
  14. private GameObject lightPoint;
  15. [SerializeField]
  16. private float baseDistance = 0.1f;
  17. [SerializeField]
  18. private float minScale = 0.2f;
  19. [SerializeField]
  20. private float maxScale = 2f;
  21. private void Start()
  22. {
  23. if (pokeInteractor == null)
  24. {
  25. pokeInteractor = GetComponentInParent<PokeInteractor>();
  26. }
  27. if (ligthMat == null)
  28. {
  29. ligthMat = GetComponentInChildren<MeshRenderer>().material;
  30. }
  31. if (lightPoint == null)
  32. {
  33. lightPoint = transform.GetChild(0).gameObject;
  34. }
  35. }
  36. private void Update()
  37. {
  38. bool light_active = false;
  39. if (pokeInteractor.HasInteractable)
  40. {
  41. // pokeinter pokeInteractor.Interactable
  42. Vector3 touchPoint = pokeInteractor.TouchPoint;
  43. Vector3 touchNormal = pokeInteractor.TouchNormal;
  44. if (touchPoint != Vector3.zero)
  45. {
  46. if (pokeInteractor.InteractorButtonUpPosition != Vector3.zero)
  47. {
  48. transform.position = new Vector3(touchPoint.x, touchPoint.y, pokeInteractor.InteractorButtonUpPosition.z);
  49. }
  50. else
  51. {
  52. transform.position = touchPoint;
  53. }
  54. transform.rotation = Quaternion.FromToRotation(Vector3.back, pokeInteractor.TouchNormal);
  55. float distance = Vector3.Distance(pokeInteractor.transform.position, transform.position);
  56. distance *= Mathf.Sign(Vector3.Dot(pokeInteractor.TouchNormal, pokeInteractor.transform.position - transform.position));
  57. float scale = Mathf.Clamp(distance / baseDistance, minScale, maxScale);
  58. lightPoint.transform.localScale = Vector3.one * scale;
  59. ligthMat.SetFloat("_Opacity",Mathf.Clamp( 0.4f / (scale * 0.8f),0,1f));
  60. light_active = distance > 0.001f;
  61. }
  62. }
  63. else
  64. {
  65. light_active = false;
  66. //lightPoint.gameObject.SetActive(false);
  67. }
  68. if (light_active != lightPoint.activeSelf)
  69. {
  70. lightPoint.SetActive(light_active);
  71. }
  72. }
  73. }
  74. }