使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果-創(chuàng)新互聯(lián)

使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供六枝網(wǎng)站建設(shè)、六枝做網(wǎng)站、六枝網(wǎng)站設(shè)計(jì)、六枝網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、六枝企業(yè)網(wǎng)站模板建站服務(wù),十多年六枝做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
Shader "Custom/Edge"
{
 Properties
 {
 _MainTex ("Texture", 2D) = "white" {}
 _OffsetUV ("OffsetUV", Range(0, 1)) = 0.1
 _EdgeColor ("EdgeColor", Color) = (1, 0, 0, 1)
 _AlphaTreshold ("Treshold", Range(0, 1)) = 0.5 
 }
 SubShader
 {
 Tags { "Queue" = "Transparent" }
 Blend SrcAlpha OneMinusSrcAlpha
 
 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 struct appdata
 {
 float4 vertex : POSITION;
 fixed2 uv : TEXCOORD0;
 };
 
 struct v2f
 { 
 float4 vertex : SV_POSITION;
 fixed2 uv[5] : TEXCOORD0;
 };
 
 sampler2D _MainTex;
 float4 _MainTex_ST;
 fixed _OffsetUV;
 fixed4 _EdgeColor;
 fixed _AlphaTreshold;
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 
 o.uv[0] = v.uv; 
    o.uv[1] = v.uv + fixed2(0, _OffsetUV); //up 
    o.uv[2] = v.uv + fixed2(-_OffsetUV, 0); //left 
    o.uv[3] = v.uv + fixed2(0, -_OffsetUV); //bottom 
    o.uv[4] = v.uv + fixed2(_OffsetUV, 0); //right 
 
 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 {
 fixed4 original = tex2D(_MainTex, i.uv[0]); 
    fixed alpha = original.a;
 
    fixed p1 = tex2D(_MainTex, i.uv[1]).a; 
    fixed p2 = tex2D(_MainTex, i.uv[2]).a; 
    fixed p3 = tex2D(_MainTex, i.uv[3]).a; 
    fixed p4 = tex2D(_MainTex, i.uv[4]).a; 
  
    alpha = p1 + p2 + p3 + p4 + alpha; 
    alpha /= 5; 
 
    if (alpha < _AlphaTreshold) original.rgb = _EdgeColor.rgb; 
  
    return original; 
 }
 ENDCG
 }
 }
}

2.

使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果

Shader "Custom/Edge"
{
 Properties
 {
 _Edge ("Edge", Range(0, 0.2)) = 0.043
 _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1)
 _MainTex ("MainTex", 2D) = "white" {}
 }
 SubShader
 {
 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 fixed _Edge;
 fixed4 _EdgeColor;
 sampler2D _MainTex;
 
 struct appdata
 {
 float4 vertex : POSITION;
 fixed2 uv : TEXCOORD0;
 };
 
 struct v2f
 {
 float4 vertex : SV_POSITION;
 float4 objVertex : TEXCOORD0;
 fixed2 uv : TEXCOORD1;
 };
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 o.objVertex = v.vertex;
 o.uv = v.uv;
 
 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 { 
 fixed x = i.uv.x;
 fixed y = i.uv.y;
  
 if((x < _Edge) || (abs(1 - x) < _Edge) || (y < _Edge) || (abs(1 - y) < _Edge)) 
 {
  return _EdgeColor * abs(cos(_Time.y));
 }
 else 
 {
  fixed4 color = tex2D(_MainTex, i.uv);
  return color;
 }
 
 //return i.objVertex;
 //return fixed4(i.uv, 0, 1);
 }
 ENDCG
 }
 }
}

3.如下圖,左邊是一個(gè)Image,右邊是一個(gè)Plane。

使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
Shader "Custom/Edge" 
{ 
 Properties 
 { 
  _Edge ("Edge", Range(0, 0.2)) = 0.043 
  _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1) 
 _FlowColor ("FlowColor", Color) = (1, 1, 1, 1) 
 _FlowSpeed ("FlowSpeed", Range(0, 10)) = 3
 _MainTex ("MainTex", 2D) = "white" {} 
 } 
 SubShader 
 { 
 Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" } 
 
  Pass 
  { 
 ZWrite Off 
 Blend SrcAlpha OneMinusSrcAlpha 
 
   CGPROGRAM 
   #pragma vertex vert 
   #pragma fragment frag 
   #include "UnityCG.cginc" 
 
   fixed _Edge; 
   fixed4 _EdgeColor; 
 fixed4 _FlowColor;
 float _FlowSpeed;
 sampler2D _MainTex;
 
   struct appdata 
   { 
    float4 vertex : POSITION; 
    fixed2 uv : TEXCOORD0; 
   }; 
 
   struct v2f 
   { 
    float4 vertex : SV_POSITION; 
    fixed2 uv : TEXCOORD1; 
   }; 
 
   v2f vert (appdata v) 
   { 
    v2f o; 
    o.vertex = UnityObjectToClipPos(v.vertex); 
    o.uv = v.uv; 
    return o; 
   } 
    
   fixed4 frag (v2f i) : SV_Target 
   {  
    fixed x = i.uv.x; 
    fixed y = i.uv.y; 
 
    if((x < _Edge) || (abs(1 - x) < _Edge) || (y < _Edge) || (abs(1 - y) < _Edge)) 
    { 
  //點(diǎn)旋轉(zhuǎn)公式:
  //假設(shè)對圖片上任意點(diǎn)(x,y),繞一個(gè)坐標(biāo)點(diǎn)(rx0,ry0)逆時(shí)針旋轉(zhuǎn)a角度后的新的坐標(biāo)設(shè)為(x0,y0),有公式:
  //x0 = (x - rx0) * cos(a) - (y - ry0) * sin(a) + rx0 ;
  //y0 = (x - rx0) * sin(a) + (y - ry0) * cos(a) + ry0 ;
 
  float a = _Time.y * _FlowSpeed; 
  float2 rotUV;
 
  x -= 0.5;
  y -= 0.5;
  rotUV.x = x * cos(a) - y * sin(a) + 0.5;
  rotUV.y = x * sin(a) + y * cos(a) + 0.5;
  
  fixed temp = saturate(rotUV.x - 0.5);//-0.5作用是調(diào)整流動(dòng)顏色的比例
     return _EdgeColor * (1 - temp) + _FlowColor * temp;
    } 
    else 
    { 
     //fixed4 color = tex2D(_MainTex, i.uv); 
     return fixed4(1, 1, 1, 0); 
    } 
   } 
   ENDCG 
  } 
 } 
}

4.通過觀察上面的效果圖,會(huì)發(fā)現(xiàn)右邊的Plane出現(xiàn)了鋸齒。而解決鋸齒一般的方法就是做模糊處理,模糊處理一般又有貼圖處理和代碼處理之分,這里使用的是貼圖處理。貼圖處理需要提供一張邊界模糊的貼圖。

使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果

使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果

如上圖,左下是內(nèi)邊反鋸齒的圖,右上是未經(jīng)處理的圖。

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 
 
Shader "Custom/Edge2" 
{ 
 Properties 
 { 
  _Edge ("Edge", Range(0, 0.2)) = 0.043 
  _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1) 
  _FlowColor ("FlowColor", Color) = (1, 1, 1, 1) 
  _FlowSpeed ("FlowSpeed", Range(0, 10)) = 3 
  _MainTex ("MainTex", 2D) = "white" {} 
 } 
 SubShader 
 { 
  Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" } 
 
  Pass 
  { 
   ZWrite Off 
   Blend SrcAlpha OneMinusSrcAlpha 
 
   CGPROGRAM 
   #pragma vertex vert 
   #pragma fragment frag 
   #include "UnityCG.cginc" 
 
   fixed _Edge; 
   fixed4 _EdgeColor; 
   fixed4 _FlowColor; 
   float _FlowSpeed; 
   sampler2D _MainTex; 
 
   struct appdata 
   { 
    float4 vertex : POSITION; 
    fixed2 uv : TEXCOORD0; 
   }; 
 
   struct v2f 
   { 
    float4 vertex : SV_POSITION; 
    fixed2 uv : TEXCOORD1; 
   }; 
 
   v2f vert (appdata v) 
   { 
    v2f o; 
    o.vertex = UnityObjectToClipPos(v.vertex);  
    o.uv = v.uv; 
    return o; 
   } 
    
   fixed4 frag (v2f i) : SV_Target 
   {    
 fixed4 color = tex2D(_MainTex, i.uv);
 float alpha = color.a;
 
 fixed x = i.uv.x; 
    fixed y = i.uv.y; 
 float a = _Time.y * _FlowSpeed; 
    float2 rotUV; 
 
    x -= 0.5; 
    y -= 0.5; 
    rotUV.x = x * cos(a) - y * sin(a) + 0.5; 
    rotUV.y = x * sin(a) + y * cos(a) + 0.5; 
      
    fixed temp = saturate(rotUV.x - 0.5);//-0.5作用是調(diào)整流動(dòng)顏色的比例 
 fixed4 finalColor = _EdgeColor * (1 - temp) + _FlowColor * temp;  
 
 finalColor.a = alpha;
 return finalColor; 
   } 
   ENDCG 
  } 
 } 
}

關(guān)于使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道了解更多相關(guān)知識。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站標(biāo)題:使用UnityShader3怎么實(shí)現(xiàn)2D描邊效果-創(chuàng)新互聯(lián)
URL分享:http://muchs.cn/article48/dshdhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站營銷、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站策劃、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)