Unity技术分享Unity教程合集

简易二阶BEZIER曲线编辑器

2016-03-21  本文已影响115人  影子丢了

Editor部分

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

[CustomEditor(typeof(CurveObject))]
public class CurveEditor : Editor {

    public Vector3[] paths;
    CurveObject script;

    private bool _editor = false;
    private string editorState = "OFF";

    private Transform startPoint;
    private Transform endPosPoint;
    private Transform CtlBar1;
    private Transform CtlBar2;

    private Vector3 _currentChoose;


    private bool select1;
    private bool select2;

    private Vector3 Select;
    void OnEnable()
    {
        script = (CurveObject)target;
    }
    public void OnSceneGUI()
    {
        if(startPoint == null || endPosPoint == null || CtlBar1 == null || CtlBar2 == null)
            return;

        script.start = this.startPoint.transform.position;
        script.end = this.endPosPoint.transform.position;
        script.ctlBar1 = this.CtlBar1.transform.position;
        script.ctlBar2 = this.CtlBar2.transform.position;
            
        paths = script.UpdateBezier();

        Handles.color = Color.white;
        Handles.DrawPolyLine(paths);
        //Handles.DrawBezier(script.start,script.end,script.ctlBar1,script.ctlBar2,Color.white,null,1.0f);

        Handles.color = new Color(0,1,0,0.2f);

        if(select1)
        {
        Handles.DrawSolidDisc(this.CtlBar1.transform.position,Vector3.up,HandleUtility.GetHandleSize(this.CtlBar1.transform.position)*0.3f);
        Vector3 handle = Handles.PositionHandle(this.CtlBar1.transform.position, Quaternion.identity);
        this.CtlBar1.transform.position = handle;
        }
        if(select2)
        {
            Handles.DrawSolidDisc(this.CtlBar2.transform.position,Vector3.up,HandleUtility.GetHandleSize(this.CtlBar1.transform.position)*0.3f);
            Vector3 handle = Handles.PositionHandle(this.CtlBar2.transform.position, Quaternion.identity);
            this.CtlBar2.transform.position = handle;
        }
    }
    public override void OnInspectorGUI ()
    {
        base.OnInspectorGUI ();

        GUILayout.BeginVertical();
        startPoint = (Transform)EditorGUILayout.ObjectField(new GUIContent("起点"),startPoint,typeof(Transform),true);
        endPosPoint = (Transform)EditorGUILayout.ObjectField(new GUIContent("终点"),endPosPoint,typeof(Transform),true);
        CtlBar1 = (Transform)EditorGUILayout.ObjectField(new GUIContent("控制点1"),CtlBar1,typeof(Transform),true);
        if(GUILayout.Button(new GUIContent("SELETE")))
        {
            select1 = true;
            select2 = false;
            Select = this.CtlBar1.transform.position;
            FocusSceneObj(Select);
        }
        CtlBar2 = (Transform)EditorGUILayout.ObjectField(new GUIContent("控制点2"),CtlBar2,typeof(Transform),true);
        if(GUILayout.Button(new GUIContent("SELETE")))
        {
            select1 = false;
            select2 = true;
            Select = this.CtlBar2.transform.position;
            FocusSceneObj(Select);
        }
        if(GUILayout.Button(new GUIContent("Editor" + " " +editorState)))
        {
            _editor = !_editor;
            var model = (_editor == true)?(editorState = "ON"):(editorState = "OFF");
        }
        GUILayout.EndVertical();

    }
    void FocusSceneObj(Vector3 Position)
    {
        Object[] intialFocus = Selection.objects;
        GameObject tempFocusView = new GameObject("Temp Focus View");
        tempFocusView.transform.position = Position;
        try
        {
            Selection.objects = new Object[]{tempFocusView};
            //焦点
            SceneView.lastActiveSceneView.Focus();
            //F键
            SceneView.lastActiveSceneView.FrameSelected();
            Selection.objects = intialFocus;
        }
        catch(System.NullReferenceException)
        {
            //do nothing
        }
        Object.DestroyImmediate(tempFocusView);
    }
}

UnityEngine部分

using UnityEngine;
using System.Collections;

public class CurveObject : MonoBehaviour {

    [HideInInspector]
    public Vector3 start;
    [HideInInspector]
    public Vector3 end;
    [HideInInspector]
    public Vector3 ctlBar1;
    [HideInInspector]
    public Vector3 ctlBar2;

    public Vector3[] UpdateBezier()
    {

        Vector3[] line = new Vector3[100];
        for(int i =1; i <= 100; i++)
        {
            //参数的取值范围 0 - 1 返回曲线没一点的位置
            //为了精确这里使用i * 0.01 得到当前点的坐标
            line[i - 1] = CalculateBezierPoint( (float)(i *0.01),start,ctlBar1,ctlBar2,end);
        }
        return line;
    }

    Vector3 CalculateBezierPoint(float t,
        Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
    {
        float u = 1 - t;
        float tt = t*t;
        float uu = u*u;
        float uuu = uu * u;
        float ttt = tt * t;

        Vector3 p = uuu * p0; 
        p += 3 * uu * t * p1;
        p += 3 * u * tt * p2; 
        p += ttt * p3; 

        return p;
    }
}

上一篇下一篇

猜你喜欢

热点阅读