Created
July 13, 2021 17:52
-
-
Save herohiralal/f9854503540aae87fbbd563952ee9e09 to your computer and use it in GitHub Desktop.
URP pixelation post-processing FX shader.
This file contains hidden or 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 "Universal Render Pipeline/Post-Processing/Pixelated" | |
{ | |
Properties | |
{ | |
[MainTex] [HideInInspector] _MainTex ("Base Map", 2D) = "white" { } | |
_Width ("Width", Int) = 320 | |
_Height ("Height", Int) = 180 | |
_PixelRadius ("Pixel Radius", Range(0, 0.5)) = 0.25 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"RenderType"="Opaque" | |
"RenderPipeline" = "Universal" | |
} | |
Pass | |
{ | |
HLSLPROGRAM | |
#pragma vertex Vert | |
#pragma fragment Frag | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
struct Attributes | |
{ | |
float3 PositionOS : POSITION; | |
float2 UV : TEXCOORD0; | |
}; | |
struct VertexToFragment | |
{ | |
float4 PositionHCS : SV_POSITION; | |
float2 UV : TEXCOORD0; | |
}; | |
TEXTURE2D(_MainTex); | |
SAMPLER(sampler_MainTex); | |
CBUFFER_START(UnityPerMaterial) | |
float _Width; | |
float _Height; | |
float _PixelRadius; | |
CBUFFER_END | |
float2 Remap(in const float2 In, in const float2 InMinMax, in const float2 OutMinMax) | |
{ | |
return OutMinMax.x + (In - InMinMax.x) * (OutMinMax.y - OutMinMax.x) / (InMinMax.y - InMinMax.x); | |
} | |
VertexToFragment Vert(const Attributes Input) | |
{ | |
VertexToFragment Output; | |
Output.PositionHCS = TransformObjectToHClip(Input.PositionOS); | |
Output.UV = Input.UV; | |
return Output; | |
} | |
float4 Frag(const VertexToFragment Input) : SV_Target | |
{ | |
const float2 TargetResolution = floor(clamp(float2(_Width, _Height), 0, _ScreenParams.xy)); | |
const float2 ResolutionFactor = saturate(1 / TargetResolution); | |
const float2 PixelationOffset = Input.UV % ResolutionFactor; | |
const float2 PixelationOffsetNormalized = (PixelationOffset / ResolutionFactor); | |
const float2 PixelatedUV = Input.UV - PixelationOffset; | |
const float4 ScreenTexture = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, PixelatedUV); | |
const float4 ScreenTextureR = float4(ScreenTexture.r, 0, 0, 1); | |
const float4 ScreenTextureG = float4(0, ScreenTexture.g, 0, 1); | |
const float4 ScreenTextureB = float4(0, 0, ScreenTexture.b, 1); | |
const float RedMask = (PixelationOffsetNormalized.x >= 0.5 - _PixelRadius) | |
&& (PixelationOffsetNormalized.x <= 0.5) | |
&& (PixelationOffsetNormalized.y > 0.5 - _PixelRadius) | |
&& (PixelationOffsetNormalized.y < 0.5); | |
const float GreenMask = (PixelationOffsetNormalized.x > 0.5) | |
&& (PixelationOffsetNormalized.x < 0.5 + _PixelRadius) | |
&& (PixelationOffsetNormalized.y > 0.5 - _PixelRadius) | |
&& (PixelationOffsetNormalized.y < 0.5); | |
const float BlueMask = (PixelationOffsetNormalized.x > 0.5 - (_PixelRadius * 0.5)) | |
&& (PixelationOffsetNormalized.x < 0.5 + (_PixelRadius * 0.5)) | |
&& (PixelationOffsetNormalized.y >= 0.5) | |
&& (PixelationOffsetNormalized.y < 0.5 + _PixelRadius); | |
const float4 AnalogScreenTexture = (ScreenTextureR * RedMask) | |
+ (ScreenTextureG * GreenMask) | |
+ (ScreenTextureB * BlueMask); | |
return AnalogScreenTexture * 2; | |
} | |
ENDHLSL | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment