查看: 2939|回复: 1
打印 上一主题 下一主题

[其他] 贝塞尔平滑linerenderer 画线

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-9-29 00:56:30 |只看该作者 |倒序浏览

用linerenderer手指以后快速画线时 会有破线的现象 我根据网上的贝塞尔曲线资料 自己修改下 使画出来的线更平滑 废话不说 直接上代码
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class DrawLine : MonoBehaviour {

  5.     // Use this for initialization

  6.     private List<Vector3> list;
  7.     private bool IsDraw = false;
  8.     private LineRenderer lineRenderer;
  9.     void Start () {
  10.         lineRenderer = GetComponent<LineRenderer>();
  11.     }
  12.      
  13.     // Update is called once per frame
  14.     void Update()
  15.     {

  16.         if (Input.GetMouseButtonDown(0))
  17.         {
  18.             if (list == null)
  19.                 list = new List<Vector3>();

  20.             list.Clear();
  21.             IsDraw = true;
  22.             lineRenderer.SetVertexCount(0);
  23.         }
  24.         if (Input.GetMouseButton(0))//记录划线点
  25.         {

  26.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  27.             RaycastHit hit;
  28.             if (Physics.Raycast(ray, out hit, 1000))
  29.             {
  30.                 Vector3 point = hit.point;
  31.                 if (!hit.collider.name.Equals("Terrain"))
  32.                 {
  33.                     return;
  34.                 }
  35.                 list.Add(point);
  36.                 print(list.Count);
  37.             }

  38.         }

  39.         if (Input.GetMouseButtonUp(0))
  40.         {
  41.             IsDraw = false;
  42.         }
  43.        drawBezierCurve();
  44.       //  drawInputPointCurve();

  45.     }

  46.     private void drawBezierCurve()
  47.     {
  48.         if(IsDraw&&list.Count>0){
  49.             List<Vector3> bcList;
  50.             BezierCurve bc= new BezierCurve();
  51.             bcList = bc.CreateCurve(list);//  通过贝塞尔曲线 平滑划线点
  52.             lineRenderer.SetVertexCount(bcList.Count); //maxVertices < 65536 && maxIndices < 65536*3  点的密度 可以降低 太多会超过可设顶点数
  53.             for (int i = 0; i < bcList.Count; i++)
  54.             {
  55.                 Vector3 v = bcList[i];
  56.                 v += new Vector3(0, 0.5f, 0);
  57.                 lineRenderer.SetPosition(i, v);
  58.             }

  59.         }

  60.     }

  61.     private void drawInputPointCurve()
  62.     {
  63.         if (IsDraw && list.Count > 0)
  64.         {
  65.             lineRenderer.SetVertexCount(list.Count);
  66.             for (int i = 0; i < list.Count; i++)
  67.             {
  68.                 Vector3 v = list[i];
  69.                 v += new Vector3(0, 0.5f, 0);
  70.                 lineRenderer.SetPosition(i, v);
  71.             }

  72.         }
  73.     }
  74. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

1

听众

266

积分

设计实习生

Rank: 2

纳金币
34
精华
0

最佳新人

沙发
发表于 2016-2-21 20:28:43 |只看该作者
请问这段代码:BezierCurve bc = new BezierCurve();提示:未能找到类型或命名空间名称"BezierCurve" (是否缺少using指令或程序集引用?) 这是什么原因?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-20 19:49 , Processed in 0.068589 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部