Axis.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine;
  2. namespace Rokid.UXR.Components
  3. {
  4. public class Axis : MonoBehaviour
  5. {
  6. [Range(0.2f, 0.5f)]
  7. public float arrowBottomSize = 0.2f;
  8. [Range(0.2f, 0.5f)]
  9. public float arrowHeight = 0.3f;
  10. [Range(0.2f, 5)]
  11. public float xLength = 2;
  12. [Range(0.2f, 5)]
  13. public float yLength = 2;
  14. [Range(0.2f, 5)]
  15. public float zLength = 2;
  16. [Range(0.05f, 0.1f)]
  17. public float lineTickness = 0.05f;//轴线的粗细
  18. [Range(0.1f, 1)]
  19. public float centerSize = 0.1f;
  20. void Start()
  21. {
  22. transform.Find("X").GetComponent<MeshFilter>().mesh = DrawArrow(arrowHeight, arrowBottomSize, xLength, lineTickness, lineTickness);
  23. transform.Find("Y").GetComponent<MeshFilter>().mesh = DrawArrow(arrowHeight, arrowBottomSize, yLength, lineTickness, lineTickness);
  24. transform.Find("Z").GetComponent<MeshFilter>().mesh = DrawArrow(arrowHeight, arrowBottomSize, zLength, lineTickness, lineTickness);
  25. UpdateArrowTsf();
  26. }
  27. /// <summary>
  28. /// inspector 数据发生改变是调用
  29. /// </summary>
  30. void OnValidate()
  31. {
  32. // transform.Find("X").GetComponent<MeshFilter>().mesh = DrawArrow(arrowHeight, arrowBottomSize, xLength, lineTickness, lineTickness);
  33. // transform.Find("Y").GetComponent<MeshFilter>().mesh = DrawArrow(arrowHeight, arrowBottomSize, yLength, lineTickness, lineTickness);
  34. // transform.Find("Z").GetComponent<MeshFilter>().mesh = DrawArrow(arrowHeight, arrowBottomSize, zLength, lineTickness, lineTickness);
  35. // UpdateArrowTsf();
  36. }
  37. /// <summary>
  38. /// 根据输入箭头的长度不同更新箭头的位置
  39. /// </summary>
  40. void UpdateArrowTsf()
  41. {
  42. transform.Find("X").localPosition = new Vector3(xLength, 0, 0);
  43. transform.Find("Y").localPosition = new Vector3(0, yLength, 0);
  44. transform.Find("Z").localPosition = new Vector3(0, 0, zLength);
  45. transform.Find("Center").localScale = Vector3.one * centerSize;
  46. }
  47. /// <summary>
  48. /// 绘制箭头
  49. /// </summary>
  50. /// <param name="arrowHeight"></param>
  51. /// <param name="arrowBottom"></param>
  52. /// <param name="height"></param>
  53. /// <param name="width"></param>
  54. /// <param name="length"></param>
  55. /// <returns></returns>
  56. public Mesh DrawArrow(float arrowHeight, float arrowBottom, float height, float width, float length)
  57. {
  58. var arrow = new Mesh();
  59. arrow.Clear();
  60. Vector3[] vertexs = new Vector3[13];
  61. //箭头
  62. vertexs[0] = new Vector3(-arrowBottom / 2, 0, -arrowBottom / 2);
  63. vertexs[1] = new Vector3(arrowBottom / 2, 0, -arrowBottom / 2);
  64. vertexs[2] = new Vector3(arrowBottom / 2, 0, arrowBottom / 2);
  65. vertexs[3] = new Vector3(-arrowBottom / 2, 0, arrowBottom / 2);
  66. vertexs[4] = new Vector3(0, arrowHeight, 0);
  67. //箭头尾部的线段
  68. vertexs[5] = new Vector3(-width / 2, 0, -length / 2);
  69. vertexs[6] = new Vector3(width / 2, 0, -length / 2);
  70. vertexs[7] = new Vector3(width / 2, 0, length / 2);
  71. vertexs[8] = new Vector3(-width / 2, 0, length / 2);
  72. vertexs[9] = new Vector3(-width / 2, -height, -length / 2);
  73. vertexs[10] = new Vector3(width / 2, -height, -length / 2);
  74. vertexs[11] = new Vector3(width / 2, -height, length / 2);
  75. vertexs[12] = new Vector3(-width / 2, -height, length / 2);
  76. arrow.vertices = vertexs;
  77. //顶点法线遵循左手螺旋定则
  78. int[] vertexIndices = new int[] {
  79. //bottom
  80. 0,1,2,0,2,3,
  81. //up01
  82. 0,4,1,
  83. //up02
  84. 1,4,2,
  85. //up03
  86. 2,4,3,
  87. //up04
  88. 0,3,4,
  89. //箭头尾部...
  90. 5,10,9,5,6,10,
  91. 6,11,10,6,7,11,
  92. 7,12,11,7,8,12,
  93. 5,9,12,5,12,8,
  94. 9,10,12,10,11,12
  95. };
  96. arrow.vertices = vertexs;
  97. //自动计算法线
  98. arrow.RecalculateNormals();
  99. arrow.SetIndices(vertexIndices, MeshTopology.Triangles, 0);
  100. return arrow;
  101. }
  102. }
  103. }