XBoundingBox.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using SC.XR.Unity.Module_InputSystem;
  6. using UnityEngine.Events;
  7. using UnityEngine.XR;
  8. namespace XRTool.Util
  9. {
  10. public class XBoundingBox : BoundingBox
  11. {
  12. private bool isJiXingScale = false;
  13. public bool IsJiXingScale
  14. {
  15. get => isJiXingScale;
  16. set
  17. {
  18. isJiXingScale = value;
  19. SetScaleModel(isJiXingScale);
  20. }
  21. }
  22. /// <summary>
  23. /// 设置边角的颜色(锁定)
  24. /// </summary>
  25. /// <param name="color"></param>
  26. public void SetZoomColor(bool isLock, Color color)
  27. {
  28. List<Handle> hands = new List<Handle>();
  29. hands.AddRange(SideBoundingBoxRoot.handles);
  30. hands.AddRange(CornerBoundingBoxRoot.handles);
  31. for (int i = 0; i < hands.Count; i++)
  32. {
  33. Handle handle = hands[i];
  34. UnityUtil.ChangeMateColor(handle.visual.GetComponent<Renderer>(), color);
  35. handle.root.GetComponent<Collider>().enabled = !isLock;
  36. }
  37. }
  38. /// <summary>
  39. /// 设置缩放模式
  40. /// </summary>
  41. /// <param name="isJiXing">是否是畸形缩放模式,true代表畸形,false代表整体缩放模式</param>
  42. public void SetScaleModel(bool isJiXing = true)
  43. {
  44. //if (!isJiXing)
  45. //{
  46. // ActiveHandle = HandleType.Rotation | HandleType.Scale;
  47. //}
  48. //else
  49. //{
  50. // ActiveHandle = HandleType.Rotation | HandleType.Scale | HandleType.AxisScale;
  51. //}
  52. }
  53. /// <summary>
  54. /// 获取相对于Point最近的点
  55. /// </summary>
  56. /// <param name="point"></param>
  57. /// <returns></returns>
  58. public Vector3 GetCastPoint(Vector3 point)
  59. {
  60. Vector3 center = transform.position;
  61. if (SideBoundingBoxRoot != null && SideBoundingBoxRoot.handles != null)
  62. {
  63. point.y = center.y;
  64. center = SideBoundingBoxRoot.handles[0].visual.transform.position;
  65. float min = center.y;
  66. float minDis = Vector3.Distance(center, point);
  67. for (int i = 1; i < SideBoundingBoxRoot.handles.Length; i++)
  68. {
  69. float dis = Vector3.Distance(point, SideBoundingBoxRoot.handles[i].visual.transform.position);
  70. if (dis < minDis && Mathf.Abs(Vector3.Dot(Vector3.up, SideBoundingBoxRoot.handles[i].visual.transform.up)) < 0.5f)
  71. {
  72. center = SideBoundingBoxRoot.handles[i].visual.transform.position;
  73. minDis = dis;
  74. }
  75. if (SideBoundingBoxRoot.handles[i].visual.transform.position.y < min)
  76. {
  77. min = SideBoundingBoxRoot.handles[i].visual.transform.position.y;
  78. }
  79. }
  80. center.y = min;
  81. }
  82. return center;
  83. }
  84. }
  85. }