蘇州專業(yè)做網(wǎng)站公司有哪些小說推廣關(guān)鍵詞怎么弄
文章目錄
- 前言
- 一、實(shí)現(xiàn)高光反射原理
- 1、原理:
- 2、公式:
- 二、實(shí)現(xiàn)
- 1、定義 _SpecularColor 作為高光反射的顏色
- 2、定義 _SpecularIntensity 作為反射系數(shù),控制高光反射的強(qiáng)度
- 3、定義 _Smoothness 作為高光指數(shù),用于模型高光范圍
- 4、模擬出水面,波瀾起伏的效果(用法線紋理代替原本的法線信息,計(jì)算出 N ? \vec{N} N)
- 6、計(jì)算出 半角向量 H ? \vec{H} H
- 7、帶入公式,得到Blinn-Phone高光效果
- 8、與上一篇文章中,計(jì)算得到水下扭曲的結(jié)果,相加輸出即可
- 三、最終效果
前言
在上一篇文章中,我們實(shí)現(xiàn)了水體的水下扭曲效果。
- Unity中URP實(shí)現(xiàn)水體(水下的扭曲)
在這篇文章中,我們來實(shí)現(xiàn)水面的高光效果,這里高光光照模型我們使用 Bllinn Phone 光照模型。
一、實(shí)現(xiàn)高光反射原理
1、原理:
- Unity中Shader光照模型Blinn-Phong原理及實(shí)現(xiàn)
2、公式:
Specular = SpecularColor * Ks * pow(max(0,dot(N,H)),Shininess)
- Specular:高光反射的最終顏色
- SpecularColor:高光反射顏色
- Ks:反射系數(shù)
- N:頂點(diǎn)法向量
- H:入射光線L 和 視線向量V的半角向量
- Shininess:高光指數(shù),用于模擬高光的范圍
二、實(shí)現(xiàn)
1、定義 _SpecularColor 作為高光反射的顏色
_SpecularColor(“Specular Color”,Color) = (1,1,1,1)
2、定義 _SpecularIntensity 作為反射系數(shù),控制高光反射的強(qiáng)度
_SpecularIntensity(“Specular Intensity”,Float) = 0.6
3、定義 _Smoothness 作為高光指數(shù),用于模型高光范圍
_Smoothness(“Smoothness”,Float) = 10
4、模擬出水面,波瀾起伏的效果(用法線紋理代替原本的法線信息,計(jì)算出 N ? \vec{N} N)
- 在屬性面板接收法線貼圖,用于代替原本的法線 N ? \vec{N} N
_NormalTex(“NormalTex”,2D) = “white”{}
- 在Pass中,申明 法線紋理 和 采樣器
TEXTURE2D(_NormalTex);SAMPLER(sampler_NormalTex);
- 在 Varyings 中,定義一個(gè)四維變量,用于存儲(chǔ)兩組方向相反的流動(dòng)uv
float4 normalUV : TEXCOORD5;
- 在頂點(diǎn)著色器中,計(jì)算得到兩組方向相反的流動(dòng)uv,用于法線紋理的采樣(目的是實(shí)現(xiàn)波浪隨機(jī)凌亂的效果)
o.normalUV.xy = TRANSFORM_TEX(v.uv,_NormalTex) + _Time.y * _WaterSpeed;
o.normalUV.zw = TRANSFORM_TEX(v.uv,_NormalTex) + _Time.y * _WaterSpeed * half2(-1,1);
- 在片元著色器中,對法線紋理用上面兩組uv分別采樣,得到兩個(gè)法線紋理,使其相乘得到混亂波浪的效果。
half4 normalTex1 = SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.normalUV.xy);
half4 normalTex2 = SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.normalUV.zw);
half4 normalTex = normalTex1 * normalTex2;
- 用法線紋理 替代原本模型的法線信息
half4 N = normalize(normalTex);
6、計(jì)算出 半角向量 H ? \vec{H} H
H ? = L ? + V ? \vec{H} = \vec{L} + \vec{V} H=L+V
- 計(jì)算出指向光線向量 L ? \vec{L} L
得到主光信息后,使用其方向成員即可
Light light = GetMainLight();
half3 L = light.direction;
- 計(jì)算出指向視線的向量 V ? \vec{V} V
使用世界空間下,攝像機(jī)坐標(biāo) 減去 頂點(diǎn)坐標(biāo) 即可
half3 V = normalize(_WorldSpaceCameraPos.xyz - i.positionWS.xyz);
- 相加得到半角向量
half3 H = normalize(L + V);
7、帶入公式,得到Blinn-Phone高光效果
Specular = SpecularColor * Ks * pow(max(0,dot(N,H)),Shininess)
half4 specular = _SpecularColor * _SpecularIntensity * pow(max(0,dot(N,H)),_Smoothness);
8、與上一篇文章中,計(jì)算得到水下扭曲的結(jié)果,相加輸出即可
col += specular;
三、最終效果
//水的深度
Shader "MyShader/URP/P4_8"
{Properties {[Header(Base)]_WaterColor1("WaterColor1",Color) = (1,1,1,1)_WaterColor2("WaterColor2",Color) = (1,1,1,1)_WaterSpeed("WaterSpeed",Range(0,1)) = 0.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,0.5)) = 0[Header(Specular)]_SpecularColor("Specular Color",Color) = (1,1,1,1)_SpecularIntensity("Specular Intensity",Float) = 0.6_Smoothness("Smoothness",Float) = 10}SubShader{Tags{//告訴引擎,該Shader只用于 URP 渲染管線"RenderPipeline"="UniversalPipeline"//渲染類型"RenderType"="Transparent"//渲染隊(duì)列"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 _FoamColor;half _FoamRange;half _FoamNoise;half4 _FoamTex_ST;half _Distort;half4 _NormalTex_ST;half4 _SpecularColor;half _SpecularIntensity;half _Smoothness;CBUFFER_ENDTEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);TEXTURE2D(_FoamTex);SAMPLER(sampler_FoamTex);TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);TEXTURE2D(_NormalTex);SAMPLER(sampler_NormalTex);//struct appdata//頂點(diǎn)著色器的輸入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;};//v2f vert(Attributes v)//頂點(diǎn)著色器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);//計(jì)算得到泡沫紋理采樣需要的頂點(diǎn)世界空間下的坐標(biāo)值的流動(dòng)效果o.uv += o.positionWS.xz *_FoamTex_ST.xy + _Time.y * _WaterSpeed;//計(jì)算得到水下扭曲紋理的流動(dòng)UVo.normalUV.xy = TRANSFORM_TEX(v.uv,_NormalTex) + _Time.y * _WaterSpeed;o.normalUV.zw = TRANSFORM_TEX(v.uv,_NormalTex) + _Time.y * _WaterSpeed * half2(-1,1);o.normalWS = TransformObjectToWorldNormal(v.normalOS);return o;}//fixed4 frag(v2f i) : SV_TARGET//片元著色器half4 frag(Varyings i) : SV_TARGET{//1、水的深度//獲取屏幕空間下的 UV 坐標(biāo)float2 screenUV = i.positionCS.xy / _ScreenParams.xy;half depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,screenUV).x;//深度圖轉(zhuǎn)化到觀察空間下float depthScene = LinearEyeDepth(depthTex,_ZBufferParams);//獲取水面模型頂點(diǎn)在觀察空間下的Z值(可以在頂點(diǎn)著色器中,對其直接進(jìn)行轉(zhuǎn)化得到頂點(diǎn)觀察空間下的坐標(biāo))float4 depthWater = depthScene + i.positionVS.z;//2、水的顏色,線性插值得到水 和 接觸物體的水的 顏色的過度half4 waterColor = lerp(_WaterColor1,_WaterColor2,depthWater);//3、水面泡沫//對泡沫紋理進(jìn)行采樣(這里使用頂點(diǎn)世界空間下的坐標(biāo)進(jìn)行紋理采樣,防止水體縮放影響泡沫的平鋪和重復(fù)方式)half4 foamTex = SAMPLE_TEXTURE2D(_FoamTex,sampler_FoamTex,i.uv.xy);foamTex = pow(foamTex,_FoamNoise);//這里增加一個(gè)調(diào)整深度圖范圍的功能half4 foamRange = depthWater * _FoamRange;//使用泡沫紋理 和 泡沫范圍 比較得到泡沫遮罩half4 foamMask = step(foamRange,foamTex);//給泡沫加上顏色half4 foamColor = foamMask * _FoamColor;half4 col = foamColor + waterColor;//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,_Distort);half4 cameraOpaqueTex = SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,distortUV);col *= cameraOpaqueTex;//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);//修改法線實(shí)現(xiàn),波光粼粼的效果half4 N = normalize(normalTex);half3 H = normalize(L + V);half4 specular = _SpecularColor * _SpecularIntensity * pow(max(0,dot(N,H)),_Smoothness);col += specular;//水的反射//水的焦散return col;}ENDHLSL}}FallBack "Hidden/Shader Graph/FallbackError"
}