提交测试代码
This commit is contained in:
@@ -22,10 +22,16 @@ namespace F2RopeLine2.FishingLine
|
||||
[Min(0.001f)]
|
||||
[SerializeField] private float maxDeltaTime = 0.0333333f;
|
||||
|
||||
[Header("Pin Follow")]
|
||||
[Header("Water Surface")]
|
||||
[SerializeField] private bool constrainToWaterSurface = true;
|
||||
[SerializeField] private Transform waterSurfaceTransform;
|
||||
[SerializeField] private float waterSurfaceHeight;
|
||||
[Min(0)]
|
||||
[SerializeField] private int ignoreHeadNodeCount = 1;
|
||||
[Min(0)]
|
||||
[SerializeField] private int ignoreTailNodeCount = 1;
|
||||
[Min(0f)]
|
||||
[SerializeField] private float pinFollowStrength = 50f;
|
||||
[SerializeField] private bool writeBackVirtualNodeTransforms = true;
|
||||
[SerializeField] private float waterSurfaceFollowSpeed = 12f;
|
||||
|
||||
[Header("Stability")]
|
||||
[Min(1)]
|
||||
@@ -40,15 +46,15 @@ namespace F2RopeLine2.FishingLine
|
||||
[SerializeField] private float wakeDistanceThreshold = 0.001f;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool drawDebugSamples = false;
|
||||
[SerializeField] private bool drawDebugSamples;
|
||||
[SerializeField] private Color debugSampleColor = new(1f, 0.2f, 0.2f, 1f);
|
||||
[Min(0.001f)]
|
||||
[SerializeField] private float debugSampleRadius = 0.015f;
|
||||
|
||||
private readonly List<Vector3> positions = new();
|
||||
private readonly List<Vector3> previousPositions = new();
|
||||
private readonly List<FishingLineNode> sampledNodes = new();
|
||||
private readonly List<Vector3> lastPinnedNodePositions = new();
|
||||
private readonly List<long> sampledPointKeys = new();
|
||||
private readonly List<Vector3> lastPinnedPointPositions = new();
|
||||
private readonly List<float> lastRestLengths = new();
|
||||
private bool[] pinnedFlags = System.Array.Empty<bool>();
|
||||
private float accumulatedTime;
|
||||
@@ -96,70 +102,72 @@ namespace F2RopeLine2.FishingLine
|
||||
}
|
||||
|
||||
solver = sourceSolver;
|
||||
var nodes = solver.OrderedNodes;
|
||||
var points = solver.ChainPoints;
|
||||
var restLengths = solver.RestLengths;
|
||||
var pinnedIndices = solver.PinnedIndices;
|
||||
if (nodes.Count == 0)
|
||||
if (points.Count == 0)
|
||||
{
|
||||
lineRenderer.positionCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
var topologyChanged = EnsureBuffers(nodes, pinnedIndices);
|
||||
if (topologyChanged)
|
||||
var topologyChanged = EnsureBuffers(points, pinnedIndices);
|
||||
if (topologyChanged || ShouldWake(points, restLengths))
|
||||
{
|
||||
WakeUp();
|
||||
}
|
||||
|
||||
if (ShouldWake(nodes, restLengths))
|
||||
{
|
||||
WakeUp();
|
||||
}
|
||||
|
||||
Simulate(nodes, restLengths, deltaTime);
|
||||
ApplyToRenderer(nodes);
|
||||
CacheFrameState(nodes, restLengths);
|
||||
Simulate(points, restLengths, deltaTime);
|
||||
ApplyToRenderer();
|
||||
CacheFrameState(points, restLengths);
|
||||
}
|
||||
|
||||
private bool EnsureBuffers(
|
||||
IReadOnlyList<FishingLineNode> nodes,
|
||||
IReadOnlyList<FishingLineSolver.ChainPoint> points,
|
||||
IReadOnlyList<int> pinnedIndices)
|
||||
{
|
||||
var topologyChanged = positions.Count != nodes.Count;
|
||||
var previousPositionMap = new Dictionary<FishingLineNode, Vector3>(sampledNodes.Count);
|
||||
var previousHistoryMap = new Dictionary<FishingLineNode, Vector3>(sampledNodes.Count);
|
||||
for (var i = 0; i < sampledNodes.Count; i++)
|
||||
var topologyChanged = sampledPointKeys.Count != points.Count;
|
||||
if (!topologyChanged)
|
||||
{
|
||||
var sampledNode = sampledNodes[i];
|
||||
if (sampledNode == null)
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (sampledPointKeys[i] == points[i].Key)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
previousPositionMap[sampledNode] = positions[i];
|
||||
previousHistoryMap[sampledNode] = previousPositions[i];
|
||||
topologyChanged = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var previousPositionMap = new Dictionary<long, Vector3>(sampledPointKeys.Count);
|
||||
var previousHistoryMap = new Dictionary<long, Vector3>(sampledPointKeys.Count);
|
||||
for (var i = 0; i < sampledPointKeys.Count; i++)
|
||||
{
|
||||
previousPositionMap[sampledPointKeys[i]] = positions[i];
|
||||
previousHistoryMap[sampledPointKeys[i]] = previousPositions[i];
|
||||
}
|
||||
|
||||
positions.Clear();
|
||||
previousPositions.Clear();
|
||||
sampledNodes.Clear();
|
||||
pinnedFlags = new bool[nodes.Count];
|
||||
sampledPointKeys.Clear();
|
||||
pinnedFlags = new bool[points.Count];
|
||||
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
var node = nodes[i];
|
||||
sampledNodes.Add(node);
|
||||
var point = points[i];
|
||||
sampledPointKeys.Add(point.Key);
|
||||
|
||||
if (node != null && previousPositionMap.TryGetValue(node, out var preservedPosition))
|
||||
if (previousPositionMap.TryGetValue(point.Key, out var preservedPosition))
|
||||
{
|
||||
positions.Add(preservedPosition);
|
||||
previousPositions.Add(previousHistoryMap[node]);
|
||||
previousPositions.Add(previousHistoryMap[point.Key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
var position = node != null ? node.Position : Vector3.zero;
|
||||
positions.Add(position);
|
||||
previousPositions.Add(position);
|
||||
positions.Add(point.Position);
|
||||
previousPositions.Add(point.Position);
|
||||
}
|
||||
|
||||
for (var i = 0; i < pinnedIndices.Count; i++)
|
||||
@@ -174,11 +182,14 @@ namespace F2RopeLine2.FishingLine
|
||||
return topologyChanged;
|
||||
}
|
||||
|
||||
private void Simulate(IReadOnlyList<FishingLineNode> nodes, IReadOnlyList<float> restLengths, float deltaTime)
|
||||
private void Simulate(
|
||||
IReadOnlyList<FishingLineSolver.ChainPoint> points,
|
||||
IReadOnlyList<float> restLengths,
|
||||
float deltaTime)
|
||||
{
|
||||
if (isSleeping)
|
||||
{
|
||||
PinLogicalNodes(nodes, simulationStep);
|
||||
PinLogicalPoints(points);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -188,31 +199,33 @@ namespace F2RopeLine2.FishingLine
|
||||
var subStepCount = 0;
|
||||
while (accumulatedTime >= simulationStep && subStepCount < maxSubStepsPerFrame)
|
||||
{
|
||||
SimulateStep(nodes, restLengths, simulationStep);
|
||||
SimulateStep(points, restLengths, simulationStep);
|
||||
accumulatedTime -= simulationStep;
|
||||
subStepCount++;
|
||||
}
|
||||
|
||||
if (subStepCount == 0)
|
||||
{
|
||||
PinLogicalNodes(nodes, simulationStep);
|
||||
ApplySleep(nodes);
|
||||
PinLogicalPoints(points);
|
||||
ApplySleep();
|
||||
}
|
||||
|
||||
EvaluateSleepState(nodes, restLengths);
|
||||
EvaluateSleepState(restLengths);
|
||||
}
|
||||
|
||||
private void SimulateStep(IReadOnlyList<FishingLineNode> nodes, IReadOnlyList<float> restLengths, float stepDelta)
|
||||
private void SimulateStep(
|
||||
IReadOnlyList<FishingLineSolver.ChainPoint> points,
|
||||
IReadOnlyList<float> restLengths,
|
||||
float stepDelta)
|
||||
{
|
||||
var gravity = Physics.gravity * gravityScale * stepDelta * stepDelta;
|
||||
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
if (pinnedFlags[i])
|
||||
{
|
||||
var pinnedPosition = nodes[i].Position;
|
||||
positions[i] = pinnedPosition;
|
||||
previousPositions[i] = pinnedPosition;
|
||||
positions[i] = points[i].Position;
|
||||
previousPositions[i] = points[i].Position;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -224,7 +237,7 @@ namespace F2RopeLine2.FishingLine
|
||||
|
||||
for (var iteration = 0; iteration < solverIterations; iteration++)
|
||||
{
|
||||
PinLogicalNodes(nodes, stepDelta);
|
||||
PinLogicalPoints(points);
|
||||
|
||||
for (var segmentIndex = 0; segmentIndex < restLengths.Count; segmentIndex++)
|
||||
{
|
||||
@@ -232,24 +245,22 @@ namespace F2RopeLine2.FishingLine
|
||||
}
|
||||
}
|
||||
|
||||
PinLogicalNodes(nodes, stepDelta);
|
||||
ApplySleep(nodes);
|
||||
ApplyWaterSurfaceConstraint(stepDelta);
|
||||
PinLogicalPoints(points);
|
||||
ApplySleep();
|
||||
}
|
||||
|
||||
private void PinLogicalNodes(IReadOnlyList<FishingLineNode> nodes, float deltaTime)
|
||||
private void PinLogicalPoints(IReadOnlyList<FishingLineSolver.ChainPoint> points)
|
||||
{
|
||||
var followWeight = Mathf.Clamp01(pinFollowStrength * deltaTime);
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
if (!pinnedFlags[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var targetPosition = nodes[i].Position;
|
||||
positions[i] = Vector3.Lerp(positions[i], targetPosition, followWeight);
|
||||
previousPositions[i] = targetPosition;
|
||||
positions[i] = targetPosition;
|
||||
positions[i] = points[i].Position;
|
||||
previousPositions[i] = points[i].Position;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,9 +306,19 @@ namespace F2RopeLine2.FishingLine
|
||||
positions[segmentIndex + 1] -= correction;
|
||||
}
|
||||
|
||||
private void ApplySleep(IReadOnlyList<FishingLineNode> nodes)
|
||||
private void ApplyWaterSurfaceConstraint(float stepDelta)
|
||||
{
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
if (!constrainToWaterSurface || positions.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var surfaceHeight = waterSurfaceTransform != null ? waterSurfaceTransform.position.y : waterSurfaceHeight;
|
||||
var startIndex = Mathf.Clamp(ignoreHeadNodeCount, 0, positions.Count);
|
||||
var endExclusive = Mathf.Clamp(positions.Count - ignoreTailNodeCount, startIndex, positions.Count);
|
||||
var followFactor = Mathf.Clamp01(waterSurfaceFollowSpeed * stepDelta);
|
||||
|
||||
for (var i = startIndex; i < endExclusive; i++)
|
||||
{
|
||||
if (pinnedFlags[i])
|
||||
{
|
||||
@@ -305,21 +326,25 @@ namespace F2RopeLine2.FishingLine
|
||||
}
|
||||
|
||||
var current = positions[i];
|
||||
var previous = previousPositions[i];
|
||||
var velocityMagnitude = (current - previous).magnitude;
|
||||
var authoredDistance = Vector3.Distance(current, nodes[i].Position);
|
||||
|
||||
if (velocityMagnitude <= sleepVelocityThreshold && authoredDistance <= sleepDistanceThreshold)
|
||||
if (current.y >= surfaceHeight)
|
||||
{
|
||||
previousPositions[i] = current;
|
||||
continue;
|
||||
}
|
||||
|
||||
var nextY = Mathf.Lerp(current.y, surfaceHeight, followFactor);
|
||||
positions[i] = new Vector3(current.x, nextY, current.z);
|
||||
|
||||
var previous = previousPositions[i];
|
||||
previousPositions[i] = new Vector3(
|
||||
previous.x,
|
||||
Mathf.Lerp(previous.y, nextY, followFactor),
|
||||
previous.z);
|
||||
}
|
||||
}
|
||||
|
||||
private void EvaluateSleepState(IReadOnlyList<FishingLineNode> nodes, IReadOnlyList<float> restLengths)
|
||||
private void ApplySleep()
|
||||
{
|
||||
var isStable = true;
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
for (var i = 0; i < positions.Count; i++)
|
||||
{
|
||||
if (pinnedFlags[i])
|
||||
{
|
||||
@@ -327,7 +352,24 @@ namespace F2RopeLine2.FishingLine
|
||||
}
|
||||
|
||||
var velocityMagnitude = (positions[i] - previousPositions[i]).magnitude;
|
||||
if (velocityMagnitude > sleepVelocityThreshold)
|
||||
if (velocityMagnitude <= sleepVelocityThreshold)
|
||||
{
|
||||
previousPositions[i] = positions[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EvaluateSleepState(IReadOnlyList<float> restLengths)
|
||||
{
|
||||
var isStable = true;
|
||||
for (var i = 0; i < positions.Count; i++)
|
||||
{
|
||||
if (pinnedFlags[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((positions[i] - previousPositions[i]).magnitude > sleepVelocityThreshold)
|
||||
{
|
||||
isStable = false;
|
||||
break;
|
||||
@@ -367,26 +409,28 @@ namespace F2RopeLine2.FishingLine
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldWake(IReadOnlyList<FishingLineNode> nodes, IReadOnlyList<float> restLengths)
|
||||
private bool ShouldWake(
|
||||
IReadOnlyList<FishingLineSolver.ChainPoint> points,
|
||||
IReadOnlyList<float> restLengths)
|
||||
{
|
||||
if (!isSleeping)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (lastPinnedNodePositions.Count != nodes.Count || lastRestLengths.Count != restLengths.Count)
|
||||
if (lastPinnedPointPositions.Count != points.Count || lastRestLengths.Count != restLengths.Count)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
if (!pinnedFlags[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Vector3.Distance(nodes[i].Position, lastPinnedNodePositions[i]) > wakeDistanceThreshold)
|
||||
if (Vector3.Distance(points[i].Position, lastPinnedPointPositions[i]) > wakeDistanceThreshold)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -403,12 +447,14 @@ namespace F2RopeLine2.FishingLine
|
||||
return false;
|
||||
}
|
||||
|
||||
private void CacheFrameState(IReadOnlyList<FishingLineNode> nodes, IReadOnlyList<float> restLengths)
|
||||
private void CacheFrameState(
|
||||
IReadOnlyList<FishingLineSolver.ChainPoint> points,
|
||||
IReadOnlyList<float> restLengths)
|
||||
{
|
||||
lastPinnedNodePositions.Clear();
|
||||
for (var i = 0; i < nodes.Count; i++)
|
||||
lastPinnedPointPositions.Clear();
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
lastPinnedNodePositions.Add(nodes[i] != null ? nodes[i].Position : Vector3.zero);
|
||||
lastPinnedPointPositions.Add(points[i].Position);
|
||||
}
|
||||
|
||||
lastRestLengths.Clear();
|
||||
@@ -425,22 +471,12 @@ namespace F2RopeLine2.FishingLine
|
||||
accumulatedTime = 0f;
|
||||
}
|
||||
|
||||
private void ApplyToRenderer(IReadOnlyList<FishingLineNode> nodes)
|
||||
private void ApplyToRenderer()
|
||||
{
|
||||
lineRenderer.positionCount = positions.Count;
|
||||
for (var i = 0; i < positions.Count; i++)
|
||||
{
|
||||
lineRenderer.SetPosition(i, positions[i]);
|
||||
|
||||
if (!writeBackVirtualNodeTransforms)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (nodes[i].IsRuntimeVirtualNode)
|
||||
{
|
||||
nodes[i].SetVisualPosition(positions[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,30 @@ namespace F2RopeLine2.FishingLine
|
||||
{
|
||||
public class FishingLineSolver : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class ChainPoint
|
||||
{
|
||||
public long Key;
|
||||
public Vector3 Position;
|
||||
public bool IsLogical;
|
||||
public FishingLineNode LogicalNode;
|
||||
public int SegmentIndex;
|
||||
public int StableIndex;
|
||||
|
||||
public string GetDebugName()
|
||||
{
|
||||
if (IsLogical)
|
||||
{
|
||||
return LogicalNode != null ? LogicalNode.GetDebugName() : $"L[{StableIndex}]";
|
||||
}
|
||||
|
||||
return $"V[S{SegmentIndex}:{StableIndex}]";
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
private struct SegmentLayout
|
||||
{
|
||||
public float TotalLength;
|
||||
public float[] GapLengths;
|
||||
|
||||
public int VirtualNodeCount => Mathf.Max(0, GapLengths.Length - 1);
|
||||
@@ -43,23 +63,19 @@ namespace F2RopeLine2.FishingLine
|
||||
[SerializeField] private bool showNodeLabels = true;
|
||||
[SerializeField] private bool showSegmentLabels = true;
|
||||
|
||||
private readonly List<FishingLineNode> orderedNodes = new();
|
||||
private readonly List<ChainPoint> chainPoints = new();
|
||||
private readonly List<float> restLengths = new();
|
||||
private readonly List<int> pinnedIndices = new();
|
||||
private readonly List<FishingLineNode> firstSegmentVirtualPool = new();
|
||||
private readonly List<FishingLineNode> otherSegmentVirtualPool = new();
|
||||
private readonly List<ConfigurableJoint> runtimeJoints = new();
|
||||
|
||||
private Transform virtualNodeRoot;
|
||||
private bool chainDirty = true;
|
||||
|
||||
public Transform AnchorTransform => anchorTransform;
|
||||
private int runtimeVirtualPointCount;
|
||||
|
||||
public float FirstSegmentLength => firstSegmentLength;
|
||||
|
||||
public float FirstSegmentStep => firstSegmentStep;
|
||||
|
||||
public IReadOnlyList<FishingLineNode> OrderedNodes => orderedNodes;
|
||||
public IReadOnlyList<ChainPoint> ChainPoints => chainPoints;
|
||||
|
||||
public IReadOnlyList<float> RestLengths => restLengths;
|
||||
|
||||
@@ -69,34 +85,11 @@ namespace F2RopeLine2.FishingLine
|
||||
|
||||
public int LogicalNodeCount => logicalNodes?.Length ?? 0;
|
||||
|
||||
public int RuntimeVirtualNodeCount => firstSegmentVirtualPool.Count + otherSegmentVirtualPool.Count;
|
||||
public int RuntimeVirtualNodeCount => runtimeVirtualPointCount;
|
||||
|
||||
public int ActiveRuntimeVirtualNodeCount
|
||||
{
|
||||
get
|
||||
{
|
||||
var activeCount = 0;
|
||||
for (var i = 0; i < firstSegmentVirtualPool.Count; i++)
|
||||
{
|
||||
if (firstSegmentVirtualPool[i] != null && firstSegmentVirtualPool[i].gameObject.activeSelf)
|
||||
{
|
||||
activeCount++;
|
||||
}
|
||||
}
|
||||
public int ActiveRuntimeVirtualNodeCount => runtimeVirtualPointCount;
|
||||
|
||||
for (var i = 0; i < otherSegmentVirtualPool.Count; i++)
|
||||
{
|
||||
if (otherSegmentVirtualPool[i] != null && otherSegmentVirtualPool[i].gameObject.activeSelf)
|
||||
{
|
||||
activeCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return activeCount;
|
||||
}
|
||||
}
|
||||
|
||||
public int OrderedNodeCount => orderedNodes.Count;
|
||||
public int OrderedNodeCount => chainPoints.Count;
|
||||
|
||||
public int SegmentCount => restLengths.Count;
|
||||
|
||||
@@ -110,11 +103,6 @@ namespace F2RopeLine2.FishingLine
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
EnsureVirtualNodeRoot();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (autoBuildOnStart)
|
||||
@@ -145,7 +133,9 @@ namespace F2RopeLine2.FishingLine
|
||||
RebuildRuntimeChain();
|
||||
}
|
||||
|
||||
if (lineRenderer != null && orderedNodes.Count > 1)
|
||||
SyncLogicalPointPositions();
|
||||
|
||||
if (lineRenderer != null && chainPoints.Count > 1)
|
||||
{
|
||||
lineRenderer.Render(this, Time.deltaTime);
|
||||
}
|
||||
@@ -162,8 +152,6 @@ namespace F2RopeLine2.FishingLine
|
||||
[ContextMenu("Build Line")]
|
||||
public void BuildLine()
|
||||
{
|
||||
EnsureVirtualNodeRoot();
|
||||
CleanupDeadReferences();
|
||||
ConfigureStartNode();
|
||||
ConfigureLogicalJoints();
|
||||
RebuildRuntimeChain();
|
||||
@@ -187,33 +175,14 @@ namespace F2RopeLine2.FishingLine
|
||||
BuildLine();
|
||||
}
|
||||
|
||||
public bool TryGetOrderedNode(int index, out FishingLineNode node)
|
||||
{
|
||||
if (index < 0 || index >= orderedNodes.Count)
|
||||
{
|
||||
node = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
node = orderedNodes[index];
|
||||
return true;
|
||||
}
|
||||
|
||||
public float GetActualDistance(int segmentIndex)
|
||||
{
|
||||
if (segmentIndex < 0 || segmentIndex >= orderedNodes.Count - 1)
|
||||
if (segmentIndex < 0 || segmentIndex >= chainPoints.Count - 1)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
var fromNode = orderedNodes[segmentIndex];
|
||||
var toNode = orderedNodes[segmentIndex + 1];
|
||||
if (fromNode == null || toNode == null)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
return Vector3.Distance(fromNode.Position, toNode.Position);
|
||||
return Vector3.Distance(chainPoints[segmentIndex].Position, chainPoints[segmentIndex + 1].Position);
|
||||
}
|
||||
|
||||
public string GetRuntimeDebugSummary()
|
||||
@@ -224,8 +193,6 @@ namespace F2RopeLine2.FishingLine
|
||||
.Append(" logical:")
|
||||
.Append(LogicalNodeCount)
|
||||
.Append(" runtimeVirtual:")
|
||||
.Append(ActiveRuntimeVirtualNodeCount)
|
||||
.Append("/")
|
||||
.Append(RuntimeVirtualNodeCount)
|
||||
.Append(" ordered:")
|
||||
.Append(OrderedNodeCount)
|
||||
@@ -233,23 +200,26 @@ namespace F2RopeLine2.FishingLine
|
||||
.Append(TotalLineLength.ToString("F2"))
|
||||
.Append("m");
|
||||
|
||||
for (var i = 0; i < orderedNodes.Count; i++)
|
||||
for (var i = 0; i < chainPoints.Count; i++)
|
||||
{
|
||||
var node = orderedNodes[i];
|
||||
if (node == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var point = chainPoints[i];
|
||||
builder.AppendLine()
|
||||
.Append('#')
|
||||
.Append(i)
|
||||
.Append(' ')
|
||||
.Append(node.GetDebugSummary());
|
||||
.Append(point.GetDebugName())
|
||||
.Append(" pos:")
|
||||
.Append(point.Position);
|
||||
|
||||
if (point.IsLogical && point.LogicalNode != null)
|
||||
{
|
||||
builder.Append(" body:")
|
||||
.Append(point.LogicalNode.Body != null ? "yes" : "no");
|
||||
}
|
||||
|
||||
if (i < restLengths.Count)
|
||||
{
|
||||
builder.Append(" seg rest:")
|
||||
builder.Append(" seg rest:")
|
||||
.Append(restLengths[i].ToString("F3"))
|
||||
.Append(" actual:")
|
||||
.Append(GetActualDistance(i).ToString("F3"));
|
||||
@@ -346,74 +316,49 @@ namespace F2RopeLine2.FishingLine
|
||||
|
||||
private void RebuildRuntimeChain()
|
||||
{
|
||||
CleanupDeadReferences();
|
||||
orderedNodes.Clear();
|
||||
chainPoints.Clear();
|
||||
restLengths.Clear();
|
||||
pinnedIndices.Clear();
|
||||
TotalLineLength = 0f;
|
||||
runtimeVirtualPointCount = 0;
|
||||
|
||||
if (logicalNodes == null || logicalNodes.Length < 2)
|
||||
{
|
||||
DestroyAllRuntimeVirtualNodes();
|
||||
chainDirty = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var segmentLayouts = new SegmentLayout[logicalNodes.Length - 1];
|
||||
var firstSegmentVirtualCount = 0;
|
||||
var otherSegmentVirtualCount = 0;
|
||||
for (var segmentIndex = 0; segmentIndex < segmentLayouts.Length; segmentIndex++)
|
||||
{
|
||||
segmentLayouts[segmentIndex] = BuildSegmentLayout(segmentIndex);
|
||||
if (segmentIndex == 0)
|
||||
{
|
||||
firstSegmentVirtualCount = segmentLayouts[segmentIndex].VirtualNodeCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
otherSegmentVirtualCount += segmentLayouts[segmentIndex].VirtualNodeCount;
|
||||
}
|
||||
}
|
||||
|
||||
EnsureFirstSegmentVirtualPool(firstSegmentVirtualCount);
|
||||
EnsureOtherSegmentVirtualPool(otherSegmentVirtualCount);
|
||||
|
||||
orderedNodes.Add(logicalNodes[0]);
|
||||
AddLogicalPoint(logicalNodes[0], 0);
|
||||
pinnedIndices.Add(0);
|
||||
|
||||
var otherVirtualCursor = 0;
|
||||
for (var segmentIndex = 0; segmentIndex < segmentLayouts.Length; segmentIndex++)
|
||||
{
|
||||
var fromNode = logicalNodes[segmentIndex];
|
||||
var toNode = logicalNodes[segmentIndex + 1];
|
||||
var layout = segmentLayouts[segmentIndex];
|
||||
var segmentVirtualNodes = GetSegmentVirtualNodes(segmentIndex, layout.VirtualNodeCount, ref otherVirtualCursor);
|
||||
|
||||
for (var virtualIndex = 0; virtualIndex < segmentVirtualNodes.Count; virtualIndex++)
|
||||
if (fromNode == null || toNode == null)
|
||||
{
|
||||
var virtualNode = segmentVirtualNodes[virtualIndex];
|
||||
virtualNode.SetRuntimeVirtual(true, orderedNodes.Count);
|
||||
virtualNode.name = $"VirtualNode_{segmentIndex}_{virtualIndex}";
|
||||
virtualNode.transform.SetParent(virtualNodeRoot, false);
|
||||
orderedNodes.Add(virtualNode);
|
||||
continue;
|
||||
}
|
||||
|
||||
var layout = segmentLayouts[segmentIndex];
|
||||
AddVirtualPoints(fromNode.Position, toNode.Position, layout, segmentIndex);
|
||||
|
||||
for (var gapIndex = 0; gapIndex < layout.GapLengths.Length; gapIndex++)
|
||||
{
|
||||
var gapLength = layout.GapLengths[gapIndex];
|
||||
restLengths.Add(gapLength);
|
||||
TotalLineLength += gapLength;
|
||||
restLengths.Add(layout.GapLengths[gapIndex]);
|
||||
TotalLineLength += layout.GapLengths[gapIndex];
|
||||
}
|
||||
|
||||
orderedNodes.Add(toNode);
|
||||
pinnedIndices.Add(orderedNodes.Count - 1);
|
||||
|
||||
PlaceVirtualNodesBetween(fromNode, toNode, layout, segmentVirtualNodes);
|
||||
AddLogicalPoint(toNode, segmentIndex + 1);
|
||||
pinnedIndices.Add(chainPoints.Count - 1);
|
||||
}
|
||||
|
||||
DisableUnusedFirstSegmentVirtualNodes(firstSegmentVirtualCount);
|
||||
DisableUnusedOtherSegmentVirtualNodes(otherSegmentVirtualCount);
|
||||
UpdateJointLimitsFromConfig();
|
||||
chainDirty = false;
|
||||
}
|
||||
|
||||
@@ -424,14 +369,14 @@ namespace F2RopeLine2.FishingLine
|
||||
{
|
||||
return new SegmentLayout
|
||||
{
|
||||
TotalLength = totalLength,
|
||||
GapLengths = BuildFirstSegmentGaps(totalLength, firstSegmentStep),
|
||||
};
|
||||
}
|
||||
|
||||
var virtualCount = logicalNodes[segmentIndex].FixedVirtualNodesToNext;
|
||||
var sourceNode = logicalNodes[segmentIndex];
|
||||
var virtualCount = sourceNode != null ? sourceNode.FixedVirtualNodesToNext : 0;
|
||||
var gapCount = Mathf.Max(1, virtualCount + 1);
|
||||
var gapLength = gapCount > 0 ? totalLength / gapCount : totalLength;
|
||||
var gapLength = totalLength / gapCount;
|
||||
var gaps = new float[gapCount];
|
||||
for (var i = 0; i < gaps.Length; i++)
|
||||
{
|
||||
@@ -440,11 +385,74 @@ namespace F2RopeLine2.FishingLine
|
||||
|
||||
return new SegmentLayout
|
||||
{
|
||||
TotalLength = totalLength,
|
||||
GapLengths = gaps,
|
||||
};
|
||||
}
|
||||
|
||||
private void AddLogicalPoint(FishingLineNode logicalNode, int logicalIndex)
|
||||
{
|
||||
if (logicalNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
chainPoints.Add(new ChainPoint
|
||||
{
|
||||
Key = BuildLogicalPointKey(logicalIndex),
|
||||
Position = logicalNode.Position,
|
||||
IsLogical = true,
|
||||
LogicalNode = logicalNode,
|
||||
SegmentIndex = logicalIndex,
|
||||
StableIndex = logicalIndex,
|
||||
});
|
||||
}
|
||||
|
||||
private void AddVirtualPoints(Vector3 fromPosition, Vector3 toPosition, SegmentLayout layout, int segmentIndex)
|
||||
{
|
||||
if (layout.VirtualNodeCount == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var direction = toPosition - fromPosition;
|
||||
var distance = direction.magnitude;
|
||||
var normalizedDirection = distance > 0.0001f ? direction / distance : Vector3.down;
|
||||
var accumulatedDistance = 0f;
|
||||
|
||||
for (var virtualIndex = 0; virtualIndex < layout.VirtualNodeCount; virtualIndex++)
|
||||
{
|
||||
accumulatedDistance += layout.GapLengths[virtualIndex];
|
||||
var stableIndex = segmentIndex == 0
|
||||
? layout.VirtualNodeCount - 1 - virtualIndex
|
||||
: virtualIndex;
|
||||
|
||||
chainPoints.Add(new ChainPoint
|
||||
{
|
||||
Key = BuildVirtualPointKey(segmentIndex, stableIndex),
|
||||
Position = fromPosition + normalizedDirection * accumulatedDistance,
|
||||
IsLogical = false,
|
||||
LogicalNode = null,
|
||||
SegmentIndex = segmentIndex,
|
||||
StableIndex = stableIndex,
|
||||
});
|
||||
runtimeVirtualPointCount++;
|
||||
}
|
||||
}
|
||||
|
||||
private void SyncLogicalPointPositions()
|
||||
{
|
||||
for (var i = 0; i < chainPoints.Count; i++)
|
||||
{
|
||||
var point = chainPoints[i];
|
||||
if (!point.IsLogical || point.LogicalNode == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
point.Position = point.LogicalNode.Position;
|
||||
}
|
||||
}
|
||||
|
||||
private static float[] BuildFirstSegmentGaps(float totalLength, float step)
|
||||
{
|
||||
if (totalLength <= 0f)
|
||||
@@ -480,167 +488,6 @@ namespace F2RopeLine2.FishingLine
|
||||
return divisibleGaps;
|
||||
}
|
||||
|
||||
private void PlaceVirtualNodesBetween(
|
||||
FishingLineNode fromNode,
|
||||
FishingLineNode toNode,
|
||||
SegmentLayout layout,
|
||||
IReadOnlyList<FishingLineNode> segmentVirtualNodes)
|
||||
{
|
||||
if (layout.VirtualNodeCount == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var direction = toNode.Position - fromNode.Position;
|
||||
var distance = direction.magnitude;
|
||||
var normalizedDirection = distance > 0.0001f ? direction / distance : Vector3.down;
|
||||
|
||||
var accumulatedDistance = 0f;
|
||||
for (var i = 0; i < layout.VirtualNodeCount; i++)
|
||||
{
|
||||
accumulatedDistance += layout.GapLengths[i];
|
||||
var position = fromNode.Position + normalizedDirection * accumulatedDistance;
|
||||
var virtualNode = segmentVirtualNodes[i];
|
||||
var wasActive = virtualNode.gameObject.activeSelf;
|
||||
if (!Application.isPlaying || !wasActive)
|
||||
{
|
||||
virtualNode.SetVisualPosition(position);
|
||||
}
|
||||
|
||||
virtualNode.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
private List<FishingLineNode> GetSegmentVirtualNodes(int segmentIndex, int virtualCount, ref int otherVirtualCursor)
|
||||
{
|
||||
var result = new List<FishingLineNode>(virtualCount);
|
||||
if (virtualCount <= 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (segmentIndex == 0)
|
||||
{
|
||||
for (var i = 0; i < virtualCount; i++)
|
||||
{
|
||||
result.Add(firstSegmentVirtualPool[virtualCount - 1 - i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
for (var i = 0; i < virtualCount; i++)
|
||||
{
|
||||
result.Add(otherSegmentVirtualPool[otherVirtualCursor + i]);
|
||||
}
|
||||
|
||||
otherVirtualCursor += virtualCount;
|
||||
return result;
|
||||
}
|
||||
|
||||
private void EnsureFirstSegmentVirtualPool(int targetCount)
|
||||
{
|
||||
EnsureVirtualNodeRoot();
|
||||
|
||||
while (firstSegmentVirtualPool.Count < targetCount)
|
||||
{
|
||||
var node = CreateRuntimeVirtualNode($"FirstSegmentVirtualNode_{firstSegmentVirtualPool.Count}");
|
||||
firstSegmentVirtualPool.Add(node);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureOtherSegmentVirtualPool(int targetCount)
|
||||
{
|
||||
EnsureVirtualNodeRoot();
|
||||
|
||||
while (otherSegmentVirtualPool.Count < targetCount)
|
||||
{
|
||||
var node = CreateRuntimeVirtualNode($"OtherSegmentVirtualNode_{otherSegmentVirtualPool.Count}");
|
||||
otherSegmentVirtualPool.Add(node);
|
||||
}
|
||||
}
|
||||
|
||||
private FishingLineNode CreateRuntimeVirtualNode(string nodeName)
|
||||
{
|
||||
var runtimeObject = new GameObject(nodeName);
|
||||
runtimeObject.transform.SetParent(virtualNodeRoot, false);
|
||||
|
||||
var node = runtimeObject.AddComponent<FishingLineNode>();
|
||||
node.SetRuntimeVirtual(true, -1);
|
||||
runtimeObject.hideFlags = HideFlags.DontSave;
|
||||
runtimeObject.SetActive(false);
|
||||
return node;
|
||||
}
|
||||
|
||||
private void DisableUnusedFirstSegmentVirtualNodes(int usedCount)
|
||||
{
|
||||
for (var i = 0; i < firstSegmentVirtualPool.Count; i++)
|
||||
{
|
||||
var node = firstSegmentVirtualPool[i];
|
||||
var active = i < usedCount;
|
||||
if (node != null)
|
||||
{
|
||||
node.gameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DisableUnusedOtherSegmentVirtualNodes(int usedCount)
|
||||
{
|
||||
for (var i = 0; i < otherSegmentVirtualPool.Count; i++)
|
||||
{
|
||||
var node = otherSegmentVirtualPool[i];
|
||||
var active = i < usedCount;
|
||||
if (node != null)
|
||||
{
|
||||
node.gameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DestroyAllRuntimeVirtualNodes()
|
||||
{
|
||||
for (var i = firstSegmentVirtualPool.Count - 1; i >= 0; i--)
|
||||
{
|
||||
DestroyNodeObject(firstSegmentVirtualPool[i]);
|
||||
}
|
||||
|
||||
for (var i = otherSegmentVirtualPool.Count - 1; i >= 0; i--)
|
||||
{
|
||||
DestroyNodeObject(otherSegmentVirtualPool[i]);
|
||||
}
|
||||
|
||||
firstSegmentVirtualPool.Clear();
|
||||
otherSegmentVirtualPool.Clear();
|
||||
}
|
||||
|
||||
private void CleanupDeadReferences()
|
||||
{
|
||||
firstSegmentVirtualPool.RemoveAll(node => node == null);
|
||||
otherSegmentVirtualPool.RemoveAll(node => node == null);
|
||||
runtimeJoints.RemoveAll(joint => joint == null);
|
||||
}
|
||||
|
||||
private void EnsureVirtualNodeRoot()
|
||||
{
|
||||
if (virtualNodeRoot != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var existing = transform.Find("_RuntimeVirtualNodes");
|
||||
if (existing != null)
|
||||
{
|
||||
virtualNodeRoot = existing;
|
||||
return;
|
||||
}
|
||||
|
||||
var root = new GameObject("_RuntimeVirtualNodes");
|
||||
root.transform.SetParent(transform, false);
|
||||
root.hideFlags = HideFlags.DontSave;
|
||||
virtualNodeRoot = root.transform;
|
||||
}
|
||||
|
||||
private void UpdateJointLimitsFromConfig()
|
||||
{
|
||||
for (var i = 1; i < logicalNodes.Length; i++)
|
||||
@@ -689,43 +536,33 @@ namespace F2RopeLine2.FishingLine
|
||||
return sourceNode != null ? sourceNode.SegmentLengthToNext : 0f;
|
||||
}
|
||||
|
||||
private void DestroyNodeObject(FishingLineNode node)
|
||||
private static long BuildLogicalPointKey(int logicalIndex)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return (1L << 62) | (uint)logicalIndex;
|
||||
}
|
||||
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Destroy(node.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
DestroyImmediate(node.gameObject);
|
||||
}
|
||||
private static long BuildVirtualPointKey(int segmentIndex, int stableIndex)
|
||||
{
|
||||
return ((long)(segmentIndex + 1) << 32) | (uint)stableIndex;
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (!drawDebugChain || orderedNodes.Count < 2 || restLengths.Count == 0)
|
||||
if (!drawDebugChain || chainPoints.Count < 2 || restLengths.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Gizmos.color = debugColor;
|
||||
for (var i = 0; i < orderedNodes.Count - 1; i++)
|
||||
for (var i = 0; i < chainPoints.Count - 1; i++)
|
||||
{
|
||||
if (orderedNodes[i] == null || orderedNodes[i + 1] == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var from = chainPoints[i].Position;
|
||||
var to = chainPoints[i + 1].Position;
|
||||
Gizmos.DrawLine(from, to);
|
||||
Gizmos.DrawSphere(from, debugNodeRadius);
|
||||
|
||||
Gizmos.DrawLine(orderedNodes[i].Position, orderedNodes[i + 1].Position);
|
||||
Gizmos.DrawSphere(orderedNodes[i].Position, debugNodeRadius);
|
||||
|
||||
var midPoint = (orderedNodes[i].Position + orderedNodes[i + 1].Position) * 0.5f;
|
||||
var actualLength = Vector3.Distance(orderedNodes[i].Position, orderedNodes[i + 1].Position);
|
||||
var midPoint = (from + to) * 0.5f;
|
||||
var actualLength = Vector3.Distance(from, to);
|
||||
var restLength = i < restLengths.Count ? restLengths[i] : 0f;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@@ -739,10 +576,7 @@ namespace F2RopeLine2.FishingLine
|
||||
#endif
|
||||
}
|
||||
|
||||
if (orderedNodes.Count > 0 && orderedNodes[^1] != null)
|
||||
{
|
||||
Gizmos.DrawSphere(orderedNodes[^1].Position, debugNodeRadius);
|
||||
}
|
||||
Gizmos.DrawSphere(chainPoints[^1].Position, debugNodeRadius);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (!showNodeLabels)
|
||||
@@ -750,19 +584,14 @@ namespace F2RopeLine2.FishingLine
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < orderedNodes.Count; i++)
|
||||
for (var i = 0; i < chainPoints.Count; i++)
|
||||
{
|
||||
var node = orderedNodes[i];
|
||||
if (node == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var point = chainPoints[i];
|
||||
var pinned = pinnedIndices.Contains(i) ? " Pinned" : string.Empty;
|
||||
UnityEditor.Handles.color = debugColor;
|
||||
UnityEditor.Handles.Label(
|
||||
node.Position + Vector3.up * 0.04f,
|
||||
$"#{i} {node.GetDebugName()}{pinned}");
|
||||
point.Position + Vector3.up * 0.04f,
|
||||
$"#{i} {point.GetDebugName()}{pinned}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user