// setup everything IEnumerator setup() { mInitState = 1; mGenerators = new CurvyGenerator[Sections]; // Add some start CP's to the spline TrackSpline.Add().position = Vector3.zero; TrackSpline.Add().position = new Vector3(0, 0, CPStepSize); mDir = Vector3.forward; int num = TailCP + HeadCP + Sections * SectionCPCount - 1; for (int i = 0; i < num; i++) { addTrackCP(); } TrackSpline.Refresh(); // setting anchors so new CP's won't change orientation of existing ones for (int i = TailCP; i < TrackSpline.ControlPointCount - HeadCP; i += SectionCPCount) { TrackSpline.ControlPoints[i].OrientationAnchor = true; } // build Curvy Generators for (int i = 0; i < Sections; i++) { mGenerators[i] = buildGenerator(); mGenerators[i].name = "Generator " + i; } // and wait until they're initialized for (int i = 0; i < Sections; i++) { while (!mGenerators[i].IsInitialized) { yield return(0); } } // let all generators do their extrusion for (int i = 0; i < Sections; i++) { updateSectionGenerator(mGenerators[i], i * SectionCPCount + TailCP, (i + 1) * SectionCPCount + TailCP); } mInitState = 2; mUpdateIn = SectionCPCount; // finally place the controller Controller.AbsolutePosition = TrackSpline.ControlPoints[TailCP + 2].Distance; Controller.InitialPosition = TrackSpline.ControlPoints[TailCP + 2].Distance; // just to be sure }
public void CreateControlPoint( ) { CurvySplineSegment prevPoint = _spline.ControlPoints [_spline.ControlPointCount - 1]; _spline.Add(); CurvySplineSegment newPoint = _spline.ControlPoints [_spline.ControlPointCount - 1]; float angle = Mathf.Sin(Time.timeSinceLevelLoad); Vector3 axis = prevPoint.transform.forward; int distance = 4; //not working?? //_offsetQuat *= Quaternion.Euler (0.0f, 0.0f, Random.Range (-4.0f, 4.0f)); //newPoint.transform.localRotation = _offsetQuat; //newPoint.transform.localEulerAngles = new Vector3( (_spline.ControlPointCount * 10.0f ) % 360.0f, newPoint.transform.localEulerAngles.y, newPoint.transform.localEulerAngles.z ); //newPoint.transform.localEulerAngles = new Vector3( 10.0f * Mathf.Sin(_spline.ControlPointCount / 2.0f), newPoint.transform.localEulerAngles.y, Mathf.Cos(Random.Range (-0.03f, 0.03f)) ); newPoint.transform.localEulerAngles = new Vector3(newPoint.transform.localEulerAngles.x, 10.0f * Mathf.Sin(_spline.ControlPointCount / 2.0f), Mathf.Cos(Random.Range(-0.03f, 0.03f))); newPoint.transform.position = prevPoint.transform.position + newPoint.transform.forward * distance; //draw red debug lines out Vector3 rightPt = new Vector3(newPoint.transform.position.x + 20.0f, newPoint.transform.position.y, newPoint.transform.position.z); Vector3 leftPt = new Vector3(newPoint.transform.position.x - 20.0f, newPoint.transform.position.y, newPoint.transform.position.z); AxKDebugLines.AddLine(newPoint.transform.position, rightPt, Color.red, 9999999.0f); AxKDebugLines.AddLine(newPoint.transform.position, leftPt, Color.red, 9999999.0f); }
// Update is called once per frame void Update() { // Add Control Point by mouseclick if (Input.GetMouseButtonDown(0)) { Vector3 p = Input.mousePosition; p.z = 10; p = Camera.main.ScreenToWorldPoint(p); mSpline.Add(p); // remove segments we've already passed if (RemoveUnusedSegments) { var current = Walker.CurrentSegment; if (current) { // If we're not on the first segment, we can savely remove some Control Points! // NOTE: While we usually would have used current.SegmentIndex to make it work with disabled AutoEndTangents (where the first CP isn't the first segment), // we use ControlPointIndex here for single good reason: // We don't want to refresh the spline each time we delete a segment, but just at the end of the deletion process. Deleting a ControlPoint resets the segment hierarchy // (it will be repopulated during Refresh), so we can't use SegmentIndex nor mSpline[n] to access the first segment in the deletion loop. int curIdx = current.ControlPointIndex; int toDelete = curIdx - 2; // keep two extra segments to prevent curve change of active segment if (toDelete > 0) { for (int i = 0; i < toDelete; i++) { mSpline.Delete(mSpline.ControlPoints[0], false); // don't refresh! Warning: This resets Segment info of the spline! } mSpline.RefreshImmediately(); // now refresh the spline } } } } }
void addRandomCP(ref CurvySpline spline, int count, int totalLength) { Vector3[] pos = new Vector3[count]; float segLength = totalLength / (float)(count - 1); pos[0] = Vector3.zero; for (int i = 1; i < count; i++) { int dir = Random.Range(0, 2); int sign = Random.Range(0f, 1f) > 0.5f ? 1 : -1; switch (dir) { case 0: pos[i] = pos[i - 1] + new Vector3(segLength * sign, 0, 0); break; case 1: pos[i] = pos[i - 1] + new Vector3(0, segLength * sign, 0); break; case 2: pos[i] = pos[i - 1] + new Vector3(0, 0, segLength * sign); break; } } spline.Add(pos); spline.Refresh(); }
static void CreateCurvySpline() { CurvySpline spl = null; // If there's a Default Prefab, use that GameObject def = Resources.Load("DefaultCurvySpline") as GameObject; if (def) { GameObject splGO = PrefabUtility.InstantiatePrefab(def) as GameObject; if (splGO) { PrefabUtility.DisconnectPrefabInstance(splGO); splGO.name = "Curvy Spline"; spl = splGO.GetComponent <CurvySpline>(); } } if (spl == null) { // Otherwise create a spline from scratch spl = CurvySpline.Create(); spl.Interpolation = CurvyInterpolation.CatmulRom; spl.AutoEndTangents = true; spl.Closed = true; spl.Add(new Vector3(-2, -1, 0), new Vector3(0, 2, 0), new Vector3(2, -1, 0)); } Selection.activeObject = spl; Undo.RegisterCreatedObjectUndo(spl.gameObject, "Create Spline"); }
public void CreateControlPoint(Transform playerTransform) { _spline.Add(); CurvySplineSegment newPoint = _spline.ControlPoints [_spline.ControlPointCount - 1]; newPoint.transform.localEulerAngles = new Vector3(playerTransform.localEulerAngles.x, playerTransform.localEulerAngles.y, playerTransform.localEulerAngles.z); newPoint.transform.position = playerTransform.position; }
public void StartATL() { _spline = GameObject.FindObjectOfType <CurvySpline> (); _spline.Add(); for (int i = 0; i < 10; i++) { //CreateControlPoint (); } }
static void CreateCurvySpline() { CurvySpline spl = CurvySpline.Create(); spl.Interpolation = CurvyInterpolation.CatmulRom; spl.AutoEndTangents = true; spl.Closed = true; spl.Add(new Vector3(-2, -1, 0), new Vector3(0, 2, 0), new Vector3(2, -1, 0)); Selection.activeObject = spl; }
void addCP() { mAngleStep = Mathf.PI * 2 / (CPCount + CPCount * 0.25f); var cp = mSpline.Add(); cp.localPosition = new Vector3(Mathf.Sin(mCurrentAngle) * Radius, Mathf.Cos(mCurrentAngle) * Radius, 0); mCurrentAngle = Mathf.Repeat(mCurrentAngle + mAngleStep, Mathf.PI * 2); }
// Add a Control Point and set it's position CurvySplineSegment addCP(Vector3 mousePos) { Vector3 p = Camera.main.ScreenToWorldPoint(mousePos); p.y *= -1; // flip Y to get the correct world position var cp = mSpline.Add(); cp.localPosition = p; InfoText.text = "Control Points: " + mSpline.ControlPointCount.ToString(); // set info text return cp; }
CurvySpline CreateSpline(CurvyInterpolation type, int points, int granularity) { CurvySpline spl = CurvySpline.Create(); spl.Interpolation = type; spl.Granularity = granularity; spl.Closed = false; spl.ShowGizmos = false; spl.AutoEndTangents = true; for (int i = 0; i < points; i++) { spl.Add(null, false).Position = Random.insideUnitCircle * 10; } spl.RefreshImmediately(); return(spl); }
public CurvySplineSegment Deserialize(CurvySpline spline, CurvySerializationSpace space = CurvySerializationSpace.WorldSpline) { if (spline != null) { if (spline.ControlPointCount > 0) { return(Deserialize(spline.ControlPoints[spline.ControlPointCount - 1], space)); } else { var cp = spline.Add(); #if UNITY_EDITOR Undo.RegisterCreatedObjectUndo(cp.gameObject, "Deserialize"); #endif DeserializeInto(cp, space); return(cp); } } return(null); }
// Update is called once per frame void Update() { // Add Control Point by mouseclick if (Input.GetMouseButtonDown(0)) { Vector3 p = Input.mousePosition; p.z = 10; p = Camera.main.ScreenToWorldPoint(p); mSpline.Add(p); // remove the oldest segment, if it's no longer used if (RemoveUnusedSegments) { var seg = mSpline.DistanceToSegment(Walker.Distance); if (seg != mSpline[0]) { Walker.Distance -= mSpline[0].Length; mSpline.Delete(mSpline[0]); } } } }
/// <summary> /// Resizes the spline to have a certain number of Control Points /// </summary> /// <param name="count">number of Control Points</param> protected void PrepareControlPoints(int count) { /* * Spline.Clear(); * while (count-- > 0) * Spline.Add(); * Spline.Refresh(); */ int delta = count - Spline.ControlPointCount; bool upd = delta != 0; while (delta > 0) { Spline.Add(); delta--; } while (delta < 0) { Spline.Delete(Spline.LastVisibleControlPoint); delta++; } // Revert to default settings for (int i = 0; i < Spline.ControlPoints.Count; i++) { Spline.ControlPoints[i].Reset(); var mcg = Spline.ControlPoints[i].GetMetadata <MetaCGOptions>(); if (mcg) { mcg.Reset(); } } if (upd) { Spline.SetDirtyAll(); Spline.Refresh(); } }
void addRandomCP(ref CurvySpline spline, int count, int totalLength) { Vector3[] pos=new Vector3[count]; float segLength = totalLength / (float)(count - 1); pos[0] = Vector3.zero; for (int i = 1; i < count; i++) { int dir = Random.Range(0, 2); int sign=Random.Range(0f,1f)>0.5f ? 1 : -1; switch (dir) { case 0: pos[i] = pos[i - 1] + new Vector3(segLength * sign, 0, 0); break; case 1: pos[i] = pos[i - 1] + new Vector3(0,segLength * sign,0); break; case 2: pos[i] = pos[i - 1] + new Vector3(0,0,segLength * sign); break; } } spline.Add(pos); spline.Refresh(); }
public void StartATL() { _spline = GameObject.FindObjectOfType <CurvySpline> (); _spline.Add(); }
public void AddCP() { checkForSpline(); Spline.Add(Random.insideUnitCircle * 50); }