一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - UnityShader3實現2D描邊效果

UnityShader3實現2D描邊效果

2022-03-11 11:30lyh916 C#

這篇文章主要為大家詳細介紹了UnityShader3實現2D描邊效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

1.

UnityShader3實現2D描邊效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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實現2D描邊效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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。

UnityShader3實現2D描邊效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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出現了鋸齒。而解決鋸齒一般的方法就是做模糊處理,模糊處理一般又有貼圖處理和代碼處理之分,這里使用的是貼圖處理。貼圖處理需要提供一張邊界模糊的貼圖。

UnityShader3實現2D描邊效果

UnityShader3實現2D描邊效果

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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 
    
  
}

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

原文鏈接:https://blog.csdn.net/lyh916/article/details/51487918

延伸 · 閱讀

精彩推薦
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
主站蜘蛛池模板: 国产欧美又粗又猛又爽老 | 青青久久精品国产 | 免费成年网站 | 欧美a在线 | 大陆国语自产精品视频在 | 秋霞宅宅236理论片 秋霞一级黄色片 | 人禽l交视频在线播放 视频 | 亚洲AV久久久噜噜噜久久 | 午夜勾魂曲 | 欧美成人aa久久狼窝动画 | 91精品国产色综合久久 | 青青青国产视频 | 精品国产欧美一区二区三区成人 | 亚洲第一二三四区 | 亚洲第一天堂无码专区 | 天天射夜夜爽 | 欧美一级高清片免费一级 | 精品欧美一区二区三区在线观看 | 亚洲高清中文字幕一区二区三区 | 日本中文字幕一区二区高清在线 | 小舞丝袜调教喷水沦为肉奴 | zzjj中国| 国产一区二区三区在线看 | 精品国产乱码久久久久久免费流畅 | 射18p| 青青草在线观看 | 天天综合天天综合 | 亚洲天天综合网 | 激情五月姐姐 | 成年人免费在线播放 | japanesen女同| 亚洲精品网址 | 亚洲一二三区视频 | 2020精品极品国产色在线观看 | 亚洲波霸 | 2019中文字幕 | 亚洲国产欧美久久香综合 | 99色亚洲 | 成人欧美视频在线看免费 | aaa大片 | 日本黄a |