使用unity shader畫線,畫圓


原文鏈接 : http://www.manew.com/thread-98632-1-1.html


基於unity 5.4.1, 因為部分的代碼只有5.4.1的API里面有


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

}
SubShader
{
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          }      }  }



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: