当前位置:   article > 正文

Unity生成贝塞尔曲线路劲_unity bezier namespace

unity bezier namespace

需求:我要一条波浪形的路线

图片01

前言

最近接到需求想要验证一下在波浪形路线下的效果怎么样,由于是在前人的工程下继续开发,已经定下行走路线的基调是按路劲点行进。我创建一条路线总不可能一个个点摆吧,之前是按mesh点获取路劲,但是这个需要一直返工测试,验证起来还是比较费劲的。

生成二阶曲线

图片02
计算方法

		/// <summary>
		///二阶贝塞尔
		/// </summary>
		/// <param name="startPoint"></param>
		/// <param name="endPoint"></param>
		/// <param name="middlePoint"></param>
		public void CreatTwoBezierCurve(Vector3 startPoint, Vector3 endPoint, Vector3 middlePoint)
		{
			  allPoints.Clear();
			  for (int i = 0; i < nultiple; i++)
			  {
			      float tempPercent = (float) i / (float) nultiple;
			      float dis1 = Vector3.Distance(startPoint, middlePoint);
			      Vector3 point1 = startPoint + Vector3.Normalize(middlePoint - startPoint) * dis1 * tempPercent;
			      float dis2 = Vector3.Distance(middlePoint, endPoint);
			      Vector3 point2 = middlePoint + Vector3.Normalize(endPoint - middlePoint) * dis2 * tempPercent;
			      float dis3 = Vector3.Distance(point1, point2);
			      Vector3 linePoint = point1 + Vector3.Normalize(point2 - point1) * dis3 * tempPercent;
			      allPoints.Add(linePoint);
			  }
			  allPoints.Add(endPoint);
		}
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

生成三阶曲线

图片03

计算方法

        /// <summary>
        /// 三阶贝塞尔
        /// </summary>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        /// <param name="middlePoint1"></param>
        public void CreatThreeBezierCurve(Vector3 startPoint, Vector3 endPoint, Vector3 middlePoint1,
            Vector3 middlePoint2)
        {
            allPoints.Clear();
            for (int i = 0; i < nultiple; i++)
            {
                float tempPercent = (float) i / (float) nultiple;
                float dis1 = Vector3.Distance(startPoint, middlePoint1);
                Vector3 pointL1 = startPoint + Vector3.Normalize(middlePoint1 - startPoint) * dis1 * tempPercent;
                float dis2 = Vector3.Distance(middlePoint1, middlePoint2);
                Vector3 pointL2 = middlePoint1 + Vector3.Normalize(middlePoint2 - middlePoint1) * dis2 * tempPercent;
                float dis3 = Vector3.Distance(pointL1, pointL2);
                Vector3 pointLeft = pointL1 + Vector3.Normalize(pointL2 - pointL1) * dis3 * tempPercent;


                float dis4 = Vector3.Distance(middlePoint2, endPoint);
                Vector3 pointR1 = middlePoint2 + Vector3.Normalize(endPoint - middlePoint2) * dis4 * tempPercent;
                float dis5 = Vector3.Distance(pointL2, pointR1);
                Vector3 pointRight = pointL2 + Vector3.Normalize(pointR1 - pointL2) * dis5 * tempPercent;


                float disLeftAndRight = Vector3.Distance(pointLeft, pointRight);
                Vector3 linePoint =
                    pointLeft + Vector3.Normalize(pointRight - pointLeft) * disLeftAndRight * tempPercent;
                allPoints.Add(linePoint);
            }

            allPoints.Add(endPoint);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

整理生成正弦函数

完成代码

using System.Collections.Generic;
using UnityEngine;

namespace BowenkLib
{
    public class BownekBezierTest : MonoBehaviour
    {
        /// <summary>
        /// 曲线圆滑度
        /// </summary>
        [Range(5, 50)]public int nultiple = 5;
        /// <summary>
        /// 波长 但是这里直接换成一个正弦的长度
        /// </summary>
        [Range(2, 30)] public float WaveLength = 6;
        /// <summary>
        /// 波峰高度控制
        /// </summary>
        [Range(1, 50)] public int Height = 1;
        /// <summary>
        /// 周期控制
        /// </summary>
        [Range(1, 20)] public int Cyclic = 1;

        public List<Vector3> allPoints = new List<Vector3>();

        public Transform Start;
        public Transform End;
        public Transform Contrl1;
        public Transform Contrl2;

        private void OnDrawGizmos()
        {
            CreatPath();
        }

        /// <summary>
        /// 创建正弦路劲
        /// </summary>
        private void CreatPath()
        {
            if(Start == null) return;

            allPoints.Clear();
            var nextPoint = Start.position;
            for (var i = 0; i < Cyclic; i++)
            {
                nextPoint = OnePointDraw(nextPoint, WaveLength, Height);
            }
        }

        /// <summary>
        /// 生成正弦波浪 x轴方向
        /// </summary>
        /// <param name="startPoint">起点</param>
        /// <param name="waveLength">波长</param>
        private Vector3 OnePointDraw(Vector3 startPoint, float waveLength, float height)
        {
            var endPoint = startPoint + new Vector3(waveLength, 0, 0);
            var mPoint1 = startPoint + new Vector3(waveLength / 3, height, 0);
            var mPoint2 = startPoint + new Vector3((waveLength / 3) * 2, -height, 0);
            var path = GetBezierCurve(startPoint, endPoint, mPoint1, mPoint2, nultiple);
            for (var i = 1; i < path.Count; i++)
            {
                Debug.DrawLine(path[i-1], path[i], Color.yellow);
            }
            allPoints.AddRange(path);
            return endPoint;
        }

        /// <summary>
        /// 画出二阶曲线
        /// </summary>
        private void TwoDraw()
        {
            CreatTwoBezierCurve(Start.position, End.position, Contrl1.position);
            for (int i = 1; i < allPoints.Count; i++)
            {
                Debug.DrawLine(allPoints[i - 1], allPoints[i], Color.blue);
            }
        }
        
        /// <summary>
        /// 画出三阶曲线
        /// </summary>
        private void ThreeDraw()
        {
            if (Start == null || End == null || Contrl1 == null || Contrl2 == null)
                return;

            CreatThreeBezierCurve(Start.position, End.position, Contrl1.position, Contrl2.position);
            for (int i = 1; i < allPoints.Count; i++)
            {
                Debug.DrawLine(allPoints[i - 1], allPoints[i], Color.blue);
            }
        }

        /// <summary>
        ///二阶贝塞尔
        /// </summary>
        public void CreatTwoBezierCurve(Vector3 startPoint, Vector3 endPoint, Vector3 middlePoint)
        {
            allPoints.Clear();
            for (int i = 0; i < nultiple; i++)
            {
                float tempPercent = (float) i / (float) nultiple;
                float dis1 = Vector3.Distance(startPoint, middlePoint);
                Vector3 point1 = startPoint + Vector3.Normalize(middlePoint - startPoint) * dis1 * tempPercent;
                float dis2 = Vector3.Distance(middlePoint, endPoint);
                Vector3 point2 = middlePoint + Vector3.Normalize(endPoint - middlePoint) * dis2 * tempPercent;
                float dis3 = Vector3.Distance(point1, point2);
                Vector3 linePoint = point1 + Vector3.Normalize(point2 - point1) * dis3 * tempPercent;
                allPoints.Add(linePoint);
            }

            allPoints.Add(endPoint);
        }

        /// <summary>
        /// 三阶贝塞尔
        /// </summary>
        public void CreatThreeBezierCurve(Vector3 startPoint, Vector3 endPoint, Vector3 middlePoint1,
            Vector3 middlePoint2)
        {
            allPoints.Clear();
            for (int i = 0; i < nultiple; i++)
            {
                float tempPercent = (float) i / (float) nultiple;
                float dis1 = Vector3.Distance(startPoint, middlePoint1);
                Vector3 pointL1 = startPoint + Vector3.Normalize(middlePoint1 - startPoint) * dis1 * tempPercent;
                float dis2 = Vector3.Distance(middlePoint1, middlePoint2);
                Vector3 pointL2 = middlePoint1 + Vector3.Normalize(middlePoint2 - middlePoint1) * dis2 * tempPercent;
                float dis3 = Vector3.Distance(pointL1, pointL2);
                Vector3 pointLeft = pointL1 + Vector3.Normalize(pointL2 - pointL1) * dis3 * tempPercent;


                float dis4 = Vector3.Distance(middlePoint2, endPoint);
                Vector3 pointR1 = middlePoint2 + Vector3.Normalize(endPoint - middlePoint2) * dis4 * tempPercent;
                float dis5 = Vector3.Distance(pointL2, pointR1);
                Vector3 pointRight = pointL2 + Vector3.Normalize(pointR1 - pointL2) * dis5 * tempPercent;


                float disLeftAndRight = Vector3.Distance(pointLeft, pointRight);
                Vector3 linePoint =
                    pointLeft + Vector3.Normalize(pointRight - pointLeft) * disLeftAndRight * tempPercent;
                allPoints.Add(linePoint);
            }

            allPoints.Add(endPoint);
        }
        
        /// <summary>
        /// 获取两点 之间的正弦点
        /// </summary>
        /// <param name="startPoint">起点</param>
        /// <param name="endPoint">终点</param>
        /// <param name="mPoint1">波峰1</param>
        /// <param name="mPoint2">波峰2</param>
        /// <param name="smooth">点数(控制圆滑度)</param>
        public List<Vector3> GetBezierCurve(Vector3 startPoint, Vector3 endPoint, Vector3 mPoint1, Vector3 mPoint2, int smooth)
        {
            List<Vector3> temp = new List<Vector3>();
            for (int i = 0; i < smooth; i++)
            {
                float tempPercent = (float) i / (float) smooth;
                float dis1 = Vector3.Distance(startPoint, mPoint1);
                Vector3 pointL1 = startPoint + Vector3.Normalize(mPoint1 - startPoint) * dis1 * tempPercent;
                float dis2 = Vector3.Distance(mPoint1, mPoint2);
                Vector3 pointL2 = mPoint1 + Vector3.Normalize(mPoint2 - mPoint1) * dis2 * tempPercent;
                float dis3 = Vector3.Distance(pointL1, pointL2);
                Vector3 pointLeft = pointL1 + Vector3.Normalize(pointL2 - pointL1) * dis3 * tempPercent;


                float dis4 = Vector3.Distance(mPoint2, endPoint);
                Vector3 pointR1 = mPoint2 + Vector3.Normalize(endPoint - mPoint2) * dis4 * tempPercent;
                float dis5 = Vector3.Distance(pointL2, pointR1);
                Vector3 pointRight = pointL2 + Vector3.Normalize(pointR1 - pointL2) * dis5 * tempPercent;


                float disLeftAndRight = Vector3.Distance(pointLeft, pointRight);
                Vector3 linePoint =
                    pointLeft + Vector3.Normalize(pointRight - pointLeft) * disLeftAndRight * tempPercent;
                temp.Add(linePoint);
            }

            temp.Add(endPoint);
            return temp;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190

这时候就可以直接在 Inspector面板直接控制你想要的波浪线了
图片04

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/580290
推荐阅读
相关标签
  

闽ICP备14008679号