3 ways to draw 3d lines in unity3d

本文介绍了在Unity3D中绘制3D线条的三种不同解决方案:使用LineRenderer组件,通过Graphics类创建网格,以及直接使用GL进行绘制。LineRenderer适合静态线,而GL方法在性能上更优但控制度较低。对于动态和大规模线的绘制,Graphics方案在Unity3D Pro中是更好的选择。
摘要由CSDN通过智能技术生成

Just as I was thinking about an interesting demo to play withdrawing functions in Unity3D,Mrdoob publishedhis Harmony drawingtool made with HTML5/Canvas. It looks really cool, so I though howabout doing this in 3D? I only had to figure out how to drawlines.

I did some research and below Ipresent 3 different solutions. Youcan grab the source of the examples discussedbelow here.

Drawing lines with LineRenderer [demo]

When it comes to lines, the first thing you'll bump into in theUnity3D API is the LineRenderercomponent. As the name suggests, it is used to drawlines so it seems the right tool for the job. Lines in this caseare defined by 2 or more points (segments), a material and awidth.

It has an important limitation: the line must be continuous.So if you need two lines, you need two renderers. The other problemis that the Line Renderer acts very strangely when new points areadded dynamically. The width of the line does not seem to rendercorrectly. It's either buggy or just wasn't designed for suchuse. Because of these limitations I had to create aseparate Line Renderer for each tiny bit of line I'm drawing.

It was easy to implement, but not very fast since I end up spawninglots of GameObjects each with a LineRenderer attached. It seems tobe the only option if you don't have Unity3DProthough.

Drawing lines as a meshusing Graphics [demo]

The Graphics classallows to draw a mesh directly without the overhead of creatinggame objects and components to hold it.It runsmuch faster than Line Renderer, but you need to create thelines yourself. This is a bit more difficult but also gives youtotal control of the lines - their color, material, width andorientation.

Since meshes are composed of surfaces rather than lines or points,in 3D space a line is best rendered as a very thin quad. A quad isdescribed with 4 vertices, and usually you'll only have the startand end points and a width. Based on this data you can compute aline like this:

C#:
  1. Vector3 normal = Vector3. Cross (start,end );
  2. Vector3 side = Vector3. Cross (normal,end-start );
  3. side. Normalize ( );
  4. Vector3 a = start + side *  (lineWidth/  2 );
  5. Vector3 b = start + side *  (lineWidth/ - 2 );
  6. Vector3 c = end + side *  (lineWidth/  2 );
  7. Vector3 d = end + side *  (lineWidth/ - 2 );

 

First, you get the normal of the plane on which both start and endvectors lie. This will be the plane on which the line-quad willlocated. The cross product of the normal and of the differencebetween end and start vectors gives you the side vector (the "thin"side of the quad). You need to normalize it to make it a unitvector. Finally calculate all 4 points of the rectangle by addingthe side vector multiplied by half width to both start and endpoints in both directions. In the sourcecode all this happensin MakeQuad and AddLine methods,so take a look in there.

It wasn't easy to implement, but once I was there it runs prettyfast.

Direct drawing withGL [demo]

No fast is fastenough! Instead of leaving this topic andlive happily with the Graphics solution, I kept searching forsomething even better. And I found the GLclass. GL is used to "issue rendering commands similar toOpenGL's immediate mode". This sounds like fast, doesn't it? Itis!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值