unity shader(一)绘制点线圆

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值