Curve 曲线 工具

2018/06/06 15:07
阅读数 484

    最近研究了曲线绘制的工具,主要是2D方程的绘制。综合了许多工具,完成了一下两个脚本。

绘制的工具:

 

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 
 5 public class Curve : MaskableGraphic
 6 {
 7     public Color color;
 8     public float m_LineWidth = 1;
 9     [Range(1, 10)]
10     public int Acc = 2;
11     public System.Func<float, float> xConvert = x=>x;
12     public System.Func<float, float> yConvert = y => y;
13     public System.Func<float, float> MainFunc = f => f;
14     protected override void OnPopulateMesh(VertexHelper vh)
15     {
16         var rect = this.rectTransform.rect;
17         vh.Clear();
18         Debug.Log(rect.xMin);
19         Debug.Log(rect.xMax);
20         Vector2 pos_first = new Vector2(rect.xMin, CalcY(0) * rect.height+rect.yMin);
21 
22         for (float x = rect.xMin + Acc; x < rect.xMax; x += Acc)
23         {
24             Vector2 pos = new Vector2(x, CalcY((x - rect.xMin) / rect.width) * rect.height+rect.yMin);
25             var quad = GenerateQuad(pos_first, pos);
26 
27             vh.AddUIVertexQuad(quad);
28 
29             pos_first = pos;
30         }
31 
32         Vector2 pos_last = new Vector2(rect.xMax, CalcY(1) * rect.height+rect.yMin);
33         vh.AddUIVertexQuad(GenerateQuad(pos_first, pos_last));
34 
35         for (int i = 0; i < vh.currentVertCount - 4; i += 4)
36         {
37             vh.AddTriangle(i + 1, i + 2, i + 4);
38             vh.AddTriangle(i + 1, i + 2, i + 7);
39         }
40         Debug.Log("PopulateMesh..." + vh.currentVertCount);
41     }
42 
43     //根据曲线组件来计算Y
44     //**** x 为 (x - rect.xMin) / rect.width)  所以范围 为 0~1
45     //返回值 为y ,取值范围限定在 0~1;
46     private float CalcY(float x)
47     {
48         return yConvert((MainFunc(xConvert(x))));
49     }
50 
51     private UIVertex[] GenerateQuad(Vector2 pos1, Vector2 pos2)
52     {
53         float dis = Vector2.Distance(pos1, pos2);
54         float y = m_LineWidth * 0.5f * (pos2.x - pos1.x) / dis;
55         float x = m_LineWidth * 0.5f * (pos2.y - pos1.y) / dis;
56 
57         if (y <= 0)
58             y = -y;
59         else
60             x = -x;
61 
62         UIVertex[] vertex = new UIVertex[4];
63 
64         vertex[0].position = new Vector3(pos1.x + x, pos1.y + y);
65         vertex[1].position = new Vector3(pos2.x + x, pos2.y + y);
66         vertex[2].position = new Vector3(pos2.x - x, pos2.y - y);
67         vertex[3].position = new Vector3(pos1.x - x, pos1.y - y);
68 
69         for (int i = 0; i < vertex.Length; i++)
70         {
71             vertex[i].color = color;
72         }
73 
74         return vertex;
75     }
76 }

 

控制脚本:

 

 1 using UnityEngine;
 2 using System.Collections;
 3 using System;
 4 [RequireComponent(typeof(Curve))]
 5 public class CurveItem : MonoBehaviour {
 6     private Curve curve;
 7     private Vector2[] posArray;
 8 
 9     //绘制谱图x 最小值
10     [Header("绘制谱图x 最小值")]
11     public float xMin;
12     //绘制谱图 x 最大值 
13     [Header("绘制谱图x 最大值")]
14     public float xMax;
15     //绘制 谱图 y 最小值
16     [Header("绘制谱图y 最小值")]
17     public float yMin;
18     //绘制谱图 y 最大值
19     [Header("绘制谱图y 最大值")]
20     public float yMax;
21     [Header("绘制谱图取样间隔")]
22     public float delta;
23     // Use this for initialization
24     void Start () {
25         curve = this.GetComponentInChildren<Curve>();
26     }
27     
28 
29     #region 主要方法
30   
31 
32     public void ShowPutu(Func<float, float> func)
33     {
34         //    DrawLine(LineDrawer.GetSampleArray(func, xMin, xMax, delta));
35         curve.MainFunc = func;
36         curve.xConvert = MathTool.GetLinear(0, xMin, 1, xMax);
37         curve.yConvert = MathTool.GetLinear(yMin, 0, yMax, 1f);
38         curve.enabled = false;
39         curve.enabled = true;
40     }
41 
42   
43 
44 
45     #endregion
46 
47     #region Test
48 
49     private Func<float, float> func = x=>x*x;
50 
51     [ContextMenu("谱图")]
52     private void Test()
53     {
54         ShowPutu(func);
55     }
56 
57     #endregion
58 }

 

 

Math工具

 

 1   
 2     //获取线性方程
 3     public static Func<float, float> GetLinear(float x0, float y0, float x1, float y1)
 4     {
 5         Func<float, float> linear = x => 
 6         {
 7             float a = (y1 - y0) / (x1 - x0);
 8             return x*a+y0-x0*a;
 9         };
10         return linear;
11     }
View Code

 

 

 

 

调用以下方法即可。

ShowPutu(Func<float, float> func)

 将两个脚本都挂载在Canvas下的一个空物体上。

演示 x=>x*x:

演示 x=>Mathf.Asin(x):

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部