Unity Shader實(shí)現(xiàn)3D翻頁效果
本文實(shí)例為大家分享了Unity Shader實(shí)現(xiàn)3D翻頁效果的具體代碼,供大家參考,具體內(nèi)容如下
參考文章:UnityShader使用Plane實(shí)現(xiàn)翻書效果
效果圖:


原理:Shader頂點(diǎn)動(dòng)畫
在頂點(diǎn)著色器進(jìn)行對(duì)頂點(diǎn)Y值的偏移(使用了Sin函數(shù)模擬翻頁時(shí)產(chǎn)生的彎曲),對(duì)頂點(diǎn)X值的偏移實(shí)現(xiàn)紙張?jiān)诜摃r(shí)的收縮(一般是不用收縮),最后對(duì)頂點(diǎn)進(jìn)行圍繞Z軸旋轉(zhuǎn)實(shí)現(xiàn)Plane翻頁(Z軸是本例的旋轉(zhuǎn)軸,請(qǐng)根據(jù)你具體情況修改,上面的兩個(gè)偏移同理)。
Shader "Unlit/PaperTurnMilkShader"
{
Properties
{
//正面圖
_MainTex ("Texture", 2D) = "white" {}
//背面圖
_SrcTex("SrcTex", 2D) = "white"{}
//旋轉(zhuǎn)角度
_Angle("Angle", Range(0,180)) = 0
//彎曲程度
_WeightY("Weight Y", Range(0,3)) = 0.2
//收縮程度(值越大翻頁時(shí)紙張?jiān)酵鶅?nèi)部靠攏)具體情況可測(cè)試
_WeightX("Weight X", Range(0,1)) = 0
//波長(zhǎng)(值越大翻頁時(shí)的彎曲次數(shù)越多)
_WaveLength("WaveLength", Range(0,2)) = 0.4
}
SubShader
{
//關(guān)閉批處理(因?yàn)樾薷牧隧旤c(diǎn)位置)
Tags { "RenderType"="Opaque" "IgnoreProjector" = "True" "Queue" = "Geometry" "DisableBatching" = "True"}
LOD 100
//渲染正面
Pass
{
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#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;
float _Angle;
float _WeightY;
float _WeightX;
float _WaveLength;
v2f vert (appdata v)
{
v2f o;
//對(duì)頂點(diǎn)進(jìn)行往X正方向偏移5個(gè)單位是為了離開旋轉(zhuǎn)中心點(diǎn),不然翻頁時(shí)的旋轉(zhuǎn)點(diǎn)是會(huì)在紙張中心進(jìn)行圍繞Z軸旋轉(zhuǎn)(Z軸是紙張垂直線)
v.vertex += float4(5, 0, 0, 0);
float s;
float c;
//使用sincos獲取 sin(弧度), cos(弧度) ,radians(角度)=弧度 ,_Angle前加負(fù)號(hào)是控制旋轉(zhuǎn)方向,可根據(jù)DX是右手法則順時(shí)針旋轉(zhuǎn),故應(yīng)該逆向翻頁要取負(fù)數(shù)
sincos(radians(-_Angle), s, c);
//圍繞Z軸旋轉(zhuǎn)變換矩陣
float4x4 rotate = {
c,s,0,0,
-s,c,0,0,
0,0,1,0,
0,0,0,1
};
//weight:_Angle在[0,90]變換區(qū)間時(shí),weight會(huì)從0變?yōu)?;_Angle在[90,180]變換區(qū)間時(shí),weight會(huì)從1變?yōu)?.
//weight可理解為是剛開始翻頁(0°)到翻頁到垂直時(shí)(90°)時(shí),對(duì)其彎曲程度從小變大;(這個(gè)是對(duì)頂點(diǎn)Y值影響的結(jié)果)
//同理,收縮程度也是一樣道理
float weight = 1 - abs(90 - _Angle) / 90;
v.vertex.y += sin(v.vertex.x * _WaveLength) * weight * _WeightY;
v.vertex.x -= v.vertex.x * weight * _WeightX;
//在進(jìn)行偏移之后,再對(duì)頂點(diǎn)進(jìn)行圍繞Z軸旋轉(zhuǎn)_Angle角度
v.vertex = mul(rotate, v.vertex);
//之后要偏移回來,因?yàn)槲覀円呀?jīng)做完了上面的旋轉(zhuǎn)操作了
v.vertex -= float4(5, 0, 0, 0);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
//渲染背面(和上方渲染正面PASS唯一的差別是:片元著色器的采樣紋理改為_SrcTex(背面圖)
Pass
{
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _SrcTex;
float4 _MainTex_ST;
float _Angle;
float _WeightY;
float _WeightX;
float _WaveLength;
v2f vert(appdata v)
{
v2f o;
v.vertex += float4(5, 0, 0, 0);
float s;
float c;
//使用sincos獲取 sin(弧度), cos(弧度) ,radians(角度)=弧度
sincos(radians(-_Angle), s, c);
//圍繞Z軸旋轉(zhuǎn)變換矩陣
float4x4 rotate = {
c,s,0,0,
-s,c,0,0,
0,0,1,0,
0,0,0,1
};
float weight = 1 - abs(90 - _Angle) / 90;
v.vertex.y += sin(v.vertex.x * _WaveLength) * weight * _WeightY;
v.vertex.x -= v.vertex.x * weight * _WeightX;
v.vertex = mul(rotate, v.vertex);
v.vertex -= float4(5, 0, 0, 0);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_SrcTex, i.uv);
return col;
}
ENDCG
}
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#?JWT權(quán)限驗(yàn)證的實(shí)現(xiàn)
本文主要介紹了C#?JWT權(quán)限驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Ruby創(chuàng)建數(shù)組方法總結(jié)
在本篇文章里小編給大家分享了關(guān)于Ruby創(chuàng)建數(shù)組方法的知識(shí)點(diǎn)內(nèi)容,對(duì)戲有興趣的朋友們學(xué)習(xí)下。2019-01-01
Asp.Net中避免重復(fù)提交和彈出提示框的實(shí)例代碼
本文分為前臺(tái)和后臺(tái)代碼實(shí)現(xiàn)避免重復(fù)提交和彈出提示框效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02
C#中Arraylist的sort函數(shù)用法實(shí)例分析
這篇文章主要介紹了C#中Arraylist的sort函數(shù)用法,較為詳細(xì)的分析了ArrayList的sort函數(shù)的功能、定義及具體使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

