shader学习之路——百叶窗效果

 public class BaiYeChuang : MonoBehaviour
    {

        // Use this for initialization
        public Material mat;
        public RenderTexture tex;
        public Shader BY;

        void OnEnable()
        {
            if (mat == null)
                mat = new Material(BY);
            mat.SetFloat("_StartTime", Time.timeSinceLevelLoad);
            mat.SetFloat("_StartFlag", 1);
            if (tex == null)
                tex = CameraController.mainCamera.GetComponent<RTTCache>().GetRttCache;
            mat.SetTexture("_MainTex2", tex);
        }

        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            Graphics.Blit(source, destination, mat);
        }
    }
Shader "Unlit/UI-BY"
{
	Properties
	 {
		 _MainTex ("Texture", 2D) = "white" {}
		_MainTex2 ("Texture2", 2D) = "white" {}
		_StartFlag("开始标记", float) = 0
		_SpeedFactor("速度",Range(0.01,10)) = 0.2
		_StartTime("时间初始标记,不要手动设置",float) = 1
		_Column("百叶窗的列数",float ) = 15
	 }
	 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 设置为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(_MainTex2, i.uv);
			 }
			 else
			 {
				col = tex2D(_MainTex , i.uv);
			 }
 
			 return col;
		 }
		 ENDCG
		 }
	 }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值