public static CalculateBezier ( Vector3 p0, Vector3 p1, Vector3 cp0, Vector3 cp1, float t ) : Vector3 | ||
p0 | Vector3 | |
p1 | Vector3 | |
cp0 | Vector3 | |
cp1 | Vector3 | |
t | float | |
리턴 | Vector3 |
public override void AddPoint(Vector3 point) { //base.AddPoint(Point); if (m_AllSubKeyPathpoints.Count == 0) { QuadPoint _firstPoint = CreateFirstMeshPoint(point) as QuadPoint; m_AllSubKeyPathpoints.Add(_firstPoint); return; } QuadPoint previousQuaPoint = m_AllSubKeyPathpoints[m_AllSubKeyPathpoints.Count - 1] as QuadPoint; //上一个Key路径点 Vector3 direction = point - previousQuaPoint.Point_Center; //两个Key路径点的向量 if (m_AllSubKeyPathpoints.Count == 1) { //第一个点的方向有第二个点决定 previousQuaPoint.CenterLeftDirNor = new Vector3(-1, 0, 0); m_AllSubMeshPoints.Add(previousQuaPoint.GetQuadRight()); m_AllSubMeshPoints.Add(previousQuaPoint.GetQuadLeft()); } Vector3 controllPoint1 = VectorExpand.GetPointPerpendicularTo(previousQuaPoint.GetQuadLeft(), previousQuaPoint.Point_Center, point); //获得垂直于上一个QuadPoint 点且在direction 平面的单位控制点 List <QuadPoint> subQuadPoint = new List <QuadPoint>(); // 子节点Point for (int dex = 0; dex < previousQuaPoint.SubsectionCount; dex++) { Vector3 _subPoint = Curve.CalculateBezier(previousQuaPoint.Point_Center, point, controllPoint1, (dex + 1) * 1f / (previousQuaPoint.SubsectionCount)); //根据二阶贝塞尔曲线获得 一个分割点 QuadPoint currentSubPoint; int previousStartIndex = m_AllSubMeshPoints.Count - 2; if (dex == 0) { currentSubPoint = CreateSubKeyPoint(previousQuaPoint, _subPoint) as QuadPoint; } else { currentSubPoint = CreateSubKeyPoint(subQuadPoint[subQuadPoint.Count - 1], _subPoint) as QuadPoint; } subQuadPoint.Add(currentSubPoint); m_AllSubTrangles.Add(previousStartIndex); m_AllSubTrangles.Add(previousStartIndex + 1); m_AllSubMeshPoints.Add(currentSubPoint.GetQuadRight()); m_AllSubMeshPoints.Add(currentSubPoint.GetQuadLeft()); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); //上一个Right,上一个Left,当前的Left m_AllSubTrangles.Add(previousStartIndex); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); //上一个Right,当前的Left,但情感的Left if (dex == previousQuaPoint.SubsectionCount - 1) { m_AllSubKeyPathpoints.Add(currentSubPoint); } } }
void FillJoint(VertexHelper vh, UIVertex vp0, UIVertex vp1, UIVertex[] prvLineVert, Color color, float width = -1) { Vector3 forwardWidthVector = vp1.position - vp0.position; Vector3 prvWidthVector = prvLineVert[1].position - prvLineVert[0].position; Vector3 prvVector = Vector3.Cross(prvWidthVector, new Vector3(0, 0, 1)); Vector3 p0; Vector3 p1; Vector3 center = (vp0.position + vp1.position) / 2f; if (Vector3.Dot(prvVector, forwardWidthVector) > 0) { p0 = vp1.position; p1 = prvLineVert[1].position; } else { p0 = vp0.position; p1 = prvLineVert[0].position; } if (width < 0) { width = m_width; } Vector3 cp0 = (p0 + p1 - center * 2).normalized * width * fillRatio + center; float angle = Vector3.Angle(p0 - center, p1 - center); int currentVert = vh.currentVertCount; int divideCount = (int)(angle / fillDivideAngle); if (divideCount == 0) { divideCount = 1; } float unit = 1f / divideCount; vh.AddVert(center, color, Vector2.one * 0.5f); vh.AddVert(p0, color, Vector2.zero); for (int n = 0; n < divideCount; n++) { vh.AddVert(Curve.CalculateBezier(p0, p1, cp0, unit * (n + 1)), color, Vector2.zero); vh.AddTriangle(currentVert + 2 + n, currentVert, currentVert + 1 + n); } }
private bool CheckPointOnBezierCurve(Vector3 p0, Vector3 c0, Vector3 c1, Vector3 p1, Vector2 sp) { // % int // # double var t = CalculateMinT(p0, c0, c1, p1, sp); if (Vector3.Distance(sp, Curve.CalculateBezier(p0, p1, c0, c1, t)) < m_width / 2f) { return(true); } /* unityeditor function. * Debug.LogWarning("can't build because UnityEditor...-_-;"); * dist = UnityEditor.HandleUtility.DistancePointBezier(sp, p0, p1, c0, c1); */ return(false); }
Vector2 EvaluatePoint(LinePoint p0, LinePoint p1, float t) { //t = t * t;//보정... if (p0.isNextCurve && !p1.isPrvCurve) { return(Curve.CalculateBezier(p0.point, p1.point, p0.nextCurvePoint, t)); } if (!p0.isNextCurve && p1.isPrvCurve) { return(Curve.CalculateBezier(p0.point, p1.point, p1.prvCurvePoint, t)); } if (p0.isNextCurve && p1.isPrvCurve) { return(Curve.CalculateBezier(p0.point, p1.point, p0.nextCurvePoint, p1.prvCurvePoint, t)); } //직선의 경우. return(Vector2.Lerp(p0.point, p1.point, t)); }
private float CalculateMinT(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 sp) { float mint = 0.5f; float currentLength = float.MaxValue; //float currentDot = float.MaxValue; for (float f = 0f; f < 1f; f += 0.02f) { var p = Curve.CalculateBezier(p0, p3, p1, p2, f); var l = (p.x - sp.x) * (p.x - sp.x) + (p.y - sp.y) * (p.y - sp.y); // Debug.Log(f + " ===> " + l); if (l < currentLength) { // Debug.Log("!!!!!!!!!hit!!!!!!!!"); //var d = Curve.CalculateBezierDerivative(p0, p1, p2, p3, f); currentLength = l; mint = f; } } return(mint); }
public override void AddPoint(Vector3 point) { //base.AddPoint(point); if (m_AllSubKeyPathpoints.Count == 0) { Quad3DPoint _firstPoint = CreateFirstMeshPoint(point) as Quad3DPoint; m_AllSubKeyPathpoints.Add(_firstPoint); m_AllSubTrangles.Clear(); m_AllSubMeshPoints.Clear(); return; } Quad3DPoint previousQuaPoint = m_AllSubKeyPathpoints[m_AllSubKeyPathpoints.Count - 1] as Quad3DPoint; //上一个Key路径点 Vector3 direction = point - previousQuaPoint.Point_Center; //两个Key路径点的向量 if (m_AllSubKeyPathpoints.Count == 1) { //处理第一个Key点 //Debug.Log("Deal With The First Point"); previousQuaPoint.CenterDownUptDirNor = Vector3.Cross(new Vector3(-1, 0, 0), direction).normalized; previousQuaPoint.CenterLeftDirNor = Vector3.Cross(direction, previousQuaPoint.CenterDownUptDirNor).normalized; m_AllSubMeshPoints.Add(previousQuaPoint.GetQuadUpRight()); //Up Right m_AllSubMeshPoints.Add(previousQuaPoint.GetQuadUpLeft()); //Up left m_AllSubMeshPoints.Add(previousQuaPoint.GetQuadDownRight()); //Down Right m_AllSubMeshPoints.Add(previousQuaPoint.GetQuadDownLeft()); //Down Left m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 3); //下三角 m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 3); // m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 4); //上三角 } List <Quad3DPoint> subQuadPoint = new List <Quad3DPoint>(); // 子节点Point if (m_AllSubKeyPathpoints.Count >= 2) { Vector3 KeyPathDir1 = m_AllSubKeyPathpoints[m_AllSubKeyPathpoints.Count - 1].Point_Center - m_AllSubKeyPathpoints[m_AllSubKeyPathpoints.Count - 2].Point_Center; //上两个KeyPath Point 方向 Vector3 KeyPathDir2 = point - m_AllSubKeyPathpoints[m_AllSubKeyPathpoints.Count - 1].Point_Center; //上一个KeyPath 点到当前KeyPath Point 的方向 float angle = Vector3.Angle(KeyPathDir1, KeyPathDir2); Debug.Log("angle=" + angle); //if (angle >= 89.5f) //{ //需要绘制一个三角锥 而不是长方体 // if (point.y < AllKeyPathpoints[AllKeyPathpoints.Count - 1].Point_Center.y) // { //在下面 // } // else // { // } // return; //}//if }//if //获得左右两个面曲线的控制点 Vector3 controllPointLeftRight = VectorExpand.GetPointPerpendicularTo(previousQuaPoint.GetQuadUpLeft() - new Vector3(0, previousQuaPoint.LineHeight / 2f, 0), previousQuaPoint.Point_Center, point); //获得垂直于上一个QuadPoint 点且在direction 平面的单位控制点 for (int dex = 0; dex < previousQuaPoint.SubsectionCount; dex++) { Vector3 _subPoint = Curve.CalculateBezier(previousQuaPoint.Point_Center, point, controllPointLeftRight, (dex + 1) * 1f / (previousQuaPoint.SubsectionCount)); //根据二阶贝塞尔曲线获得 一个分割点 Quad3DPoint currentSubPoint; int previousStartIndex = m_AllSubMeshPoints.Count - 4; if (dex == 0) { currentSubPoint = CreateSubKeyPoint(previousQuaPoint, _subPoint) as Quad3DPoint; } else { currentSubPoint = CreateSubKeyPoint(subQuadPoint[subQuadPoint.Count - 1], _subPoint) as Quad3DPoint; } subQuadPoint.Add(currentSubPoint); #region Up Plane m_AllSubTrangles.Add(previousStartIndex); m_AllSubTrangles.Add(previousStartIndex + 1); m_AllSubMeshPoints.Add(currentSubPoint.GetQuadUpRight()); m_AllSubMeshPoints.Add(currentSubPoint.GetQuadUpLeft()); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); //上面 左三角 m_AllSubTrangles.Add(previousStartIndex); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); //上面 右三角 m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); #endregion #region Down Plane m_AllSubMeshPoints.Add(currentSubPoint.GetQuadDownRight()); m_AllSubMeshPoints.Add(currentSubPoint.GetQuadDownLeft()); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(previousStartIndex + 2); //下面 右倒三角以显示 m_AllSubTrangles.Add(previousStartIndex + 2); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(previousStartIndex + 3);; //下面左倒三角以显示 #endregion #region LeftPlane m_AllSubTrangles.Add(previousStartIndex + 1); m_AllSubTrangles.Add(previousStartIndex + 3); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); //左侧 到下三角 m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 3); m_AllSubTrangles.Add(previousStartIndex + 1); //左侧倒上三角 #endregion #region Right Plane m_AllSubTrangles.Add(previousStartIndex); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 4); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); m_AllSubTrangles.Add(previousStartIndex); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); m_AllSubTrangles.Add(previousStartIndex + 2); #endregion #region Forward Plane m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 2); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 4); //前倒 下三角 m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 1); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 4); m_AllSubTrangles.Add(m_AllSubMeshPoints.Count - 3); //前倒 上三角 #endregion if (dex == previousQuaPoint.SubsectionCount - 1) { m_AllSubKeyPathpoints.Add(currentSubPoint); //Debug.Log("Add Key Path " + dex); } } }