Last active
October 20, 2025 15:36
-
-
Save IJEMIN/4689164c78e4da3410bb994e8c8ba029 to your computer and use it in GitHub Desktop.
Simple URP Dithering 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 "Custom/DitheringShader" | |
| { | |
| Properties | |
| { | |
| _BaseMap("Base Map", 2D) = "white" | |
| } | |
| SubShader | |
| { | |
| Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" } | |
| Pass | |
| { | |
| Name "ForwardLitLike" | |
| Tags { "LightMode" = "UniversalForward" } | |
| ZWrite On | |
| Cull Back | |
| HLSLPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" | |
| #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
| TEXTURE2D(_BaseMap); | |
| SAMPLER(sampler_BaseMap); | |
| TEXTURE2D(_DitherPattern); | |
| SAMPLER(sampler_DitherPattern); | |
| CBUFFER_START(UnityPerMaterial) | |
| float4 _BaseMap_ST; | |
| CBUFFER_END | |
| struct Attributes | |
| { | |
| float4 positionOS : POSITION; | |
| float2 uv : TEXCOORD0; | |
| }; | |
| struct Varyings { | |
| float4 positionCS : SV_POSITION; | |
| float2 uv : TEXCOORD0; | |
| }; | |
| // 8x8 베이어 행렬 (Bayer Matrix) | |
| // 픽셀을 그릴지 말지를 결정하는 임계값(Threshold) 패턴입니다. | |
| // 값들은 0~63 범위이며, 64로 나누어 0~1 범위로 정규화해서 사용합니다. | |
| static const float dither_matrix[64] = | |
| { | |
| 0, 32, 8, 40, 2, 34, 10, 42, | |
| 48, 16, 56, 24, 50, 18, 58, 26, | |
| 12, 44, 4, 36, 14, 46, 6, 38, | |
| 60, 28, 52, 20, 62, 30, 54, 22, | |
| 3, 35, 11, 43, 1, 33, 9, 41, | |
| 51, 19, 59, 27, 49, 17, 57, 25, | |
| 15, 47, 7, 39, 13, 45, 5, 37, | |
| 63, 31, 55, 23, 61, 29, 53, 21 | |
| }; | |
| Varyings vert(Attributes IN) | |
| { | |
| Varyings OUT; | |
| OUT.positionCS = TransformObjectToHClip(IN.positionOS); | |
| OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap); | |
| return OUT; | |
| } | |
| half4 frag(Varyings IN) : SV_Target | |
| { | |
| float4 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv); | |
| float2 screenUV = IN.positionCS.xy / IN.positionCS.w; | |
| screenUV = screenUV * 0.5 + 0.5; | |
| int2 pixelPos = int2(screenUV * _ScreenParams.xy); | |
| int x = pixelPos.x % 8; | |
| int y = pixelPos.y % 8; | |
| int index = y * 8 + x; | |
| float ditherValue = dither_matrix[index] / 64.0; | |
| if(baseColor.a - ditherValue <= 0) | |
| { | |
| discard; | |
| } | |
| return baseColor; | |
| } | |
| ENDHLSL | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment