使用噪声图实现火烧和水波纹效果;
溶解
关闭裁剪,根据noise纹理取样,r通道和_BurnAmount比较,裁剪掉小于_BurnAmount的片元;
通过菲尼尔得到裁剪边缘,添加火焰燃烧的颜色进行混合;
阴影pass需要单独写,同样做裁剪;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| Pass { Tags {"LightMode" = "ForwardBase"}
Cull Off
CGPROGRAM #pragma vertex vert #pragma fragment frag
#include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc"
#pragma multi_compile_fwdbase
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; float4 tangent :TANGENT; };
struct v2f { float4 uv : TEXCOORD0; float2 uvBurnMap : TEXCOORD1; float4 pos : SV_POSITION; float3 lightDir : TEXCOORD2; float3 worldPos : TEXCOORD3; SHADOW_COORDS(5) };
float _BurnAmount; float _LineWidth; sampler2D _MainTex; float4 _MainTex_ST; sampler2D _BumpMap; float4 _BumpMap_ST; fixed4 _BurnFirstColor; fixed4 _BurnSecondColor; sampler2D _BurnMap; float4 _BurnMap_ST;
v2f vert (appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex); o.uv.zw = TRANSFORM_TEX(v.uv, _BumpMap); o.uvBurnMap = TRANSFORM_TEX(v.uv,_BurnMap);
TANGENT_SPACE_ROTATION; o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz; o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz; TRANSFER_SHADOW(o);
return o; }
fixed4 frag (v2f i) : SV_Target { fixed3 burn = tex2D(_BurnMap,i.uvBurnMap).rgb; clip(burn.r - _BurnAmount);
float3 tangentLightDir = normalize(i.lightDir); fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
fixed3 albedo = tex2D(_MainTex,i.uv.xy).rgb; fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal,tangentLightDir));
fixed t = 1 - smoothstep(0.0, _LineWidth, burn.r - _BurnAmount); fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t); burnColor = pow(burnColor, 5);
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed3 finalColor = lerp(ambient + diffuse * atten, burnColor, t * step(0.0001,_BurnAmount)); return fixed4(finalColor, 1); } ENDCG }
|
水波纹
设置速度来扰动法线法线;扰动的偏移乘以屏幕左边的z值模拟深度越大折射越大的效果;
GrabPass抓取屏幕画面做纹理,供折射取样;
MainTex是一张噪音纹理图,取样做速度偏移,模拟水波纹;
fresnel混合折射反射,得到最终颜色;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| fixed4 frag (v2f i) : SV_Target { float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w); fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos)); float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);
fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb; fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb; fixed3 bump = normalize(bump1 + bump2);
float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy; i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy; fixed3 refrCol = tex2D( _RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump))); fixed4 texColor = tex2D(_MainTex, i.uv.xy + speed); fixed3 reflDir = reflect(-viewDir, bump); fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb * _Color.rgb;
fixed fresnel = pow(1 - saturate(dot(viewDir,bump)),4); fixed3 finalColor = reflCol * fresnel + refrCol *(1-fresnel);
return fixed4(finalColor,1.0); }
|
噪音图还用在全局雾和生成地形,以及动态生成资源上;
1.溶解
2.水波纹
3.全局雾
4.生成地形