public static void SegmentCurveGizmo(CurvySplineSegment seg, Color col, float stepSize=0.05f) { Matrix4x4 mat = Gizmos.matrix; Gizmos.matrix = Matrix; Gizmos.color = col; if (seg.Spline.Interpolation == CurvyInterpolation.Linear) { Gizmos.DrawLine(seg.Interpolate(0), seg.Interpolate(1)); return; } Vector3 p = seg.Interpolate(0); for (float f = stepSize; f < 1; f += stepSize) { Vector3 p1 = seg.Interpolate(f); Gizmos.DrawLine(p, p1); p = p1; } Gizmos.DrawLine(p, seg.Interpolate(1)); Gizmos.matrix = mat; }
public static void InterpolateBezierHandles(CurvyInterpolation interpolation, float offset, bool?freeMoveHandles, params CurvySplineSegment[] controlPoints) { if (controlPoints.Length == 0) { return; } offset = Mathf.Clamp01(offset); foreach (CurvySplineSegment cp in controlPoints) { bool movestate = freeMoveHandles.HasValue ? freeMoveHandles.Value : cp.FreeHandles; cp.FreeHandles = movestate; CurvySplineSegment other = cp.PreviousSegment; if (other) { cp.HandleIn = other.Interpolate(1 - offset, interpolation) - cp.transform.localPosition; } else { cp.HandleIn = cp.Interpolate(0, interpolation); } if (cp.FreeHandles) { if (cp.IsValidSegment) { cp.HandleOut = cp.Interpolate(offset, interpolation) - cp.transform.localPosition; } else { cp.HandleIn = Vector3.zero; } } } controlPoints[0].Spline.Refresh(); }
void DoInterpolate() { if (!mSegment.Spline.IsInitialized) { return; } bool calc = !Input.IsNone; if (calc) { System.Type metaType = System.Type.GetType(MetaDataType.Value); float inputF = (UseWorldUnits.Value) ? mSegment.DistanceToLocalF(Input.Value) : Input.Value; if (StorePosition.UseVariable) { StorePosition.Value = (UseCache.Value) ? mSegment.InterpolateFast(inputF) : mSegment.Interpolate(inputF); } if (StoreTangent.UseVariable) { StoreTangent.Value = mSegment.GetTangent(inputF); } if (StoreUpVector.UseVariable) { StoreUpVector.Value = mSegment.GetOrientationUpFast(inputF); } if (StoreRotation.UseVariable) { StoreRotation.Value = (StoreUpVector.IsNone) ? mSegment.GetOrientationFast(inputF) : Quaternion.LookRotation(mSegment.GetTangent(inputF), StoreUpVector.Value); } if (StoreScale.UseVariable) { StoreScale.Value = mSegment.InterpolateScale(inputF); } if (StoreTF.UseVariable) { StoreTF.Value = mSegment.LocalFToTF(inputF); } if (StoreSegmentDistance.UseVariable) { StoreSegmentDistance.Value = mSegment.LocalFToDistance(inputF); } if (StoreDistance.UseVariable) { StoreDistance.Value = (StoreSegmentDistance.UseVariable) ? StoreSegmentDistance.Value + mSegment.Distance : mSegment.LocalFToDistance(inputF) + mSegment.Distance; } if (StoreSegmentF.UseVariable) { StoreSegmentF.Value = inputF; } if (metaType != null) { if (StoreMetadata.UseVariable) { StoreMetadata.Value = mSegment.GetMetaData(metaType); } if (StoreInterpolatedMetadata.useVariable) { StoreInterpolatedMetadata.SetValue(mSegment.InterpolateMetadata(metaType, inputF)); } } } // General if (StoreLength.UseVariable) { StoreLength.Value = mSegment.Length; } if (StoreSegmentIndex.UseVariable) { StoreSegmentIndex.Value = mSegment.SegmentIndex; } if (StoreControlPointIndex.UseVariable) { StoreControlPointIndex.Value = mSegment.ControlPointIndex; } }
/// <summary> /// Adds a Control Point /// </summary> /// <remarks>If you add several Control Points in a row, just refresh the last one!</remarks> /// <param name="insertAfter">an ancestor Control Point. If null, the CP will added at the end</param> /// <param name="refresh">whether the spline should be recalculated.</param> /// <returns>a Control Point</returns> public CurvySplineSegment Add(CurvySplineSegment insertAfter, bool refresh) { GameObject go = new GameObject("NewCP", typeof(CurvySplineSegment)); go.transform.parent = transform; CurvySplineSegment cp = go.GetComponent<CurvySplineSegment>(); int idx = mControlPoints.Count; if (insertAfter) { if (insertAfter.IsValidSegment) go.transform.position = insertAfter.Interpolate(0.5f); else if (insertAfter.NextTransform) go.transform.position = Vector3.Lerp(insertAfter.NextTransform.position, insertAfter.Transform.position, 0.5f); idx = insertAfter.ControlPointIndex + 1; } mControlPoints.Insert(idx,cp); _RenameControlPointsByIndex(); if (refresh) RefreshImmediately(); return cp; }
void DoInterpolate() { if (!mSegment.Spline.IsInitialized) { return; } bool calc = !Input.IsNone; if (calc) { System.Type metaType = System.Type.GetType(MetaDataType.Value); float inputF = (UseWorldUnits.Value) ? mSegment.DistanceToLocalF(Input.Value) : Input.Value; if (StorePosition.IsNone == false) { StorePosition.Value = (UseCache.Value) ? mSegment.InterpolateFast(inputF) : mSegment.Interpolate(inputF); } if (StoreTangent.IsNone == false) { StoreTangent.Value = mSegment.GetTangent(inputF); } if (StoreUpVector.IsNone == false) { StoreUpVector.Value = mSegment.GetOrientationUpFast(inputF); } if (StoreRotation.IsNone == false) { StoreRotation.Value = (StoreUpVector.IsNone) ? mSegment.GetOrientationFast(inputF) : Quaternion.LookRotation(mSegment.GetTangent(inputF), StoreUpVector.Value); } if (StoreScale.IsNone == false) { CurvySplineSegment nextControlPoint = mSegment.Spline.GetNextControlPoint(mSegment); StoreScale.Value = nextControlPoint ? Vector3.Lerp(mSegment.transform.lossyScale, nextControlPoint.transform.lossyScale, inputF) : mSegment.transform.lossyScale; } if (StoreTF.IsNone == false) { StoreTF.Value = mSegment.LocalFToTF(inputF); } if (StoreSegmentDistance.IsNone == false) { StoreSegmentDistance.Value = mSegment.LocalFToDistance(inputF); } if (StoreDistance.IsNone == false) { StoreDistance.Value = (StoreSegmentDistance.IsNone == false) ? StoreSegmentDistance.Value + mSegment.Distance : mSegment.LocalFToDistance(inputF) + mSegment.Distance; } if (StoreSegmentF.IsNone == false) { StoreSegmentF.Value = inputF; } if (metaType != null) { if (metaType.IsSubclassOf(typeof(CurvyMetadataBase)) == false) { //this if statement's branch does not exclude classes inheriting from CurvyMetadataBase but not from CurvyInterpolatableMetadataBase, but that's ok, those classes are handled below Debug.LogError("Meta data type " + metaType.FullName + " should be a subclass of CurvyInterpolatableMetadataBase<T>"); } else { if (StoreMetadata.IsNone == false) { MethodInfo genericMethodInfo = mSegment.GetType().GetMethod("GetMetadata").MakeGenericMethod(metaType); StoreMetadata.Value = (Object)genericMethodInfo.Invoke(mSegment, new System.Object[] { false }); } if (StoreInterpolatedMetadata.IsNone == false) { Type argumentType = CurvyGetValue.GetInterpolatableMetadataGenericType(metaType); if (argumentType == null) { Debug.LogError("Meta data type " + metaType.FullName + " should be a subclass of CurvyInterpolatableMetadataBase<T>"); } else { MethodInfo genericMethodInfo = mSegment.GetType().GetMethod("GetInterpolatedMetadata").MakeGenericMethod(metaType, argumentType); StoreInterpolatedMetadata.SetValue(genericMethodInfo.Invoke(mSegment, new System.Object[] { inputF })); } } } } } // General if (StoreLength.IsNone == false) { StoreLength.Value = mSegment.Length; } if (StoreSegmentIndex.IsNone == false) { StoreSegmentIndex.Value = mSegment.Spline.GetSegmentIndex(mSegment); } if (StoreControlPointIndex.IsNone == false) { StoreControlPointIndex.Value = mSegment.Spline.GetControlPointIndex(mSegment); } }