## 赖张殷-Eagle

Shader "Custom/Draw"
{
Properties
{
_Point1("Point1",vector) = (100,100,0,0)
_Point2("Point2",vector) = (200,200,0,0)
_LP1("linePoint1",vector) = (300,100,0,0)
_LP2("linePoint2",vector) = (600,400,0,0)
_LineWidth("LineWidth",range(1,20)) = 2.0

}
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
};

struct v2f
{
float4 vertex : SV_POSITION;
};
float4 _Point1;
float4 _Point2;
float4 _LP1;
float4 _LP2;
float _LineWidth;

v2f vert (appdata v)
{
v2f o;  
		//5.4.1以上的API才有的代码
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
//绘制圆形，此处半径使用了固定值1000和500,当然大家也可以把他们写成可调的参数
if( pow((i.vertex.x- _Point1.x ),2) + pow((i.vertex.y- _Point1.y ),2) <1000   )
{
return fixed4(0,1,0,1);
}
if( pow((i.vertex.x- _Point2.x ),2) + pow((i.vertex.y- _Point2.y ),2) <500   )
{
return fixed4(1,0,0,1);
}

//绘制直线上两点
if( pow((i.vertex.x- _LP1.x ),2) + pow((i.vertex.y- _LP1.y ),2) <100   )
{
return fixed4(0,0,1,1);
}
if( pow((i.vertex.x- _LP2.x ),2) + pow((i.vertex.y- _LP2.y ),2) <100   )
{
return fixed4(0,0,1,1);
}

//计算点到直线的距离
float d = abs((_LP2.y-_LP1.y)*i.vertex.x + (_LP1.x - _LP2.x)*i.vertex.y +_LP2.x*_LP1.y -_LP2.y*_LP1.x )/sqrt(pow(_LP2.y-_LP1.y,2) + pow(_LP1.x-_LP2.x,2));
//小于或者等于线宽的一半时，属于直线范围
if(d<=_LineWidth/2)
{
return fixed4(0.8,0.2,0.5,1);
}

//绘制网格直线
if( (unsigned int)i.vertex.x% (unsigned int)(0.25*_ScreenParams.x)==0 )
{
return fixed4(0,0,1,1);
}
if( (unsigned int)i.vertex.y% (unsigned int)(0.1*_ScreenParams.x)==0 )
{
return fixed4(0,0,1,1);
}
//默认返回白色
return fixed4(1,1,1,1);

}
ENDCG
}
}
}

2016-10-25 21:06:50

2015-08-14 19:00:14

#### Unity3d Unity 几种画线方式

2017-11-25 11:37:45

#### Unity实现画线条功能

2016-07-27 15:34:07

2015-03-14 23:22:08

2014-11-01 23:56:15

#### Unity中画实线与虚线

2016-11-24 14:58:02

#### 【Unity3d开发记录】Unity动态画曲线、弧线（GL画线）在UI上的显示

2016-04-28 07:59:40

#### Unity GUI（uGUI）扩展实例：自定义曲线控件

2017-01-05 11:52:33

#### Unity3D 自定义UI组件画圆、圆弧、扇形

2017-04-11 11:59:56