IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> unity urp自定义后处理 雾效 深度雾,高度雾 简版 -> 正文阅读

[游戏开发]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
        }
    }
}
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-01-08 14:22:27  更:2022-01-08 14:24:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 19:52:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码