public void GetPoint(TweenPlugPathPoint output, float perc, TweenPlugPathPoint[] wps, TweenPlugPath p) { int numSections = wps.Length - 1; // Considering also control points int tSec = (int)Mathf.Floor(perc * numSections); int currPt = numSections - 1; if (currPt > tSec) { currPt = tSec; } float u = perc * numSections - currPt; var aPt = currPt == 0 ? p.controlPoints[0].a : wps[currPt - 1]; var bPt = wps[currPt]; var cPt = wps[currPt + 1]; var dPt = currPt + 2 > wps.Length - 1 ? p.controlPoints[1].a : wps[currPt + 2]; var ptCount = p.pathPointCount; for (int i = 0; i < ptCount; i++) { float a = aPt.vals[i], b = bPt.vals[i], c = cPt.vals[i], d = dPt.vals[i]; output.vals[i] = .5f * ( (-a + 3f * b - 3f * c + d) * (u * u * u) + (2f * a - 5f * b + 4f * c - d) * (u * u) + (-a + c) * u + 2f * b); } }
void SetTimeToLengthTables(TweenPlugPath p, int subdivisions) { TweenPlugPathPoint prevP = _cachePoint1, currP = _cachePoint2; var ptCount = p.pathPointCount; float pathLen = 0; float incr = 1f / subdivisions; float[] timesTable = new float[subdivisions]; float[] lengthsTable = new float[subdivisions]; GetPoint(prevP, 0, p.wps, p); for (int i = 1; i < subdivisions + 1; ++i) { float perc = incr * i; GetPoint(currP, perc, p.wps, p); pathLen += TweenPlugPathPoint.Distance(currP, prevP, ptCount); prevP.Copy(currP, ptCount); timesTable[i - 1] = perc; lengthsTable[i - 1] = pathLen; } // Assign p.length = pathLen; p.timesTable = timesTable; p.lengthsTable = lengthsTable; }
public void Copy(TweenPlugPathPoint src, int length) { if (vals == null || vals.Length < length) { vals = new float[length]; } System.Array.Copy(src.vals, vals, length); }
public bool Equal(TweenPlugPathPoint other, int length) { for (int i = 0; i < length; i++) { if (vals[i] != other.vals[i]) { return(false); } } return(true); }
public bool Approximately(TweenPlugPathPoint other, int length) { for (int i = 0; i < length; i++) { if (!Mathf.Approximately(vals[i], other.vals[i])) { return(false); } } return(true); }
public void FinalizePath(TweenPlugPath p, TweenPlugPathPoint[] wps, bool isClosedPath) { // Add starting and ending control points (uses only one vector per control point) int wpsLen = wps.Length; if (p.controlPoints == null || p.controlPoints.Length != 2) { p.controlPoints = new TweenPlugPathControlPoint[2]; } if (isClosedPath) { p.controlPoints[0] = new TweenPlugPathControlPoint(wps[wpsLen - 2], _emptyPoint); p.controlPoints[1] = new TweenPlugPathControlPoint(wps[1], _emptyPoint); } else { p.controlPoints[0] = new TweenPlugPathControlPoint(wps[1], _emptyPoint); var lastP = wps[wpsLen - 1]; var lastP2 = wps[wpsLen - 2]; var newPt = new TweenPlugPathPoint(lastP.vals.Length); for (int i = 0; i < newPt.vals.Length; i++) { var lastPVal = lastP.vals[i]; var diffVVal = lastPVal - lastP2.vals[i]; newPt.vals[i] = lastPVal + diffVVal; } p.controlPoints[1] = new TweenPlugPathControlPoint(newPt, _emptyPoint); } // p.subdivisions = (wpsLen + 2) * p.subdivisionsXSegment; var subdivisions = wpsLen * p.subdivisionsXSegment; if (p.isConstantSpeed) { // Store time to len tables SetTimeToLengthTables(p, subdivisions); } else { //only generate path length SetPathLength(p, subdivisions); } }
public void GetPoint(TweenPlugPathPoint output, float perc, TweenPlugPathPoint[] wps, TweenPlugPath p) { var ptCount = p.pathPointCount; if (perc <= 0) { p.linearWPIndex = 1; output.Copy(wps[0], ptCount); return; } int startPIndex = 0; int endPIndex = 0; int count = p.timesTable.Length; for (int i = 1; i < count; i++) { if (p.timesTable[i] >= perc) { startPIndex = i - 1; endPIndex = i; break; } } float startPPerc = p.timesTable[startPIndex]; float partialPerc = perc - startPPerc; float partialLen = p.length * partialPerc; var wp0 = wps[startPIndex]; var wp1 = wps[endPIndex]; p.linearWPIndex = endPIndex; //output = wp0 + clamp(wp1 - wp0, partialLen) for (int i = 0; i < ptCount; i++) { output.vals[i] = wp1.vals[i] - wp0.vals[i]; } output.ClampMagnitude(partialLen, ptCount); for (int i = 0; i < ptCount; i++) { output.vals[i] += wp0.vals[i]; } }
public static float Distance(TweenPlugPathPoint a, TweenPlugPathPoint b, int length) { //NOTE: assume b has the same length as a if (length == 1) { return(Mathf.Abs(a.vals[0] - b.vals[0])); } float sqrLen = 0f; for (int i = 0; i < length; i++) { var delta = a.vals[i] - b.vals[i]; sqrLen += delta * delta; } return(Mathf.Sqrt(sqrLen)); }
// Linear exception: also sets waypoints lengths and doesn't set lengthsTable since it's useless void SetTimeToLengthTables(TweenPlugPath p, int subdivisions) { float pathLen = 0; int wpsLen = p.wps.Length; var ptCount = p.pathPointCount; var prevP = p.wps[0]; for (int i = 1; i < wpsLen; i++) { var currP = p.wps[i]; float dist = TweenPlugPathPoint.Distance(currP, prevP, ptCount); pathLen += dist; prevP = currP; } // Assign p.length = pathLen; p.lengthsTable = null; p.timesTable = null; }
public object GetValueFromPathPoint(MaterialTrack.ValueType valueType, TweenPlugPathPoint pt) { switch (valueType) { case MaterialTrack.ValueType.Color: return(pt.valueColor); case MaterialTrack.ValueType.Vector: return(pt.valueVector4); case MaterialTrack.ValueType.Float: case MaterialTrack.ValueType.Range: return(pt.valueFloat); case MaterialTrack.ValueType.TexOfs: case MaterialTrack.ValueType.TexScale: return(pt.valueVector2); } return(null); }
void SetPathLength(TweenPlugPath p, int subdivisions) { TweenPlugPathPoint prevP = _cachePoint1, currP = _cachePoint2; var ptCount = p.pathPointCount; float pathLen = 0; float incr = 1f / subdivisions; GetPoint(prevP, 0, p.wps, p); for (int i = 1; i < subdivisions + 1; ++i) { float perc = incr * i; GetPoint(currP, perc, p.wps, p); pathLen += TweenPlugPathPoint.Distance(currP, prevP, ptCount); prevP.Copy(currP, ptCount); } // Assign p.length = pathLen; p.timesTable = null; p.lengthsTable = null; }
public object GetValueFromPathPoint(PropertyTrack.ValueType valueType, TweenPlugPathPoint pt) { switch (valueType) { case PropertyTrack.ValueType.Float: return(pt.valueFloat); case PropertyTrack.ValueType.Integer: return(Mathf.RoundToInt(pt.valueFloat)); case PropertyTrack.ValueType.Long: return(System.Convert.ToInt64(Mathf.RoundToInt(pt.valueFloat))); case PropertyTrack.ValueType.Double: return(System.Convert.ToDouble(pt.valueFloat)); case PropertyTrack.ValueType.Vector2: return(pt.valueVector2); case PropertyTrack.ValueType.Vector3: return(pt.valueVector3); case PropertyTrack.ValueType.Vector4: return(pt.valueVector4); case PropertyTrack.ValueType.Color: return(pt.valueColor); case PropertyTrack.ValueType.Rect: return(pt.valueRect); case PropertyTrack.ValueType.Quaternion: //TODO: quaternion is treated as euler angles return(Quaternion.Euler(pt.valueVector3)); } return(null); }
protected override Color GetValue(TweenPlugPathPoint pt) { return(pt.valueVector4); }
protected override bool Equal(TweenPlugPathPoint pt, Color curVal) { return(pt.valueColor == curVal); }
protected override bool Equal(TweenPlugPathPoint pt, Vector4 curVal) { return(pt.valueVector4 == curVal); }
protected override Vector3 GetValue(TweenPlugPathPoint pt) { return(pt.valueVector3); }
protected override bool ApproximatelyEqual(TweenPlugPathPoint pt, double curVal) { return(pt.Approximately((float)curVal)); }
public TweenPlugPathControlPoint(TweenPlugPathPoint a, TweenPlugPathPoint b) { this.a = a; this.b = b; }
protected override bool Equal(TweenPlugPathPoint pt, int curVal) { return(pt.valueFloat == curVal); }
protected abstract bool Equal(TweenPlugPathPoint pt, T curVal);
protected abstract bool ApproximatelyEqual(TweenPlugPathPoint pt, T curVal);
protected override long GetValue(TweenPlugPathPoint pt) { return(Mathf.RoundToInt(pt.valueFloat)); }
protected override bool ApproximatelyEqual(TweenPlugPathPoint pt, Quaternion curVal) { return(pt.Approximately(curVal.eulerAngles)); }
protected override bool Equal(TweenPlugPathPoint pt, Quaternion curVal) { return(pt.valueVector3 == curVal.eulerAngles); }
protected override Quaternion GetValue(TweenPlugPathPoint pt) { return(Quaternion.Euler(pt.valueVector3)); }
protected override Rect GetValue(TweenPlugPathPoint pt) { return(pt.valueRect); }
protected override bool ApproximatelyEqual(TweenPlugPathPoint pt, int curVal) { return(pt.Approximately(curVal)); }
protected abstract T GetValue(TweenPlugPathPoint pt);
protected override float GetValue(TweenPlugPathPoint pt) { return(pt.valueFloat); }
// Clones this path with the given loop increment public TweenPlugPath CloneIncremental(int loopIncrement) { if (_incrementalClone != null) { if (_incrementalIndex == loopIncrement) { return(_incrementalClone); } } var _incCachePt = new TweenPlugPathPoint(pathPointCount); int wpsLen = wps.Length; int ptCount = pathPointCount; //diff for (int i = 0; i < ptCount; i++) { _incCachePt.vals[i] = wps[wpsLen - 1].vals[i] - wps[0].vals[i]; } var incrWps = new TweenPlugPathPoint[wps.Length]; for (int i = 0; i < wpsLen; ++i) { incrWps[i] = new TweenPlugPathPoint(ptCount); for (int j = 0; j < ptCount; j++) { incrWps[i].vals[j] = wps[i].vals[j] + (_incCachePt.vals[j] * loopIncrement); //wps[i] + (diff * loopIncrement) } } int cpsLen = controlPoints.Length; var incrCps = new TweenPlugPathControlPoint[cpsLen]; for (int i = 0; i < cpsLen; ++i) { var a = new TweenPlugPathPoint(ptCount); var b = new TweenPlugPathPoint(ptCount); //incrCps[i] = controlPoints[i] + (diff * loopIncrement); for (int j = 0; j < ptCount; j++) { a.vals[j] = controlPoints[i].a.vals[j] + (_incCachePt.vals[j] * loopIncrement); b.vals[j] = controlPoints[i].b.vals[j] + (_incCachePt.vals[j] * loopIncrement); } incrCps[i] = new TweenPlugPathControlPoint(a, b); } _incrementalClone = new TweenPlugPath(); _incrementalIndex = loopIncrement; _incrementalClone.type = type; _incrementalClone.subdivisionsXSegment = subdivisionsXSegment; _incrementalClone.wps = incrWps; _incrementalClone.controlPoints = incrCps; _incrementalClone._cachePoint = _incCachePt; _incrementalClone.length = length; _incrementalClone.timesTable = timesTable; _incrementalClone.lengthsTable = lengthsTable; _incrementalClone._decoder = _decoder; return(_incrementalClone); }