PlayAreaMono.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using SC.XR.Unity.Module_InputSystem;
  2. using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class PlayAreaMono : SafetyAreaBase
  7. {
  8. private Color[] colors;
  9. private void Awake()
  10. {
  11. DontDestroyOnLoad(this.gameObject);
  12. }
  13. // Update is called once per frame
  14. public override void Update()
  15. {
  16. base.Update();
  17. List<Vector4> positionList = GetInteractionObjectPosition();
  18. //Debug.Log(positionList.Count);
  19. if (positionList.Count != 0)
  20. {
  21. meshRenderer.sharedMaterial.SetInt("positionCount", positionList.Count);
  22. meshRenderer.sharedMaterial.SetVectorArray("interactionPosition", positionList);
  23. }
  24. distance = GetDistance(Camera.main.transform.position);
  25. float fillPercentage = GetPlaneFillPercentage(Camera.main.transform.position);
  26. float paramAlpha = Mathf.Lerp(0.6f, 0.99f, SafetyAreaManager.Instance.AlphaParam);
  27. float displayAlpha = fillPercentage < 0.45f ? 1.1f : //超出安全区域圈外
  28. fillPercentage > paramAlpha ? 0f : //无需触发
  29. Mathf.Min(1.0f, (paramAlpha - fillPercentage) / 0.08f); //在灵敏度范围内0~1显示
  30. alpha = displayAlpha;
  31. //Debug.LogError("Alpha:" + alpha + " FillPercentage:" + fillPercentage + " paramAlpha:" + paramAlpha);
  32. meshRenderer.sharedMaterial.SetFloat("alpha", displayAlpha);
  33. Color colorStart = colorSections[SafetyAreaManager.Instance.OriginSafetyAreaColorIndex].startColor;
  34. Color colorEnd = colorSections[SafetyAreaManager.Instance.OriginSafetyAreaColorIndex].endColor;
  35. meshRenderer.sharedMaterial.SetColor("_Color1", colorStart);
  36. meshRenderer.sharedMaterial.SetColor("_Color2", colorEnd);
  37. var lerp = Mathf.PingPong(Time.time, duration) / duration;
  38. meshRenderer.sharedMaterial.SetColor("settingsColor", Color.Lerp(nearColorStart, nearColorEnd, lerp)) ;
  39. meshRenderer.sharedMaterial.SetVector("_MainTex_ST", new Vector4(Mathf.RoundToInt(perimeter / PlayAreaConstant.SAFETY_AREA_HEIGHT * 7f), 7f, 0f, -0.05f));
  40. if (SafetyAreaManager.Instance.ShowAreaWhenBowHead)
  41. {
  42. if (Vector3.Angle(Camera.main.transform.forward, Vector3.down) < PlayAreaConstant.HEAD_BOW_ANGLE)
  43. {
  44. meshRenderer.sharedMaterial.SetInt("bowHead", 1);
  45. }
  46. else
  47. {
  48. meshRenderer.sharedMaterial.SetInt("bowHead", 0);
  49. }
  50. }
  51. else
  52. {
  53. meshRenderer.sharedMaterial.SetInt("bowHead", 0);
  54. }
  55. //TODO StateMachine
  56. //if (!SafetyAreaManager.Instance.isSettingSafetyArea && !SafetyAreaManager.Instance.isDisableSafetyArea)
  57. //{
  58. // int currentRelocState = API_GSXR_Slam.GSXR_Get_OfflineMapRelocState();
  59. // if (currentRelocState == 0)
  60. // {
  61. // nomapUI.SetActive(true);
  62. // meshRenderer.enabled = false;
  63. // }
  64. // else
  65. // {
  66. // nomapUI.SetActive(false);
  67. // if (distance > 5f)
  68. // {
  69. // slamLostUI.gameObject.SetActive(true);
  70. // meshRenderer.enabled = false;
  71. // }
  72. // else
  73. // {
  74. // slamLostUI.gameObject.SetActive(false);
  75. // meshRenderer.enabled = true;
  76. // if (alpha >= 10f)
  77. // {
  78. // outOfSafetyArea.SetActive(true);
  79. // }
  80. // else
  81. // {
  82. // outOfSafetyArea.SetActive(false);
  83. // }
  84. // }
  85. // }
  86. //}
  87. //else
  88. //{
  89. // outOfSafetyArea.SetActive(false);
  90. // nomapUI.SetActive(false);
  91. // slamLostUI.gameObject.SetActive(false);
  92. // meshRenderer.enabled = true;
  93. //}
  94. if (existSafetyAreaStateMachine != null)
  95. {
  96. existSafetyAreaStateMachine.Breathe();
  97. }
  98. }
  99. public void SetColor(Color[] colors)
  100. {
  101. this.colors = colors;
  102. }
  103. public Color[] GetColor()
  104. {
  105. return this.colors;
  106. }
  107. public void SetMesh(Mesh mesh, Color[] colors, float perimeter)
  108. {
  109. SetColor(colors);
  110. meshFilter = this.gameObject.AddComponent<MeshFilter>();
  111. meshRenderer = this.gameObject.AddComponent<MeshRenderer>();
  112. //meshRenderer.material = Resources.Load<Material>("Material/SafetyEdgeMat");
  113. meshFilter.mesh = mesh;
  114. GroundHeightStep groundHeightStep = SafetyAreaManager.Instance.GetStep<GroundHeightStep>(SafetyAreaStepEnum.GroundHeight);
  115. //originHeight = groundHeightStep.GetPlaneHeight();
  116. //SetOriginHeight(groundHeightStep.GetPlaneHeight());
  117. this.perimeter = perimeter;
  118. }
  119. public float GetPlaneFillPercentage(Vector3 position)
  120. {
  121. Vector3 localPosition = transform.InverseTransformPoint(position);
  122. List<int> effectIndices = SafetyAreaVertexHelper.CaculateEffectVerticeIndices(localPosition, 1f);
  123. if (effectIndices.Count == 0) return 0f;
  124. int fillCount = 0;
  125. for (int i = 0; i < effectIndices.Count; i++)
  126. {
  127. if (colors[effectIndices[i]] == Color.white)
  128. {
  129. fillCount++;
  130. }
  131. }
  132. return (float)fillCount / effectIndices.Count;
  133. }
  134. public float GetDistance(Vector3 position)
  135. {
  136. if (position.y >= 2.5f) return float.PositiveInfinity;
  137. return Vector3.Distance(transform.position, position);
  138. }
  139. }