SafetyAreaBase.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using SC.XR.Unity.Module_InputSystem;
  2. using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class SafetyAreaBase : MonoBehaviour
  6. {
  7. //protected float originHeight;
  8. protected MeshFilter meshFilter;
  9. protected List<Vector4> positionList;
  10. protected float perimeter;
  11. protected ExistSafetyAreaStateMachine existSafetyAreaStateMachine;
  12. public MeshRenderer meshRenderer;
  13. public GameObject outOfSafetyArea;
  14. public GameObject nomapUI;
  15. public GameObject slamLostUI;
  16. public float distance;
  17. public float alpha;
  18. protected float duration = 1.0f;
  19. protected List<ColorSection> colorSections = new List<ColorSection>()
  20. {
  21. new ColorSection(){ startColor = new Color(0.129f, 0.235f, 0.886f), endColor = new Color(0.118f, 0.365f, 0.051f) },
  22. new ColorSection(){ startColor = new Color(0.890f, 0.102f, 0.090f), endColor = new Color(0f, 0.482f, 0.012f) },
  23. new ColorSection(){ startColor = new Color(0.263f, 0.145f, 0.176f), endColor = new Color(0.639f, 0.098f, 0.3012f) }
  24. };
  25. protected Color nearColorStart = new Color(0.8902f, 0.102f, 0.09f, 1f);
  26. protected Color nearColorEnd = new Color(0.8902f, 0.5843f, 0.102f, 1f);
  27. public void Init()
  28. {
  29. if (existSafetyAreaStateMachine == null)
  30. {
  31. existSafetyAreaStateMachine = new ExistSafetyAreaStateMachine();
  32. existSafetyAreaStateMachine.InitStateMachine(this);
  33. }
  34. ChangeState(ExistSafetyAreaEnum.Normal);
  35. }
  36. public void Release()
  37. {
  38. if (existSafetyAreaStateMachine != null)
  39. {
  40. existSafetyAreaStateMachine.ExitCurrentState();
  41. }
  42. }
  43. /// <summary>
  44. /// 设置原始高度用于计算高度delta
  45. /// </summary>
  46. /// <param name="height"></param>
  47. //public void SetOriginHeight(float height)
  48. //{
  49. // this.originHeight = height;
  50. //}
  51. /// <summary>
  52. /// 获取原始高度
  53. /// </summary>
  54. /// <returns></returns>
  55. //public float GetOriginHeight()
  56. //{
  57. // return this.originHeight;
  58. //}
  59. /// <summary>
  60. /// 设置周长
  61. /// </summary>
  62. /// <param name="perimeter"></param>
  63. public void SetPerimeter(float perimeter)
  64. {
  65. this.perimeter = perimeter;
  66. }
  67. /// <summary>
  68. /// 获取周长
  69. /// </summary>
  70. /// <returns></returns>
  71. public float GetPerimter()
  72. {
  73. return perimeter;
  74. }
  75. /// <summary>
  76. /// 重新设置高度
  77. /// </summary>
  78. /// <param name="height"></param>
  79. public void ResetPlaneHeight(float height)
  80. {
  81. //float heightdelta = height - originHeight;
  82. //this.transform.position = new Vector3(this.transform.position.x, heightdelta, this.transform.position.z);
  83. }
  84. public void SetMaterial(Material mat)
  85. {
  86. meshRenderer.sharedMaterial = mat;
  87. }
  88. public virtual void Update()
  89. {
  90. if (outOfSafetyArea == null)
  91. {
  92. GameObject outOfSafetyAreaResource = Resources.Load<GameObject>("OutofSafetyArea");
  93. outOfSafetyArea = GameObject.Instantiate(outOfSafetyAreaResource, this.transform);
  94. }
  95. if (nomapUI == null)
  96. {
  97. GameObject nomapUIResource = Resources.Load<GameObject>("NoMapUI");
  98. nomapUI = GameObject.Instantiate(nomapUIResource, this.transform);
  99. }
  100. if (slamLostUI == null)
  101. {
  102. GameObject slamLostResource = Resources.Load<GameObject>("SlamLostUI");
  103. slamLostUI = GameObject.Instantiate(slamLostResource, this.transform);
  104. }
  105. if (meshRenderer == null)
  106. {
  107. meshRenderer = this.GetComponent<MeshRenderer>();
  108. }
  109. if (meshFilter == null)
  110. {
  111. meshFilter = this.GetComponent<MeshFilter>();
  112. }
  113. //if (!SafetyAreaManager.Instance.isSettingSafetyArea)
  114. //{
  115. // meshRenderer.enabled = API_GSXR_Slam.GSXR_Get_OfflineMapRelocState() == 1;
  116. //}
  117. //else
  118. //{
  119. // meshRenderer.enabled = true;
  120. //}
  121. }
  122. public virtual void ChangeState(ExistSafetyAreaEnum existSafetyAreaEnum)
  123. {
  124. if (existSafetyAreaStateMachine != null)
  125. {
  126. existSafetyAreaStateMachine.ChangeState(existSafetyAreaEnum);
  127. }
  128. }
  129. protected List<Vector4> GetInteractionObjectPosition()
  130. {
  131. if (positionList == null)
  132. {
  133. positionList = new List<Vector4>();
  134. }
  135. positionList.Clear();
  136. if (Module_InputSystem.instance == null) return positionList;
  137. Vector3 headTransformPosition = Camera.main.transform.position + Camera.main.transform.forward * 0.5f;
  138. Vector3 viewportPosition = Camera.main.WorldToViewportPoint(headTransformPosition);
  139. positionList.Add(new Vector4(viewportPosition.x, viewportPosition.y, viewportPosition.z, 1f));
  140. if (Module_InputSystem.instance.IsSomeDeviceActiveWithoutHead)
  141. {
  142. if (Module_InputSystem.instance.GetInputDeviceStatus(InputDeviceType.GGT26Dof))
  143. {
  144. InputDeviceBase inputDevice = Module_InputSystem.instance.GetInputDevice<InputDeviceBase>(InputDeviceType.GGT26Dof);
  145. foreach (var part in inputDevice.inputDevicePartList)
  146. {
  147. if (part.inputDataBase.isVaild == true)
  148. {
  149. GameObject handModlePart = (part as InputDeviceHandPart).inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.forefinger, JOINT.One).gameObject;
  150. Vector3 partPosition = handModlePart.transform.position;// + handModlePart.transform.forward * 0.5f;
  151. Vector3 viewportPartPosition = Camera.main.WorldToViewportPoint(partPosition);
  152. positionList.Add(new Vector4(viewportPartPosition.x, viewportPartPosition.y, viewportPartPosition.z, 1f));
  153. }
  154. }
  155. }
  156. else if (Module_InputSystem.instance.GetInputDeviceStatus(InputDeviceType.KS))
  157. {
  158. InputDeviceBase inputDevice = Module_InputSystem.instance.GetInputDevice<InputDeviceBase>(InputDeviceType.KS);
  159. foreach (var part in inputDevice.inputDevicePartList)
  160. {
  161. if (part.inputDataBase.isVaild == true)
  162. {
  163. GameObject KSPart = part.inputDevicePartUIBase.gameObject;
  164. Vector3 partPosition = KSPart.transform.position;// + KSPart.transform.forward * 0.5f;
  165. Vector3 viewportPartPosition = Camera.main.WorldToViewportPoint(partPosition);
  166. positionList.Add(new Vector4(viewportPartPosition.x, viewportPartPosition.y, viewportPartPosition.z, 1f));
  167. }
  168. }
  169. }
  170. else if (Module_InputSystem.instance.GetInputDeviceStatus(InputDeviceType.BT3Dof))
  171. {
  172. InputDeviceBase inputDevice = Module_InputSystem.instance.GetInputDevice<InputDeviceBase>(InputDeviceType.BT3Dof);
  173. foreach (var part in inputDevice.inputDevicePartList)
  174. {
  175. if (part.inputDataBase.isVaild == true)
  176. {
  177. GameObject BT3DofPart = part.inputDevicePartUIBase.gameObject;
  178. Vector3 partPosition = BT3DofPart.transform.position;// + BT3DofPart.transform.forward * 0.5f;
  179. Vector3 viewportPartPosition = Camera.main.WorldToViewportPoint(partPosition);
  180. positionList.Add(new Vector4(viewportPartPosition.x, viewportPartPosition.y, viewportPartPosition.z, 1f));
  181. }
  182. }
  183. }
  184. }
  185. if (positionList.Count < 3)
  186. {
  187. for (int i = positionList.Count; i < 3; i++)
  188. {
  189. positionList.Add(new Vector4(viewportPosition.x, viewportPosition.y, viewportPosition.z, 1f));
  190. }
  191. }
  192. return positionList;
  193. }
  194. }