42 lines
1.5 KiB
Plaintext
42 lines
1.5 KiB
Plaintext
Shader "Hidden/Custom/Pixelate"
|
|
{
|
|
SubShader
|
|
{
|
|
Tags { "RenderPipeline" = "UniversalPipeline" }
|
|
ZTest Always Cull Off ZWrite Off
|
|
|
|
Pass
|
|
{
|
|
Name "PixelatePass"
|
|
|
|
HLSLPROGRAM
|
|
#pragma vertex Vert
|
|
#pragma fragment Frag
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
// URP 提供的前端 Blit 工具,拥有内置的 Varyings, Vert 函数和防颠倒运算
|
|
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
|
|
|
|
float _PixelateStrengthX;
|
|
float _PixelateStrengthY;
|
|
|
|
half4 Frag(Varyings input) : SV_Target
|
|
{
|
|
float2 uv = input.texcoord;
|
|
float2 resolutionRatio = saturate(float2(_PixelateStrengthX, _PixelateStrengthY));
|
|
// 使用当前渲染目标分辨率,而不是设备固定分辨率,兼容动态分辨率和渲染缩放。
|
|
float2 targetResolution = max(floor(_ScreenParams.xy * resolutionRatio), float2(1.0, 1.0));
|
|
|
|
float2 uvScaled = uv * targetResolution;
|
|
float2 uvFloor = floor(uvScaled);
|
|
float2 uvCenter = uvFloor + 0.5;
|
|
float2 uvPixelated = uvCenter / targetResolution;
|
|
|
|
// Blitter框架将自动把 SourceTexture 注入为 _BlitTexture
|
|
return SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uvPixelated);
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|