当前位置: 移动技术网 > IT编程>开发语言>c# > UnityShader实现百叶窗效果

UnityShader实现百叶窗效果

2020年03月09日  | 移动技术网IT编程  | 我要评论

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

shader实现以上百叶窗效果,主要通过shader实现c#只是做开关控制

看到一篇文章弄的比较复杂,觉得可以都通过shader来实现,就动手了。

shader定义了2张texture,自己随便找2张图片拖进去就行。

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上

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);
  }
 }
 }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网