Don’t ask why, I want to draw topological height lines in post processing.
And it’s somewhat working but only in the scene view, in the game view the lines are bouncing every time I move the camera and it’s driving me nuts.
Of course I did read a couple of posts and it seems that this might have something to do with the depth texture jittering for TAA - but anti aliasing is disabled …
Here is the C# part:
using System;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
[Serializable]
[PostProcess(typeof(HeightLinesPPFXRenderer), PostProcessEvent.BeforeTransparent, "Custom/HeightLinesPPFX")]
public sealed class HeightLinesPPFX : PostProcessEffectSettings
{
[Range(0f, 1f), Tooltip("HeightLinesPPFX effect intensity.")]
public FloatParameter blend = new FloatParameter { value = 0.5f };
[Range(0f, 10f), Tooltip("HeightLinesPPFX thickness.")]
public FloatParameter thickness = new FloatParameter { value = 0.5f };
public ColorParameter color = new ColorParameter { value = Color.white };
}
public sealed class HeightLinesPPFXRenderer : PostProcessEffectRenderer<HeightLinesPPFX>
{
public override DepthTextureMode GetCameraFlags()
{
return DepthTextureMode.Depth;
}
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/HeightLinesPPFX"));
sheet.properties.SetMatrix("_ViewProjectInverse", (context.camera.projectionMatrix * context.camera.worldToCameraMatrix).inverse);
sheet.properties.SetFloat("_LineThickness", settings.thickness);
sheet.properties.SetColor("_LineColor", settings.color);
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}
}
And there goes the shader:
Shader "Hidden/Custom/HeightLinesPPFX"
{
HLSLINCLUDE
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
float4x4 UNITY_MATRIX_MVP;
float4x4 _ViewProjectInverse;
float _LineThickness;
float3 _LineColor;
struct FragInput
{
float4 vertex : SV_Position;
float2 texcoord : TEXCOORD0;
float3 cameraDir : TEXCOORD1;
};
FragInput VertMain(AttributesDefault v)
{
FragInput o;
o.vertex = mul(UNITY_MATRIX_MVP, float4(v.vertex.xyz, 1.0));
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
#if UNITY_UV_STARTS_AT_TOP
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
#endif
float4 cameraLocalDir = mul(_ViewProjectInverse, float4(o.texcoord.x * 2.0 - 1.0, o.texcoord.y * 2.0 - 1.0, 0.5, 1.0));
cameraLocalDir.xyz /= cameraLocalDir.w;
cameraLocalDir.xyz -= _WorldSpaceCameraPos;
float4 cameraForwardDir = mul(_ViewProjectInverse, float4(0.0, 0.0, 0.5, 1.0));
cameraForwardDir.xyz /= cameraForwardDir.w;
cameraForwardDir.xyz -= _WorldSpaceCameraPos;
o.cameraDir = cameraLocalDir.xyz / length(cameraForwardDir.xyz);
return o;
}
float4 FragMain(FragInput i) : SV_Target
{
float3 sceneColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).rgb;
float sceneDepth = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).r;
float depth01 = Linear01Depth(sceneDepth);
float linearDepth = LinearEyeDepth(sceneDepth);
float3 worldPosition = (i.cameraDir * linearDepth) + _WorldSpaceCameraPos;
float isLine = float(abs(fmod(worldPosition.y, 10)) < _LineThickness && sceneDepth > 0.0);
sceneColor = lerp(sceneColor, _LineColor, isLine);
//sceneColor = float3(1-depth01, 0, 0);
return float4(sceneColor, 1.0);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma vertex VertMain
#pragma fragment FragMain
ENDHLSL
}
}
}
The effect (in the scene view) looks somewhat like the one in this post:
But only appears when I move the camera.
Any help is highly appreciated.