line renderer 画线
来源:互联网 发布:pc机安装mac 编辑:程序博客网 时间:2024/06/05 14:47
Unity3D 绘制线条的方法可谓多种多样,但是绘制的线条可以在 Game 视图下面显示的方法并不多,本篇通过 Line Renderer 方法绘制可以在 Game 视图下面显示的线条。原文链接:http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/。
新建立一个 C# 脚本,取名:DrawLinesByLineRender.cs 代码如下:
001
using
UnityEngine;
002
using
System.Collections;
003
004
public
class
DrawLinesByLineRender : MonoBehaviour {
005
006
public
Shader shader;
007
008
private
Vector3 curr;
009
private
Vector3 last =
new
Vector3(0,0,-100.0f);
010
011
private
int
canvasIndex = 0;
012
private
float
lineSizeLarge = 0.02f;
013
private
float
lineSizeSmall = 0.02f;
014
015
private
Color lineColorLarge =
new
Color(0,0,0,0.5f);
016
private
Color lineColorSmall =
new
Color(0,0,0,0.1f);
017
018
private
ArrayList points;
019
020
GUIStyle labelStyle;
021
GUIStyle linkStyle;
022
023
private
float
speed = 100f;
024
025
void
Start () {
026
labelStyle =
new
GUIStyle();
027
labelStyle.normal.textColor = Color.black;
028
029
linkStyle =
new
GUIStyle();
030
linkStyle.normal.textColor = Color.blue;
031
032
points =
new
ArrayList();
033
}
034
035
void
OnGUI() {
036
GUI.Label (
new
Rect (10, 10, 300, 24),
"LR. Cursor keys to rotate (with Shift for slow)"
, labelStyle);
037
int
vc = canvasIndex + points.Count;
038
GUI.Label (
new
Rect (10, 26, 300, 24),
"Drawin "
+ vc +
" lines. 'C' to clear"
, labelStyle);
039
040
GUI.Label (
new
Rect (10, Screen.height - 20, 250, 24),
".Inspired by a demo from "
, labelStyle);
041
if
(GUI.Button (
new
Rect (150, Screen.height - 20, 300, 24),
"mrdoob"
, linkStyle)) {
042
Application.OpenURL(
"http://mrdoob.com/lab/javascript/harmony/"
);
043
}
044
}
045
046
void
Update () {
047
float
sp = speed * Time.deltaTime;
048
if
(Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)) sp = sp * 0.1f;
049
if
(Input.GetKey(KeyCode.UpArrow)) transform.Rotate(-sp, 0, 0);
050
if
(Input.GetKey(KeyCode.DownArrow)) transform.Rotate(sp, 0, 0);
051
if
(Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -sp, 0);
052
if
(Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, sp, 0);
053
054
if
(Input.GetKeyDown(KeyCode.C)) {
055
points =
new
ArrayList();
056
foreach
(Transform line
in
transform) {
057
GameObject go = line.gameObject;
058
Destroy(go.GetComponent(
typeof
(LineRenderer)));
059
Destroy(line);
060
}
061
}
062
063
if
(Input.GetMouseButton(0)) {
064
curr = Camera.main.ScreenToWorldPoint(
new
Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f));
065
curr = transform.InverseTransformPoint(curr);
066
067
if
(last.z != -100.0f) {
068
createLine(last, curr, lineSizeLarge, lineColorLarge);
069
070
foreach
(Vector3 p
in
points) {
071
Vector3 s = p;
072
float
d = Vector3.Distance(s, curr);
073
if
(d < 1 && Random.value > 0.9f) createLine(s, curr, lineSizeSmall, lineColorSmall);
074
}
075
076
points.Add(curr);
077
}
078
079
last = curr;
080
}
else
{
081
last.z = -100.0f;
082
}
083
084
085
}
086
087
private
void
createLine(Vector3 start, Vector3 end,
float
lineSize, Color c) {
088
GameObject canvas =
new
GameObject(
"canvas"
+ canvasIndex);
089
canvas.transform.parent = transform;
090
canvas.transform.rotation = transform.rotation;
091
LineRenderer lines = (LineRenderer) canvas.AddComponent<LineRenderer>();
092
lines.material =
new
Material(shader);
093
lines.material.color = c;
094
lines.useWorldSpace =
false
;
095
lines.SetWidth(lineSize, lineSize);
096
lines.SetVertexCount(2);
097
lines.SetPosition(0, start);
098
lines.SetPosition(1, end);
099
canvasIndex++;
100
}
101
}
新建立一个空对象,然后把 DrawLinesByLineRender.cs 组件挂载到空对象上,如图:
运行游戏,最终效果如图:
0 0
- line renderer 画线
- unity使用line renderer画线
- particle renderer line
- line renderer实现贝塞尔曲线
- Unity Canvas UI line Renderer
- C# 画线 Draw a Line
- XCL-Charts画线图(Line Chart)
- renderer
- renderer
- 【Unity】开发日志5——线性渲染 Line Renderer
- Unity基础,Line Renderer 和射线的应用
- Draw Line and Bar Chart with Matplotlib 画线和图表
- iOS Line drawing tool(画线类, 直线和 折线)
- iOS Line drawing tool(画线类, 直线和 折线)
- opencv3_java 在图形图像上画线LinesOnImage Imgproc.line
- 画线
- 画线
- 画线
- 48. Rotate Image
- 针对MD5加密
- boost::asio::ip::tcp::socket is connected?(如何知道socket的链接是链接或断开?)
- 个人所得税
- Android 上拉刷新下拉加载
- line renderer 画线
- Redis集群部署3.0
- numpy 数组的体会
- 上文程序的调优
- maven配置web项目
- java io
- 作为一个入门级计算机菜鸟,谈谈我对部分主流语言的态度
- Android内存优化之防止OOM篇
- 目标检测