Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question 3D Pixelization Shader with 1 pixel Outline

Discussion in 'Shaders' started by BoredVoid, May 7, 2023.

  1. BoredVoid

    BoredVoid

    Joined:
    Feb 29, 2020
    Posts:
    12
    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?
    Code (CSharp):
    1. Shader "Pixel"
    2. {
    3.         SubShader
    4.     {
    5.         Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}
    6.         LOD 100
    7.         ZWrite Off Cull Off
    8.         Pass
    9.         {
    10.             Name "PixelPass"
    11.  
    12.             HLSLPROGRAM
    13.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    14.             // The Blit.hlsl file provides the vertex shader (Vert),
    15.             // input structure (Attributes) and output strucutre (Varyings)
    16.             #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
    17.  
    18.             #pragma vertex Vert
    19.             #pragma fragment frag
    20.  
    21.             TEXTURE2D_X(_CameraOpaqueTexture);
    22.             SAMPLER(sampler_CameraOpaqueTexture);
    23.  
    24.             //depth texture
    25.             TEXTURE2D_X(_CameraDepthTexture);
    26.             SAMPLER(sampler_CameraDepthTexture);
    27.  
    28.             int _ScreenSizeX;
    29.             int _ScreenSizeY;
    30.             float4 _OutlineColor;
    31.             float _OutlineThreshold;
    32.  
    33.             half4 frag (Varyings input) : SV_Target
    34.             {
    35.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    36.                 //pixelate screen to fit the screen size with point sampling
    37.                 float2 uv = input.texcoord.xy;
    38.                 float2 pixelSize = float2(1.0f / _ScreenSizeX, 1.0f / _ScreenSizeY);
    39.                 float2 pixelCenter = floor(uv / pixelSize) * pixelSize + pixelSize * 0.5f;
    40.                 //apply outline color if the one around different depth
    41.                 float depth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy);
    42.                 //check one pixel above, below, left, right and if the depth is different, apply outline color
    43.                 float depthAbove = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy + float2(0.0f, pixelSize.y));
    44.                 float depthBelow = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy - float2(0.0f, pixelSize.y));
    45.                 float depthLeft = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy - float2(pixelSize.x, 0.0f));
    46.                 float depthRight = SAMPLE_TEXTURE2D_X(_CameraDepthTexture,sampler_CameraDepthTexture, pixelCenter.xy + float2(pixelSize.x, 0.0f));
    47.                 float depthDiff = abs(depth - depthAbove) + abs(depth - depthBelow) + abs(depth - depthLeft) + abs(depth - depthRight);
    48.                 float4 color = float4(0.0f, 0.0f, 0.0f, 1.0f);
    49.                 if (depthDiff > _OutlineThreshold)
    50.                 {
    51.                     color = _OutlineColor;
    52.                 }
    53.                 else
    54.                 {
    55.                     color = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, pixelCenter);
    56.                 }
    57.  
    58.  
    59.                 return color;
    60.  
    61.             }
    62.             ENDHLSL
    63.         }
    64.     }
    65. }
     
  2. BoredVoid

    BoredVoid

    Joined:
    Feb 29, 2020
    Posts:
    12
    Figured out i can always devide pixelSize with 2. Still not perfect though
     
  3. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    95
    You're using abs for the difference. This means that it doesnt care if the compared depths are either behind or front, ending up with two pixel wide outlines.