翻书shader
//把下面的shader挂载到plane上,调节_Angle
Shader "Unlit/PageTurning"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex("MainTex",2D)="White"{}
_SecTex("SecTex",2D)="White"{}
_Angle("Angle",Range(0,180))=0
_Warp("Warp",Range(0,10))=0
_WarpPos("WarpPos",Range(0,1))=0
_Downward("Downward",Range(0,1))=0
}
SubShader
{
pass//正面
{
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
fixed4 _Color;
float _Angle;
float _Warp;
float _Downward;
float _WarpPos;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata_base v)
{
v2f o;
v.vertex += float4(5,0,0,0);将旋转轴移到边上
float s;
float c;//radians(x) 函数将角度值转换为弧度值
// 该函数是同时计算x的sin值和cos值,其中s=sin(x),c=cos(x)。该函数用于“同时需要计算sin值和cos值的情况”,比分别运算要快很多!
sincos(radians(-_Angle),s,c);
float4x4 rotate={
c,s,0,0,
-s,c,0,0,
0,0,1,0,
0,0,0,1};//旋转矩阵
//saturate(x) 把x限制到[0,1]之间
float rangeF=saturate(1 - abs(90-_Angle)/90);
v.vertex.y += -_Warp*sin(v.vertex.x * 0.33 - _WarpPos* v.vertex.x)*rangeF;//使用正弦函数的第一段的弧度,v.vertex.x大约是0-5*PI/2所以*0.33.
//v.vertex.x -= rangeF * v.vertex.x*_Downward;
//v.vertex.y = 0.05*_Warp * (_Angle/90) * v.vertex.x * v.vertex.x;
v.vertex = mul(rotate,v.vertex);
v.vertex += float4(-5,0,0,0);//旋转后移回来
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):COLOR
{
fixed4 color = tex2D(_MainTex,-i.uv);
return _Color * color;
}
ENDCG
}
pass
{
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
fixed4 _Color;
float _Angle;
float _Warp;
float _Downward;
float _WarpPos;
sampler2D _SecTex;
float4 _MainTex_ST;
v2f vert(appdata_base v)
{
v2f o;
v.vertex += float4(5,0,0,0);
float s;
float c;
sincos(radians(-_Angle),s,c);
float4x4 rotate ={
c ,s ,0 ,0,
-s ,c, 0 ,0,
0 ,0 ,1 ,0,
0 ,0 ,0, 1
};
float rangeF = saturate(1 - abs(90-_Angle)/90);
v.vertex.y += -_Warp * sin(v.vertex.x * 0.33 - _WarpPos* v.vertex.x) * rangeF;
v.vertex = mul(rotate,v.vertex);
v.vertex += float4(-5,0,0,0);
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):COLOR
{
float2 uv = i.uv;
uv.x = -uv.x;
fixed4 color = tex2D(_SecTex,-uv);
return _Color * color;
}
ENDCG
}
}
}
转载自https://blog.csdn.net/qq_39741605/article/details/80678229