国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

UnityShader3實現2D描邊效果

2020-01-24 00:09:21
字體:
來源:轉載
供稿:網友

本文實例為大家分享了UnityShader3實現2D描邊效果的具體代碼,供大家參考,具體內容如下

1.

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.

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.如下圖,左邊是一個Image,右邊是一個Plane。

// 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))     {   //點旋轉公式:  //假設對圖片上任意點(x,y),繞一個坐標點(rx0,ry0)逆時針旋轉a角度后的新的坐標設為(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作用是調整流動顏色的比例     return _EdgeColor * (1 - temp) + _FlowColor * temp;    }     else     {      //fixed4 color = tex2D(_MainTex, i.uv);      return fixed4(1, 1, 1, 0);     }    }    ENDCG   }  } } 

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

如上圖,左下是內邊反鋸齒的圖,右上是未經處理的圖。

// 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作用是調整流動顏色的比例  fixed4 finalColor = _EdgeColor * (1 - temp) + _FlowColor * temp;    finalColor.a = alpha; return finalColor;    }    ENDCG   }  } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闸北区| 唐海县| 水城县| 临沂市| 阆中市| 平泉县| 和平区| 内江市| 抚松县| 柘城县| 黄陵县| 盐山县| 巴楚县| 建瓯市| 辽中县| 台湾省| 兴业县| 黎川县| 方正县| 永善县| 大姚县| 宁远县| 育儿| 滨州市| 越西县| 南宫市| 顺平县| 绥滨县| 常熟市| 锡林郭勒盟| 年辖:市辖区| 兰州市| 大洼县| 镇宁| 望江县| 朝阳区| 娱乐| 洛浦县| 太谷县| 公安县| 田林县|