public override void AppendSamples(Cable.SampledCable samples, Vector3 origin, float distance, float spoolSeparation, bool reverse, bool orientation) { // If the distance or radius are roughly zero, add the first sample and bail out. if (ScaledRadius < 1e-4 || distance < 1e-4) { samples.AppendSample(CableToWorld(origin)); return; } // Calculate angle using arc length: float angle = distance / ScaledRadius; int sampleCount = Mathf.CeilToInt(angle / (Mathf.PI * 0.05f)); Vector3 axisOffset = GetCablePlaneNormal() * distance * spoolSeparation / sampleCount; angle *= (orientation?-1:1) * (reverse?-1:1); float theta = -angle / sampleCount; // Decide whether to start at the origin or the end: Vector3 result = origin; // Sample cable: samples.AppendSample(CableToWorld(result), !reverse); for (int i = 0; i < sampleCount; ++i) { result = result.Rotate2D(theta); samples.AppendSample(CableToWorld(result) + axisOffset * (i + 1), !reverse); } if (reverse) { samples.ReverseLastSamples(sampleCount + 1); } }
public override void AppendSamples(Cable.SampledCable samples, Vector3 origin, float distance, float spoolSeparation, bool reverse, bool orientation) { if (convexHull == null || convexHull.hull.Count == 0) { return; } int current; Vector3 originSample; origin = ProjectToSurface(origin, out current); originSample = CableToWorld(origin); samples.AppendSample(originSample); if (distance < 1E-4) { return; } int direction = (orientation?-1:1) * (reverse?-1:1); Vector3 axisOffset = GetCablePlaneNormal() * spoolSeparation; int count = 1; float accumDistance = 0; while (accumDistance < Mathf.Abs(distance)) { // Get next hull sample: int next = current = (int)Utils.Mod(current - direction, convexHull.hull.Count); Vector3 sample = CableToWorld(new Vector3(convexHull.hull[next].x, convexHull.hull[next].y, origin.z)); // Calculate hull segment distance: float segmentDistance = Vector3.Distance(originSample, sample); // Accumulate segment distance to total distance: if (accumDistance + segmentDistance <= distance) { samples.AppendSample(sample + axisOffset * accumDistance, !reverse); } else { Vector3 interpolatedSample = Vector3.Lerp(originSample, sample, (Mathf.Abs(distance) - accumDistance) / segmentDistance); samples.AppendSample(interpolatedSample + axisOffset * accumDistance, !reverse); } originSample = sample; accumDistance += segmentDistance; count++; } if (reverse) { samples.ReverseLastSamples(count); } }