Ver.0.3.0.33
This commit is contained in:
@@ -12,6 +12,7 @@ namespace AibisDream.FixSystem
|
||||
private Transform _end;
|
||||
|
||||
private LineRenderer _lineRenderer;
|
||||
private LineRenderer _stiffLineRenderer; // 用于渲染刚性部分
|
||||
private Vector3[] _points;
|
||||
|
||||
private CableSystem _cableSystem;
|
||||
@@ -19,6 +20,17 @@ namespace AibisDream.FixSystem
|
||||
[SerializeField]
|
||||
private CableConfig config = CableConfig.GeneDefaultConfig();
|
||||
|
||||
public void ShowCable()
|
||||
{
|
||||
_lineRenderer.enabled = true;
|
||||
_stiffLineRenderer.enabled = true;
|
||||
}
|
||||
public void HideCable()
|
||||
{
|
||||
_lineRenderer.enabled = false;
|
||||
_stiffLineRenderer.enabled = false;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InitComponentRefs();
|
||||
@@ -30,7 +42,22 @@ namespace AibisDream.FixSystem
|
||||
_cableSystem = transform.parent.GetComponent<CableSystem>();
|
||||
|
||||
_lineRenderer = GetComponent<LineRenderer>();
|
||||
_lineRenderer.positionCount = config.resolution;
|
||||
_lineRenderer.positionCount = config.resolution - config.stiffCount;
|
||||
|
||||
// 创建第二个LineRenderer用于刚性部分
|
||||
GameObject stiffObj = new GameObject("StiffLineRenderer");
|
||||
stiffObj.transform.SetParent(transform);
|
||||
_stiffLineRenderer = stiffObj.AddComponent<LineRenderer>();
|
||||
_stiffLineRenderer.positionCount = config.stiffCount;
|
||||
|
||||
// 复制第一个LineRenderer的属性到第二个
|
||||
_stiffLineRenderer.startWidth = _lineRenderer.startWidth;
|
||||
_stiffLineRenderer.endWidth = _lineRenderer.startWidth;
|
||||
_stiffLineRenderer.material = _lineRenderer.material;
|
||||
_stiffLineRenderer.startColor = _lineRenderer.startColor;
|
||||
_stiffLineRenderer.endColor = _lineRenderer.startColor;
|
||||
_stiffLineRenderer.sortingLayerName = _cableSystem.CableReelRef.GetComponent<SpriteRenderer>().sortingLayerName;
|
||||
_stiffLineRenderer.sortingOrder = _cableSystem.CableReelRef.GetComponent<SpriteRenderer>().sortingOrder - 1;
|
||||
|
||||
// 连线起止点
|
||||
_start = _cableSystem.CableRootPos;
|
||||
@@ -55,31 +82,54 @@ namespace AibisDream.FixSystem
|
||||
void DrawLine()
|
||||
{
|
||||
var points = UpdateRope();
|
||||
for (int i = 0; i < config.resolution; i++)
|
||||
|
||||
// 渲染刚性部分
|
||||
for (int i = 0; i < config.stiffCount; i++)
|
||||
{
|
||||
_lineRenderer.SetPosition(i, points[i] + Vector3.forward * -.3f);
|
||||
_stiffLineRenderer.SetPosition(i, points[i] + Vector3.forward * -.3f);
|
||||
}
|
||||
|
||||
// 渲染非刚性部分
|
||||
for (int i = 0; i < config.resolution - config.stiffCount; i++)
|
||||
{
|
||||
_lineRenderer.SetPosition(i, points[i + config.stiffCount] + Vector3.forward * -.3f);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3[] UpdateRope()
|
||||
{
|
||||
|
||||
float t = Mathf.InverseLerp(config.dstMin, config.dstMax, (_start.position - _end.position).magnitude);
|
||||
float F = Mathf.Lerp(config.forceMin, config.forceMax, t);
|
||||
_points[0] = _start.position;
|
||||
_points[^1] = _end.position;
|
||||
for (int ik = 0; ik < config.k; ik++)
|
||||
|
||||
// 计算出线方向(从转盘出线口到起始点)
|
||||
Vector3 dir = (_start.position - _cableSystem.CableReelRef.transform.position).normalized;
|
||||
|
||||
// 设置刚性部分
|
||||
for (int i = 0; i < config.stiffCount; i++)
|
||||
{
|
||||
for (int i = 1; i < _points.Length - 1; i++)
|
||||
float t_stiff = (float)i / (config.stiffCount - 1);
|
||||
_points[i] = _start.position + dir * (config.stiffLength * t_stiff);
|
||||
}
|
||||
|
||||
// 设置末端点
|
||||
_points[^1] = _end.position;
|
||||
|
||||
// 只对非刚性部分进行物理模拟
|
||||
for (int ik = 0; ik < config.k; ik++)
|
||||
{
|
||||
// 确保非刚性部分的第一个点与刚性部分的最后一个点重合
|
||||
_points[config.stiffCount] = _points[config.stiffCount - 1];
|
||||
|
||||
for (int i = config.stiffCount + 1; i < _points.Length - 1; i++)
|
||||
{
|
||||
Vector3 offsetPrev = _points[i - 1] - _points[i];
|
||||
Vector3 offsetNext = _points[i + 1] - _points[i];
|
||||
Vector3 velocity = offsetPrev.normalized * (offsetPrev.magnitude * F) +
|
||||
offsetNext.normalized * (offsetNext.magnitude * F);
|
||||
_points[i] += velocity * Time.deltaTime / config.k;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < _points.Length - 1; i++)
|
||||
for (int i = config.stiffCount + 1; i < _points.Length - 1; i++)
|
||||
{
|
||||
_points[i] += Vector3.down * (9.8f * Time.deltaTime) / config.k;
|
||||
}
|
||||
@@ -98,7 +148,7 @@ namespace AibisDream.FixSystem
|
||||
}
|
||||
public Vector3 GetDirection()
|
||||
{
|
||||
return _points[^1]-_points[_points.Length-1];
|
||||
return _points[^1] - _points[^2];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +161,8 @@ namespace AibisDream.FixSystem
|
||||
public float forceMin;
|
||||
public float forceMax;
|
||||
public int k;
|
||||
public int stiffCount; // 刚性部分点数
|
||||
public float stiffLength; // 刚性部分长度
|
||||
|
||||
public static CableConfig GeneDefaultConfig()
|
||||
{
|
||||
@@ -121,7 +173,9 @@ namespace AibisDream.FixSystem
|
||||
dstMax = 1.0f,
|
||||
forceMin = 0.1f,
|
||||
forceMax = 150f,
|
||||
k = 10
|
||||
k = 10,
|
||||
stiffCount = 3,
|
||||
stiffLength = 0.1f
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user