InputRayDrawer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace EZXR.Glass.Inputs
  5. {
  6. public class InputRayDrawer : MonoBehaviour
  7. {
  8. InputInfoBase handInfo;
  9. /// <summary>
  10. /// 手指关节点0
  11. /// </summary>
  12. public Transform joint0;
  13. /// <summary>
  14. /// 手指关节点1
  15. /// </summary>
  16. public Transform joint1;
  17. /// <summary>
  18. /// 线
  19. /// </summary>
  20. public LineRenderer lineRenderer;
  21. /// <summary>
  22. /// 实线和虚线的material
  23. /// </summary>
  24. public Material[] materials;
  25. ///// <summary>
  26. ///// 线总长度
  27. ///// </summary>
  28. //public float length = 5f;
  29. /// <summary>
  30. /// 单线段长度
  31. /// </summary>
  32. public float eachLength = 0.01f;
  33. /// <summary>
  34. /// 是否是实线
  35. /// </summary>
  36. bool solidLine;
  37. /// <summary>
  38. /// 贝塞尔曲线的p2点
  39. /// </summary>
  40. Vector3 p2;
  41. Vector3 p0;
  42. Vector3 p1;
  43. // Start is called before the first frame update
  44. void Start()
  45. {
  46. }
  47. /// <summary>
  48. /// 重置lineRenderer为直线
  49. /// </summary>
  50. void ResetPositions()
  51. {
  52. for (int i = 0; i < lineRenderer.positionCount; i++)
  53. {
  54. lineRenderer.SetPosition(i, new Vector3(0, 0, eachLength * (lineRenderer.positionCount - 1 - i)));
  55. }
  56. }
  57. // Update is called once per frame
  58. void Update()
  59. {
  60. if (joint0 != null)
  61. {
  62. //Debug.Log(handInfo.curRayContactingPoint.ToString());
  63. if (!handInfo.IsRayGrabbing)
  64. {
  65. p2 = joint1.position;
  66. }
  67. p0 = joint0.position;
  68. p1 = joint1.position;
  69. transform.position = joint0.position;
  70. lineRenderer.positionCount = Mathf.Clamp(Mathf.CeilToInt(Vector3.Magnitude(joint1.position - joint0.position) / eachLength), 2, 10000);
  71. for (int i = 0; i < lineRenderer.positionCount; i++)
  72. {
  73. lineRenderer.SetPosition(i, CalculateBezier(1 - ((float)i / (lineRenderer.positionCount - 1))));
  74. }
  75. }
  76. }
  77. Vector3 CalculateBezier(float t)
  78. {
  79. Vector3 p0p1 = (1 - t) * p0 + t * p1;
  80. Vector3 p1p2 = (1 - t) * p1 + t * p2;
  81. Vector3 result = (1 - t) * p0p1 + t * p1p2;
  82. return result;
  83. }
  84. public void SetUp(InputInfoBase handInfo, Transform joint0, Transform joint1)
  85. {
  86. this.handInfo = handInfo;
  87. this.joint0 = joint0;
  88. this.joint1 = joint1;
  89. Gradient gradient = new Gradient();
  90. gradient.mode = GradientMode.Blend;
  91. gradient.SetKeys(
  92. new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.white, 1.0f) },
  93. new GradientAlphaKey[] { new GradientAlphaKey(0, 0.0f), new GradientAlphaKey(0.2f, 0.75f), new GradientAlphaKey(1, 1.0f) }
  94. );
  95. lineRenderer.colorGradient = gradient;
  96. //lineRenderer.positionCount = Mathf.CeilToInt(length / eachLength);
  97. Vector3[] positions = new Vector3[lineRenderer.positionCount];
  98. for (int i = 0; i < positions.Length; i++)
  99. {
  100. positions[i] = new Vector3(0, 0, eachLength * (positions.Length - 1 - i));
  101. }
  102. lineRenderer.SetPositions(positions);
  103. }
  104. /// <summary>
  105. /// 设置线的状态是实线还是虚线
  106. /// </summary>
  107. /// <param name="solidLine"></param>
  108. public void SetLineState(bool solidLine)
  109. {
  110. this.solidLine = solidLine;
  111. if (solidLine)
  112. {
  113. lineRenderer.material = materials[0];
  114. lineRenderer.textureMode = LineTextureMode.Stretch;
  115. }
  116. else
  117. {
  118. //ResetPositions();
  119. lineRenderer.material = materials[1];
  120. lineRenderer.textureMode = LineTextureMode.RepeatPerSegment;
  121. }
  122. }
  123. /// <summary>
  124. /// 设置贝塞尔曲线用的p2点(最终显示的曲线的终点,是滤波之后的点),p0是射线的起点,p1是贝塞尔曲线用到的折弯点(射线的终点)
  125. /// </summary>
  126. /// <param name="p2"></param>
  127. public void SetLineBezierPoint(Vector3 p2)
  128. {
  129. this.p2 = p2;
  130. }
  131. }
  132. }