void Awake() { // Store the instance of RageSpline to avoid calling it every frame for speed/convenience. // Cast to IRageSpline for cleaner API access rageSpline = GetComponent(typeof(RageSpline)) as IRageSpline; // Array for movement Target positions per control point targetPositions = new Vector3[rageSpline.GetPointCount()]; // Store the original control point positions originalPositions = new Vector3[rageSpline.GetPointCount()]; for (int index = 0; index < rageSpline.GetPointCount(); index++) { originalPositions[index] = rageSpline.GetPosition(index); } }
void Awake() { // Store the instance of RageSpline to avoid calling it every frame for speed/convenience. // Cast to IRageSpline for cleaner API access rageSpline = GetComponent(typeof(RageSpline)) as IRageSpline; // Array for movement target positions per control point targetPositions = new Vector3[rageSpline.GetPointCount()]; // Store the original control point positions originalPositions = new Vector3[rageSpline.GetPointCount()]; for (int index = 0; index < rageSpline.GetPointCount(); index++) { originalPositions[index] = rageSpline.GetPosition(index); } }
void Update() { timeSinceLastShake += Time.deltaTime; // Is it time for a new shake? if (timeSinceLastShake > currentShakeGap) { // Iterate through all the control points for (int index = 0; index < rageSpline.GetPointCount(); index++) { // Randomise a new shake vector Vector3 shakeVector = new Vector3( Random.Range(-0.5f * shakeSizeInNormalSpace.x, 0.5f * shakeSizeInNormalSpace.x), Random.Range(-0.5f * shakeSizeInNormalSpace.y, 0.5f * shakeSizeInNormalSpace.y), 0f); // Get normal and tangent for the control point. We will shake along the normal. Vector3 normal = rageSpline.GetNormal(index); Vector3 tangent = Vector3.Cross(normal, Camera.main.transform.forward); // Set a new position for the control point targetPositions[index] = originalPositions[index] + shakeVector.x * tangent + shakeVector.y * normal; } // When is the next new shake? currentShakeGap = Random.Range(minFrameGap, maxFrameGap); timeSinceLastShake = 0f; } else { // Iterate through all the control points for (int index = 0; index < rageSpline.GetPointCount(); index++) { // Interpolate toward Target positions with Vector3.Lerp() Vector3 currentPosition = rageSpline.GetPosition(index); rageSpline.SetPoint(index, Vector3.Lerp(currentPosition, targetPositions[index], Time.deltaTime * (1f / easing))); } } // Finally refresh the visible mesh rageSpline.RefreshMesh(true, true, false); // Faster version (possible artifacts) //rageSpline.RefreshMesh(false, false, false); }
private void ShuffleSpline(IRageSpline rageSpline, float shuffleAmount) { for (int i = 0; i < rageSpline.GetPointCount(); i++) { Vector3 point = rageSpline.GetPosition(i); point += new Vector3(Random.Range(shuffleAmount * -0.5f, shuffleAmount * 0.5f), Random.Range(shuffleAmount * -0.5f, shuffleAmount * 0.5f), 0f); rageSpline.SetPoint(i, point); } }
/// <summary> Special case for start-end points, where the out tangent must be copied instead of the in tangent </summary> public static bool MergeStartEndPoints(this IRageSpline rageSpline, bool debug) { if (rageSpline.GetPointCount() <= 2) { return(false); } var lastPointIdx = rageSpline.GetPointCount() - 1; var lastPointPos = rageSpline.GetPositionWorldSpace(lastPointIdx); var firstPointPos = rageSpline.GetPositionWorldSpace(0); if (Vector3.Distance(firstPointPos, lastPointPos) < 0.0001f) { if (debug) { Debug.Log("\t Removing endpoint overlap "); } rageSpline.SetInControlPositionWorldSpace(0, rageSpline.GetInControlPositionWorldSpace(lastPointIdx)); rageSpline.RemovePoint(lastPointIdx); // Then deletes the last point return(true); } return(false); }
// Update is called once per frame void FixedUpdate() { for (int i = 0; i < rageSpline.GetPointCount() - 1; i++) { if (rageSpline.GetPositionWorldSpace(i + 1).x < cam.transform.position.x - (1024f / 768f) * cam.orthographicSize * 2f) { rageSpline.RemovePoint(i - 1); float x = (rageSpline.GetPositionWorldSpace(rageSpline.GetPointCount() - 1).x + pointGapWidth); rageSpline.AddPointWorldSpace(rageSpline.GetPointCount(), GetNewLandscapePoint(x), transform.right * pointGapWidth * 0.33f); transform.position += new Vector3(pointGapWidth, 0f, 0f); for (int a = 0; a < rageSpline.GetPointCount(); a++) { rageSpline.SetPoint(a, rageSpline.GetPosition(a) + new Vector3(-pointGapWidth, 0f, 0f)); } rageSpline.RefreshMesh(); } } curSteepness = steepness + transform.position.x * 0.0001f; maxY = Mathf.Clamp(transform.position.x * 0.05f, 0f, 10f); }
void Start() { // GUI has four types of objects. Controlpoints, in and out controls and a line between all of these. // All these are RageSpline objects, instantiated from a prefab controlPoints = new RageSpline[rageSpline.GetPointCount()]; inControlPoints = new RageSpline[rageSpline.GetPointCount()]; outControlPoints = new RageSpline[rageSpline.GetPointCount()]; handleLines = new RageSpline[rageSpline.GetPointCount()]; for (int index = 0; index < rageSpline.GetPointCount(); index++) { // Instantiate control point GUI object from the controlPointPrefab -prefab GameObject controlPointGO = Instantiate( controlPointPrefab, rageSpline.GetPositionWorldSpace(index) + new Vector3(0f, 0f, -1f), Quaternion.identity) as GameObject; // Save the new RageSpline instance reference of the GUI object controlPoints[index] = controlPointGO.GetComponent(typeof(RageSpline)) as RageSpline; controlPoints[index].SetFillColor1(unSelectedColor); // Instantiate inCtrl handle GUI object from the handleControlPointPrefab -prefab GameObject inControlPointGO = Instantiate( handleControlPointPrefab, rageSpline.GetInControlPositionWorldSpace(index) + new Vector3(0f, 0f, -1f), Quaternion.identity) as GameObject; // Save the new RageSpline instance reference of the GUI object inControlPoints[index] = inControlPointGO.GetComponent(typeof(RageSpline)) as RageSpline; inControlPoints[index].SetFillColor1(unSelectedColor); // Instantiate outCtrl handle GUI object from the handleControlPointPrefab -prefab GameObject outControlPointGO = Instantiate( handleControlPointPrefab, rageSpline.GetOutControlPositionWorldSpace(index) + new Vector3(0f, 0f, -1f), Quaternion.identity) as GameObject; // Save the new RageSpline instance reference of the GUI object outControlPoints[index] = outControlPointGO.GetComponent(typeof(RageSpline)) as RageSpline; outControlPoints[index].SetFillColor1(unSelectedColor); // Instantiate line GUI object from the handleLinePrefab -prefab GameObject handleLineGO = Instantiate( handleLinePrefab, rageSpline.GetPositionWorldSpace(index) + new Vector3(0f, 0f, -0.5f), Quaternion.identity) as GameObject; handleLines[index] = handleLineGO.GetComponent(typeof(RageSpline)) as RageSpline; } refreshEditorGUI(); }
public static bool RemoveOverlap(this IRageSpline path, int index0, int index1, float snapRadius, bool debug) { if (index1 == 0) { index0 = path.GetPointCount() - 1; } if (Vector3.Distance(path.GetPositionWorldSpace(index0), path.GetPositionWorldSpace(index1)) <= snapRadius) { if (debug) { Debug.Log("overlap path pos = " + path.GetPositionWorldSpace(index0)); } path.SetNatural(index1, false); path.SetInControlPositionWorldSpace(index1, path.GetInControlPositionWorldSpace(index0)); path.RemovePoint(index0); // Then deletes the duplicate previous point return(true); } return(false); }
void Update() { // Iterate through all the control points for (int index = 0; index < rageSpline.GetPointCount(); index++) { // Get the current control point position in localspace coordinates Vector3 oldPosition = rageSpline.GetPosition(index); // Randomise a new shake vector Vector3 shakeVector = new Vector3( Random.Range(-0.5f * shakeSize.x, 0.5f * shakeSize.x), Random.Range(-0.5f * shakeSize.y, 0.5f * shakeSize.y), 0f); // Set a new position for the control point rageSpline.SetPoint(index, oldPosition + shakeVector); } // Finally refresh the visible mesh rageSpline.RefreshMesh(true, true, true); // Faster version (possible artifacts) // rageSpline.RefreshMesh(false, false, false); }
private void ShuffleSpline(IRageSpline rageSpline, float shuffleAmount) { for (int i = 0; i < rageSpline.GetPointCount(); i++) { Vector3 point = rageSpline.GetPosition(i); point += new Vector3(Random.Range(shuffleAmount*-0.5f, shuffleAmount*0.5f), Random.Range(shuffleAmount*-0.5f, shuffleAmount*0.5f), 0f); rageSpline.SetPoint(i, point); } }