顶点动画shader要关闭动态合批”DisableBatching”=”True”;
序列帧动画

纹理取样有Scale和offset,将上面的png图当做纹理,每次按间隔时间偏移取样纹理的起点;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| fixed4 frag (v2f i) : SV_Target { float time = floor(_Time.y * _Speed); float row = floor(time/_HorizontalAmount); float column = floor(time/_VerticalAmount); half2 uv = i.uv + half2(column,-row); uv.x /= _HorizontalAmount; uv.y /= _VerticalAmount;
fixed4 color = tex2D(_MainTex,uv)*_Color; return color; }
|

滚动卷轴背景

两张背景图根据时间横向左移动; baselayer移动速度慢,展现一种远景的效果;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| ZWrite Off Blend SrcAlpha OneMinusSrcAlpha
v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex) + frac(float2(_ScrollX, 0.0)* _Time.y); o.uv.zw = TRANSFORM_TEX(v.uv, _DetailTex) + frac(float2(_Scroll2X, 0.0)* _Time.y); return o; }
fixed4 frag (v2f i) : SV_Target { fixed4 firstLayer = tex2D(_MainTex,i.uv.xy); fixed4 secondLayer = tex2D(_DetailTex,i.uv.zw); fixed4 color = lerp(firstLayer,secondLayer,secondLayer.a);
return fixed4(color.rgb * _Multiplier,1.0); }
|

平面水波纹+阴影

将以Quad当成波,有振幅,波长,频率;
通过参数调整,控制水波的起伏,速度,水波的大小;
顶点动画的阴影要跟动,需要在ShadowCasterpass中也做顶点偏移;
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| Shader "MyShader/WaterWave" { Properties { _MainTex ("Main Tex", 2D) = "white" {} _Color ("Color Tint", Color) = (1, 1, 1, 1) _Magnitude ("Distortion Magnitude", Float) = 1 _Frequency ("Distortion Frequency", Float) = 1 _InvWaveLength ("Distortion Inverse Wave Length", Float) = 10 _Speed ("Speed", Float) = 0.5 } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"} Pass { Tags { "LightMode"="ForwardBase" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color; float _Magnitude; float _Frequency; float _InvWaveLength; float _Speed; struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o;
float offset = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength)* _Magnitude; v.vertex.x += offset; o.pos = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); o.uv += float2(0.0, _Time.y * _Speed); return o; } fixed4 frag(v2f i) : SV_Target { fixed4 c = tex2D(_MainTex, i.uv); c.rgb *= _Color.rgb; return c; } ENDCG }
Pass{ Tags{"LightMode" = "ShadowCaster"} CGPROGRAM #pragma vertex vert #pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
float _Magnitude; float _Frequency; float _InvWaveLength; float _Speed;
struct v2f { V2F_SHADOW_CASTER; };
v2f vert(appdata_base v){ v2f o; float offset = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength)* _Magnitude; v.vertex.x += offset; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) return o; } float4 frag(v2f i): SV_Target { SHADOW_CASTER_FRAGMENT(i) }
ENDCG } } FallBack "Diffuse" }
|

公告牌效果
2D的纸片永远朝向摄像机;
根据观察方向变换顶点;
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
| Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}
Pass { Tags {"LightMode" = "ForwardBase"} ZWrite Off Cull Off Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM #pragma vertex vert #pragma fragment frag
#include "UnityCG.cginc"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; };
struct v2f { float2 uv : TEXCOORD0; float4 pos : SV_POSITION; };
sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color; float _VerticalBillboard;
v2f vert (appdata v) { v2f o; float3 center = float3(0,0,0); float3 viewer = mul(unity_WorldToObject,float4(_WorldSpaceCameraPos,1));
float3 normalDir = viewer - center; normalDir.y = normalDir.y * _VerticalBillboard; normalDir = normalize(normalDir);
float3 upDir = abs(normalDir.y) > 0.999? float3(0,0,1) : float3(0,1,0); float3 rightDir = normalize(cross(upDir,normalDir)); upDir = normalize(cross(rightDir,normalDir));
float3 centerOffs = v.vertex.xyz - center; float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir.z * centerOffs.z;
o.pos = UnityObjectToClipPos(float4(localPos,1.0)); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; }
fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); col.rgb *= _Color.rgb; return col; } ENDCG }
|
