服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity shader实现百叶窗特效

Unity shader实现百叶窗特效

2022-08-10 10:56张宇成 C#

这篇文章主要为大家详细介绍了Unity shader实现百叶窗特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了unity shader百叶窗展示的具体代码,供大家参考,具体内容如下

1.将图片划分为水平n栏,代码如下:

?
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
shader "unlit/bycshader"
{
 properties
 {
  [perrendererdata] _maintex ("sprite texture", 2d) = "white" {}
  _color ("tint", color) = (1,1,1,1)
 
  _stencilcomp ("stencil comparison", float) = 8
  _stencil ("stencil id", float) = 0
  _stencilop ("stencil operation", float) = 0
  _stencilwritemask ("stencil write mask", float) = 255
  _stencilreadmask ("stencil read mask", float) = 255
 
  _colormask ("color mask", float) = 15
 
  _lan("lan",float) = 10
 
  [toggle(unity_ui_alphaclip)] _useuialphaclip ("use alpha clip", float) = 0
 }
 
 subshader
 {
  tags
  {
   "queue"="transparent"
   "ignoreprojector"="true"
   "rendertype"="transparent"
   "previewtype"="plane"
   "canusespriteatlas"="true"
  }
 
  stencil
  {
   ref [_stencil]
   comp [_stencilcomp]
   pass [_stencilop]
   readmask [_stencilreadmask]
   writemask [_stencilwritemask]
  }
 
  cull off
  lighting off
  zwrite off
  ztest [unity_guiztestmode]
  blend srcalpha oneminussrcalpha
  colormask [_colormask]
 
  pass
  {
   name "default"
  cgprogram
   #pragma vertex vert
   #pragma fragment frag
   #pragma target 2.0
 
   #include "unitycg.cginc"
   #include "unityui.cginc"
 
   #pragma multi_compile __ unity_ui_clip_rect
   #pragma multi_compile __ unity_ui_alphaclip
 
   struct appdata_t
   {
    float4 vertex : position;
    float4 color : color;
    float2 texcoord : texcoord0;
    unity_vertex_input_instance_id
   };
 
   struct v2f
   {
    float4 vertex : sv_position;
    fixed4 color : color;
    float2 texcoord : texcoord0;
    float4 worldposition : texcoord1;
    unity_vertex_output_stereo
   };
 
   v2f vert(appdata_t v)
   {
    v2f out;
    unity_setup_instance_id(v);
    unity_initialize_vertex_output_stereo(out);
    out.worldposition = v.vertex;
    out.vertex = unityobjecttoclippos(out.worldposition);
 
    out.texcoord = v.texcoord;
 
    out.color = v.color;
    return out;
   }
 
   sampler2d _maintex;
 
   float _lan;
 
   float4 frag(v2f in) : sv_target
   {
    //從這裡開始
    float2 uv = in.texcoord;
 
    uv.x*= _lan;
    uv.x = frac(uv.x);
    return float4(uv.xy,1.0,1.0);
   }
  endcg
  }
 }
}

Unity shader实现百叶窗特效

如上图,划分为n栏后,对每一栏进行单独处理,即可做到每一栏都同时进行颜色消减。

2.对每一栏同时进行颜色消减(控制阈值可以通过c#代码实现)

代码如下:

?
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
shader "unlit/bycshader"
{
 properties
 {
  [perrendererdata] _maintex ("sprite texture", 2d) = "white" {}
  _color ("tint", color) = (1,1,1,1)
 
  _stencilcomp ("stencil comparison", float) = 8
  _stencil ("stencil id", float) = 0
  _stencilop ("stencil operation", float) = 0
  _stencilwritemask ("stencil write mask", float) = 255
  _stencilreadmask ("stencil read mask", float) = 255
 
  _colormask ("color mask", float) = 15
 
  _lan("lan",float) = 10
  _stepx("stepx",range(0.0,1.0))=1.0
 
  [toggle(unity_ui_alphaclip)] _useuialphaclip ("use alpha clip", float) = 0
 }
 
 subshader
 {
  tags
  {
   "queue"="transparent"
   "ignoreprojector"="true"
   "rendertype"="transparent"
   "previewtype"="plane"
   "canusespriteatlas"="true"
  }
 
  stencil
  {
   ref [_stencil]
   comp [_stencilcomp]
   pass [_stencilop]
   readmask [_stencilreadmask]
   writemask [_stencilwritemask]
  }
 
  cull off
  lighting off
  zwrite off
  ztest [unity_guiztestmode]
  blend srcalpha oneminussrcalpha
  colormask [_colormask]
 
  pass
  {
   name "default"
  cgprogram
   #pragma vertex vert
   #pragma fragment frag
   #pragma target 2.0
 
   #include "unitycg.cginc"
   #include "unityui.cginc"
 
   #pragma multi_compile __ unity_ui_clip_rect
   #pragma multi_compile __ unity_ui_alphaclip
 
   struct appdata_t
   {
    float4 vertex : position;
    float4 color : color;
    float2 texcoord : texcoord0;
    unity_vertex_input_instance_id
   };
 
   struct v2f
   {
    float4 vertex : sv_position;
    fixed4 color : color;
    float2 texcoord : texcoord0;
    float4 worldposition : texcoord1;
    unity_vertex_output_stereo
   };
 
   v2f vert(appdata_t v)
   {
    v2f out;
    unity_setup_instance_id(v);
    unity_initialize_vertex_output_stereo(out);
    out.worldposition = v.vertex;
    out.vertex = unityobjecttoclippos(out.worldposition);
 
    out.texcoord = v.texcoord;
 
    out.color = v.color;
    return out;
   }
 
   sampler2d _maintex;
 
   float _lan;
   float _stepx;
 
   float4 frag(v2f in) : sv_target
   {
    
    float2 uv = in.texcoord;
    uv.x*= _lan;
 uv.x = frac(uv.x);
 
 //從這裡開始,顏色值大於指定值stepx的進行消減
 int needdiscard = step(_stepx,uv.x);
 if(needdiscard == 1){
  discard;
 }
 return float4(uv.xy,1.0,1.0);
   }
  endcg
  }
 }
}

效果如下:

Unity shader实现百叶窗特效

3.加上切变,百叶窗在关闭打开时,是有透视变化的。用切变可以近似模拟透视,因为透视的实现代价很大,所以用切变。

添加一张图片,并进行切变

Unity shader实现百叶窗特效

代码如下:

?
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
shader "unlit/bycshader"
{
 properties
 {
  [perrendererdata] _maintex ("sprite texture", 2d) = "white" {}
  _color ("tint", color) = (1,1,1,1)
 
  _stencilcomp ("stencil comparison", float) = 8
  _stencil ("stencil id", float) = 0
  _stencilop ("stencil operation", float) = 0
  _stencilwritemask ("stencil write mask", float) = 255
  _stencilreadmask ("stencil read mask", float) = 255
 
  _colormask ("color mask", float) = 15
 
  _lan("lan",float) = 10
  _stepx("stepx",range(0.0,1.0))=1.0
 
  [toggle(unity_ui_alphaclip)] _useuialphaclip ("use alpha clip", float) = 0
 }
 
 subshader
 {
  tags
  {
   "queue"="transparent"
   "ignoreprojector"="true"
   "rendertype"="transparent"
   "previewtype"="plane"
   "canusespriteatlas"="true"
  }
 
  stencil
  {
   ref [_stencil]
   comp [_stencilcomp]
   pass [_stencilop]
   readmask [_stencilreadmask]
   writemask [_stencilwritemask]
  }
 
  cull off
  lighting off
  zwrite off
  ztest [unity_guiztestmode]
  blend srcalpha oneminussrcalpha
  colormask [_colormask]
 
  pass
  {
   name "default"
  cgprogram
   #pragma vertex vert
   #pragma fragment frag
   #pragma target 2.0
 
   #include "unitycg.cginc"
   #include "unityui.cginc"
 
   #pragma multi_compile __ unity_ui_clip_rect
   #pragma multi_compile __ unity_ui_alphaclip
 
   struct appdata_t
   {
    float4 vertex : position;
    float4 color : color;
    float2 texcoord : texcoord0;
    unity_vertex_input_instance_id
   };
 
   struct v2f
   {
    float4 vertex : sv_position;
    fixed4 color : color;
    float2 texcoord : texcoord0;
    float4 worldposition : texcoord1;
    unity_vertex_output_stereo
   };
 
   v2f vert(appdata_t v)
   {
    v2f out;
    unity_setup_instance_id(v);
    unity_initialize_vertex_output_stereo(out);
    out.worldposition = v.vertex;
    out.vertex = unityobjecttoclippos(out.worldposition);
 
    out.texcoord = v.texcoord;
 
    out.color = v.color;
    return out;
   }
 
   sampler2d _maintex;
 
   float _lan;
   float _stepx;
 
   float4 frag(v2f in) : sv_target
   {
 
    //這裡進行裁剪
    float2 uv = in.texcoord;
    uv.x*= _lan;
 uv.x = frac(uv.x);
 int needdiscard = step(_stepx,uv.x);
 if(needdiscard == 1){
  discard;
 }
 
 //这里进行切变
 float x1 = uv.x;
 uv = in.texcoord;
 uv+=float2(-0.5,-0.5);
    uv.x-=x1;//切變時,先將重心調整到中心,然后绕每一栏的起点进行切变(这里类似于绕某一点旋转,所以后面要进行反向操作,加了就减,减了就加)
    float2x2 qiebian = float2x2(1,0,(1.0-_stepx),1);
    uv = mul(qiebian,uv);
    uv-=float2(-0.5,-0.5);
    uv.x+=x1;
 
 float4 color= tex2d(_maintex, uv);
 
 
 return color;
   }
  endcg
  }
 }
}

效果如下:

Unity shader实现百叶窗特效

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/a003655/article/details/87199386

延伸 · 阅读

精彩推荐
  • C#C#在Excel表格中插入、编辑和删除批注

    C#在Excel表格中插入、编辑和删除批注

    这篇文章主要为大家详细介绍了C#如何在Excel表格中插入、编辑和删除批注,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    E-iceblue6142022-02-24
  • C#C#多线程与异步的区别详解

    C#多线程与异步的区别详解

    多线程和异步操作两者都可以达到避免调用线程阻塞的目的,从而提高软件的可响应性。甚至有些时候我们就认为多线程和异步操作是等同的概念。但是,...

    123si4092022-01-06
  • C#C#实现txt定位指定行完整实例

    C#实现txt定位指定行完整实例

    这篇文章主要介绍了C#实现txt定位指定行的方法,涉及C#针对文本文件进行光标定位的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    我心依旧11442021-10-20
  • C#C#中创建PDF网格并插入图片的方法

    C#中创建PDF网格并插入图片的方法

    这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中。对c# pdf 网格 插入图片的知识感兴趣的朋友一起看看吧...

    Yesi11842021-12-09
  • C#Unity shader实现自由放大缩小效果

    Unity shader实现自由放大缩小效果

    这篇文章主要为大家详细介绍了Unity shader实现自由放大缩小效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    clzmin9892022-03-11
  • C#C#与PHP的md5计算结果不同的解决方法

    C#与PHP的md5计算结果不同的解决方法

    今天在用C#接入我的登录api发现了一个问题,登陆的时候无论如何都会出现用户名和密码错误的问题,后来通过查找排除找的了问题的原因是因为C#与PHP的...

    ifengge3652021-12-15
  • C#C#(asp.net)多线程用法示例(可用于同时处理多个任务)

    C#(asp.net)多线程用法示例(可用于同时处理多个任务)

    这篇文章主要介绍了C#(asp.net)多线程Thread用法,可用于同时处理多个任务,以简单数学运算为例讲述了Thread类实现多线程的相关技巧,需要的朋友可以参考下...

    smartsmile20126582021-11-24
  • C#WPF实现类似360安全卫士界面的程序源码分享

    WPF实现类似360安全卫士界面的程序源码分享

    最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟...

    JackWang-CUMT11622021-10-27