Skip to content

Commit cfc5f8f

Browse files
authored
Merge pull request #587 from sdslabs/post-process-dither-and-damage
Post process dither and damage
2 parents 0eabdfc + f846419 commit cfc5f8f

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
r=round( r * 31.0f ) / 31.0f;
21+
b=round( b * 31.0f ) / 31.0f;
22+
g=round( g * 63.0f ) / 63.0f;
23+
24+
return float4( r, g, b, 1.0f );
25+
}
26+
27+
float dithering(in float2 coord, inout float v)
28+
{
29+
int ordered_matrix[8][8] = {
30+
{ 0, 32, 8, 40, 2, 34, 10, 42 },
31+
{ 48, 16, 56, 24, 50, 18, 58, 26 },
32+
{ 12, 44, 4, 36, 14, 46, 6, 38 },
33+
{ 60, 28, 52, 20, 62, 30, 54, 22 },
34+
{ 3, 35, 11, 43, 1, 33, 9, 41 },
35+
{ 51, 19, 59, 27, 49, 17, 57, 25 },
36+
{ 15, 47, 7, 39, 13, 45, 5, 37 },
37+
{ 63, 31, 55, 23, 61, 29, 53, 21 }
38+
};
39+
float offset = (float(ordered_matrix[(int)(coord.x) & 7][(int)(coord.y) & 7] ) + 1 ) / 64.0f - 0.5;
40+
v = v + offset * 0.4;
41+
42+
return v;
43+
}
44+
45+
46+
float4 main(DamageVSOutput input) : SV_TARGET
47+
{
48+
float4 rgbl = InputTexture.Sample(AnisotropicSampler, input.tex);
49+
float2 uv=float2( input.tex * resolution / 3.0f );
50+
float r=dithering( uv, rgbl.r );
51+
float b=dithering( uv, rgbl.b );
52+
float g=dithering( uv, rgbl.g );
53+
rgbl = closest( r, g, b );
54+
55+
return rgbl;
56+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "register_locations_pixel_shader.h"
2+
Texture2D InputTexture : register(t0);
3+
SamplerState AnisotropicSampler : register(s0);
4+
5+
struct DamageVSOutput
6+
{
7+
float4 pos : SV_POSITION;
8+
float2 tex : TEXCOORD;
9+
};
10+
11+
float4 main(DamageVSOutput input) : SV_TARGET
12+
{
13+
float4 rgbl = InputTexture.Sample(AnisotropicSampler, input.tex);
14+
rgbl = ( float4( rgbl.r, rgbl.g, rgbl.b, 1.0f ) );
15+
16+
return rgbl;
17+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
21+
r = round( r * 7.0f ) / 7.0f;
22+
g = round( g * 7.0f )/7.0f;
23+
b = round( b * 3.0f ) / 3.0f;
24+
25+
return float4( r, g, b, 1.0f );
26+
};
27+
28+
float dithering(in float2 coord, inout float v)
29+
{
30+
int ordered_matrix[8][8] = {
31+
{ 0, 32, 8, 40, 2, 34, 10, 42 },
32+
{ 48, 16, 56, 24, 50, 18, 58, 26 },
33+
{ 12, 44, 4, 36, 14, 46, 6, 38 },
34+
{ 60, 28, 52, 20, 62, 30, 54, 22 },
35+
{ 3, 35, 11, 43, 1, 33, 9, 41 },
36+
{ 51, 19, 59, 27, 49, 17, 57, 25 },
37+
{ 15, 47, 7, 39, 13, 45, 5, 37 },
38+
{ 63, 31, 55, 23, 61, 29, 53, 21 }
39+
};
40+
float offset = (float(ordered_matrix[(int)(coord.x) & 7][(int)(coord.y) & 7]) + 1 ) / 64.0f - 0.5;
41+
v = v + offset * 0.4;
42+
43+
return v;
44+
}
45+
46+
float4 main(DamageVSOutput input) : SV_TARGET
47+
{
48+
float4 rgbl = InputTexture.Sample(AnisotropicSampler, input.tex);
49+
float2 uv=float2( input.tex * resolution / 3.0f );
50+
float r=dithering( uv, rgbl.r );
51+
float b=dithering( uv, rgbl.b );
52+
float g=dithering( uv, rgbl.g );
53+
rgbl = closest( r, g, b );
54+
55+
return rgbl;
56+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 main(DamageVSOutput input) : SV_TARGET
20+
{
21+
float4 rgbl = InputTexture.Sample(AnisotropicSampler, input.tex);
22+
float2 uv = ( input.tex - 0.5 );
23+
float time = timeMs * 0.006;
24+
float freq = 0.5;
25+
float intensity = max( abs(uv.x), abs(uv.y) );
26+
intensity = max( intensity, (abs(uv.x) + abs(uv.y)) * 0.66667);
27+
float sinTime = abs( sin( time ) );
28+
float div = ( sinTime + 1.0f );
29+
float3 col = float3( ( (intensity) * sinTime + rgbl.r ) / div, rgbl.g / div, rgbl.b / div);
30+
rgbl = ( float4( col, 1.0f ) );
31+
32+
return rgbl;
33+
}

0 commit comments

Comments
 (0)