Unity URP高度雾效果Shader
生活随笔
收集整理的這篇文章主要介紹了
Unity URP高度雾效果Shader
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)現(xiàn)原理
見(jiàn)這篇文章Unity Shader-深度相關(guān)知識(shí)總結(jié)與效果實(shí)現(xiàn)(LinearDepth,Reverse Z,世界坐標(biāo)重建,軟粒子,高度霧,運(yùn)動(dòng)模糊,掃描線效果)_puppet_master的專欄-CSDN博客_shader深度
?核心Shader代碼
Shader "Universal Render Pipeline/Dejavu/HeightFog" {Properties{_MainTex("Base (RGB)", 2D) = "white" {}[HDR]_FogColor("_FogColor (default = 1,1,1,1)", color) = (1,1,1,1)}HLSLINCLUDE #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"CBUFFER_START(UnityPerMaterial)float4 _MainTex_ST;half4 _FogColor;float _FogStartHeight;float _FogHeight;float _FogIntensity;CBUFFER_END//TEXTURE2D(_MainTex);//SAMPLER(sampler_MainTex);sampler2D _MainTex;TEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);struct appdata {float4 positionOS : POSITION;float2 uv : TEXCOORD0;UNITY_VERTEX_INPUT_INSTANCE_ID};struct v2f {float4 positionCS : SV_POSITION;float2 uv : TEXCOORD0;float3 viewRayWorld : TEXCOORD1;UNITY_VERTEX_OUTPUT_STEREO};//vertex shaderv2f vert(appdata v){v2f o;UNITY_SETUP_INSTANCE_ID(v);UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);o.positionCS = TransformObjectToHClip(v.positionOS.xyz);float sceneRawDepth = 1;#if defined(UNITY_REVERSED_Z)sceneRawDepth = 1 - sceneRawDepth; #endiffloat3 worldPos = ComputeWorldSpacePosition(v.uv, sceneRawDepth, UNITY_MATRIX_I_VP);o.viewRayWorld = worldPos - _WorldSpaceCameraPos.xyz;o.uv = v.uv;return o;}//fragment shaderfloat4 frag(v2f i) : SV_Target{float sceneRawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.uv);float linear01Depth = Linear01Depth(sceneRawDepth, _ZBufferParams);float3 worldPos = _WorldSpaceCameraPos.xyz + ( linear01Depth) * i.viewRayWorld;float blendParam = saturate((_FogStartHeight - worldPos.y) / _FogHeight);blendParam = max(linear01Depth * _FogHeight, blendParam);half4 screenCol = tex2D(_MainTex, i.uv);return lerp(screenCol, _FogColor, blendParam * _FogIntensity);}ENDHLSL//開(kāi)始SubShaderSubShader{//Tags {"RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}Tags{ "RenderPipeline" = "UniversalPipeline" "RenderType" = "Overlay" "Queue" = "Transparent-499" "DisableBatching" = "True" }LOD 100ZTest Always Cull Off ZWrite OffBlend one zeroPass{Name "HeightFog"//后處理效果一般都是這幾個(gè)狀態(tài)//使用上面定義的vertex和fragment shaderHLSLPROGRAM#pragma vertex vert#pragma fragment fragENDHLSL}}//后處理效果一般不給fallback,如果不支持,不顯示后處理即可 }實(shí)現(xiàn)效果
工程鏈接
GitHub - Dejavu0709/StudyForShader??中的HeightFog文件夾
總結(jié)
以上是生活随笔為你收集整理的Unity URP高度雾效果Shader的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 战争雷霆陆战全地图要点简析 陆战全地图特
- 下一篇: Unity URP世界空间后处理扫描圈效