unity vectorline 设置颜色怎么设置画线时间

Unity3D GL方式画线 | Unity3D教程手册
当前位置 :
>> Unity3D GL方式画线
Unity3D GL方式画线
GL方式画线如下图:
相关文章:
Unity3D GL方式画线
把如下脚本放在摄像机上:
using UnityE
using System.C
[RequireComponent(typeof (Camera))]
public class VectorLine : MonoBehaviour
public int numberOfPoints = 2;
public Color lineColor = Color.
public int lineWidth = 3;
public bool drawLines =
private Material lineM
private Vector2[] lineP
void Awake()
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass {" +
BindChannels { Bind \"Color\",color }" +
Blend SrcAlpha OneMinusSrcAlpha" +
ZWrite Off Cull Off Fog { Mode Off }" +
lineMaterial.hideFlags = HideFlags.HideAndDontS
lineMaterial.shader.hideFlags = HideFlags.HideAndDontS
// Creates a simple two point line
void Start()
linePoints = new Vector2[2];
// Sets line endpoints to center of screen and mouse position
void Update()
linePoints[0] = new Vector2(0.5f, 0.5f);
linePoints[1] = new Vector2(Input.mousePosition.x/Screen.width, Input.mousePosition.y/Screen.height);
void OnPostRender()
if (!drawLines || linePoints == null || linePoints.Length & 2)
float nearClip = cam.nearClipPlane + 0.00001f;
int end = linePoints.Length - 1;
float thisWidth = 1f/Screen.width * lineWidth * 0.5f;
lineMaterial.SetPass(0);
GL.Color(lineColor);
if (lineWidth == 1)
GL.Begin(GL.LINES);
for (int i = 0; i & ++i)
GL.Vertex(cam.ViewportToWorldPoint(new Vector3(linePoints[i].x, linePoints[i].y, nearClip)));
GL.Vertex(cam.ViewportToWorldPoint(new Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip)));
GL.Begin(GL.QUADS);
for (int i = 0; i & ++i)
Vector3 perpendicular = (new Vector3(linePoints[i+1].y, linePoints[i].x, nearClip) -
new Vector3(linePoints[i].y, linePoints[i+1].x, nearClip)).normalized * thisW
Vector3 v1 = new Vector3(linePoints[i].x, linePoints[i].y, nearClip);
Vector3 v2 = new Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip);
GL.Vertex(cam.ViewportToWorldPoint(v1 - perpendicular));
GL.Vertex(cam.ViewportToWorldPoint(v1 + perpendicular));
GL.Vertex(cam.ViewportToWorldPoint(v2 + perpendicular));
GL.Vertex(cam.ViewportToWorldPoint(v2 - perpendicular));
void OnApplicationQuit()
DestroyImmediate(lineMaterial);
【上一篇】
【下一篇】
您可能还会对这些文章感兴趣!unity GUI绘制直线条 - CSDN博客
unity GUI绘制直线条
using UnityE
using System.C
public class DrawLine : MonoBehaviour {
& & public Vector2[] m_//特征点位置
& & public Color m_lineC
& & private static Texture2D m_//最终渲染得到的带有折线的纹理
& & public void InitCanvas(Vector2[] point, int width, int height) {
& & & & m_point =
& & & & m_texure = new Texture2D(width,height);
& & public IEnumerator Draw()
& & & & //清空纹理对象
& & & & for (int i = 0; i & 100; i++)
& & & & & & for (int j = 0; j & 100; j++)
& & & & & & {
& & & & & & & & m_texure.SetPixel(i, j, Color.white);
& & & & & & }
& & & & Vector2 currentPoint = m_point[0];
& & & & for (int i = 1; i & m_point.L i++) {
& & & & & & for (float j = 0; j & 1; j = j + 0.01f) {
& & & & & & & & Vector2 temp = Vector2.Lerp(m_point[i-1],m_point[i],j);
& & & & & & & & m_texure.SetPixel(Convert.ToInt32(temp.x),Convert.ToInt32(temp.y),m_lineColor);
& & & & & & }
& & & & & & currentPoint = m_point[i];
& & & & m_texure.Apply();
& & & & yield return m_
& & void OnPostRender()
& & & & StartCoroutine(Draw());
& & void Start() {
& & & & InitCanvas(m_point, 100, 100);
& & void OnGUI()
& & & & GUI.DrawTexture(new Rect(0, 0, 100, 100), m_texure);
本文已收录于以下专栏:
相关文章推荐
LineRenderer线渲染器主要是用于在3D中渲染线段,虽然我们也可以使用GL图像库来渲染线段,但是使用LineRenderer我们可以对线段进行更多的操作,例如:设置颜色,宽度等。在这里要注意L...
由于要做Native2D的A* 算法寻路,所以必须进行Scene的网格划分,而进行了网格划分需要进行可视化的调试,需求就是这个逻辑。具体是实现如下效果,这里绿色的区域就是角色不能进入的区域...
U3D目前发现的一个画线最好的工具。
画一条直线
// Make Vector2 in this case we just use 2 elements...
var lin...
不记得在哪里看到的代码,效果很简单,修改了一下,可以用,还是有一定的参考价值
using UnityE
using System.C
public class ...
图形化调试可以加速开发。
例如在战斗中,可能需要知道所有单位的仇恨值,如果这些信息全打log的话,很难有直观感受,
而如果在Scene窗口里,单位头顶有一个球,越红表示仇恨越高,越暗表示仇...
雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:/archives/561
在Unity3D中绘制一个平面图形可不容易,可别惯性思维觉得,嗯,这引擎3D的事情都能解决了,2D的不叫事情。其实除去布置UI,在Unity3D中绘制一个2D平面图形是很蛋疼的一件事情。不然的话,也不...
private var cur : int = 0;
private var orgP
private var endP
static var lineMat...
using UnityE
using System.C
public class Line : MonoBehaviour
public Materia...
Unity中连线
       在unity中做连线的功能,有很多中做法。在这里总结一下,可能还有更好的方法,希望有好的方法就提出来共同进步。(说明:主要是拐直角的线的效果,而不是很华丽的线条效果。...
他的最新文章
讲师:何宇健
讲师:董岩
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Unity(87)
参考博客 :
最终实现效果:(数字显示的位置不对,找了很久也不知道什么原因,有大神知道后,评论一下)
就直接上代码了:
拖动小球移动的代码:
using UnityE
using System.C
public class DragToMove : MonoBehaviour {
void Update()
if(Input.GetMouseButton(0))
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray.origin,ray.direction, out hit))
Vector3 hitPoint = hit.
transform.position = new Vector3(hitPoint.x, 3.5f, hitPoint.z);
显示数字的代码:
using UnityE
using System.C
public class ShowSizeManager : MonoBehaviour
public static ShowSizeManager _I
public TextM
public TextM
public TextM
public TextM
void Start()
_Instance =
one = transform.FindChild(&1&).GetComponent&TextMesh&();
two = transform.FindChild(&2&).GetComponent&TextMesh&();
three = transform.FindChild(&3&).GetComponent&TextMesh&();
four = transform.FindChild(&4&).GetComponent&TextMesh&();
public void ShowText(Vector3 point, float distance, string type)
distance = Mathf.Round(distance);
switch (type)
one.GetComponent&MeshRenderer&().enabled =
one.transform.position =
one.text = distance.ToString();
two.GetComponent&MeshRenderer&().enabled =
two.transform.position =
two.text = distance.ToString();
three.GetComponent&MeshRenderer&().enabled =
three.transform.position =
three.text = distance.ToString();
four.GetComponent&MeshRenderer&().enabled =
four.transform.position =
four.text = distance.ToString();
transform.LookAt(-Camera.main.transform.position);
public void HideMesh(string type)
switch(type)
case &1&: one.GetComponent&MeshRenderer&().enabled =
case &2&: one.GetComponent&MeshRenderer&().enabled =
case &3&: one.GetComponent&MeshRenderer&().enabled =
case &4&: one.GetComponent&MeshRenderer&().enabled =
画线的代码:
using UnityE
using System.C
public class RenderSize : MonoBehaviour
public static RenderSize _
public GameO
private LineR
private LineR
private LineR
private LineR
void Start()
_instance =
forward = transform.FindChild(&forward&).GetComponent&LineRenderer&();
back = transform.FindChild(&back&).GetComponent&LineRenderer&();
left = transform.FindChild(&left&).GetComponent&LineRenderer&();
right = transform.FindChild(&right&).GetComponent&LineRenderer&();
void FixedUpdate()
RayCheck(go);
private void RayCheck(GameObject go)
Check(go, Vector3.forward, forward, &1&);
Check(go, Vector3.back, back, &2&);
Check(go, Vector3.left, left, &3&);
Check(go, Vector3.right, right, &4&);
private void Check(GameObject go, Vector3 Direction, LineRenderer line, string type)
if (Physics.Raycast(go.transform.position, Direction, out hit, 100))
line.enabled =
Debug.DrawLine(go.transform.position, hit.point, Color.red);
Vector3 v0 = go.transform.
Vector3 v2 = hit.
line.SetPosition(0, v0);
line.SetPosition(1, v2);
float distance = Vector3.Distance(go.transform.position, hit.point);
Vector3 point = (transform.position + hit.point) / 2;
ShowSizeManager._Instance.ShowText(point, distance, type);
line.enabled =
ShowSizeManager._Instance.HideMesh(type);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:131254次
积分:2649
积分:2649
排名:第14980名
原创:108篇
转载:75篇
译文:14篇
(1)(5)(5)(15)(9)(14)(8)(7)(19)(26)(5)(42)(6)(6)(2)(22)(1)(4)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'Unity学习(九)Bresenham快速画直线算法
在游戏中求直线,一般情况会想到通过斜率计算,但是这会涉及到大量的浮点运算,一旦玩家过多,还是会有比较大的消耗。
通常在电脑中画直线,圆形等各种形状并不是真正的直线,因为屏幕的像素非常小,所以肉眼看起来很像直线,其实都是由格子组成的。底层的算法比较多,这里就先学习Bresenham算法,它是光栅化的画直线算法。直线光栅化是指用像素点来模拟直线,如下图:
以上图为例,进行推导模拟:
1.起点为x1,y1时:y轴误差量为e1,e1+m=0.7&0.5,那么e2=e1+m-1=-0.3,y=y1+1=y2。
2.起点为x2,y2时:y轴误差量为e2,e2+m=-0.3+0.7=0.4,那么e3=e2+m=0.4,y=y2。
3.起点为x3,y2时:y轴误差量为e3,e3+m=0.4+0.7=1.1&0.5,那么e4=e3-1=0.1,y=y2+1=y3。
4.起点为x4,y3时:以此类推...知道x轴到达终点。
其中e+m&0.5,可以表示为2*(e+m) & 1
根据起点和终点可以得出x轴增量:dx。将上面的公式都乘以dx。
可以得到:
e= e*dx+m*dx = eps + dy
e= e+m-1 = eps+dy-dx
实现流程:
1.首先判断x,y的增量,谁大就累加谁。(上面是坐标系的右上方向)
2.循环累加x,累加误差量eps+dy
3.判断eps+dy是否大于dx,如果小于不做处理,如果大于等于dx,那么新的y=y+1,新的eps=eps-dx。
为了兼容xy坐标系的各个方向,我们可以先算出增量的方向,比如上面如果是坐标系的左下方向,y=y-1。
代码如下:
public&bool&LinePath(int&x1,&int&y1,&int&x2,&int&y2,&ref&List&mpaths)&&
&&&&int&dx&=&x2&-&x1;&&
&&&&int&dy&=&y2&-&y1;&&
&&&&int&ux&=&(dx&&&0)&?&1&:&-1;&
&&&&int&uy&=&(dy&&&0)&?&1&:&-1;
&&&&int&x&=&x1,&y&=&y1,&
&&&&x2&+=&&y2&+=&&&
&&&&eps&=&0;&dx&=&Mathf.Abs(dx);&dy&=&Mathf.Abs(dy);&&
&&&&if&(dx&&&dy)&&
&&&&&&&&for&(x&=&x1;&x&!=&x2;&x&+=&ux)&&
&&&&&&&&{&&
&&&&&&&&&&&&mpaths.Add(new&Vector2(x,&y));&&
&&&&&&&&&&&&eps&+=&&&
&&&&&&&&&&&&if&((eps&&&&1)&&=&dx)&&
&&&&&&&&&&&&{&&
&&&&&&&&&&&&&&&&y&+=&&&
&&&&&&&&&&&&&&&&eps&-=&&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&else&&
&&&&&&&&for&(y&=&y1;&y&!=&y2;&y&+=&uy)&&
&&&&&&&&{&&
&&&&&&&&&&&&mpaths.Add(new&Vector2(x,&y));&&
&&&&&&&&&&&&eps&+=&&&
&&&&&&&&&&&&if&((eps&&&&1)&&=&dy)&&
&&&&&&&&&&&&{&&
&&&&&&&&&&&&&&&&x&+=&&eps&-=&&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&return&true;&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 unity vector3 赋值 的文章

 

随机推荐