Unity3D Shader實(shí)現(xiàn)貼圖切換效果
本文實(shí)例為大家分享了shader實(shí)現(xiàn)基于世界坐標(biāo)的貼圖置換效果。
效果如下:
設(shè)置面板如下:
可在面板上設(shè)置切換方向,與切換對(duì)象,及其切換速度。
shader實(shí)現(xiàn)如下:
Shader "XM/Effect/SwapTexture" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _TargetTex ("Target Tex", 2D) = "white" {}//目標(biāo)貼圖 [KeywordEnum(Up, Down, Left, Right, Forward, Back)] _mode ("Mode", Int) = 0//切換方向 _SwapBlend ("Blend", Range(0,1)) = 0//0-1混合值 _SwapMin("Min Value", Float) = 0//最小世界坐標(biāo) _SwapMax("Max Value", Float) = 0//最大世界坐標(biāo) _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows vertex:vert // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; sampler2D _TargetTex; struct Input { float2 uv_MainTex; float3 worldPos; }; half _mode; half _SwapBlend; float _SwapMin; float _SwapMax; half _Glossiness; half _Metallic; fixed4 _Color; void vert (inout appdata_full v, out Input o) { UNITY_INITIALIZE_OUTPUT(Input,o); } void surf (Input IN, inout SurfaceOutputStandard o) { half useTarget = 0; float targetValue = 0; switch(_mode)//模式選擇 { case 0://up targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin; useTarget = IN.worldPos.y > targetValue?0:1; break; case 1://down targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin; useTarget = IN.worldPos.y < targetValue?0:1; break; case 2://left targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin; useTarget = IN.worldPos.x < targetValue?0:1; break; case 3://right targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin; useTarget = IN.worldPos.x > targetValue?0:1; break; case 4://forward targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin; useTarget = IN.worldPos.z > targetValue?0:1; break; case 5://back targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin; useTarget = IN.worldPos.z < targetValue?0:1; break; } // Albedo comes from a texture tinted by color fixed4 c; if(useTarget == 1) { c = tex2D (_TargetTex, IN.uv_MainTex); } else { c = tex2D (_MainTex, IN.uv_MainTex); } c *= _Color; o.Albedo = c.rgb; // Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
配合使用的腳本如下:
using System; using System.Collections; using UnityEngine; namespace XM.Effect { public class SwapTexture : MonoBehaviour { public enum SwapDirection { Up, Down, Left, Right, Forward, Back } public Shader _shader;//"XM/Effect/SwapTexture” shader public SwapDirection _swapDir;//切換方向 public Renderer _target;//目標(biāo)對(duì)象 [Range(0, 1)] public float _speed;//速度 private Material _matOld; private Material _matNew; public void Swap(Texture tex, Action<bool> onComplete) { if (_matOld != null) { StopAllCoroutines(); if (null != onComplete) { onComplete(false); } _target.material = _matOld; } _matOld = _target.material; _matNew = new Material(_shader); _matNew.SetTexture("_MainTex", _target.material.GetTexture("_MainTex")); _matNew.SetTexture("_TargetTex", tex); _matNew.SetInt("_mode", (int)_swapDir); _matNew.SetFloat("_SwapBlend", 0); StartCoroutine("_StartChange", onComplete); } private IEnumerator _StartChange(Action<bool> onComplete) { float deltaVal = 0; _target.material = _matNew; Vector3 vtMin; Vector3 vtMax; float minVal = 0; float maxVal = 0; while (deltaVal != 1) { vtMin = _target.bounds.min; vtMax = _target.bounds.max; switch (_swapDir) { case SwapDirection.Up: case SwapDirection.Down: minVal = vtMin.y; maxVal = vtMax.y; break; case SwapDirection.Left: case SwapDirection.Right: minVal = vtMin.x; maxVal = vtMax.x; break; case SwapDirection.Forward: case SwapDirection.Back: minVal = vtMin.z; maxVal = vtMax.z; break; } minVal -= 0.01f; maxVal += 0.01f; _matNew.SetFloat("_SwapMin", minVal); _matNew.SetFloat("_SwapMax", maxVal); deltaVal = Mathf.Clamp01(deltaVal + _speed * Time.deltaTime); _matNew.SetFloat("_SwapBlend", deltaVal); yield return null; } _matOld.SetTexture("_MainTex", _matNew.GetTexture("_TargetTex")); _target.material = _matOld; _matNew = null; _matOld = null; if (null != onComplete) { onComplete(true); } } public void OnDrawGizmos() { if (_target != null) { Gizmos.DrawWireCube(_target.bounds.center, _target.bounds.size); Gizmos.DrawWireSphere(_target.bounds.min, 0.1f); Gizmos.DrawWireSphere(_target.bounds.max, 0.1f); } } //test public Texture testTex; private void OnGUI() { if (GUILayout.Button("SwapTexture")) { Swap(testTex, (t) => { Debug.Log("Swap>" + t); }); } } // } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Unity3D Shader實(shí)現(xiàn)掃描顯示效果
欄 目:C#教程
下一篇:Unity C#打包AssetBundle與場(chǎng)景詳解
本文標(biāo)題:Unity3D Shader實(shí)現(xiàn)貼圖切換效果
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4850.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)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 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)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 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#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)