文章目錄
- 前言
- 一、優化水的深度
- 1、我們把 水流動的方向 和 水深淺過渡值,整合到一個四維變量中
- 2、修改 水體流動方向
- 3、在片元著色器中,修改使用過渡變量
- 二、優化泡沫
- 三、優化水下的扭曲
- 1、修復原本擾動UV的計算
- 四、優化水面高光
- 1、把高光強度、光滑度、高光扭曲值,整合為一個四維變量
- 2、替換計算 高光反射 使用的法線 使用的 扭曲值
- 3、替換高光反射公式計算時,使用的光滑度
- 五、優化水的反射
- 六、優化水的焦散
- 七、最終優化代碼
前言
在之前的文章中,我們已經實現了水體幾乎全部的效果。
-
Unity中URP實現水體效果(水的深度)
-
Unity中URP實現水體效果(泡沫)
-
Unity中URP實現水體(水下的扭曲)
-
Unity中URP下實現水體(水面高光)
-
Unity中URP實現水體(水的焦散)
但是,還有一些細節需要修改,代碼可以優化。
在這篇文章中,我們就對水體Shader進行整理優化。
一、優化水的深度
1、我們把 水流動的方向 和 水深淺過渡值,整合到一個四維變量中
_WaterBase(“Direction(XY) Atten(Z) Null(W)”,Vector) = (1,1,1,1)
2、修改 水體流動方向
因為流動是,修改采樣時使用的uv,在 頂點著色器 和 片元著色器 中,都有使用。所以,把它放在 v2f 結構體來存儲,只需要計算一次。
- 在v2f結構體定義
float2 timeOffset : TEXCOORD6;
- 在頂點著色器計算,由方向值調節的流動
代替原本所有語句中使用到的 _Time.y * _WaterSpeed
o.timeOffset = _Time.y * _WaterSpeed * _WaterBase.xy;
3、在片元著色器中,修改使用過渡變量
half4 waterColor = lerp(_WaterColor1, _WaterColor2, depthWater * _WaterBase.z);
二、優化泡沫
要優化泡沫的話,也是和深度同樣的優化方法。只是我這里為了方便調節效果,就不做優化了。
三、優化水下的扭曲
1、修復原本擾動UV的計算
- 這是原本的扭曲采樣使用的uv,存在這種不正常的偏移效果
float2 distortUV = lerp(screenUV,normalTex.xy,_Distort);
- 修改直接對屏幕uv乘以擾動值,在加上normalTex的方法實現線性過渡
float2 distortUV = screenUV * _Distort + normalTex.xy;
四、優化水面高光
1、把高光強度、光滑度、高光扭曲值,整合為一個四維變量
_SpecularBase(“Specular : Intensity(X) Smoothness(Y) Distort(Z) Null(W)”,Vector) = (0.6,10,0.5,1)
2、替換計算 高光反射 使用的法線 使用的 扭曲值
half4 N = lerp(half4(i.normalWS, 1), normalize(normalTex), _SpecularBase.z);
3、替換高光反射公式計算時,使用的光滑度
half4 specular = _SpecularColor * _SpecularBase.x * pow(max(0, dot(N.xyz, H)), _SpecularBase.y);
五、優化水的反射
- 使用水下扭曲值,控制水反射法線的過渡值。從而和高光反射使用的法線分離開
half3 normalReflection = lerp(half4(i.normalWS, 1), normalize(normalTex), _Distort).xyz;
六、優化水的焦散
- 水的焦散目前沒有什么好優化的,就保持原本的吧。
七、最終優化代碼
記著在優化時,編譯Shader把所有的警告消除
//水的深度
Shader "MyShader/URP/P4_8"
{Properties{[Header(Base)]_WaterColor1("WaterColor1",Color) = (1,1,1,1)_WaterColor2("WaterColor2",Color) = (1,1,1,1)[PowerSlider(3)]_WaterSpeed("WaterSpeed",Range(0,1)) = 0.1_WaterBase("Direction(XY) Atten(Z) Null(W)",Vector) = (1,1,1,1)[Header(Foam)]_FoamTex("FoamTex",2D) = "white"{}_FoamColor("FoamColor",Color) = (1,1,1,1)_FoamRange("FoamRange",Range(0,5)) = 1_FoamNoise("FoamNoise",Range(0,3)) = 1[Header(Distort)]_NormalTex("NormalTex",2D) = "white"{}[PowerSlider(3)]_Distort("Distort",Range(0,1)) = 0[Header(Specular)]_SpecularColor("Specular Color",Color) = (1,1,1,1)_SpecularBase("Specular : Intensity(X) Smoothness(Y) Distort(Z) Null(W)",Vector) = (0.6,10,0.5,1)[Header(Reflection)]_ReflectionTex("ReflectionTex",Cube) = "white"{}[Header(Caustics)]_CausticsTex("CausticsTex",2D) = "white"{}}SubShader{Tags{//告訴引擎,該Shader只用于 URP 渲染管線"RenderPipeline"="UniversalPipeline"//渲染類型"RenderType"="Transparent"//渲染隊列"Queue"="Transparent"}//Blend SrcAlpha OneMinusSrcAlphaZWrite OffPass{HLSLPROGRAM#pragma vertex vert#pragma fragment frag// Pragmas#pragma target 2.0// Includes#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"CBUFFER_START(UnityPerMaterial)half4 _WaterColor1;half4 _WaterColor2;half _WaterSpeed;half4 _WaterBase;half4 _FoamColor;half _FoamRange;half _FoamNoise;half4 _FoamTex_ST;half _Distort;half4 _NormalTex_ST;half4 _SpecularColor;half4 _SpecularBase;half4 _CausticsTex_ST;CBUFFER_ENDTEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);TEXTURE2D(_FoamTex);SAMPLER(sampler_FoamTex);TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);TEXTURE2D(_NormalTex);SAMPLER(sampler_NormalTex);TEXTURECUBE(_ReflectionTex);SAMPLER(sampler_ReflectionTex);TEXTURE2D(_CausticsTex);SAMPLER(sampler_CausticsTex);//struct appdata//頂點著色器的輸入struct Attributes{float3 positionOS : POSITION;float2 uv : TEXCOORD0;half3 normalOS : NORMAL;};//struct v2f//片元著色器的輸入struct Varyings{float4 positionCS : SV_POSITION;float2 uv : TEXCOORD0; //foamUVfloat4 screenPos : TEXCOORD1;float3 positionVS : TEXCOORD2;float3 positionWS : TEXCOORD3;float3 normalWS : TEXCOORD4;float4 normalUV : TEXCOORD5;float2 timeOffset : TEXCOORD6;};//v2f vert(Attributes v)//頂點著色器Varyings vert(Attributes v){Varyings o = (Varyings)0;o.positionWS = TransformObjectToWorld(v.positionOS);o.positionVS = TransformWorldToView(o.positionWS);o.positionCS = TransformWViewToHClip(o.positionVS);o.screenPos = ComputeScreenPos(o.positionCS);//計算得到泡沫紋理采樣需要的頂點世界空間下的坐標值的流動效果o.uv += o.positionWS.xz * _FoamTex_ST.xy + _Time.y * _WaterSpeed;//計算得到水下扭曲紋理的流動UVo.timeOffset = _Time.y * _WaterSpeed * _WaterBase.xy;o.normalUV.xy = TRANSFORM_TEX(v.uv, _NormalTex) + o.timeOffset;o.normalUV.zw = TRANSFORM_TEX(v.uv, _NormalTex) + o.timeOffset * float2(-1.3, 1.5);o.normalWS = TransformObjectToWorldNormal(v.normalOS);return o;}//fixed4 frag(v2f i) : SV_TARGET//片元著色器half4 frag(Varyings i) : SV_TARGET{//1、水的深度//獲取屏幕空間下的 UV 坐標float2 screenUV = i.positionCS.xy / _ScreenParams.xy;half depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV).x;//深度圖轉化到觀察空間下float depthScene = LinearEyeDepth(depthTex, _ZBufferParams);//獲取水面模型頂點在觀察空間下的Z值(可以在頂點著色器中,對其直接進行轉化得到頂點觀察空間下的坐標)float4 depthWater = depthScene + i.positionVS.z;//2、水的顏色,線性插值得到水 和 接觸物體的水的 顏色的過度half4 waterColor = lerp(_WaterColor1, _WaterColor2, depthWater * _WaterBase.z);//3、水面泡沫//對泡沫紋理進行采樣(這里使用頂點世界空間下的坐標進行紋理采樣,防止水體縮放影響泡沫的平鋪和重復方式)half4 foamTex = SAMPLE_TEXTURE2D(_FoamTex, sampler_FoamTex, i.uv.xy);foamTex = pow(abs(foamTex), _FoamNoise);//這里增加一個調整深度圖范圍的功能half4 foamRange = depthWater * _FoamRange;//使用泡沫紋理 和 泡沫范圍 比較得到泡沫遮罩half4 foamMask = step(foamRange, foamTex);//給泡沫加上顏色half4 foamColor = foamMask * _FoamColor;//4、水下的扭曲half4 normalTex1 = SAMPLE_TEXTURE2D(_NormalTex, sampler_NormalTex, i.normalUV.xy);half4 normalTex2 = SAMPLE_TEXTURE2D(_NormalTex, sampler_NormalTex, i.normalUV.zw);half4 normalTex = normalTex1 * normalTex2;//float2 distortUV = lerp(screenUV,normalTex.xy,_Distort);float2 distortUV = screenUV * _Distort + normalTex.xy;half depthDistortTex = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, distortUV).x;half depthDistortScene = LinearEyeDepth(depthDistortTex, _ZBufferParams);half depthDistortWater = depthDistortScene + i.positionVS.z;float2 opaqueUV = distortUV;if (depthDistortWater < 0) opaqueUV = screenUV;half4 opaqueTex = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, opaqueUV);//5、水的高光//Specular = SpecularColor * Ks * pow(max(0,dot(N,H)), Shininess)Light light = GetMainLight();half3 L = light.direction;half3 V = normalize(_WorldSpaceCameraPos.xyz - i.positionWS.xyz);//修改法線實現,波光粼粼的效果half4 N = lerp(half4(i.normalWS, 1), normalize(normalTex), _SpecularBase.z);half3 H = normalize(L + V);half4 specular = _SpecularColor * _SpecularBase.x * pow(max(0, dot(N.xyz, H)), _SpecularBase.y);//6、水的反射half3 normalReflection = lerp(half4(i.normalWS, 1), normalize(normalTex), _Distort).xyz;half3 reflectionUV = reflect(-V, normalReflection.xyz);half4 reflectionTex = SAMPLE_TEXTURECUBE(_ReflectionTex, sampler_ReflectionTex, reflectionUV);half fresnel = 1 - saturate(dot(normalReflection, V));half4 reflection = reflectionTex * fresnel;//7、水的焦散float4 depthVS = 1;depthVS.xy = i.positionVS.xy * depthDistortScene / -i.positionVS.z;depthVS.z = depthDistortScene;float4 depthWS = mul(unity_CameraToWorld, depthVS);float4 depthOS = mul(unity_WorldToObject, depthWS);float2 uv1 = depthOS.xz * _CausticsTex_ST.xy + depthWS.y * 0.1 + i.timeOffset;float2 uv2 = depthOS.xz * _CausticsTex_ST.xy + depthWS.y * 0.1 + i.timeOffset * float2(-1.3, 1.5);half4 causticsTex1 = SAMPLE_TEXTURE2D(_CausticsTex, sampler_CausticsTex, uv1);half4 causticsTex2 = SAMPLE_TEXTURE2D(_CausticsTex, sampler_CausticsTex, uv2);half4 causticsTex = min(causticsTex1, causticsTex2);half4 col = (foamColor + waterColor) * opaqueTex + (specular * reflection) + causticsTex;return col;}ENDHLSL}}FallBack "Hidden/Shader Graph/FallbackError"
}