内置渲染管线
Shader "SunY/C11-VertexAnimationWithShadow" { 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" "DissableBathing" = "True" } Pass { Tags { "LightMode" = "ForwardBase" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag 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 x = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength) * _Magnitude; v.vertex.x += x; // 计算二维水面的波浪 // float z = sin(_Frequency * _Time.y + (v.vertex.x + vertex.y) * _InvWaveLength) * _Magnitude; // v.vertex.z += z; o.pos = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); o.uv.y += _Time.y * _Speed; return o; } fixed4 frag(v2f i) : SV_Target { fixed4 color = tex2D(_MainTex, i.uv); color.rgb *= _Color.rgb; return color; } ENDCG } Pass { Tags { "LightMode" = "ShadowCaster" } CGPROGRAM #include "UnityCG.cginc" #pragma multi_compile_shadowcaster #pragma vertex vert #pragma fragment frag float _Magnitude; float _Frequency; float _InvWaveLength; float _Speed; struct a2v { float4 vertex : POSITION; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; }; struct v2f { V2F_SHADOW_CASTER; }; v2f vert(a2v v) { v2f o; float x = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength) * _Magnitude; v.vertex.x += x; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) return o; } fixed4 frag(v2f i) : SV_Target { SHADOW_CASTER_FRAGMENT(i) } ENDCG } } }
URP渲染管线
Shader "SunY/C11-U-VertexAnimationWithShadow" { 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" "IgnorProjector" = "True" "RenderType" = "Transparent" "DissableBathing" = "True" "RenderPipeline" = "UniversalPipeline" } Pass { Tags { "LightMode" = "UniversalForward" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off HLSLPROGRAM #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #pragma vertex vert #pragma fragment frag CBUFFER_START(UnityPerMaterial) float4 _MainTex_ST; half4 _Color; float _Magnitude; float _Frequency; float _InvWaveLength; float _Speed; CBUFFER_END TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o; float x = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength) * _Magnitude; v.vertex.x += x; o.pos = TransformObjectToHClip(v.vertex); o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); o.uv.y += _Time.y * _Speed; return o; } half4 frag(v2f i) : SV_Target { half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv); color.rgb *= _Color.rgb; return color; } ENDHLSL } Pass { Tags{ "LightMode" = "ShadowCaster" "RenderPipeline" = "UniversalPipeline" } HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" struct a2v { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; }; CBUFFER_START(UnityPerMaterial) float _Magnitude; float _Frequency; float _InvWaveLength; float _Speed; float3 _LightDirection; float4 _ShadowBias; half4 _MainLightShadowParams; CBUFFER_END float3 ApplyShadowBias(float3 positionWS, float3 normalWS, float3 lightDirection) { float invNdotL = 1.0 - saturate(dot(lightDirection, normalWS)); float scale = invNdotL * _ShadowBias.y; positionWS = lightDirection * _ShadowBias.xxx + positionWS; positionWS = normalWS * scale.xxx + positionWS; return positionWS; } v2f vert(a2v v) { v2f o = (v2f)0; float x = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength) * _Magnitude; v.vertex.x += x; float3 worldPos = TransformObjectToWorld(v.vertex.xyz); half3 normalWS = TransformObjectToWorldNormal(v.normal); worldPos = ApplyShadowBias(worldPos, normalWS, _LightDirection); o.vertex = TransformWorldToHClip(worldPos); return o; } real4 frag(v2f i) : SV_Target { return 0; } ENDHLSL } } }
浙公网安备 33010602011771号