UnityShader實(shí)現(xiàn)百葉窗效果
本文實(shí)例為大家分享了UnityShader百葉窗展示的具體代碼,供大家參考,具體內(nèi)容如下
shader實(shí)現(xiàn)以上百葉窗效果,主要通過shader實(shí)現(xiàn)C#只是做開關(guān)控制
看到一篇文章弄的比較復(fù)雜,覺得可以都通過shader來實(shí)現(xiàn),就動(dòng)手了。
shader定義了2張texture,自己隨便找2張圖片拖進(jìn)去就行。
Shader "Unlit/NewUnlitShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _MainTex2 ("Texture2", 2D) = "white" {} _StartFlag("開始標(biāo)記", float) = 0 _SpeedFactor("速度",Range(0.01,10)) = 0.1 _StartTime("時(shí)間初始標(biāo)記,不要手動(dòng)設(shè)置",float) = 1 _Column("百葉窗的列數(shù)",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#監(jiān)聽鍵盤事件,設(shè)置為1,作為開始動(dòng)畫的標(biāo)記 //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#控制開關(guān),點(diǎn)擊鍵盤任意按鍵。掛到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); } } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:詳解LINQ入門(中篇)
欄 目:C#教程
下一篇:dotnet core鏈接mongodb代碼實(shí)例
本文標(biāo)題:UnityShader實(shí)現(xiàn)百葉窗效果
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4593.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什