SafetyPlaneMono.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. public class SafetyPlaneMono : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  8. {
  9. private bool isFreeze = false;
  10. private bool canFill = false;
  11. private int lastPaintIndex = -1;
  12. private PointerEventData currentPointerEventData;
  13. private Mesh mesh;
  14. private Color[] colors;
  15. private GroundHeightStep groundHeightStep;
  16. private Action<PointerEventData> OnPointerClickDown
  17. {
  18. get;
  19. set;
  20. }
  21. private Action<PointerEventData> OnPointerClickUp
  22. {
  23. get;
  24. set;
  25. }
  26. public void Init()
  27. {
  28. if (groundHeightStep == null)
  29. {
  30. groundHeightStep = SafetyAreaManager.Instance.GetStep<GroundHeightStep>(SafetyAreaStepEnum.GroundHeight);
  31. }
  32. MeshFilter meshFilter = this.gameObject.AddComponent<MeshFilter>();
  33. meshFilter.mesh = SafetyAreaVertexHelper.GeneratePlaneMesh();
  34. mesh = meshFilter.mesh;
  35. MeshRenderer meshRenderer = this.gameObject.AddComponent<MeshRenderer>();
  36. meshRenderer.material = Resources.Load<Material>("Material/SafetyPlaneMat");
  37. MeshCollider meshCollider = this.gameObject.AddComponent<MeshCollider>();
  38. meshCollider.sharedMesh = mesh;
  39. ClearMeshColor();
  40. }
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. if (!isFreeze)
  45. {
  46. GameObject interactiveObject = GetInteractiveGameObject();
  47. if (interactiveObject != null)
  48. {
  49. groundHeightStep.SetPlaneHeight(interactiveObject.transform.position.y);
  50. }
  51. groundHeightStep.SetHeadPosition(GetHeadTransform().position);
  52. this.gameObject.transform.position = Vector3.up * groundHeightStep.GetPlaneHeight();
  53. }
  54. else
  55. {
  56. if (canFill)
  57. {
  58. FillNearest(currentPointerEventData);
  59. }
  60. }
  61. }
  62. public void FreezePlaneHeight()
  63. {
  64. isFreeze = true;
  65. }
  66. public void UnFreezePlaneHeight()
  67. {
  68. isFreeze = false;
  69. }
  70. private void EnableFill(PointerEventData eventData)
  71. {
  72. canFill = true;
  73. }
  74. private void DisableFill(PointerEventData eventData)
  75. {
  76. canFill = false;
  77. }
  78. public void ResetPlaneHeight()
  79. {
  80. groundHeightStep.ResetPlaneHeight();
  81. }
  82. private GameObject GetInteractiveGameObject()
  83. {
  84. return null;
  85. //return API_Module_InputSystem_BT3Dof.GetPointer().gameObject;
  86. }
  87. private Transform GetHeadTransform()
  88. {
  89. return API_GSXR_Slam.SlamManager.head.transform;
  90. }
  91. public void OnPointerDown(PointerEventData eventData)
  92. {
  93. currentPointerEventData = eventData;
  94. OnPointerClickDown?.Invoke(eventData);
  95. }
  96. public void OnPointerUp(PointerEventData eventData)
  97. {
  98. OnPointerClickUp?.Invoke(eventData);
  99. }
  100. public void RegistPointerDownFillEvent()
  101. {
  102. OnPointerClickDown += EnableFill;
  103. }
  104. public void UnRegistPointerDownFillEvent()
  105. {
  106. OnPointerClickDown -= EnableFill;
  107. }
  108. public void RegistPointerUpFillEvent()
  109. {
  110. OnPointerClickUp += DisableFill;
  111. }
  112. public void UnRegistPointerUpFillEvent()
  113. {
  114. OnPointerClickUp -= DisableFill;
  115. }
  116. public void RegistPointerUpEvent(Action<PointerEventData> callback)
  117. {
  118. OnPointerClickUp += callback;
  119. }
  120. public void UnRegistPointerUpEvent(Action<PointerEventData> callback)
  121. {
  122. OnPointerClickUp -= callback;
  123. }
  124. public void FillNearest(PointerEventData pointerEventData)
  125. {
  126. if(currentPointerEventData == null)
  127. {
  128. Debug.LogError("currentPointerEventData == null");
  129. return;
  130. }
  131. Vector3 raycastPosition = pointerEventData.pointerCurrentRaycast.worldPosition;
  132. Vector3 pointerLocalPosition = transform.InverseTransformPoint(raycastPosition);
  133. List<int> effectIndices = SafetyAreaVertexHelper.CaculateEffectVerticeIndices(pointerLocalPosition);
  134. for (int i = 0; i < effectIndices.Count; i++)
  135. {
  136. int index = effectIndices[i];
  137. colors[index] = Color.red;
  138. lastPaintIndex = index;
  139. }
  140. mesh.colors = colors;
  141. }
  142. public void GenerateEdgeMesh(Action<Mesh> onGenerateMesh)
  143. {
  144. if (lastPaintIndex == -1)
  145. {
  146. Debug.LogError("lastPaintIndex == -1");
  147. return;
  148. }
  149. SafetyAreaEightNeighbourHelper.EightNeighbours(lastPaintIndex, (index) =>
  150. {
  151. return colors[index] == Color.red;
  152. }, (edgeIndices) =>
  153. {
  154. float planeHeight = groundHeightStep.GetPlaneHeight();
  155. Mesh edgeMesh = SafetyAreaVertexHelper.GenerateEdgeMesh(mesh, edgeIndices, planeHeight + 5f, planeHeight);
  156. onGenerateMesh?.Invoke(edgeMesh);
  157. });
  158. }
  159. public void ClearMeshColor()
  160. {
  161. colors = Enumerable.Repeat(Color.white, mesh.vertexCount).ToArray();
  162. mesh.colors = colors;
  163. }
  164. }