Tixing.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// Tixing (梯形线波) v1.0
  5. /// 由 LineWave 改写 —— 实现从下宽到上窄的梯形形状
  6. /// 可与 LineRenderer 一起使用
  7. /// </summary>
  8. [ExecuteInEditMode]
  9. [RequireComponent(typeof(LineRenderer))]
  10. public class Tixing : MonoBehaviour
  11. {
  12. [Header("基础参数")]
  13. public Material traceMaterial;
  14. public float traceWidth = 0.3f;
  15. public int size = 200; // 点数量
  16. public float height = 5f; // 梯形高度
  17. public float bottomWidth = 8f; // 底边宽
  18. public float topWidth = 3f; // 顶边宽
  19. [Header("波动参数")]
  20. public float freq = 2.5f; // 波频
  21. public float amp = 1f; // 波幅
  22. public float speed = 1f; // 波动速度
  23. public bool horizontalWave = false; // true=横向波, false=竖向波
  24. private LineRenderer lr;
  25. private float timeOffset;
  26. void Awake()
  27. {
  28. lr = GetComponent<LineRenderer>();
  29. lr.useWorldSpace = false;
  30. if (traceMaterial != null)
  31. lr.material = traceMaterial;
  32. }
  33. void Update()
  34. {
  35. if (size < 2) size = 2;
  36. lr.positionCount = size;
  37. lr.startWidth = traceWidth;
  38. lr.endWidth = traceWidth;
  39. timeOffset += Time.deltaTime * speed;
  40. for (int i = 0; i < size; i++)
  41. {
  42. float t = (float)i / (size - 1); // 从0到1的插值比例
  43. // 当前层的宽度(从底到顶逐渐缩小)
  44. float currentWidth = Mathf.Lerp(bottomWidth, topWidth, t);
  45. float halfWidth = currentWidth * 0.5f;
  46. // 当前点的高度
  47. float y = Mathf.Lerp(0, height, t);
  48. // 波动效果(可以在X或Y上波动)
  49. float wave = Mathf.Sin((t * freq * Mathf.PI * 2f) + timeOffset) * amp;
  50. float x = 0f;
  51. if (horizontalWave)
  52. x = wave;
  53. else
  54. y += wave;
  55. // 让梯形沿X轴左右展开(绘制中心对称)
  56. float xPos = Mathf.Lerp(-halfWidth, halfWidth, 0.5f); // 中心点
  57. Vector3 pos = new Vector3(x, y, 0f);
  58. lr.SetPosition(i, pos);
  59. }
  60. }
  61. }