StationaryAreaMono.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using SC.XR.Unity.Module_InputSystem;
  2. using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class StationaryAreaMono : SafetyAreaBase
  7. {
  8. private bool isFreeze = true;
  9. private StationaryAreaStep stationaryAreaStep;
  10. private GroundHeightStep groundHeightStep;
  11. private float radius;
  12. private void Awake()
  13. {
  14. DontDestroyOnLoad(this.gameObject);
  15. }
  16. public override void Update()
  17. {
  18. base.Update();
  19. if (!isFreeze)
  20. {
  21. Vector3 headPosition = Camera.main.transform.position;
  22. stationaryAreaStep.SetCircleCenter(headPosition);
  23. Vector2 circleCenter = stationaryAreaStep.GetCircleCenter();
  24. this.gameObject.transform.position = new Vector3(circleCenter.x, 0f, circleCenter.y);
  25. }
  26. List<Vector4> positionList = GetInteractionObjectPosition();
  27. //Debug.Log(positionList.Count);
  28. if (positionList.Count != 0)
  29. {
  30. meshRenderer.sharedMaterial.SetInt("positionCount", positionList.Count);
  31. meshRenderer.sharedMaterial.SetVectorArray("interactionPosition", positionList);
  32. }
  33. distance = GetDistance(Camera.main.transform.position);
  34. float paramAlpha = Mathf.Lerp(0.4f, 0.8f, SafetyAreaManager.Instance.AlphaParam);
  35. float displayAlpha = (radius - distance) < 0f ? 1.1f : //在圈外
  36. (radius - distance) > paramAlpha ? 0f : //无需触发
  37. Mathf.Min(1.0f, ((paramAlpha - (radius - distance)) / 0.3f)); //在灵敏度范围内0~1显示
  38. alpha = displayAlpha;//Mathf.Max(0, (distance - radius * 0.5f) * SafetyAreaManager.Instance.AlphaParam);
  39. meshRenderer.sharedMaterial.SetFloat("alpha", displayAlpha);
  40. //var lerp = Mathf.PingPong(Time.time, duration) / duration;
  41. Color colorStart = colorSections[SafetyAreaManager.Instance.OriginSafetyAreaColorIndex].startColor;
  42. Color colorEnd = colorSections[SafetyAreaManager.Instance.OriginSafetyAreaColorIndex].endColor;
  43. meshRenderer.sharedMaterial.SetColor("_Color1", colorStart);
  44. meshRenderer.sharedMaterial.SetColor("_Color2", colorEnd);
  45. var lerp = Mathf.PingPong(Time.time, duration) / duration;
  46. meshRenderer.sharedMaterial.SetColor("settingsColor", Color.Lerp(nearColorStart, nearColorEnd, lerp));
  47. meshRenderer.sharedMaterial.SetVector("_MainTex_ST", new Vector4((float)perimeter / PlayAreaConstant.SAFETY_AREA_HEIGHT * 7f, 7f, 0f, -0.05f));
  48. if (outOfSafetyArea == null)
  49. {
  50. GameObject outOfSafetyAreaResource = Resources.Load<GameObject>("OutofSafetyArea");
  51. outOfSafetyArea = GameObject.Instantiate(outOfSafetyAreaResource, this.transform);
  52. }
  53. if (SafetyAreaManager.Instance.ShowAreaWhenBowHead)
  54. {
  55. if (Vector3.Angle(Camera.main.transform.forward, Vector3.down) < PlayAreaConstant.HEAD_BOW_ANGLE)
  56. {
  57. meshRenderer.sharedMaterial.SetInt("bowHead", 1);
  58. }
  59. else
  60. {
  61. meshRenderer.sharedMaterial.SetInt("bowHead", 0);
  62. }
  63. }
  64. else
  65. {
  66. meshRenderer.sharedMaterial.SetInt("bowHead", 0);
  67. }
  68. //if (!SafetyAreaManager.Instance.isSettingSafetyArea && !SafetyAreaManager.Instance.isDisableSafetyArea)
  69. //{
  70. // int currentRelocState = API_GSXR_Slam.GSXR_Get_OfflineMapRelocState();
  71. // if (currentRelocState == 0)
  72. // {
  73. // nomapUI.SetActive(true);
  74. // meshRenderer.enabled = false;
  75. // }
  76. // else
  77. // {
  78. // if (distance > 5f)
  79. // {
  80. // slamLostUI.gameObject.SetActive(true);
  81. // meshRenderer.enabled = false;
  82. // }
  83. // else
  84. // {
  85. // slamLostUI.gameObject.SetActive(false);
  86. // meshRenderer.enabled = true;
  87. // if (alpha >= 10f)
  88. // {
  89. // outOfSafetyArea.SetActive(true);
  90. // }
  91. // else
  92. // {
  93. // outOfSafetyArea.SetActive(false);
  94. // }
  95. // }
  96. // }
  97. //}
  98. //else
  99. //{
  100. // outOfSafetyArea.SetActive(false);
  101. // nomapUI.SetActive(false);
  102. // slamLostUI.gameObject.SetActive(false);
  103. // meshRenderer.enabled = true;
  104. //}
  105. if (existSafetyAreaStateMachine != null)
  106. {
  107. existSafetyAreaStateMachine.Breathe();
  108. }
  109. }
  110. public void FreezeStationaryAreaPosition()
  111. {
  112. isFreeze = true;
  113. }
  114. public void UnFreezeStationaryAreaPosition()
  115. {
  116. isFreeze = false;
  117. }
  118. public void SetRadius(float radius)
  119. {
  120. this.radius = radius;
  121. }
  122. public float GetRadius()
  123. {
  124. return this.radius;
  125. }
  126. public void SetMesh(Mesh mesh, float radius)
  127. {
  128. SetRadius(radius);
  129. if (stationaryAreaStep == null)
  130. {
  131. stationaryAreaStep = SafetyAreaManager.Instance.GetStep<StationaryAreaStep>(SafetyAreaStepEnum.StationaryArea);
  132. }
  133. if (groundHeightStep == null)
  134. {
  135. groundHeightStep = SafetyAreaManager.Instance.GetStep<GroundHeightStep>(SafetyAreaStepEnum.GroundHeight);
  136. }
  137. meshFilter = this.gameObject.AddComponent<MeshFilter>();
  138. meshRenderer = this.gameObject.AddComponent<MeshRenderer>();
  139. //meshRenderer.material = Resources.Load<Material>("Material/SafetyEdgeMat");
  140. meshFilter.mesh = mesh;
  141. //SetOriginHeight(groundHeightStep.GetPlaneHeight());
  142. perimeter = 2f * Mathf.PI * radius;
  143. }
  144. public float GetDistance(Vector3 position)
  145. {
  146. if (position.y >= 2.5f) return float.PositiveInfinity;
  147. return Vector2.Distance(new Vector2(this.gameObject.transform.position.x, this.gameObject.transform.position.z), new Vector2(position.x, position.z));
  148. }
  149. }