unity3d如何使线平滑

最近使用unity制作了绘图板的功能,不过线段绘制的时候一直不平滑,怎么使线段平滑呢?类似下图:



使用该代码即可返回平滑的点

//arrayToCurve is original Vector3 array, smoothness is the number of interpolations. 
     public static Vector3[] MakeSmoothCurve(Vector3[] arrayToCurve,float smoothness){
         List<Vector3> points;
         List<Vector3> curvedPoints;
         int pointsLength = 0;
         int curvedLength = 0;
         
         if(smoothness < 1.0f) smoothness = 1.0f;
         
         pointsLength = arrayToCurve.Length;
         
         curvedLength = (pointsLength*Mathf.RoundToInt(smoothness))-1;
         curvedPoints = new List<Vector3>(curvedLength);
         
         float t = 0.0f;
         for(int pointInTimeOnCurve = 0;pointInTimeOnCurve < curvedLength+1;pointInTimeOnCurve++){
             t = Mathf.InverseLerp(0,curvedLength,pointInTimeOnCurve);
             
             points = new List<Vector3>(arrayToCurve);
             
             for(int j = pointsLength-1; j > 0; j--){
                 for (int i = 0; i < j; i++){
                     points[i] = (1-t)*points[i] + t*points[i+1];
                 }
             }
             
             curvedPoints.Add(points[0]);
         }
         
         return(curvedPoints.ToArray());
     }

使用方法:

//javascript/unityscript example
 #pragma strict
 var points : Vector3[];
 
 var lineRenderer : LineRenderer;
 var c1 : Color = Color.yellow;
 var c2 : Color = Color.red;
 
 function Start () {
     points = Curver.MakeSmoothCurve(points,3.0);
     
     lineRenderer.SetColors(c1, c2);
     lineRenderer.SetWidth(0.5,0.5);
     lineRenderer.SetVertexCount(points.Length);
     var counter : int = 0;
     for(var i : Vector3 in points){
         lineRenderer.SetPosition(counter, i);
         ++counter;
     }
 }


参考地址:http://answers.unity3d.com/questions/392606/line-drawing-how-can-i-interpolate-between-points.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼蛋-Felix

如果对你有用,可以请我喝杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值