void Update() { // Eliminates expired displayPoints for (int i = displayCnt - 1; i >= 0; i--) { DisplayPoint point = displayPoints[i]; if (point == null || point.age > lifeTime) { displayPoints[i] = null; displayCnt--; } } // Create a new control point using object's current position if the distance in between object and the last control point is greater than minControlPtnDistance if ((controlPoints[0] - transform.position).magnitude > minControlPtnDistance) { for (int j = 3; j > 0; j--) { controlPoints[j] = controlPoints[j - 1]; } controlPoints[0] = transform.position; controlCnt++; // Camule-Spline need excatly 4 control points in order to generate display points if (controlCnt < 3) { return; } AddNewDisplayPoints(); } RenderTrail(); }
/// <summary> /// Calculate new control displayPoints based on input and generate display points based on control points /// </summary> private void AddNewDisplayPoints() { int j = displayCnt + 3; // We can have as most 400 display points as the size of displayPoints array is 400 if (displayCnt + 3 > 399) { j = 399; } displayCnt = j; // Free up spaces for new display points for (; j > 2; j--) { displayPoints[j] = displayPoints[j - 3]; } // Generating display points in between 4 control points displayPoints[2] = new DisplayPoint(CRSpline(0.33f, controlPoints[3], controlPoints[2], controlPoints[1], controlPoints[0])); displayPoints[1] = new DisplayPoint(CRSpline(0.66f, controlPoints[3], controlPoints[2], controlPoints[1], controlPoints[0])); displayPoints[0] = new DisplayPoint(CRSpline(1f, controlPoints[3], controlPoints[2], controlPoints[1], controlPoints[0])); }