Shader "Custom/Draw"
{
Properties
{
_Point1("Point1",Vector) = (100,100,0,0)
_Radius1("Radius1",Float) = 1000
_Point2("Point2",Vector) = (200,200,0,0)
_Radius2("Radius2",Float) = 500
_LP1("linePoint1",Vector) = (300,100,0,0)
_LP2("linePoint2",Vector) = (600,400,0,0)
_LineWidth("LineWidth",Range(1,20)) = 2.0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Point1;
float4 _Point2;
float _Radius1;
float _Radius2;
float4 _LP1,_LP2;
float _LineWidth;
struct appdata
{
float4 vertex:POSITION;
};
struct v2f
{
float4 vertex:SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i):SV_Target
{
//绘制圆形 此处 pow(x,2) + pow(y,2) = pow(radius,2) 遗留问题边缘抗锯齿 需要模糊
if (pow((i.vertex.x - _Point1.x),2) + pow((i.vertex.y - _Point1.y),2) < _Radius1)
{
return fixed4(0,1,0,1);
}
if (pow((i.vertex.x - _Point2.x),2) + pow((i.vertex.y - _Point2.y),2) < _Radius2)
{
return fixed4(1,0,0,1);
}
//绘制点 与圆形同理 点到圆心的距离公式 pow(x,2) + pow(y,2) = pow(radius,2)
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);
}
//点到直线的距离公式 两点组成直线 (x1,y1)和(x2,y2)
// d = abs(A*X + B * Y + C)/sqrt(pow(A,2) + pow(B,2))
//斜率相等 (y1-y2)/(x1-x2) = (y - y1)/(x - x1)
//直线方程 (y2 - y1)* x + (x1 - x2) * y + x2 * y1 - y2 * x1 = 0
// A = y2 - y1 ,B = x1 - x2,C = x2 * y1 - y2 * x1
//计算点到直线的距离
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.y) == 0)
{
return fixed4(0,0,1,1);
}
return fixed4(1,1,1,1);
}
ENDCG
}
}
FallBack "Diffuse"
}
//参考 https://www.cnblogs.com/yanhuiqingkong/p/7770075.html
08-20
1352
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-30
3613
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-02
2062
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)