栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
{
    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();
        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
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号