Just as I was thinking about an interesting demo to play withdrawing functions in
I did some research and below Ipresent
Drawing lines with LineRenderer
When it comes to lines, the first thing you'll bump into in theUnity3D API is the
It has an important limitation:
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
Drawing lines as a meshusing Graphics
The
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:
-
Vector3 normal = Vector3. Cross (start,end );
-
Vector3 side = Vector3. Cross (normal,end-start );
-
side. Normalize ( );
-
Vector3 a = start + side *
(lineWidth/ 2 ); -
Vector3 b = start + side *
(lineWidth/ - 2 ); -
Vector3 c = end + side *
(lineWidth/ 2 ); -
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 MakeQuad
AddLine
It wasn't easy to implement, but once I was there it runs prettyfast.
Direct drawing withGL
No fast is fastenough!
Being much easier to implement that the Graphics solution it isa
Conclusion
For massive & dynamic line drawing LineRenderer is not the bestsolution, but it is the only one available in Unity free version.It can surely be useful to draw limited amounts of static lines andthis is probably what it was made for. If you do have Unity3D Pro,the solution with Graphics is reasonable and very flexible but ifit is performance you're after
注:
1、linerender:
- using
UnityEngine; - using
System.Collections; -
- public
class Script1 : MonoBehaviour { -
//LineRenderer -
private LineRenderer lineRenderer; -
//定义一个Vector3,用来存储鼠标点击的位置 -
private Vector3 position; -
//用来索引端点 -
private int index = 0; -
//端点数 -
private int LengthOfLineRenderer=0; -
-
void Start() -
{ -
//添加LineRenderer组件 -
lineRenderer = gameObject.AddComponent(); -
//设置材质 -
lineRenderer.material = new Material(Shader.Find("Particles/Additive")); -
//设置颜色 -
lineRenderer.SetColors(Color.red, Color.yellow); -
//设置宽度 -
lineRenderer.SetWidth(0.02f, 0.02f); -
-
} -
-
void Update() -
{ -
//获取LineRenderer组件 -
lineRenderer = GetComponent(); -
//鼠标左击 -
if (Input.GetMouseButtonDown(0)) -
{ -
//将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中 -
position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1.0f)); -
//端点数+1 -
LengthOfLineRenderer++; -
//设置线段的端点数 -
lineRenderer.SetVertexCount(LengthOfLineRenderer); -
-
} -
//连续绘制线段 -
while (index < LengthOfLineRenderer) -
{ -
//两点确定一条直线,所以我们依次绘制点就可以形成线段了 -
lineRenderer.SetPosition(index, position); -
index++; -
} -
-
-
} -
-
void OnGUI() -
{ -
GUILayout.Label("当前鼠标X轴位置:" + Input.mousePosition.x); -
GUILayout.Label("当前鼠标Y轴位置:" + Input.mousePosition.y); -
} -
-
- }
1. GL immediate drawing functions use whatever is the "currentmaterial" set up right now. The material controls how the renderingis done (blending, textures, etc.) So unless you explicitly set itto something before using GL draw functions, the material canhappen to be anything. Also, if you call any other drawing commandsfrom inside GL drawing code, they can set material to somethingelse, so it's not good to mix them.
2. GL drawing commands execute immediately. That means if you callthem in Start(), they will be executed just once. If you call themin Update(), they will be executed before camera is rendered (andthe camera will most likely clear the screen, making the GL drawingnot visible). So the usual place to call GL drawing is most oftenin OnPostRender from a script that is attached to acamera.
Here's a small example, attach it to a camera:
-
-
static functionCreateLineMaterial ( )
-
{
-
if (!lineMaterial ) { -
"SubShader{ Pass { " + -
" Blend SrcAlphaOneMinusSrcAlpha " + -
" ZWrite Off Cull Off Fog { ModeOff } " + -
" BindChannels {" + -
" Bind\"vertex\",vertex Bind \"color\",color }" + -
"}} }" ); -
} -
}
-
-
{
-
CreateLineMaterial ( ); -
}