- Home /
Why doesn't this shader work when using it with as a custom effect on the post processing stack
During game mode, the effect doesn't do anything and while in edit mode, the game preview flickers between black and the normal camera view. Any help would be greatly appreciated.
Shader code
Shader "Custom/FirewatchFog"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_FogAmount("Fog amount", float) = 1
_ColorRamp("Color ramp", 2D) = "white" {}
_FogIntensity("Fog intensity", float) = 1
}
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;
float4 scrPos : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.scrPos = ComputeScreenPos(o.vertex);
return o;
}
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
sampler2D _ColorRamp;
float _FogAmount;
float _FogIntensity;
fixed4 frag(v2f i) : SV_Target
{
fixed4 orCol = tex2D(_MainTex, i.uv);
float depthValue = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)));
float depthValueMul = depthValue * _FogAmount;
fixed4 fogCol = tex2D(_ColorRamp, (float2(depthValueMul, 0)));
return (depthValue < 1) ? lerp(orCol, fogCol, fogCol.a * _FogIntensity) : orCol;
}
ENDCG
}
}
}
Code to add the effect to the post processing stack
using UnityEngine;
using System.Collections;
using UnityEngine.Rendering.PostProcessing;
using System;
[Serializable]
[PostProcess(typeof(RampFogRenderer), PostProcessEvent.AfterStack, "Custom/FirewatchFog")]
public sealed class RampFog : PostProcessEffectSettings
{
[Range(0,5), Tooltip("Fog intensity.")]
public FloatParameter intensity = new FloatParameter { value = 0.5f };
[Range(0, 5), Tooltip("Fog amount.")]
public FloatParameter amount = new FloatParameter { value = 0.5f };
public TextureParameter albedo = new TextureParameter();
public TextureParameter rampTexture = new TextureParameter();
}
public sealed class RampFogRenderer : PostProcessEffectRenderer<RampFog>
{
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Custom/FirewatchFog"));
sheet.properties.SetFloat("_FogIntensity", settings.intensity);
sheet.properties.SetFloat("_FogAmount", settings.amount);
sheet.properties.SetTexture("_ColorRamp", settings.rampTexture);
sheet.properties.SetTexture("_MainTex", settings.albedo);
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}
}
You're using stack 2.0? Are you following this guide? https://github.com/Unity-Technologies/PostProcessing/wiki/Quick-start
I'm using this as a reference: https://github.com/Unity-Technologies/PostProcessing/wiki/Writing-Custom-Effects
And I'm using the PostProcessing stack from the package manager, version for it is 2.0.10
Also I should add that the shader itself works fine when using it the legacy way, from a image effect script, as instructed in this post: https://forum.unity.com/threads/how-to-use-shaders-in-custom-post-effects.322009/
Stack 2.0 follows new rules, so guides for previous versions may not work as intended. Try the new guide.
Answer by WubiCookie · May 20, 2019 at 01:37 PM
Hi! I had this problem too and I just solved it, so here's the answer:
When using the BlitFullscreenTriangle
method, the input vertex positions are in clip space, so o.vertex = UnityObjectToClipPos(v.vertex);
is invalid. You must do
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
Also, there is no input texture coordinates, you must compute them from the positions:
o.texcoord = (v.vertex.xy + 1.0) * 0.5;
#if UNITY_UV_STARTS_AT_TOP
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
#endif
@Carnivorous I hope I'm not too late :D