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

[其他] unity3d 画任意线(GL方式高级应用)

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

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

跳转到指定楼层
楼主
发表于 2015-9-30 01:38:34 |只看该作者 |倒序浏览
Unity3d 画任意线(GL方式高级应用)

  1. #pragma strict

  2. private var origPoints : Vector2[];

  3. var numberOfPoints = 31;

  4. var lineColor = Color.white;

  5. var lineWidth = 3;

  6. var drawLines = true;

  7. var i = 0;

  8. private var lineMaterial : Material;

  9. private var linePoints : Vector2[];

  10. private var cam : Camera;

  11.   

  12. function Awake () {

  13. lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +

  14. "SubShader { Pass {" +

  15. "   BindChannels { Bind \"Color\",color }" +

  16. "   Blend SrcAlpha OneMinusSrcAlpha" +

  17. "   ZWrite Off Cull Off Fog { Mode Off }" +

  18. "} } }");

  19. lineMaterial.hideFlags = HideFlags.HideAndDontSave;

  20. lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;

  21. cam = camera;

  22. }

  23.   

  24. function Start () {

  25. linePoints = new Vector2[numberOfPoints];

  26. origPoints = new Vector2[numberOfPoints];

  27.   

  28. // Plot points on a circle

  29. var radians : float = 360.0/(numberOfPoints-1)*Mathf.Deg2Rad;

  30. var p = 0.0;

  31. for (i = 0; i < numberOfPoints; i++) {

  32. linePoints[i] = Vector2(.5 + .25*Mathf.Cos(p), .5 + .25*Mathf.Sin(p));

  33. origPoints[i] = linePoints[i];

  34. p += radians;

  35. }

  36. }

  37.   

  38. function Update () {

  39. for (i = 0; i < linePoints.Length; i++) {

  40. if (i%2 == 0) {var m = .4; var t = 1.0;}

  41. else {m = .5; t = .5;}

  42. linePoints[i] = (origPoints[i]-Vector2(.5, .5))*(Mathf.Sin(Time.time*t)+Mathf.PI*m)+Vector2(.5, .5);

  43. }

  44. }

  45.   

  46. function OnPostRender () {

  47. if (!drawLines || !linePoints || linePoints.Length < 2) {return;}

  48.   

  49. var nearClip = cam.nearClipPlane + .00001; // Add a bit, else there's flickering when the camera moves

  50. var end = linePoints.Length - 1;

  51. var thisWidth = 1.0/Screen.width * lineWidth * .5;

  52.   

  53. lineMaterial.SetPass(0);

  54. GL.Color(lineColor);

  55.   

  56. if (lineWidth == 1) {

  57. GL.Begin(GL.LINES);

  58. for (i = 0; i < end; i++) {

  59. GL.Vertex(cam.ViewportToWorldPoint(Vector3(linePoints[i].x, linePoints[i].y, nearClip)));

  60. GL.Vertex(cam.ViewportToWorldPoint(Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip)));

  61. }

  62. }

  63. else {

  64. GL.Begin(GL.QUADS);

  65. for (i = 0; i < end; i++) {

  66. var perpendicular = (Vector3(linePoints[i+1].y, linePoints[i].x, nearClip) -

  67. Vector3(linePoints[i].y, linePoints[i+1].x, nearClip)).normalized * thisWidth;

  68. var v1 = Vector3(linePoints[i].x, linePoints[i].y, nearClip);

  69. var v2 = Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip);

  70. GL.Vertex(cam.ViewportToWorldPoint(v1 - perpendicular));

  71. GL.Vertex(cam.ViewportToWorldPoint(v1 + perpendicular));

  72. GL.Vertex(cam.ViewportToWorldPoint(v2 + perpendicular));

  73. GL.Vertex(cam.ViewportToWorldPoint(v2 - perpendicular));

  74. }

  75. }

  76. GL.End();

  77. }

  78.   

  79. function OnApplicationQuit () {

  80. DestroyImmediate(lineMaterial);

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

使用道具 举报

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

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

GMT+8, 2025-8-13 19:39 , Processed in 0.058705 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部