using UnityEngine;
using System.Collections;
///
/// Tixing (梯形线波) v1.0
/// 由 LineWave 改写 —— 实现从下宽到上窄的梯形形状
/// 可与 LineRenderer 一起使用
///
[ExecuteInEditMode]
[RequireComponent(typeof(LineRenderer))]
public class Tixing : MonoBehaviour
{
[Header("基础参数")]
public Material traceMaterial;
public float traceWidth = 0.3f;
public int size = 200; // 点数量
public float height = 5f; // 梯形高度
public float bottomWidth = 8f; // 底边宽
public float topWidth = 3f; // 顶边宽
[Header("波动参数")]
public float freq = 2.5f; // 波频
public float amp = 1f; // 波幅
public float speed = 1f; // 波动速度
public bool horizontalWave = false; // true=横向波, false=竖向波
private LineRenderer lr;
private float timeOffset;
void Awake()
{
lr = GetComponent();
lr.useWorldSpace = false;
if (traceMaterial != null)
lr.material = traceMaterial;
}
void Update()
{
if (size < 2) size = 2;
lr.positionCount = size;
lr.startWidth = traceWidth;
lr.endWidth = traceWidth;
timeOffset += Time.deltaTime * speed;
for (int i = 0; i < size; i++)
{
float t = (float)i / (size - 1); // 从0到1的插值比例
// 当前层的宽度(从底到顶逐渐缩小)
float currentWidth = Mathf.Lerp(bottomWidth, topWidth, t);
float halfWidth = currentWidth * 0.5f;
// 当前点的高度
float y = Mathf.Lerp(0, height, t);
// 波动效果(可以在X或Y上波动)
float wave = Mathf.Sin((t * freq * Mathf.PI * 2f) + timeOffset) * amp;
float x = 0f;
if (horizontalWave)
x = wave;
else
y += wave;
// 让梯形沿X轴左右展开(绘制中心对称)
float xPos = Mathf.Lerp(-halfWidth, halfWidth, 0.5f); // 中心点
Vector3 pos = new Vector3(x, y, 0f);
lr.SetPosition(i, pos);
}
}
}