public List <Vector3> GetEdgePoints() { //Build the point list and calculate curves var points = new List <Vector3>(); for (int i = 0; i < keyPoints.Count; i++) { if (isCurve[i]) { //Get the curve control point var a = keyPoints[i]; var c = keyPoints[(i + 1) % keyPoints.Count]; var b = Bezier.Control(a, c, curvePoints[i]); //Build the curve var count = Mathf.Ceil(1 / curveDetail); for (int j = 0; j < count; j++) { var t = (float)j / count; points.Add(Bezier.Curve(a, b, c, t)); } } else { points.Add(keyPoints[i]); } } return(points); }
int NearestLine(out Vector3 position) { var near = -1; var nearDist = float.MaxValue; position = keyPoints[0]; var linePos = Vector3.zero; for (int i = 0; i < keyPoints.Count; i++) { var j = (i + 1) % keyPoints.Count; var line = keyPoints[j] - keyPoints[i]; var offset = mousePosition - keyPoints[i]; var dot = Vector3.Dot(line.normalized, offset); if (dot >= 0 && dot <= line.magnitude) { if (isCurve[i]) { linePos = Bezier.Curve(keyPoints[i], Bezier.Control(keyPoints[i], keyPoints[j], curvePoints[i]), keyPoints[j], dot / line.magnitude); } else { linePos = keyPoints[i] + line.normalized * dot; } var dist = Vector3.Distance(linePos, mousePosition); if (dist < nearDist) { nearDist = dist; position = linePos; near = i; } } } return(near); }
// Start is called before the first frame update void Start() { // define the points array size points = new Vector3[line.positionCount]; // get all vertices from line renderer and put it inside array line.GetPositions(points); // convert array into list foreach (Vector3 item in points) { listPoints.Add(item); } // draw curve line for (float i = 0; i <= 1; i = i + 0.01f) { Vector3 p = Bezier.Curve(listPoints, i); curveLine.Add(p); } curvePoints = new Vector3[curveLine.Count]; for (int i = 0; i < curveLine.Count; i++) { curvePoints[i] = curveLine[i]; } selfLine = GetComponent <LineRenderer>(); selfLine.positionCount = curveLine.Count; selfLine.SetPositions(curvePoints); }
void Update() { if (initialize == true) //messy solution to only start tController when initialize is set to true { //by external script. Better if it could be more self-sufficient if (once != true) { bezUpdate(); once = true; } if (butterState == state.flying) { // speed smoothing operations -- might look into using smooth.Dampen t += Time.deltaTime * lerpTune; speed = Mathf.Lerp(previousSpeed, originalSpeed / pathGen.travelRange * speedToRangeRatio, Mathf.Clamp(t, 0, 1)); //copy of speed so calculations don't build on up eachother current_speed = Vector3.Magnitude(followPath.transform.position - lastLoc) * 100; //distance between position at last frame and position at this frame * 100 for 1 decimal range lastLoc = followPath.transform.position; //setting previous position for next frame as current position tObject += Time.deltaTime * speed / 10 * globalSpeed; // division for precision if (Mathf.Round(tObject * 100) / 100 >= 1) //rounding t value to 2 decimal places { pathGen.newCurvePoints(); int i = 0; while (boundsBox.bounds.Contains(pathGen.pth.P4) == false) //this could use some behavior that diverts butter path in opposite direction than just tried { i++; pathGen.newCurvePoints(); if (i > 10) { Debug.LogError("tried too many times to path butter"); break; } } bezUpdate(); tObject -= tObject; t = 0.0f; previousSpeed = speed; } if (followPath != null) { if (curveTangent == true) { followPath.transform.position = bez.CurveTangentLine(tObject); } else if (curveTangentPerpendicular == true) { followPath.transform.position = Quaternion.Euler(0, 0, 90) * bez.CurveTangentLine(tObject); //follow line perpendicular to curve tangent line } else { tVecPrevious = followPath.transform.position; //storing previous position vector followPath.transform.position = bez.Curve(tObject); //assigning new position vector based on curve function followPath.transform.LookAt(followPath.transform.position + (followPath.transform.position - tVecPrevious)); //subtracting one from the other and adding to current to find facing vector/direction and assigning lookAt vector } } } } }
public void SegTest() { int subDivX = 3; float Xincrement = 1f / (float)subDivX; Bezier bez = new Bezier(); seg = new TreeSeg(subDivX, 6, bez); //Assert.AreEqual(seg.ringVerts[0].curvePoint.x, bez.Curve(Xincrement).x); //Assert.AreEqual(seg.ringVerts[0].curvePoint.y, bez.Curve(Xincrement).y); //Assert.AreEqual(seg.ringVerts[0].curvePoint.z, bez.Curve(Xincrement).z); Assert.AreEqual(seg.ringVerts[0].curvePoint.x, 0); Assert.AreEqual(seg.ringVerts[0].curvePoint.y, 0); Assert.AreEqual(seg.ringVerts[0].curvePoint.z, 0); Assert.AreEqual(seg.ringVerts[1].curvePoint.x, bez.Curve(Xincrement).x); Assert.AreEqual(seg.ringVerts[1].curvePoint.y, bez.Curve(Xincrement).y); Assert.AreEqual(seg.ringVerts[1].curvePoint.z, bez.Curve(Xincrement).z); }
void DrawSegment(int index) { var from = keyPoints[index]; var to = keyPoints[(index + 1) % keyPoints.Count]; if (isCurve[index]) { var control = Bezier.Control(from, to, curvePoints[index]); var count = Mathf.Ceil(1 / polyMesh.curveDetail); for (int i = 0; i < count; i++) { Handles.DrawLine(Bezier.Curve(from, control, to, i / count), Bezier.Curve(from, control, to, (i + 1) / count)); } } else { Handles.DrawLine(from, to); } }
/// <summary> /// Treeseg params /// </summary> /// <param name="subDivX"> divisions of curve</param> /// <param name="subDivY"> number of points on rings</param> /// <param name="bezPath"></param> public TreeSeg(int subDivX, int subDivY, Bezier bezPath) { TreeRing[] segment = new TreeRing[subDivX]; float Xincrement = 1f / (float)subDivX; // increments along Segment curve for (int i = 0; i <= subDivX - 1; i++) // POSSIBLE PROBLEM WITH TWISTED FIRST { Vector3 curvePoint = bezPath.Curve(i * Xincrement); Vector3 curvePerpendicularTangent = Vector3.Cross(bezPath.CurveTangentVectorN(i * Xincrement), new Vector3(1, 0, 0)); //Vector3 curvePerpendicularTangent = Quaternion.Euler(90, 0, 0) * bezPath.CurveTanNVec(); //Debug.Log(curvePoint);//find out what value this give me segment[i] = new TreeRing(subDivY, curvePoint, curvePerpendicularTangent); } bezierPath = bezPath; ringVerts = segment; ringsNum = ringVerts.Length; vertsInRing = ringVerts[0].verts.Length; vertsInSeg = ringsNum * vertsInRing; }
// Update is called once per frame void Update() { dot.transform.position = Bezier.Curve(listPoints, time); }