• 通过贝塞尔曲线编辑曲线


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using DG.Tweening;
    
    
    //[ExecuteInEditMode]
    public class PathManager : MonoBehaviour
    {
        public GameObject rootPoint;
        public Transform trans;
        public LineRenderer lineRenderer;
        private static PathManager Instance;
        // Use this for initialization
        public static PathManager GetInstance()
        {
            return Instance;
        }
    
        public BezierPathModel BezierPath;
    
        private void Awake()
        {
            Instance = this;
          
        }
    
        void Start()
        {
            if (!lineRenderer)
            {
                lineRenderer = gameObject.AddComponent<LineRenderer>();
            }
            else {
                lineRenderer = gameObject.GetComponent<LineRenderer>();
            }
            InitPath();
            DrawCurve(BezierPath.PathTrans);
    
            //trans.DOPath(BezierPath.PathTrans,10);
          //  trans.DOPath(BezierPath.PathTrans, 15, PathType.Linear, PathMode.Full3D);
           // rootPoint.DOPath(BezierPath.PathTrans, 15, PathType.Linear, PathMode.Full3D);
        }
    
        public void Update()
        {
           
        }
    
        void DrawCurve(List<Vector3> pos)
        {
            lineRenderer.positionCount = pos.Count;
            for (int i = 0; i < pos.Count; i++)
            {
                //   float t = i / (float)pos.Count;
                // int nodeIndex = 0;
               // Debug.Log("i"+i);
                Vector3 pixel = pos[i];
                //lineRenderer.positionCount = i;
                lineRenderer.SetPosition(i, pixel);
            }
        }
    
        public void InitPath()
        {
            BezierPath.PathTrans = calculate(BezierPath.ControlTrans, BezierPath.segmentNum);
    
        }
    
        public List<Vector3> calculate(List<Transform> poss, int precision)
        {
    
            //维度,坐标轴数(二维坐标,三维坐标...)
            int dimersion = 2;
    
            //贝塞尔曲线控制点数(阶数)
            int number = poss.Count;
    
            //控制点数不小于 2 ,至少为二维坐标系
            if (number < 2 || dimersion < 2)
                return null;
    
            List<Vector3> result = new List<Vector3>();
    
            //计算杨辉三角
            int[] mi = new int[number];
            mi[0] = mi[1] = 1;
            for (int i = 3; i <= number; i++)
            {
    
                int[] t = new int[i - 1];
                for (int j = 0; j < t.Length; j++)
                {
                    t[j] = mi[j];
                }
    
                mi[0] = mi[i - 1] = 1;
                for (int j = 0; j < i - 2; j++)
                {
                    mi[j + 1] = t[j] + t[j + 1];
                }
            }
    
            //计算坐标点
            for (int i = 0; i < precision; i++)
            {
                float t = (float)i / precision;
                Vector3 temp_ = new Vector3(0, 0, 0);
                for (int k = 0; k < number; k++)
                {
                    temp_.x += Mathf.Pow(1 - t, number - k - 1) * poss[k].position.x * Mathf.Pow(t, k) * mi[k];
                    temp_.y += Mathf.Pow(1 - t, number - k - 1) * poss[k].position.y * Mathf.Pow(t, k) * mi[k];
                    temp_.z += Mathf.Pow(1 - t, number - k - 1) * poss[k].position.z * Mathf.Pow(t, k) * mi[k];
                }
    
                result.Add(temp_);
            }
            return result;
        }
    
        public List<Vector3> ReturnPath(int id)
        {
            return BezierPath.PathTrans;
        }
    }
    
    [System.Serializable]
    public class BezierPathModel
    {
        public List<Transform> ControlTrans;
        public int segmentNum;
        public List<Vector3> PathTrans;
    
    }
  • 相关阅读:
    开源 .net license tool, EasyLicense !
    Logging with NLog
    Logging with Log4net (二)
    Logging with Debug And Trace (一)
    ThoughtWorks代码挑战——FizzBuzzWhizz
    开源插件 :MahApps.Metro.IconPacks
    Java地址:
    小程序源码下载[demo整理自github]
    多块图形合并(自动合并相交块)
    Textbox
  • 原文地址:https://www.cnblogs.com/Mr147/p/12376852.html
Copyright © 2020-2023  润新知