Unity shader护盾特效
生活随笔
收集整理的這篇文章主要介紹了
Unity shader护盾特效
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
身為小白的我看到某大神用?amplify shader editor 插件做了護盾特效,很酷。
藍色是大神做的,紅色那個是我的(感覺和大神的低了幾個檔次,哈哈)
大神的視頻鏈接:
https://www.bilibili.com/video/av28959957/?share_source=qq&ts=1535869157&share_medium=iphone&bbid=137b0753063d77d57eeb03598dd9e194
大神上傳到Asset Store商城的作品,有興趣可以支持一下他
https://www.assetstore.unity3d.com/#!/content/125555
我的源碼+模型有興趣可以下載看一下
鏈接:https://pan.baidu.com/s/1vMyEiP8CyGv6wcDnccLiTQ 密碼:ynoa
下面就進入正題了,我的簡化版shader代碼
Shader "My/Demo/Shield" {Properties{_ScanMap("ScanMap",2D)="white"{}_ScanCol("ScanColor",color)=(1,1,1,1)_ScanScale("ScanScale",range(0,0.2))=0.1_ScanFrequency("ScanFrequency",range(0,1))=0.5_ScanFresnelScale("ScanFresnelScale",range(0,1))=0.5_ScanFresnelPow("ScanFresnelPow",range(0,10))=1_ShieldMap("ShieldMap",2D)="white"{}_ShieldCol("ShieldColor",color)=(1,1,1,1)_ShieldFresnelScale("ShieldFresnelScale",range(0,1))=0.5_ShieldFresnelPow("ShieldFresnelPow",range(0,10))=1}SubShader{Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}ZWrite Off Blend SrcAlpha OneMinusSrcAlphaCGINCLUDE#include "UnityCG.cginc"sampler2D _ScanMap;float4 _ScanMap_ST;fixed4 _ScanCol;fixed _ScanScale;fixed _ScanFrequency;fixed _ScanFresnelScale;float _ScanFresnelPow;sampler2D _ShieldMap;float4 _ShieldMap_ST;fixed4 _ShieldCol;fixed _ShieldFresnelScale;float _ShieldFresnelPow;struct a2v{float4 vertex:POSITION;float4 shield:TEXCOORD0;float4 scan:TEXCOORD1;fixed4 color:COLOR;float3 normal:NORMAL;};struct v2f{float4 pos:SV_POSITION;float4 uv:TEXCOORD0;float2 uv_Line:TEXCOORD4;float3 WorldNormal:TEXCOORD1;float3 WorldPos:TEXCOORD2;float4 color:TEXCOORD3;};//ScanVert是修改頂點位置,然護盾有向外擴張的效果v2f ScanVert(a2v v){v2f o;o.pos=mul(UNITY_MATRIX_MV,v.vertex);float3 ViewNormal=mul((float3x3)UNITY_MATRIX_IT_MV,v.normal);o.pos+=float4(ViewNormal,0)*_ScanScale;o.pos=mul(UNITY_MATRIX_P,o.pos);//把頂點位置轉換到view位置然后向外擴張。o.uv.xy=TRANSFORM_TEX(v.scan,_ScanMap);o.uv.zw=TRANSFORM_TEX(v.shield,_ShieldMap);o.WorldNormal=UnityObjectToWorldNormal(v.normal);o.WorldPos=mul(_Object2World,v.vertex);o.color=v.color;return o;}fixed4 ScanFrag(v2f i):SV_Target{fixed scanAlpha=tex2D(_ScanMap,i.uv.xy+float2(0,1)*_Time.y*_ScanFrequency).a;//判斷scanAlpha是否等于0,等于0剔除if(scanAlpha==0){discard;}fixed3 WorldNormalDir=normalize(i.WorldNormal);fixed3 WorldViewDir=normalize(_WorldSpaceCameraPos.xyz-i.WorldPos.xyz);//fresnel 菲涅爾公式,邊緣比較實,里面比較虛那種感覺,可以 直接 return backCol; 看一下效果fixed fresnel=_ScanFresnelScale+(1-_ScanFresnelScale)*pow(1-saturate(dot(WorldNormalDir,WorldViewDir)),_ScanFresnelPow);fixed fresnleAlpha=lerp(0,1,fresnel);fixed4 backCol=fixed4(_ScanCol.rgb,fresnleAlpha);fixed4 shieldCol=tex2D(_ShieldMap,i.uv.zw)*_ScanCol;//shieldCol.a*scanAlpha是為了看起來中間實兩邊虛的效果(ps:紋理是漸變紋理來的)shieldCol=fixed4(shieldCol.rgb,shieldCol.a*scanAlpha);fixed4 finalCol=backCol+shieldCol;return finalCol;}v2f ShiedlVert(a2v v){v2f o;o.pos=mul(UNITY_MATRIX_MVP,v.vertex);o.uv.xy=TRANSFORM_TEX(v.scan,_ScanMap);o.uv.zw=TRANSFORM_TEX(v.shield,_ShieldMap);o.WorldNormal=UnityObjectToWorldNormal(v.normal);o.WorldPos=mul(_Object2World,v.vertex);o.color=v.color;return o;}fixed4 ShieldFrag(v2f i):SV_Target{fixed scanAlpha=tex2D(_ScanMap,i.uv.xy+float2(0,1)*_Time.y*_ScanFrequency).a;if(scanAlpha>0){discard;}fixed3 WorldNormalDir=normalize(i.WorldNormal);fixed3 WorldViewDir=normalize(_WorldSpaceCameraPos.xyz-i.WorldPos.xyz);fixed fresnel=_ShieldFresnelScale+(1-_ShieldFresnelScale)*pow(1-saturate(dot(WorldNormalDir,WorldViewDir)),_ShieldFresnelPow);fixed fresnleAlpha=lerp(0,1,fresnel);fixed4 backCol=fixed4(_ShieldCol.rgb,fresnleAlpha);fixed4 shieldCol=tex2D(_ShieldMap,i.uv.zw)*i.color*_ShieldCol;fixed4 finalCol=backCol+shieldCol;return finalCol;}ENDCGPass{CGPROGRAM#pragma vertex ScanVert#pragma fragment ScanFragENDCG}Pass{CGPROGRAM#pragma vertex ShiedlVert#pragma fragment ShieldFragENDCG}} }?
?
總結
以上是生活随笔為你收集整理的Unity shader护盾特效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Objective_C学习笔记
- 下一篇: ctfshow-萌新赛Crypto