unity urp自定义后处理 雾效 深度雾,高度雾 简版

1,c#代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FogShaderName
{
    public const string FogColor = "_FogColor";
    public const string FogIntensity = "_FogIntensity";
    public const string FogDistance = "_FogDistance";
    public const string FogHigh = "_FogHigh";
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class FogVolme : VolumeComponent,IPostProcessComponent
{
    public FloatParameter intensity = new FloatParameter(0);
    public ColorParameter fogColor = new ColorParameter(Color.white);
    public FloatParameter distance = new FloatParameter(0);
    public FogModeParameter fogMode = new FogModeParameter(FogMode.off);
    public bool IsActive()
    {
        return intensity.value > 0 && fogMode != FogMode.off;
    }

    public bool IsTileCompatible()
    {
        return false;
    }
}

public enum FogMode
{
    off,
    distance,
    high
}
[Serializable]
public sealed class FogModeParameter : VolumeParameter<FogMode>
{
    public FogModeParameter(FogMode value, bool overriderState = false) : base(value, overriderState)
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class FogFeature : ScriptableRendererFeature
{
    private FogPass fogPass;
    public Material distanceMaterial;
    public Material highMaterial;
    public override void Create()
    {
        fogPass = new FogPass();
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(fogPass);
        fogPass.Setup(renderer.cameraColorTarget,distanceMaterial,highMaterial);
    }
}

public class FogPass : ScriptableRenderPass
{
    private RenderTargetIdentifier source;
    private RenderTargetHandle tempTargetHandle;
    private Material distanceMaterial;
    private Material highMaterial;
    private int fogColorId = Shader.PropertyToID(FogShaderName.FogColor);
    private int fogIntensityId = Shader.PropertyToID(FogShaderName.FogIntensity);
    private int fogDistanceId = Shader.PropertyToID(FogShaderName.FogDistance);
    private int fogHighId = Shader.PropertyToID(FogShaderName.FogHigh);
    
    public FogPass()
    {
        tempTargetHandle.Init("tempfog");
    }

    public void Setup(RenderTargetIdentifier source,Material distanceMaterial,Material highMaterial)
    {
        this.source = source;
        this.distanceMaterial = distanceMaterial;
        this.highMaterial = highMaterial;
    }

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        FogVolme fogVolme = VolumeManager.instance.stack.GetComponent<FogVolme>();
        if (fogVolme.IsActive())
        {
            CommandBuffer cmd = CommandBufferPool.Get("FogCmd");
            var dec = renderingData.cameraData.cameraTargetDescriptor;
            dec.msaaSamples = 1;
            dec.depthBufferBits = 0;
            cmd.GetTemporaryRT(tempTargetHandle.id,dec);
            if (fogVolme.fogMode == FogMode.distance)
            {
                distanceMaterial.SetColor(fogColorId,fogVolme.fogColor.value);
                distanceMaterial.SetFloat(fogIntensityId,fogVolme.intensity.value);
                distanceMaterial.SetFloat(fogDistanceId,fogVolme.distance.value);
                cmd.Blit(source,tempTargetHandle.Identifier(),distanceMaterial);
            }
            else if (fogVolme.fogMode == FogMode.high)
            {
                highMaterial.SetColor(fogColorId,fogVolme.fogColor.value);
                highMaterial.SetFloat(fogIntensityId,fogVolme.intensity.value);
                highMaterial.SetFloat(fogHighId,fogVolme.distance.value);
                cmd.Blit(source,tempTargetHandle.Identifier(),highMaterial);
            }
            cmd.Blit(tempTargetHandle.Identifier(),source);
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
            cmd.ReleaseTemporaryRT(tempTargetHandle.id);
        }
    }
}

2,shader
深度雾

Shader "Unlit/DistanceFog"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _FogColor("FogColor",color) = (1,1,1,1)
        _FogIntensity("FogIntensity",float) = 1
        _FogDistance("FogDistance",float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline"}
        LOD 100

        Pass
        {
            Tags{"LightMode"="UniversalForward"}
            ZTest Always
            ZWrite Off
            Cull Off

            
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/core.hlsl"

            struct appdata
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 positionCS : SV_POSITION;
                float4 ssTexcoord : TEXCOORD1;
            };

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_CameraDepthTexture);
            SAMPLER(sampler_CameraDepthTexture);
            float4 _MainTex_ST;
            float4 _FogColor;
            float _FogIntensity;
            float _FogDistance;

            v2f vert (appdata v)
            {
                v2f o;
                o.positionCS = TransformObjectToHClip(v.positionOS);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.ssTexcoord = ComputeScreenPos(o.positionCS);
                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
                float2 ssuv = i.ssTexcoord.xy/i.ssTexcoord.w; //uv
                float depth = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,ssuv);//深度图采样
                float ssdepth = LinearEyeDepth(depth,_ZBufferParams);//线性深度
                
                //half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,ssuv) ;
                //half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,i.uv) + step(_FogDistance,ssdepth) * _FogIntensity * ssdepth * _FogColor;
                half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,i.uv) + pow(ssdepth,_FogDistance) * _FogIntensity * _FogColor;
                return col;
            }
            ENDHLSL
        }
    }
}

高度雾

Shader "Unlit/HighFog"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _FogColor("FogColor",color) = (1,1,1,1)
        _FogIntensity("FogIntensity",float) = 1
        _FogHigh("FogHigh",float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline"}
        LOD 100

        Pass
        {
            Tags{"LightMode"="UniversalForward"}
            ZTest Always
            ZWrite Off
            Cull Off

            
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/core.hlsl"

            struct appdata
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 positionCS : SV_POSITION;
                float4 ssTexcoord : TEXCOORD1;
            };

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_CameraDepthTexture);
            SAMPLER(sampler_CameraDepthTexture);
            float4 _MainTex_ST;
            float4 _FogColor;
            float _FogIntensity;
            float _FogHigh;

            v2f vert (appdata v)
            {
                v2f o;
                o.positionCS = TransformObjectToHClip(v.positionOS);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.ssTexcoord = ComputeScreenPos(o.positionCS);
                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
                float2 ssuv = i.ssTexcoord.xy/i.ssTexcoord.w; //uv
                float depth = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,ssuv);//深度图采样
                float ssdepth = LinearEyeDepth(depth,_ZBufferParams);//线性深度
                float3 postionWS = ComputeWorldSpacePosition(ssuv,depth,unity_MatrixInvVP);//世界坐标重建

                half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,i.uv) + step(_FogHigh,postionWS.y) * _FogIntensity * _FogColor * ssdepth;
                return col;
            }
            ENDHLSL
        }
    }
}
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值