I am trying to pixelize a 3d scene using a urp full screen shader. It does work and i even got an outline working but i would like for the outline to be 1 pixel wide, which it currently isn’t. Where is the Problem?
Shader "Pixel"
{
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}
LOD 100
ZWrite Off Cull Off
Pass
{
Name "PixelPass"
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// The Blit.hlsl file provides the vertex shader (Vert),
// input structure (Attributes) and output strucutre (Varyings)
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
#pragma vertex Vert
#pragma fragment frag
TEXTURE2D_X(_CameraOpaqueTexture);
SAMPLER(sampler_CameraOpaqueTexture);
//depth texture
TEXTURE2D_X(_CameraDepthTexture);
SAMPLER(sampler_CameraDepthTexture);
int _ScreenSizeX;
int _ScreenSizeY;
float4 _OutlineColor;
float _OutlineThreshold;
half4 frag (Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
//pixelate screen to fit the screen size with point sampling
float2 uv = input.texcoord.xy;
float2 pixelSize = float2(1.0f / _ScreenSizeX, 1.0f / _ScreenSizeY);
float2 pixelCenter = floor(uv / pixelSize) * pixelSize + pixelSize * 0.5f;
//apply outline color if the one around different depth
float depth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy);
//check one pixel above, below, left, right and if the depth is different, apply outline color
float depthAbove = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy + float2(0.0f, pixelSize.y));
float depthBelow = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy - float2(0.0f, pixelSize.y));
float depthLeft = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy - float2(pixelSize.x, 0.0f));
float depthRight = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy + float2(pixelSize.x, 0.0f));
float depthDiff = abs(depth - depthAbove) + abs(depth - depthBelow) + abs(depth - depthLeft) + abs(depth - depthRight);
float4 color = float4(0.0f, 0.0f, 0.0f, 1.0f);
if (depthDiff > _OutlineThreshold)
{
color = _OutlineColor;
}
else
{
color = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, pixelCenter);
}
return color;
}
ENDHLSL
}
}
}