void Awake() { var controlPoint = BezierUtils.GetControlPoint(targe1.position, targe2.position, hight); targe3.position = controlPoint; path = BezierUtils.GetBeizerList(targe1.position, controlPoint, targe2.position); }
/* * public class LList { * public List<Image>[] llist; * } */ public List <Image> Dline(GameObject btnStart, GameObject btnEnd, int segmentNum) { Vector3 startPos = btnStart.transform.localPosition; Vector3 endPos = btnEnd.transform.localPosition; Vector3 controlPos = new Vector3(0, 0, 0); if (endPos.y != 0) { controlPos = new Vector3(startPos.x, endPos.y, 0); } //if(startPos.y != 0) //controlPos = new Vector3(endPos.x, startPos.y, 0); else { controlPos = new Vector3(endPos.x, endPos.y, 0); } // GameObject.Find("BezierUtils").SendMessage("GetBeizerList"); Vector3[] path = BezierUtils.GetBeizerList(startPos, controlPos, endPos, segmentNum); // drawline(startPos, path[1]); for (int i = 1; i < segmentNum; i++) { Image lineObjImg = Drawline(path[i], path[i + 1]); LineList.Add(lineObjImg); } return(LineList); }
protected override void MoveUpdate() { if (Vector2.SqrMagnitude(moveUnit) > UnityExtensions.ThresholdPosition) { if (Vector2.SqrMagnitude(targetPosition - (Vector2)transform.position) > UnityExtensions.ThresholdPosition) { bezierTime += Time.deltaTime / Vector2.Distance(startPosition, targetPosition) * Config.Speed; if (bezierTime > 1) { bezierTime = 0; } transform.position = BezierUtils.Bezier2(startPosition, BezierUtils.ControlPoint(startPosition, targetPosition), targetPosition, bezierTime); //linear moving. can be used for testing // transform.position = new Vector2(transform.position.x + moveUnit.x * Time.deltaTime, // transform.position.y + moveUnit.y * Time.deltaTime); transform.Rotate(Vector3.forward, Config.RotateParameter * Time.deltaTime * 100); } else { // todo при любом кидании уничтожается - плохо Destroy(); } } }
void DrawThreePowerCurve() { // 获取三次贝塞尔方程曲线 points3 = BezierUtils.GetThreePowerBeizerList(controlPoints[0].position, controlPoints[1].position, controlPoints[2].position, controlPoints[3].position, _segmentNum); // 设置 LineRenderer 的点个数,并赋值点值 lineRenderer.positionCount = (_segmentNum); lineRenderer.SetPositions(points3); }
/// <summary> /// 贝赛尔曲线初始化 /// </summary> /// <param name="startPoint"></param> /// <param name="endPoint"></param> public virtual void Init(Transform startPoint, Transform endPoint) { // _EndPos = endPoint.position; var controlPoint = BezierUtils.GetControlPoint(startPoint.position, endPoint.position, hight); path = BezierUtils.GetBeizerList(startPoint.position, controlPoint, endPoint.position); isMove = true; }
/** * Draw the bezier path editor */ override public void OnInspectorGUI() { EditorGUILayout.LabelField("Curves", m_path.GetCountString()); EditorGUILayout.LabelField("Length", m_path.GetLengthString()); EditorGUILayout.Separator(); EditorGUILayout.LabelField("Add Bezier Curve"); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add Linear")) { Undo.RegisterSceneUndo("Add Linear"); m_path.AddCurve(BezierCurve.LINEAR); } if (GUILayout.Button("Add Quadratic")) { Undo.RegisterSceneUndo("Add Quadratic"); m_path.AddCurve(BezierCurve.QUADRATIC); } if (GUILayout.Button("Add Cubic")) { Undo.RegisterSceneUndo("Add Cubic"); m_path.AddCurve(BezierCurve.CUBIC); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); EditorGUILayout.LabelField("Connect Curves"); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Copy Begin Points")) { Undo.RegisterSceneUndo("Copy Begin Points"); BezierUtils.CopyBeginPoints(m_path.GetCurvesArray()); } if (GUILayout.Button("Close Gaps")) { Undo.RegisterSceneUndo("Close Gaps"); BezierUtils.CloseGaps(m_path.GetCurvesArray()); } if (GUILayout.Button("Copy End Points")) { Undo.RegisterSceneUndo("Copy End Points"); BezierUtils.CopyEndPoints(m_path.GetCurvesArray()); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Update curve length")) { Undo.RegisterSceneUndo("Update curve length"); BezierUtils.UpdateLength(m_path); } EditorGUILayout.EndHorizontal(); }
/// <summary> /// 绘制曲线. /// </summary> private Vector3[] DrawWire(Vector3 controlPos, int segments) { Vector3[] bezierPoses = BezierUtils.GetBeizerList(startPos.position, controlPos, endPos.position, segments); lr.positionCount = bezierPoses.Length; for (int i = 0; i <= bezierPoses.Length - 1; i++) { lr.SetPosition(i, bezierPoses[i]); } return(bezierPoses); }
private void Draw3JLine() { Gizmos.color = Color.black; Vector3 prev = posList[0]; for (float dist = 0; dist <= 1; dist += tDelta) { Vector3 next = BezierUtils.Draw3JBezierCurve(posList[0], posList[1], posList[2], dist); Gizmos.DrawLine(prev, next); prev = next; } Gizmos.DrawLine(prev, BezierUtils.Draw3JBezierCurve(posList[0], posList[1], posList[2], 1)); }
private void SpawnDrawCurveModeCurve(bool smoothAcuteAngles) { var p0 = newCurvePoints[0]; var p3 = newCurvePoints[3]; BezierUtils.GetInverseControlPoints(p0, p3, newCurvePoints[1], newCurvePoints[2], EditorState.DrawCurveFirstPointHook, EditorState.DrawCurveSecondPointHook, out var p1, out var p2); if (smoothAcuteAngles) { EditorState.CurrentSpline.SetControlPointMode(EditorState.CurrentSpline.PointsCount - 1, BezierControlPointMode.Aligned); } EditorState.CurrentSpline.AppendCurve(p1, p2, p3, BezierControlPointMode.Free, false); }
IEnumerator ShowQiqiu() { qiqiu.gameObject.SetActive(true); Vector3 endPosition = new Vector3(qiqiu.GetComponent <RectTransform>().position.x, 1300, qiqiu.GetComponent <RectTransform>().position.x); Vector3 controlerPosition = qiqiu.GetComponent <RectTransform>().position + (endPosition - qiqiu.GetComponent <RectTransform>().position) * 0.5f + new Vector3(500, 0, 0); Vector3[] positionArr = BezierUtils.GetBeizerList(qiqiu.GetComponent <RectTransform>().position, controlerPosition, endPosition, 80); for (int i = 0; i < positionArr.Length; i++) { qiqiu.GetComponent <RectTransform>().position = positionArr[i] - new Vector3(0, 0, positionArr[i].z); yield return(new WaitForSeconds(playSecond)); } qiqiu.gameObject.SetActive(false); IsStand = true; yield return(null); }
/// <summary> /// 初始化警告公告牌下方的流动线路、以及模拟机柜、机柜节点线路 /// </summary> private void InitLine() { //警告公告牌下方的流动线路 Transform triangleLabel = transform.Find("TriangleLabel"); Line line = triangleLabel.Find("Line").GetComponent <Line>(); //确定坐标,并重新初始化Line Vector3 triangleLabelPosition = triangleLabel.position; triangleLabelPosition.y = 0.1F; List <Vector3> linePoints = new List <Vector3>(); linePoints.Add(new Vector3(triangleLabelPosition.x + 1, triangleLabelPosition.y, triangleLabelPosition.z + 1)); linePoints.Add(new Vector3(triangleLabelPosition.x + 1, triangleLabelPosition.y, triangleLabelPosition.z - 1)); linePoints.Add(new Vector3(triangleLabelPosition.x - 1, triangleLabelPosition.y, triangleLabelPosition.z - 1)); linePoints.Add(new Vector3(triangleLabelPosition.x - 1, triangleLabelPosition.y, triangleLabelPosition.z + 1)); linePoints.Add(new Vector3(triangleLabelPosition.x + 1, triangleLabelPosition.y, triangleLabelPosition.z + 1)); line.points = linePoints.ToArray(); line.InitLine(); //模拟机柜、机柜节点线路 Vector3 rackPosition = transform.Find("Rack").position; //第一条 Vector3[] rackNode1Pos = BezierUtils.GetBeizerList(transform.Find("RackNode1").position, rackPosition, 20, 2); GameObject lineGameObject1 = GameObject.Instantiate(linePrefab, transform, true); Line line1 = lineGameObject1.GetComponent <Line>(); line1.points = rackNode1Pos; line1.InitLine(); //第二条 Vector3[] rackNode2Pos = BezierUtils.GetBeizerList(transform.Find("RackNode2").position, rackPosition, 20, 2); GameObject lineGameObject2 = GameObject.Instantiate(linePrefab, transform, true); Line line2 = lineGameObject2.GetComponent <Line>(); line2.points = rackNode2Pos; line2.InitLine(); }
private void OnGUI() { if (GUI.Button(new Rect(50, 0, 100, 20), "测试1")) { //new CombinationUtils().test(); Debug.Log(BezierUtils.Draw4JBezierCurve(posList[0], posList[1], posList[2], posList[3], 0.2f)); Debug.Log(BezierUtils.DrawNJBezierCurve(posList.GetRange(0, 4).ToArray(), 0.2f)); Debug.Log(BezierUtils.DrawCatmullRomByBezier(posList[0], posList[1], posList[2], posList[3], 0.2f)); Debug.Log(BezierUtils.DrawCatmullRom(posList[0], posList[1], posList[2], posList[3], 0.2f)); } if (GUI.Button(new Rect(50, 30, 100, 20), "测试2")) { new CombinationUtils().test2(); } if (GUI.Button(new Rect(50, 60, 100, 20), "划线")) { LR.positionCount = 2; LR.SetPosition(0, posList[0]); LR.SetPosition(1, posList[posList.Count - 1]); } }
private void VisualizeDrawCurveModeCurve() { var p0 = newCurvePoints[0]; var p3 = newCurvePoints[3]; if (Vector3.Distance(p0, p3) < EditorState.DrawCurveSegmentLength * DrawCurveMinLengthToVisualize) { return; } BezierUtils.GetInverseControlPoints(p0, p3, newCurvePoints[1], newCurvePoints[2], EditorState.DrawCurveFirstPointHook, EditorState.DrawCurveSecondPointHook, out var p1, out var p2); p0 = handleTransform.TransformPoint(p0); p1 = handleTransform.TransformPoint(p1); p2 = handleTransform.TransformPoint(p2); p3 = handleTransform.TransformPoint(p3); Handles.DrawBezier(p0, p3, p1, p2, EditorSettings.DrawerModeCurveColor, null, EditorSettings.SplineWidth * 1.5f); if (EditorState.DrawPoints) { if (firstControlPointSet) { var f = handleTransform.TransformPoint(newCurvePoints[1]); var size = EditorSettings.ScaleDrawerHandleOnScreen ? HandleUtility.GetHandleSize(f) : 1f; Handles.color = EditorSettings.DrawModePointColor; Handles.CubeHandleCap(0, f, Quaternion.identity, size * EditorSettings.MainPointSize, EventType.Repaint); } if (secondControlPointSet) { var g = handleTransform.TransformPoint(newCurvePoints[2]); var size = EditorSettings.ScaleDrawerHandleOnScreen ? HandleUtility.GetHandleSize(g) : 1f; Handles.color = EditorSettings.DrawModePointColor; Handles.CubeHandleCap(0, g, Quaternion.identity, size * EditorSettings.MainPointSize, EventType.Repaint); } } }
public override void OnInspectorGUI() { //base.OnInspectorGUI(); var tool = target as ShapeEditorTool; var shape = tool.target as Shape; if (shape == null || shape.ShapeData == null) { return; } EditorGUIUtility.wideMode = true; EditorGUIUtility.labelWidth = 100; EditorGUILayout.LabelField($"Selected point", $"{tool.currentPointId}"); int currentPointId = tool.currentPointId; if (shape == null || shape.ShapeData == null) { return; } var shapeData = shape.ShapeData; if (currentPointId >= 0 && currentPointId < shapeData.GetPolyPointCount()) { Color strokeColor = shapeData.GetPolyStrokeColor(currentPointId); float strokeWidth = shapeData.GetPolyStrokeWidth(currentPointId); Vector3 position = shapeData.GetPolyPosition(currentPointId); Vector3 tangentIn = shapeData.GetPolyInTangent(currentPointId); Vector3 tangentOut = shapeData.GetPolyOutTangent(currentPointId); ShapePointType pointType = shapeData.GetPolyPointType(currentPointId); EditorGUI.BeginChangeCheck(); ShapePointType oldType = pointType; pointType = (ShapePointType)EditorGUILayout.EnumPopup("Point Type", pointType); if (oldType == ShapePointType.Corner && pointType == ShapePointType.Bezier) { shapeData.SetPolyInTangent(currentPointId, ShapeEditorUtils.GetDefaultInTangent(shapeData, currentPointId)); shapeData.SetPolyOutTangent(currentPointId, ShapeEditorUtils.GetDefaultOutTangent(shapeData, currentPointId)); } if (shapeData.PolyDimension == ShapePolyDimension.TwoDimensional) { position = EditorGUILayout.Vector2Field("Position", position); } else { position = EditorGUILayout.Vector3Field("Position", position); } if (pointType == ShapePointType.Bezier || pointType == ShapePointType.BezierContinous) { if (shapeData.PolyDimension == ShapePolyDimension.TwoDimensional) { tangentIn = EditorGUILayout.Vector2Field("In Tangent", tangentIn); tangentOut = EditorGUILayout.Vector2Field("Out Tangent", tangentOut); } else { tangentIn = EditorGUILayout.Vector3Field("In Tangent", tangentIn); tangentOut = EditorGUILayout.Vector3Field("Out Tangent", tangentOut); } } if (shapeData.HasVariableStrokeColor) { strokeColor = EditorGUILayout.ColorField("Stroke Color", strokeColor); } if (shapeData.HasVariableStrokeWidth) { strokeWidth = EditorGUILayout.FloatField("Stroke Width", strokeWidth); } EditorGUI.indentLevel--; if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(shape.dataContainerObject, "Edit Point"); if (shapeData.HasVariableStrokeColor) { shapeData.SetPolyStrokeColor(currentPointId, strokeColor); } if (shapeData.HasVariableStrokeWidth) { strokeWidth = Mathf.Max(0, strokeWidth); shapeData.SetPolyStrokeWidth(currentPointId, strokeWidth); } shapeData.SetPolyPosition(currentPointId, position); shapeData.SetPolyPointType(currentPointId, pointType); shapeData.SetPolyInTangent(currentPointId, tangentIn); shapeData.SetPolyOutTangent(currentPointId, tangentOut); ShapeEditorUtils.SetDataDirty(shape); } GUILayout.BeginHorizontal(); if (GUILayout.Button("Add point")) { Undo.RecordObject(shape.dataContainerObject, "Add point"); Debug.Log("add point"); var nextId = currentPointId + 1; nextId = shapeData.IsPolygonStrokeClosed ? MathUtils.CircularModulo(nextId, shapeData.GetPolyPointCount()) : nextId; var curPos = shapeData.GetPolyPosition(currentPointId); var nextPos = nextId < shapeData.GetPolyPointCount() ? shapeData.GetPolyPosition(nextId) : curPos + Vector3.right; var curOutTangent = shapeData.GetPolyOutTangent(currentPointId); var nextInTangent = nextId < shapeData.GetPolyPointCount() ? shapeData.GetPolyInTangent(nextId) : -curOutTangent; var pos = BezierUtils.GetPointOnBezierCurve(0.5f, curPos, curPos + curOutTangent, nextPos + nextInTangent, nextPos); var tan = BezierUtils.GetTangentOnBezierCurve(0.5f, curPos, curPos + curOutTangent, nextPos + nextInTangent, nextPos); shapeData.InsertPolyPoint(nextId); shapeData.SetPolyPosition(nextId, pos); if (shapeData.GetPolyPointType(currentPointId) == ShapePointType.Corner && shapeData.GetPolyPointType(nextId) == ShapePointType.Corner) { } else { shapeData.SetPolyPointType(ShapePointType.BezierContinous); shapeData.SetPolyInTangent(nextId, -tan * .2f); shapeData.SetPolyOutTangent(nextId, tan * .2f); } currentPointId = nextId; ShapeEditorUtils.SetDataDirty(shape); } if (currentPointId != -1 && GUILayout.Button("Remove point")) { Undo.RecordObject(shape.dataContainerObject, "Remove point"); shapeData.RemovePolyPoint(currentPointId); ShapeEditorUtils.SetDataDirty(shape); } GUILayout.EndHorizontal(); } }
public static void SubdivideVertexInfoList(List <ShapeVertexInfo> vertexList, List <ShapeVertexInfo> vertexListSubdivided) { vertexListSubdivided.Clear(); if (vertexList.Count < 2) { return; } for (int i = 0; i < vertexList.Count - 1; i++) { ShapeVertexInfo v1 = vertexList[i]; ShapeVertexInfo v2 = vertexList[i + 1]; if (v1.type == ShapePointType.Corner && v2.type == ShapePointType.Corner) { //Debug.Log ("corner " + i); vertexListSubdivided.Add(v1); if (i == vertexList.Count - 2) // only draw last point when at end of shape { vertexListSubdivided.Add(v2); } continue; } BezierUtils.SubdivideBezier(v1.position, v1.position + v1.outTangent, v2.position + v2.inTangent, v2.position, subdivisionLerpTemp, subdivisionPointsTemp, subdivisionMaxAngle, subdivisionMinDist); int lastSubdivPointId = subdivisionLerpTemp.Count - 1; int firstDrawPoint = 0; int lastDrawPoint = lastSubdivPointId; if (i < vertexList.Count - 2) // hide last point except when at end of shape { lastDrawPoint -= 1; } if (i == 0) { firstDrawPoint = lastDrawPoint; } if (i == vertexList.Count - 2) { lastDrawPoint = firstDrawPoint + 1; } //Debug.Log ("bezier "+i + " min " + firstDrawPoint + " max " + lastDrawPoint + " subdivcount "+subdivisionLerp.Count); for (int j = firstDrawPoint; j <= lastDrawPoint; j++) { float lerp = Mathf.InverseLerp(0, lastSubdivPointId, j); ShapeVertexInfo v = v1; v.position = subdivisionPointsTemp[j]; v.strokeWidth = Mathf.Lerp(v1.strokeWidth, v2.strokeWidth, lerp); v.strokeColor = Color.Lerp(v1.strokeColor, v2.strokeColor, lerp); v.posOnLine = Mathf.Lerp(v1.posOnLine, v2.posOnLine, lerp); vertexListSubdivided.Add(v); } } /*for (int i = 0; i < _vertexInfoListSubdivided.Count-1; i++) { * * Color c = Color.green; * if (i == 0) * c = Color.red; * if (i == _vertexInfoListSubdivided.Count - 2) * c = Color.blue; * Debug.DrawLine (_vertexInfoListSubdivided [i].position, _vertexInfoListSubdivided [i + 1].position,c); * }*/ }