Unity 三点生成一段贝塞尔曲线脚本

来源:互联网 发布:最新网络语言 编辑:程序博客网 时间:2024/06/12 21:20
using UnityEngine;using System.Collections;//using Vectrosity;using System.Collections.Generic; public class DrawCurveTest : MonoBehaviour{    public Material lineMat;    public int segment=20;    //VectorLine line;    public Transform[] poss = new Transform[3];     List m_points3;     public GameObject cubePrefab;      public LineRenderer lineRenderer;    public Color c1 = Color.yellow;    public Color c2 = Color.red;    // Use this for initialization    void Start()    {         lineRenderer = GetComponent();        lineRenderer.material = lineMat;        lineRenderer.SetColors(c1, c2);        lineRenderer.SetWidth(0.5F, 0.5F);        lineRenderer.SetVertexCount(segment);    }     void CmakeCurve(Vector3 anchor1, Vector3 control1, Vector3 anchor2, Vector3 control2, int segments)    {        m_points3 = new List(segment);        for (int i = 0; i < segment; i++)        {            Vector3 pp= GetBezierPoint3D(ref anchor1, ref control1, ref anchor2, ref control2, (float)i / segments, cubePrefab);            m_points3.Add(pp);        }    }     private static Vector3 GetBezierPoint3D(ref Vector3 anchor1, ref Vector3 control1, ref Vector3 anchor2, ref Vector3 control2, float t,GameObject go)    {        float cx = 3 * (control1.x - anchor1.x);        float bx = 3 * (control2.x - control1.x) - cx;        float ax = anchor2.x - anchor1.x - cx - bx;        float cy = 3 * (control1.y - anchor1.y);        float by = 3 * (control2.y - control1.y) - cy;        float ay = anchor2.y - anchor1.y - cy - by;        float cz = 3 * (control1.z - anchor1.z);        float bz = 3 * (control2.z - control1.z) - cz;        float az = anchor2.z - anchor1.z - cz - bz;         //    Vector3 pop=new Vector3((ax * (t * t * t)) + (bx * (t * t)) + (cx * t) + anchor1.x,        //                    (ay * (t * t * t)) + (by * (t * t)) + (cy * t) + anchor1.y,        //                    (az * (t * t * t)) + (bz * (t * t)) + (cz * t) + anchor1.z);        //    Instantiate(go, pop, Quaternion.identity);        //return pop;        return new Vector3((ax * (t * t * t)) + (bx * (t * t)) + (cx * t) + anchor1.x,                            (ay * (t * t * t)) + (by * (t * t)) + (cy * t) + anchor1.y,                            (az * (t * t * t)) + (bz * (t * t)) + (cz * t) + anchor1.z);    }     // Update is called once per frame    void Update()    {        CmakeCurve(poss[0].position, poss[1].position, poss[2].position, poss[2].position, segment);        int i = 0;        while (i < m_points3.Count)        {            //Vector3 pos = new Vector3(i * 0.5F, Mathf.Sin(i + Time.time), 0);            lineRenderer.SetPosition(i, m_points3[i]);            i++;        }    }}
0 0
原创粉丝点击