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;
float depth = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,ssuv);
float ssdepth = LinearEyeDepth(depth,_ZBufferParams);
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;
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
}
}
}
|