注册 登录
  • 欢迎访问Sharezer Blog

Unity GL画一条贝塞尔曲线

Unity sharezer 857次浏览 已收录 0个评论

Unity GL画一条贝塞尔曲线

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;

public class DrawLineWindow : EditorWindow
{

    [MenuItem("Tools/测试")]
    public static void ShowWindow()
    {

        DrawLineWindow window = EditorWindow.GetWindow(typeof(DrawLineWindow)) as DrawLineWindow;
        window.Init();
    }

    private Material lineMaterial;
    Vector3 p0, p1, p2;

    int count = 2;
    List<Rect> rectList = new List<Rect>();

    private List<Vector3> lineInfo = new List<Vector3>();//放置点的列表

    void Init()
    {
        rectList.Clear();
        for (int i = 0; i < count; i++)
        {
            rectList.Add(new Rect(20, 20, 80, 100));
        }
    }

 

    void OnGUI()
    {
        if (!lineMaterial)
        {
            lineMaterial = new Material(Shader.Find("Standard"));
        }

        BeginWindows();
        for(int i = 0; i < count; i++)
        {
            rectList[i] = GUI.Window(i, rectList[i], DrawWindow, "Start");
        }

        EndWindows();

        lineInfo.Clear();
        {
            p0 = new Vector3(rectList[0].x, rectList[0].y, 0);
            p1 = new Vector3((rectList[1].x + rectList[0].x) / 2.0f, rectList[0].y, 0);//根据需求设置
            //p1 = new Vector3((rectList[1].x + rectList[0].x) / 2.0f, (rectList[1].y + rectList[0].y) / 2.0f, 0);
            p2 = new Vector3(rectList[1].x, rectList[1].y, 0);

            for (float t = 0; t < 1.0f; t += 0.0001f)
            {
                float x = (1 - t) * (1 - t) * p0.x + 2 * (1 - t) * t * p1.x + t * t * p2.x;
                float y = (1 - t) * (1 - t) * p0.y + 2 * (1 - t) * t * p1.y + t * t * p2.y;
                lineInfo.Add(new Vector3(x, y, 0));
            }
        }

        GL.PushMatrix(); //保存当前Matirx
        lineMaterial.SetPass(0); //刷新当前材质
        GL.LoadPixelMatrix();//设置pixelMatrix
        GL.Color(Color.black);
        GL.Begin(GL.LINES);

        int size = lineInfo.Count;
        for (int i = 0; i < size - 1; i++)
        {
            Vector3 start = lineInfo[i];
            Vector3 end = lineInfo[i + 1];
            //绘制线  
            DrawLine(start.x, start.y, end.x, end.y);
        }

        GL.End();
        GL.PopMatrix();//读取之前的Matrix
    }

    void DrawWindow(int windowID)
    {
        GUILayout.Button("Start");
        GUI.DragWindow();
    }

    void DrawLine(float x1, float y1, float x2, float y2)
    {
        //绘制线段  
        GL.Vertex(new Vector3(x1, y1, 0));
        GL.Vertex(new Vector3(x2, y2, 0));
    }

}


Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Unity GL画一条贝塞尔曲线
喜欢 (0)
[liangshaoze@sina.cn]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址