// Update is called once per frame void Update() { if (!Spline || !Spline.IsInitialized) { return; } // Runtime processing if (Application.isPlaying) { // get the TF of the current distance. // Note: It's recommended to use the TF based methods in consecutive calls, as the distance based // methods need to convert distance to TF internally each time! float tf = Spline.DistanceToTF(mDistance); // Move using cached values(slightly faster) or interpolate position now (more exact) // Note that we pass mTF and mDir by reference. These values will be changed by the Move methods mTransform.position = (FastInterpolation) ? Spline.MoveByFast(ref tf, ref mDir, Speed * Time.deltaTime, Clamping) : Spline.MoveBy(ref tf, ref mDir, Speed * Time.deltaTime, Clamping); mDistance = Spline.TFToDistance(tf); // Rotate the transform to match the spline's orientation if (SetOrientation) { transform.rotation = Spline.GetOrientationFast(tf); } } else // Editor processing: continuously place the transform to reflect property changes in the editor { InitPosAndRot(); } }
void LateUpdate() { if (CameraController) { // Camera has a regular SplineController attached. Set it to 5 units behind the player var newCamAbs = Spline.TFToDistance(mTF) - 5; CameraController.AbsolutePosition = Mathf.SmoothDamp(CameraController.AbsolutePosition, newCamAbs, ref velocity, 0.5f); } }
void Update() { if (!Spline || !Spline.IsInitialized) { return; } if (Application.isPlaying && mDistance < 2) { float tf = Spline.DistanceToTF(mDistance); mTransform.position = (FastInterpolation) ? Spline.MoveByFast(ref tf, ref mDir, Speed * Time.deltaTime, Clamping) : Spline.MoveBy(ref tf, ref mDir, Speed * Time.deltaTime, Clamping); mDistance = Spline.TFToDistance(tf); if (SetOrientation) { transform.rotation = Spline.GetOrientationFast(tf); } } }
void DoInterpolate() { if (!mSpline.IsInitialized) { return; } System.Type metaDataType; { if (String.IsNullOrEmpty(MetaDataType.Value)) { metaDataType = null; } else { #if NETFX_CORE Type[] knownTypes = this.GetType().GetAllTypes(); #else Type[] knownTypes = TypeExt.GetLoadedTypes(); #endif metaDataType = knownTypes.FirstOrDefault(t => t.FullName == MetaDataType.Value); } } bool calc = !Input.IsNone; if (calc) { float f = (UseWorldUnits.Value) ? mSpline.DistanceToTF(Input.Value) : Input.Value; if (StorePosition.IsNone == false) { StorePosition.Value = (UseCache.Value) ? mSpline.InterpolateFast(f) : mSpline.Interpolate(f); } if (StoreTangent.IsNone == false) { StoreTangent.Value = mSpline.GetTangent(f); } if (StoreUpVector.IsNone == false) { StoreUpVector.Value = mSpline.GetOrientationUpFast(f); } if (StoreRotation.IsNone == false) { StoreRotation.Value = (StoreUpVector.IsNone) ? mSpline.GetOrientationFast(f) : Quaternion.LookRotation(mSpline.GetTangent(f), StoreUpVector.Value); } if (StoreScale.IsNone == false) { float localF; CurvySplineSegment segment = mSpline.TFToSegment(f, out localF); CurvySplineSegment nextControlPoint = segment.Spline.GetNextControlPoint(segment); if (ReferenceEquals(segment, null) == false) { StoreScale.Value = nextControlPoint ? Vector3.Lerp(segment.transform.lossyScale, nextControlPoint.transform.lossyScale, localF) : segment.transform.lossyScale; } else { StoreScale.Value = Vector3.zero; } } if (StoreTF.IsNone == false) { StoreTF.Value = f; } if (StoreDistance.IsNone == false) { StoreDistance.Value = (UseWorldUnits.Value) ? Input.Value : mSpline.TFToDistance(f); } if (metaDataType != null) { if (metaDataType.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 " + metaDataType.FullName + " should be a subclass of CurvyInterpolatableMetadataBase<T>"); } else { if (StoreMetadata.IsNone == false) { MethodInfo genericMethodInfo = mSpline.GetType().GetMethod("GetMetadata").MakeGenericMethod(metaDataType); StoreMetadata.Value = (Object)genericMethodInfo.Invoke(mSpline, new System.Object[] { f }); } if (StoreInterpolatedMetadata.IsNone == false) { Type argumentType = GetInterpolatableMetadataGenericType(metaDataType); if (argumentType == null) { Debug.LogError("Meta data type " + metaDataType.FullName + " should be a subclass of CurvyInterpolatableMetadataBase<T>"); } else { MethodInfo genericMethodInfo = mSpline.GetType().GetMethod("GetInterpolatedMetadata").MakeGenericMethod(metaDataType, argumentType); StoreInterpolatedMetadata.SetValue(genericMethodInfo.Invoke(mSpline, new System.Object[] { f })); } } } } CurvySplineSegment seg = null; float segF = 0; if (StoreSegment.IsNone == false) { seg = getSegment(f, out segF); StoreSegment.Value = seg.gameObject; } if (StoreSegmentF.IsNone == false) { if (!seg) { seg = getSegment(f, out segF); } StoreSegmentF.Value = segF; } if (StoreSegmentDistance.IsNone == false) { if (!seg) { seg = getSegment(f, out segF); } StoreSegmentDistance.Value = seg.LocalFToDistance(segF); } } // General if (StoreLength.IsNone == false) { StoreLength.Value = mSpline.Length; } if (StoreCount.IsNone == false) { StoreCount.Value = mSpline.Count; } }
void Prepare() { #if UNITY_EDITOR mPerfWatch.Reset(); mPerfWatch.Start(); #endif if (Spline && StartMesh && ExtrusionParameter > 0) { StartMeshInfo = new MeshInfo(StartMesh, true, false); if (EndMesh) { EndMeshInfo = new MeshInfo(EndMesh, false, true); } else { EndMeshInfo = new MeshInfo(StartMesh, false, true); } // Calculate Steps float tf = FromTF; mSegmentInfo.Clear(); FromTF = Mathf.Clamp01(FromTF); ToTF = Mathf.Max(FromTF, Mathf.Clamp01(ToTF)); Vector3 scale; if (FromTF != ToTF) { switch (Extrusion) { case MeshExtrusion.FixedF: while (tf < ToTF) { scale = getScale(tf); mSegmentInfo.Add(new CurvyMeshSegmentInfo(this, tf, scale)); tf += ExtrusionParameter; } break; case MeshExtrusion.FixedDistance: float d = Spline.TFToDistance(FromTF); tf = Spline.DistanceToTF(d); while (tf < ToTF) { scale = getScale(tf); mSegmentInfo.Add(new CurvyMeshSegmentInfo(this, tf, d, scale)); d += ExtrusionParameter; tf = Spline.DistanceToTF(d); } break; case MeshExtrusion.Adaptive: while (tf < ToTF) { scale = getScale(tf); mSegmentInfo.Add(new CurvyMeshSegmentInfo(this, tf, scale)); int dir = 1; Spline.MoveByAngle(ref tf, ref dir, ExtrusionParameter, CurvyClamping.Clamp, 0.001f); } break; } if (!Mathf.Approximately(tf, ToTF)) { tf = ToTF; } scale = getScale(tf); mSegmentInfo.Add(new CurvyMeshSegmentInfo(this, tf, scale)); } } #if UNITY_EDITOR mPerfWatch.Stop(); DebugPerfPrepare = mPerfWatch.ElapsedTicks / (double)System.TimeSpan.TicksPerMillisecond; mPerfWatch.Reset(); #endif }