-
C#教程之UnityShader实现百叶窗效果
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
本文实例为大家分享了UnityShader百叶窗展示的具体代码,供大家参考,具体内容如下
shader实现以上百叶窗效果,主要通过shader实现C#只是做开关控制
看到一篇文章弄的比较复杂,觉得可以都通过shader来实现,就动手了。
shader定义了2张texture,自己随便找2张图片拖进去就行。
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
|
Shader "Unlit/NewUnlitShader" { Properties { _MainTex ( "Texture" , 2D) = "white" {} _MainTex2 ( "Texture2" , 2D) = "white" {} _StartFlag( "开始标记" , float ) = 0 _SpeedFactor( "速度" ,Range(0.01,10)) = 0.1 _StartTime( "时间初始标记,不要手动设置" , float ) = 1 _Column( "百叶窗的列数" , float ) = 5 } SubShader { Tags { "RenderType" = "Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fog #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; sampler2D _MainTex2; float4 _MainTex2_ST; float _StartFlag; float _Column; float _SpeedFactor; float _StartTime; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = 0; //_StartFlag 通过C#监听键盘事件,设置为1,作为开始动画的标记 //step(a,b) => if(b>=a) return 1 else return 0 //i.uv.x % (1/_Column) i.uv.x范围是0-1,分成_Column份 每份(1/_Column) fixed result = _StartFlag * step( i.uv.x % (1/_Column) ,(_Time.y - _StartTime) * _SpeedFactor ); if ( result == 0 ) { col = tex2D(_MainTex, i.uv); } else { col = tex2D(_MainTex2, i.uv); } return col; } ENDCG } } } |
C#控制开关,点击键盘任意按键。挂到panel上
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
|
using System.Collections; using System.Collections.Generic; using UnityEngine; public class baiyechuang : MonoBehaviour { // Use this for initialization Material mat; MeshRenderer meshRen; void Start () { meshRen = this .GetComponent<MeshRenderer> (); print (meshRen); mat = meshRen.material; print (mat); } void OnGUI() { if (Input.anyKeyDown) { Event e = Event.current; if (e.isMouse) { Debug.Log(e.button); } if (e.isKey) { if (e.keyCode == KeyCode.None) return ; Debug.Log(e.keyCode); mat.SetFloat ( "_StartTime" , Time.timeSinceLevelLoad); mat.SetFloat ( "_StartFlag" , 1); } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日