Unity Shader實(shí)現(xiàn)2D游戲迷霧
本文實(shí)例為大家分享了Unity Shader實(shí)現(xiàn)2D游戲迷霧的具體代碼,供大家參考,具體內(nèi)容如下
先看效果吧。
我使用的是屏幕后處理效果,首先先去Photoshop做一張圖片如下,用畫筆點(diǎn)一個(gè)點(diǎn)就可以了,使用它來對攝像機(jī)截取的圖片進(jìn)行處理。
在攝像機(jī)上添加腳本文件
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestScript : MonoBehaviour { [Range(0,3)] public float Lerp = 0;//使用它來調(diào)整可視區(qū)域的大小 public Texture2D MaskTex; public Shader ScreanShader; public Material GetMaterial { get { if(_material ==null) _material = new Material(ScreanShader); return _material; } } private Material _material = null; //src是攝像機(jī)截取到的照片,dest是處理過的圖片 void OnRenderImage(RenderTexture src, RenderTexture dest) { GetMaterial.SetTexture("_MainTex", src); GetMaterial.SetTexture("_MaskTex", MaskTex); GetMaterial.SetFloat("_Lerp", Lerp); Graphics.Blit(src, dest, GetMaterial); } }
對應(yīng)的shader,思路就是把MaskTex的顏色翻轉(zhuǎn)一下然后直接乘上去就可以了,小數(shù)越乘越小,越小顏色越黑。
Shader "Wzhhh/MyShader2" { Properties{ _MainTex("MainTex",2D) = "white"{} _MaskTex("MaskTex",2D) = "white"{} _Lerp("Lerp",Range(0,3)) = 1 } SubShader{ Pass{ Tags{ "LightMode" = "ForwardBase" } CGPROGRAM #include "Lighting.cginc" #pragma vertex vert #pragma fragment frag sampler2D _MaskTex; sampler2D _MainTex; float4 _MainTex_ST; float _AlphaBase; float _Lerp; struct a2v { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; fixed2 uv : TEXCOORD0; }; v2f vert(a2v i) { v2f o; o.pos = UnityObjectToClipPos(i.vertex); o.uv = TRANSFORM_TEX(i.texcoord, _MainTex); return o; } fixed4 frag(v2f o) :SV_TARGET{ fixed4 color = tex2D(_MaskTex, o.uv); color.r = 1 - color.r; color.g = 1 - color.g; color.b = 1 - color.b; fixed4 color2 = tex2D(_MainTex, o.uv); color2.r *= color.r*_Lerp; color2.g *= color.g*_Lerp; color2.b *= color.b*_Lerp; return color2; } ENDCG } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
這篇文章主要介紹了C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法,涉及C#文件傳輸?shù)募记?具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08WinForm中DataGridView添加,刪除,修改操作具體方法
這篇文章介紹了WinForm中DataGridView添加,刪除,修改操作具體方法,有需要的朋友可以參考一下2013-10-10C#開發(fā)Windows服務(wù)實(shí)例之實(shí)現(xiàn)禁止QQ運(yùn)行
這篇文章主要介紹了通過C#開發(fā)Windows服務(wù),查殺qq進(jìn)程的服務(wù)功能,需要的朋友可以參考下2013-10-10C# web.config之<customErrors>節(jié)點(diǎn)說明案例詳解
這篇文章主要介紹了C# web.config之<customErrors>節(jié)點(diǎn)說明案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C#調(diào)用帶結(jié)構(gòu)體指針Dll的方法
在C#到底該如何安全的調(diào)用這樣的DLL接口函數(shù)呢?本文將詳細(xì)介紹如何調(diào)用各種參數(shù)的方法,對C#結(jié)構(gòu)體指針DLL相關(guān)知識感興趣的朋友一起看看吧2021-07-07