Simple Fog (URP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Hidden/FogShader" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
} | |
SubShader | |
{ | |
// No culling or depth | |
Cull Off ZWrite Off ZTest Always | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
sampler2D _MainTex; | |
sampler _CameraDepthTexture; | |
float3 _FogColor; | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float4 depthColor = tex2D(_CameraDepthTexture, i.uv); | |
float linearDepth = Linear01Depth(depthColor.r); | |
float fogDensity = linearDepth; | |
float4 mainTexColor = tex2D(_MainTex, i.uv); | |
float4 outColor = mainTexColor; | |
outColor.rgb = lerp(mainTexColor.rgb, _FogColor, fogDensity); | |
return outColor; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.Universal; | |
public class TestRendererFeature : ScriptableRendererFeature | |
{ | |
private TestRenderPass testRenderPass; | |
public Material fogMaterial; | |
public Color fogColor; | |
class TestRenderPass : ScriptableRenderPass | |
{ | |
private Material mat; | |
private Color color; | |
private RenderTargetIdentifier source; | |
private readonly int temporaryRTId = Shader.PropertyToID("_TempRT"); | |
public TestRenderPass(Material _mat, Color _color) { | |
mat = _mat; | |
color = _color; | |
} | |
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) { | |
source = renderingData.cameraData.renderer.cameraColorTarget; | |
} | |
public override void Execute(ScriptableRenderContext _context, ref RenderingData _renderingData) { | |
CommandBuffer cmd = CommandBufferPool.Get(name: "TestRenderPass"); | |
RenderTextureDescriptor descriptor = _renderingData.cameraData.cameraTargetDescriptor; | |
descriptor.depthBufferBits = 0; | |
cmd.GetTemporaryRT(temporaryRTId, descriptor, FilterMode.Bilinear); | |
RenderTargetIdentifier rtIdentifier = new RenderTargetIdentifier(temporaryRTId); | |
cmd.CopyTexture(source, rtIdentifier); | |
mat.SetColor("_FogColor", color); | |
cmd.Blit(rtIdentifier, source, mat); | |
cmd.ReleaseTemporaryRT(temporaryRTId); | |
_context.ExecuteCommandBuffer(cmd); | |
CommandBufferPool.Release(cmd); | |
} | |
} | |
public override void Create() { | |
testRenderPass = new TestRenderPass(fogMaterial, fogColor); | |
testRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingSkybox; | |
} | |
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { | |
if (fogMaterial != null) { | |
renderer.EnqueuePass(testRenderPass); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment