简略的URP水shader
放低端机型的一个卡通水效果
1 Shader "Unlit/S_FoamWater" 2 { 3 Properties 4 { 5 _ShallowColor("浅水颜色", Color) = (0,0.59,0.93,1) 6 _DeepColor("深水颜色", Color) = (0,0.43,0.68,1) 7 _DeepCheck("深水判定高度差", float) = 5.0 8 9 _HorizonColor("水平观察颜色", Color) = (0,0.9,1,1) 10 _HorizonCheck("水平观察判定", float) = 5.0 11 12 [NoScaleOffset]_WaveTex("波浪贴图", 2D) = "white" {} 13 _Wave1Color("波浪颜色1", Color) = (0.78,0.93,1,0.54) 14 _Wave2Color("波浪颜色2", Color) = (1,1,1,0.12) 15 _Wave1Offset("波浪1偏移", Vector) = (4.4,4.4,0,0) 16 _Wave2Offset("波浪2偏移", Vector) = (2.1,2.1,0.94,-0.2) 17 _WaveSpeed("波浪速度", Vector) = (0.5,-0.4,0.1,-0.2) 18 19 _EdgeCheck("边缘泡沫深度检测", float) = 0.72 20 [NoScaleOffset]_EdgeFoamTex("边缘泡沫贴图", 2D) = "white" {} 21 _FoamOffset("泡沫偏移", Vector) = (1,1,0,0) 22 _FoamColor("泡沫颜色", Color) = (1,1,1,1) 23 _FoamSpeed("泡沫流动速度", Vector) = (0,0,0,0) 24 _FoamCutOff("泡沫截断判定", float) = 0.5 25 _FoamBlend("泡沫透明度混合", Range(0,1)) = 0 26 27 } 28 SubShader 29 { 30 Tags { "RenderType"="Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline"} 31 LOD 100 32 33 Pass 34 { 35 HLSLPROGRAM 36 #pragma vertex vert 37 #pragma fragment frag 38 // make fog work 39 #pragma multi_compile_fog 40 41 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 42 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" 43 44 struct appdata 45 { 46 float4 vertex : POSITION; 47 float2 uv : TEXCOORD0; 48 float3 normal : NORMAL; 49 }; 50 51 struct v2f 52 { 53 float2 uv : TEXCOORD0; 54 float4 vertex : SV_POSITION; 55 float4 screenPos : TEXCOORD1; 56 float3 normalDir : TEXCOORD2; 57 float4 worldPos : TEXCOORD3; 58 }; 59 60 float4 _ShallowColor; 61 float4 _DeepColor; 62 float _DeepCheck; 63 float4 _HorizonColor; 64 float _HorizonCheck; 65 66 sampler2D _WaveTex; 67 float4 _WaveTex_ST; 68 float4 _Wave1Color; 69 float4 _Wave2Color; 70 float4 _Wave1Offset; 71 float4 _Wave2Offset; 72 float4 _WaveSpeed; 73 74 sampler2D _EdgeFoamTex; 75 float4 _EdgeFoamTex_ST; 76 float4 _FoamOffset; 77 float4 _FoamColor; 78 float4 _FoamSpeed; 79 float _EdgeCheck; 80 float _FoamCutOff; 81 float _FoamBlend; 82 83 84 v2f vert (appdata v) 85 { 86 v2f o; 87 o.vertex = TransformObjectToHClip(v.vertex); 88 o.uv = TRANSFORM_TEX(v.uv, _WaveTex); 89 o.screenPos = ComputeScreenPos(o.vertex); //屏幕空间的齐次坐标 90 o.normalDir = GetVertexNormalInputs(v.normal).normalWS; 91 o.worldPos = mul(unity_ObjectToWorld,v.vertex); 92 return o; 93 } 94 95 float4 frag (v2f i) : SV_Target 96 { 97 // sample the texture 98 //float4 col = tex2D(_MainTex, i.uv); 99 100 //DEEP COLOR 101 float2 screenPos= i.screenPos.xy / i.screenPos.w; 102 float depthValue = LinearEyeDepth(SampleSceneDepth(screenPos), _ZBufferParams); 103 float depthDifference = depthValue - i.screenPos.w; 104 float deltaDeep = saturate(depthDifference / _DeepCheck); 105 float4 DeepColor = lerp(_ShallowColor, _DeepColor, deltaDeep); 106 107 //HORIZON COLOR 108 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz); 109 float deltaHorizon = pow((1.0 - saturate(dot(normalize(i.normalDir), normalize(viewDir)))), _HorizonCheck); 110 float4 HorizonColor = lerp(DeepColor, _HorizonColor, deltaHorizon); 111 112 //WAVE TEX 113 float2 Wave1UV = i.uv * _Wave1Offset.xy + _Wave1Offset.zw + _Time.w * _WaveSpeed.xy / 100; 114 float4 Wave1Col = tex2D(_WaveTex, Wave1UV) * _Wave1Color; 115 Wave1Col.a = _Wave1Color.a; 116 float2 Wave2UV = i.uv * _Wave2Offset.xy + _Wave2Offset.zw + _Time.w * _WaveSpeed.zw / 100; 117 float4 Wave2Col = tex2D(_WaveTex, Wave2UV) * _Wave2Color; 118 Wave2Col.a = _Wave2Color.a; 119 float4 WaveCol = Wave1Col*Wave1Col.a + Wave2Col*Wave2Col.a; 120 WaveCol = WaveCol + HorizonColor; 121 122 //FOAM 123 float deltaEdge = saturate(depthDifference / _EdgeCheck); 124 float2 FoamUV = i.uv * _FoamOffset.xy + _FoamOffset.zw + _Time.w * _FoamSpeed.xy / 100; 125 float4 FoamColor = tex2D(_EdgeFoamTex, FoamUV) * _FoamColor * (1-deltaEdge); 126 float cutColorValue = step(_FoamCutOff, FoamColor.r); 127 FoamColor = float4(cutColorValue,cutColorValue,cutColorValue,(1 - (1-deltaEdge) * _FoamBlend)); 128 FoamColor = FoamColor*FoamColor.a + WaveCol; 129 130 131 return FoamColor; 132 } 133 ENDHLSL 134 } 135 } 136 }
呃呃 后面增加了一个波浪的扰动
1 Shader "Unlit/S_FoamWater" 2 { 3 Properties 4 { 5 _ShallowColor("浅水颜色", Color) = (0,0.59,0.93,1) 6 _DeepColor("深水颜色", Color) = (0,0.43,0.68,1) 7 _DeepCheck("深水判定高度差", float) = 5.0 8 9 _HorizonColor("水平观察颜色", Color) = (0,0.9,1,1) 10 _HorizonCheck("水平观察判定", float) = 5.0 11 12 [NoScaleOffset]_NoiseTex("扰动贴图", 2D) = "white" {} 13 _NoiseOffSet1("扰动流动速度1", Vector) = (1,1,0,0) 14 _NoiseOffSet2("扰动流动速度2", Vector) = (1,1,0,0) 15 _NoisePower("扰动流动强度", Vector) = (1,1,1,1) 16 17 18 [NoScaleOffset]_WaveTex("波浪贴图", 2D) = "white" {} 19 _Wave1Color("波浪颜色1", Color) = (0.78,0.93,1,0.54) 20 _Wave2Color("波浪颜色2", Color) = (1,1,1,0.12) 21 _Wave1Offset("波浪1偏移", Vector) = (4.4,4.4,0,0) 22 _Wave2Offset("波浪2偏移", Vector) = (2.1,2.1,0.94,-0.2) 23 _WaveSpeed("波浪速度", Vector) = (0.5,-0.4,0.1,-0.2) 24 25 _EdgeCheck("边缘泡沫深度检测", float) = 0.72 26 [NoScaleOffset]_EdgeFoamTex("边缘泡沫贴图", 2D) = "white" {} 27 _FoamOffset("泡沫偏移", Vector) = (1,1,0,0) 28 _FoamColor("泡沫颜色", Color) = (1,1,1,1) 29 _FoamSpeed("泡沫流动速度", Vector) = (0,0,0,0) 30 _FoamCutOff("泡沫截断判定", float) = 0.5 31 _FoamBlend("泡沫透明度混合", Range(0, 1.5)) = 0 32 33 } 34 SubShader 35 { 36 Tags { "RenderType"="Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline"} 37 LOD 100 38 39 Pass 40 { 41 HLSLPROGRAM 42 #pragma vertex vert 43 #pragma fragment frag 44 // make fog work 45 #pragma multi_compile_fog 46 47 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 48 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" 49 50 struct appdata 51 { 52 float4 vertex : POSITION; 53 float2 uv : TEXCOORD0; 54 float3 normal : NORMAL; 55 }; 56 57 struct v2f 58 { 59 float2 uv : TEXCOORD0; 60 float4 vertex : SV_POSITION; 61 float4 screenPos : TEXCOORD1; 62 float3 normalDir : TEXCOORD2; 63 float4 worldPos : TEXCOORD3; 64 }; 65 66 float4 _ShallowColor; 67 float4 _DeepColor; 68 float _DeepCheck; 69 float4 _HorizonColor; 70 float _HorizonCheck; 71 72 sampler2D _NoiseTex; 73 float4 _NoiseTex_ST; 74 float4 _NoiseOffSet1; 75 float4 _NoiseOffSet2; 76 float4 _NoisePower; 77 78 sampler2D _WaveTex; 79 float4 _WaveTex_ST; 80 float4 _Wave1Color; 81 float4 _Wave2Color; 82 float4 _Wave1Offset; 83 float4 _Wave2Offset; 84 float4 _WaveSpeed; 85 86 sampler2D _EdgeFoamTex; 87 float4 _EdgeFoamTex_ST; 88 float4 _FoamOffset; 89 float4 _FoamColor; 90 float4 _FoamSpeed; 91 float _EdgeCheck; 92 float _FoamCutOff; 93 float _FoamBlend; 94 95 96 v2f vert (appdata v) 97 { 98 v2f o; 99 o.vertex = TransformObjectToHClip(v.vertex); 100 o.uv = TRANSFORM_TEX(v.uv, _WaveTex); 101 o.screenPos = ComputeScreenPos(o.vertex); //屏幕空间的齐次坐标 102 o.normalDir = GetVertexNormalInputs(v.normal).normalWS; 103 o.worldPos = mul(unity_ObjectToWorld,v.vertex); 104 return o; 105 } 106 107 float4 frag (v2f i) : SV_Target 108 { 109 // sample the texture 110 //float4 col = tex2D(_MainTex, i.uv); 111 112 //DEEP COLOR 113 float2 screenPos= i.screenPos.xy / i.screenPos.w; 114 float depthValue = LinearEyeDepth(SampleSceneDepth(screenPos), _ZBufferParams); 115 float depthDifference = depthValue - i.screenPos.w; 116 float deltaDeep = saturate(depthDifference / _DeepCheck); 117 float4 DeepColor = lerp(_ShallowColor, _DeepColor, deltaDeep); 118 119 //HORIZON COLOR 120 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz); 121 float deltaHorizon = pow((1.0 - saturate(dot(normalize(i.normalDir), normalize(viewDir)))), _HorizonCheck); 122 float4 HorizonColor = lerp(DeepColor, _HorizonColor, deltaHorizon); 123 124 //NOISE uv 125 float NoiseTex1 = tex2D(_NoiseTex, i.uv * _NoiseOffSet1.xy + _Time.w * _NoiseOffSet1.zw / 100).rg; 126 float NoiseTex2 = tex2D(_NoiseTex, i.uv + _NoiseOffSet2.xy + _Time.w * _NoiseOffSet2.zw / 100).rg; 127 float2 Wave1Offset_XY = _Wave1Offset.xy + float2(NoiseTex1 * _NoisePower.x, NoiseTex2 * _NoisePower.y); 128 float2 Wave2Offset_XY = _Wave2Offset.xy + float2(NoiseTex1 * _NoisePower.z, NoiseTex2 * _NoisePower.w); 129 130 //WAVE TEX 131 float2 Wave1UV = i.uv * Wave1Offset_XY + _Wave1Offset.zw + _Time.w * _WaveSpeed.xy / 100; 132 float4 Wave1Col = tex2D(_WaveTex, Wave1UV) * _Wave1Color; 133 Wave1Col.a = _Wave1Color.a; 134 float2 Wave2UV = i.uv * Wave2Offset_XY.xy + _Wave2Offset.zw + _Time.w * _WaveSpeed.zw / 100; 135 float4 Wave2Col = tex2D(_WaveTex, Wave2UV) * _Wave2Color; 136 Wave2Col.a = _Wave2Color.a; 137 float4 WaveCol = Wave1Col*Wave1Col.a + Wave2Col*Wave2Col.a; 138 WaveCol = WaveCol + HorizonColor; 139 140 //FOAM 141 float deltaEdge = saturate(depthDifference / _EdgeCheck); 142 float2 FoamUV = i.uv * _FoamOffset.xy + _FoamOffset.zw + _Time.w * _FoamSpeed.xy / 100; 143 float4 FoamColor = tex2D(_EdgeFoamTex, FoamUV) * _FoamColor * (1-deltaEdge); 144 float cutColorValue = step(_FoamCutOff, FoamColor.r); 145 FoamColor = float4(cutColorValue,cutColorValue,cutColorValue,(1 - deltaEdge * _FoamBlend)); 146 FoamColor = FoamColor*FoamColor.a + WaveCol; 147 148 149 return FoamColor; 150 } 151 ENDHLSL 152 } 153 } 154 }