Shader-Vertex -像苹果关闭界面一样
前几天看到了一篇Shader文章http://qmlbook.github.io/ch09/index.html
里面有个类似与苹果的效果,于是试着加入到Unity中
Shader "Custom/NewVertex" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Ler0("Lerp0",Range(0,1))=0
_Ler1("Lerp1",Range(0,1))=0
_Ler2("Lerp3",Range(0,1))=0
_Ler3("Lerp4",float)=0
_XOffset("X Offset",Range(-5,5))=0
_ZOffset("Z Offset",Range(-5,5))=0
}
SubShader {
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float _Ler0;
float _Ler1;
float _Ler2;
float _Ler3;//让图片从一个固定的大小开始变化,例如从一个小图标变化,当他为零时从0到最大
float _XOffset;
float _ZOffset;
struct v2f{
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
};
//这里是用的Plane一个平面,不能用Quad因为顶点个数少
v2f vert(appdata_full v){
v2f o;
v.vertex+=float4(_XOffset,0,_ZOffset,0); //顶点坐标偏移Plane的大小是10米,将Z轴向上移动5米坐标就变成了一个倒T型
float4 vp=v.vertex;
vp.z=lerp(vp.z*_Ler3,vp.z,_Ler0); //
float t=vp.z/(10);
t=(3.0-2.0*t)*t*t; //这个是一个0-1的数学公式 所以上面vp.z/10,,将范弄到0-1;plane大小10米
float L=1-t;//
vp.x=lerp(vp.x*_Ler3,vp.x,t*_Ler2+_Ler1*L);
v.vertex=vp;
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.uv=v.texcoord;
return o;
}
float4 frag(v2f i):COLOR{
float4 col=tex2D(_MainTex,i.uv);
return col;
}
ENDCG
}
}
}
using UnityEngine;
using System.Collections;
public class AnimPlane : MonoBehaviour {
public Material mat;
void Start () {
StartCoroutine (set());
}
IEnumerator set(){
mat.SetFloat ("_Ler0",0);
mat.SetFloat ("_Ler1",0);
mat.SetFloat ("_Ler2",0);
yield return new WaitForSeconds (2);
float time = 0;
while(time<1){
time+=Time.deltaTime*2;
mat.SetFloat("_Ler0",time);
mat.SetFloat("_Ler2",time);
yield return 0;
}
time = 0;
while(time<1){
time+=Time.deltaTime*4;
mat.SetFloat("_Ler1",time);
yield return 0;
}
}
}