查看: 892|回复: 0
打印 上一主题 下一主题

[其他] Unity3D开发之利用GL画曲线

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-2-28 23:37:37 |只看该作者 |倒序浏览

unity3d开发之利用GL画曲线
  1. using UnityEngine;

  2. using System.Collections;

  3. using System;

  4. using System.Collections.Generic;

  5. public class DrawLine : MonoBehaviour {

  6. //绘制线段材质

  7. public Material material;

  8. private List<Vector3> lineInfo;

  9. private bool startDraw = false;

  10. Event e;

  11. void Start () {

  12. //初始化鼠标线段链表

  13. lineInfo = new List<Vector3>();

  14. }

  15. void OnGUI(){

  16. e = Event.current;

  17. }

  18. // Update is called once per frame

  19. void Update () {

  20. if(e.type == EventType.MouseDown)

  21. {

  22. startDraw = true;

  23. }

  24. if(e.type==EventType.MouseDrag)

  25. {

  26. if(startDraw == true){

  27. //将每次鼠标经过的位置存储进链表

  28. lineInfo.Add(Input.mousePosition);

  29. }

  30. }

  31. if(e.type==EventType.MouseUp)

  32. {

  33. startDraw = false;

  34. lineInfo.Clear();

  35. }

  36. }

  37. //GL的绘制方法系统回调

  38. void OnPostRender()

  39. {

  40. if(!material)

  41. {

  42. Debug.LogError(“material == null”);

  43. return;

  44. }

  45. //材质通道,0为默认。

  46. material.SetPass(0);

  47. //绘制2D图像

  48. GL.LoadOrtho();

  49. //得到鼠标点信息总数量

  50. GL.Begin(GL.LINES);

  51. //遍历鼠标点的链表

  52. int size = lineInfo.Count;

  53. for(int i =0; i < size - 1;i++)

  54. {

  55. Vector3 start = lineInfo[i];

  56. Vector3 end = lineInfo[i+1];

  57. //绘制线

  58. DrawLineFun(start.x,start.y,end.x,end.y);

  59. }

  60. //结束绘制

  61. GL.End();

  62. }

  63. void DrawLineFun(float x1,float y1,float x2,float y2)

  64. {

  65. //绘制线段

  66. GL.Vertex(new Vector3(x1 / Screen.width,y1 / Screen.height,0));

  67. GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height,0));

  68. }

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

使用道具 举报

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

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-1 01:10 , Processed in 0.172765 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部