|
| 1 | +#include "register_locations_pixel_shader.h" |
| 2 | +Texture2D InputTexture : register(t0); |
| 3 | +SamplerState AnisotropicSampler : register(s0); |
| 4 | + |
| 5 | +cbuffer PerFrame : register(CUSTOM_PER_FRAME_PS_HLSL) |
| 6 | +{ |
| 7 | + float timeMs; |
| 8 | + float deltaTimeMs; |
| 9 | + float2 resolution; |
| 10 | + float2 mouse; |
| 11 | +}; |
| 12 | + |
| 13 | +struct DamageVSOutput |
| 14 | +{ |
| 15 | + float4 pos : SV_POSITION; |
| 16 | + float2 tex : TEXCOORD; |
| 17 | +}; |
| 18 | + |
| 19 | +float4 closest(float r,float g, float b) { |
| 20 | + float Palette[8][3]= |
| 21 | + { |
| 22 | + { 0.0f, 0.0f, 0.0f }, |
| 23 | + { 0.0f, 0.0f, 1.0f }, |
| 24 | + { 0.0f, 1.0f, 0.0f }, |
| 25 | + { 0.0f, 1.0f, 1.0f }, |
| 26 | + { 1.0f, 0.0f, 0.0f }, |
| 27 | + { 1.0f, 0.0f, 1.0f }, |
| 28 | + { 1.0f, 1.0f, 0.0f }, |
| 29 | + { 1.0f, 1.0f, 1.0f } |
| 30 | + }; |
| 31 | + float m = 999999999.0f; |
| 32 | + float3 closest = { 1.0f, 1.0f, 1.0f }; |
| 33 | + float3 curr = float3( r, g, b ); |
| 34 | + for (int i = 0; i < 8; i++) { |
| 35 | + float3 tr = float3( Palette[i] ); |
| 36 | + float3 error = tr - curr; |
| 37 | + float err = dot( error, error ); |
| 38 | + if ( err < m ) { |
| 39 | + m = err; |
| 40 | + closest = tr; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return float4( closest, 1.0f ); |
| 45 | +}; |
| 46 | + |
| 47 | +float dithering(in float2 coord, inout float v) |
| 48 | +{ |
| 49 | + int ordered_matrix[8][8] = { |
| 50 | + { 0, 32, 8, 40, 2, 34, 10, 42 }, |
| 51 | + { 48, 16, 56, 24, 50, 18, 58, 26 }, |
| 52 | + { 12, 44, 4, 36, 14, 46, 6, 38 }, |
| 53 | + { 60, 28, 52, 20, 62, 30, 54, 22 }, |
| 54 | + { 3, 35, 11, 43, 1, 33, 9, 41 }, |
| 55 | + { 51, 19, 59, 27, 49, 17, 57, 25 }, |
| 56 | + { 15, 47, 7, 39, 13, 45, 5, 37 }, |
| 57 | + { 63, 31, 55, 23, 61, 29, 53, 21 } |
| 58 | + }; |
| 59 | + float offset = (float(ordered_matrix[(int)(coord.x) & 7][(int)( coord.y ) & 7 ]) + 1 ) / 64.0f - 0.5; |
| 60 | + v = v + offset * 0.4; |
| 61 | + |
| 62 | + return v; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +float4 main(DamageVSOutput input) : SV_TARGET |
| 67 | +{ |
| 68 | + float4 rgbl = InputTexture.Sample(AnisotropicSampler, input.tex); |
| 69 | + float2 uv=float2( input.tex * resolution / 3.0f ); |
| 70 | + float r=dithering( uv, rgbl.r ); |
| 71 | + float b=dithering( uv, rgbl.b ); |
| 72 | + float g=dithering( uv, rgbl.g ); |
| 73 | + rgbl = closest( r, g, b ); |
| 74 | + |
| 75 | + return rgbl; |
| 76 | +} |
0 commit comments