-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAspectRatioMultiGrid.fx
More file actions
476 lines (388 loc) · 14.4 KB
/
AspectRatioMultiGrid.fx
File metadata and controls
476 lines (388 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Based and inspired on AspectRatioComposition by Daodan317081 (https://github.com/Daodan317081/reshade-shaders)
// and AspectRatioSuite by luluco250 (https://github.com/luluco250/FXShaders/)
//Made by originalnicodr
namespace AspectRatioMultiGrid
{
#include "ReShade.fxh"
#ifndef CUSTOM_ASPECT_RATIO_MAX
#define CUSTOM_ASPECT_RATIO_MAX 25
#endif
#ifndef ASPECT_RATIO_LIST_VALUES
#define ASPECT_RATIO_LIST_VALUES 2, 3/2., 4/3., 5/4., 21/9., 1, 4/5., 3/4., 2/3.
#endif
#ifndef ASPECT_RATIO_LIST_UI_VALUES
#define ASPECT_RATIO_LIST_UI_VALUES " 2\0 3/2\0 4/3\0 5/4\0 21/9\0 1\0 4/5\0 3/4\0 2/3\0"
#endif
static const float ASPECT_RATIOS[] = {ASPECT_RATIO_LIST_VALUES, 0.0};
/******************************************************************************
Uniforms
******************************************************************************/
uniform bool HideOnScreenshot <
ui_label = "Hide on screenshot";
ui_tooltip = "Avoid drawing aspect ratio black bars and grids when taking a screenshot.";
> = false;
uniform int ARMode <
ui_category = "Aspect Ratio";
ui_label = "Aspect Ratio Mode";
ui_tooltip = "Select the way you want to choose the aspect ratio being used.";
ui_type = "combo";
ui_items = "Off\0List\0Custom\0";
> = 0;
uniform int ARFromList<
ui_category = "Aspect Ratio";
ui_label = "Aspect Ratio from list";
ui_tooltip = "To edit the values on this list change the preprocessor definitions \nfor 'ASPECT_RATIO_LIST_VALUES' and 'ASPECT_RATIO_LIST_UI_VALUES'. \nThe former should have the values separated by a comma \n(and if they are a division add a dot at the end to signal its a float) \nand the latter should have the values separated by a '\\0' and a space.";
ui_type = "combo";
ui_items = ASPECT_RATIO_LIST_UI_VALUES;
> = 0;
#ifdef CUSTOM_ASPECT_RATIO_FLOAT
uniform float fUIAspectRatio <
ui_category = "Aspect Ratio";
ui_label = "Custom Aspect Ratio";
ui_tooltip = "To control aspect ratio with an int2\nremove 'CUSTOM_ASPECT_RATIO_FLOAT' from preprocessor";
ui_type = "slider";
ui_min = 0.0; ui_max = 25.0;
ui_step = 0.01;
> = 1.0;
#else
uniform int2 iUIAspectRatio <
ui_category = "Aspect Ratio";
ui_label = "Custom Aspect Ratio";
ui_tooltip = "To control aspect ratio with a float\nadd 'CUSTOM_ASPECT_RATIO_FLOAT' to preprocessor.\nOptional: 'CUSTOM_ASPECT_RATIO_MAX=xyz'";
ui_type = "slider";
ui_min = 0; ui_max = CUSTOM_ASPECT_RATIO_MAX;
> = int2(16, 9);
#endif
uniform float3 ARColor <
ui_category = "Aspect Ratio";
ui_label = "Bars Color";
ui_type = "color";
> = float3(0.0, 0.0, 0.0);
uniform float AROpacity <
ui_category = "Aspect Ratio";
ui_label = "Bars Opacity";
ui_type = "slider";
ui_min = 0.0; ui_max = 1.0;
ui_step = 0.01;
> = 1.0;
uniform float3 gridColor <
ui_category = "Grid";
ui_label = "Color";
ui_type = "color";
> = float3(0.0, 0.0, 0.0);
uniform bool gridInverseColor <
ui_category = "Grid";
ui_label = "Grid Inverse Color";
ui_tooltip = "Lets you inverse the color of the pixels behind the grid to \nfully contrast the grid against the game's screen.";
> = false;
uniform float gridOpacity <
ui_category = "Grid";
ui_label = "Opacity";
ui_type = "slider";
ui_min = 0.0; ui_max = 1.0;
ui_step = 0.01;
> = 1.0;
uniform float gridLinesWidth<
ui_category = "Grid";
ui_label = "Lines width in Pixels";
ui_min = 1.0; ui_max = 5.0;
ui_type = "slider";
ui_step = 0.001;
> = 1.0;
uniform bool RuleofThirds <
ui_category = "Grid";
ui_label = "Rule of thirds";
> = false;
uniform bool RuleofFifths <
ui_category = "Grid";
ui_label = "Rule of fifths";
> = false;
uniform bool Triangles1 <
ui_category = "Grid";
ui_label = "Triangles - 1";
> = false;
uniform bool Triangles2 <
ui_category = "Grid";
ui_label = "Triangles - 2";
> = false;
uniform bool Diagonals <
ui_category = "Grid";
> = false;
uniform bool Diamond <
ui_category = "Grid";
> = false;
uniform bool Special1 <
ui_category = "Grid";
ui_label = "Crosses - 1";
> = false;
uniform bool Special2 <
ui_category = "Grid";
ui_label = "Crosses - 2";
> = false;
uniform bool Special3 <
ui_category = "Grid";
ui_label = "Crosses - 3";
> = false;
uniform bool FibonacciBottomRight <
ui_category = "Grid";
ui_label = "Golden Ratio - bottom right";
> = false;
uniform bool FibonacciBottomLeft <
ui_category = "Grid";
ui_label = "Golden Ratio - bottom left";
> = false;
uniform bool FibonacciTopRight <
ui_category = "Grid";
ui_label = "Golden Ratio - top right";
> = false;
uniform bool FibonacciTopLeft <
ui_category = "Grid";
ui_label = "Golden Ratio - top left";
> = false;
uniform bool CustomGrid <
ui_category = "Custom Grid";
ui_label = "Custom Grid";
ui_category_closed = true;
> = false;
uniform float4 CustomGridLine1 <
ui_category = "Custom Grid";
ui_label = "Line 1";
ui_type = "slider";
ui_step = 0.001;
ui_min = 0; ui_max = 1;
> = float4(0, 0, 0.667, 1);
uniform float4 CustomGridLine2 <
ui_category = "Custom Grid";
ui_label = "Line 2";
ui_type = "slider";
ui_step = 0.001;
ui_min = 0; ui_max = 1;
> = float4(1, 0, 0.333, 1);
uniform float4 CustomGridLine3 <
ui_category = "Custom Grid";
ui_label = "Line 3";
ui_type = "slider";
ui_step = 0.001;
ui_min = 0; ui_max = 1;
> = float4(1, 1, 0.333, 0);
uniform float4 CustomGridLine4 <
ui_category = "Custom Grid";
ui_label = "Line 4";
ui_type = "slider";
ui_step = 0.001;
ui_min = 0; ui_max = 1;
> = float4(0, 1, 0.666, 0);
uniform bool UseWhiteBackground <
ui_category = "Custom Grid";
ui_label = "White Background";
ui_tooltip = "If you need more than 4 lines for a custom grid you can quickly \nmake one by turning this on, taking a pic of the resulting image, \nusing that image as a custom grid, and combining it again \nwith other custom grid by changing the positions of the lines above.";
> = false;
uniform bool CustomGridImage <
ui_category = "Custom Grid";
ui_label = "Custom Grid Image";
ui_tooltip = "To change the image being used change the 'customGrid.png' file on \nthe texture folder or change the name on the texture being used in \nthe preprocessor definition 'CUSTOM_GRID_IMAGE'. \nMake sure the image used has a white background and black lines.";
> = false;
/******************************************************************************
Functions
******************************************************************************/
#define linearstep(_a, _b, _x) saturate(((_x) - (_a)) * rcp((_b) - (_a)))
//Credits to gPlatl: https://www.shadertoy.com/view/MlcGDB
float segmentsdf(float2 P, float2 A, float2 B)
{
float2 g = B - A;
float2 h = P - A;
float2 scale = rcp(float2(ddx(P.x), ddy(P.y)));
g *= abs(scale);
h *= abs(scale);
float2 df = h - g * saturate(dot(g, h) / dot(g, g));
return dot(df, df);
}
float shade_line(float sdf, float r)
{
return linearstep(r + 0.5, r - 0.5, sqrt(max(0, sdf)));
}
void triangles(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0), float2(1, 1))); //diagonal
sdf = min(sdf, segmentsdf(texcoord, float2(0, 1), float2(1/4., 1/4.))); //line1
sdf = min(sdf, segmentsdf(texcoord, float2(1, 0), float2(3/4., 3/4.))); //line2
}
void diagonals(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0), float2(1, 1))); //diagonal1
sdf = min(sdf, segmentsdf(texcoord, float2(1, 0), float2(0, 1))); //diagonal2
}
void special(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0), float2(4/9., 1))); //d1
sdf = min(sdf, segmentsdf(texcoord, float2(0, 1), float2(4/9., 0))); //d2
sdf = min(sdf, segmentsdf(texcoord, float2(1, 0), float2(5/9., 1))); //d3
sdf = min(sdf, segmentsdf(texcoord, float2(1, 1), float2(5/9., 0))); //d4
}
void special2(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0), float2(0.5, 1))); //d1
sdf = min(sdf, segmentsdf(texcoord, float2(0.5, 1), float2(1, 0))); //d2
sdf = min(sdf, segmentsdf(texcoord, float2(0, 1), float2(0.5, 0))); //d3
sdf = min(sdf, segmentsdf(texcoord, float2(0.5, 0), float2(1, 1))); //d4
}
void special3(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0), float2(1, 0.5))); //d1
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0.5), float2(1, 0))); //d2
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0.5), float2(1, 1))); //d3
sdf = min(sdf, segmentsdf(texcoord, float2(0, 1), float2(1, 0.5))); //d4
}
void diamond(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(0, 0.5), float2(0.5, 1))); //d1
sdf = min(sdf, segmentsdf(texcoord, float2(0.5, 1), float2(1, 0.5))); //d2
sdf = min(sdf, segmentsdf(texcoord, float2(1, 0.5), float2(0.5, 0))); //d3
sdf = min(sdf, segmentsdf(texcoord, float2(0.5, 0), float2(0, 0.5))); //d4
}
void ruleOfThirds(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(1/3., 0), float2(1/3., 1))); //d1
sdf = min(sdf, segmentsdf(texcoord, float2(2/3., 0), float2(2/3., 1))); //d2
sdf = min(sdf, segmentsdf(texcoord, float2(0, 1/3.), float2(1, 1/3.))); //d3
sdf = min(sdf, segmentsdf(texcoord, float2(0, 2/3.), float2(1, 2/3.))); //d4
}
void ruleOfFifths(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, float2(1/5., 0), float2(1/5., 1))); //ly1
sdf = min(sdf, segmentsdf(texcoord, float2(2/5., 0), float2(2/5., 1))); //ly2
sdf = min(sdf, segmentsdf(texcoord, float2(3/5., 0), float2(3/5., 1))); //ly3
sdf = min(sdf, segmentsdf(texcoord, float2(4/5., 0), float2(4/5., 1))); //ly4
sdf = min(sdf, segmentsdf(texcoord, float2(0, 1/5.), float2(1, 1/5.))); //lx1
sdf = min(sdf, segmentsdf(texcoord, float2(0, 2/5.), float2(1, 2/5.))); //lx2
sdf = min(sdf, segmentsdf(texcoord, float2(0, 3/5.), float2(1, 3/5.))); //lx3
sdf = min(sdf, segmentsdf(texcoord, float2(0, 4/5.), float2(1, 4/5.))); //lx4
}
void customGrid(inout float sdf, float2 texcoord){
sdf = min(sdf, segmentsdf(texcoord, CustomGridLine1.zw, CustomGridLine1.xy));//l1
sdf = min(sdf, segmentsdf(texcoord, CustomGridLine2.zw, CustomGridLine2.xy));//l2
sdf = min(sdf, segmentsdf(texcoord, CustomGridLine3.zw, CustomGridLine3.xy));//l3
sdf = min(sdf, segmentsdf(texcoord, CustomGridLine4.zw, CustomGridLine4.xy));//l4
}
#ifndef CUSTOM_GRID_IMAGE
#define CUSTOM_GRID_IMAGE "customGrid.png"//Put your image file name here or remplace the original file
#endif
texture customGridTex <source= CUSTOM_GRID_IMAGE; > { Width = 1920; Height = 1080; MipLevels = 1; Format = RGBA8; };
uniform bool SCREENSHOT < source = "screenshot"; >;
sampler customGridSampler
{
Texture = customGridTex;
AddressU = BORDER;
AddressV = BORDER;
};
void customGridImage(float sdf, float2 texcoord){
float grid = 1 - tex2D(customGridSampler, texcoord).r;
sdf = lerp(sdf, 0, grid);
}
//Kitbash of this https://www.shadertoy.com/view/wtlGD4
//with some scaling and code to remove the buggy line
void fibonacci(inout float sdf, float2 texcoord){
texcoord.x *= 1.618;
texcoord *= 0.896;
texcoord += 0.618 * float2(-0.65, -0.402);
float l = length(texcoord);
float a = atan2(texcoord.y, -texcoord.x);
float tv = log(l) / (4.0 * log(1.618));
float v = tv - a / 6.283 - 1.0;
float2 dfdx = ddx(tv);
float2 dfdy = ddy(tv);
float t = abs(frac(v) - 0.5) * rsqrt(dot(dfdx, dfdx) + dot(dfdy, dfdy));
sdf = min(sdf, t);
}
float DrawAR(float aspectRatio, float2 texcoord){
float2 vpos = texcoord*BUFFER_SCREEN_SIZE;
float borderSize;
float retVal = 0;
if(aspectRatio < BUFFER_ASPECT_RATIO)
{
borderSize = (BUFFER_WIDTH - BUFFER_HEIGHT * aspectRatio) / 2.0;
if(vpos.x < borderSize || vpos.x > (BUFFER_WIDTH - borderSize))
retVal = 1;
}
else
{
borderSize = (BUFFER_HEIGHT - BUFFER_WIDTH / aspectRatio) / 2.0;
if(vpos.y < borderSize || vpos.y > (BUFFER_HEIGHT - borderSize))
retVal = 1;
}
return retVal;
}
float2 texcoordRemapAR(float aspectRatio, float2 texcoord){
float2 vpos = texcoord*BUFFER_SCREEN_SIZE;
float borderSize;
if(aspectRatio < BUFFER_ASPECT_RATIO)
{
borderSize = (BUFFER_WIDTH - BUFFER_HEIGHT * aspectRatio) / 2.0;
float w = BUFFER_HEIGHT * aspectRatio;
float x = texcoord.x*BUFFER_WIDTH/w - borderSize/w;
return float2(x,texcoord.y);
}
else
{
borderSize = (BUFFER_HEIGHT - BUFFER_WIDTH / aspectRatio) / 2.0;
float h = BUFFER_WIDTH / aspectRatio;
float y = texcoord.y*BUFFER_HEIGHT/h - borderSize/h;
return float2(texcoord.x, y);
}
}
float getAR(){
float ar;
switch(ARMode){
case 0:{
ar = BUFFER_ASPECT_RATIO;
}break;
case 1:{
ar = ASPECT_RATIOS[ARFromList];
}break;
case 2:{
ar = (float)iUIAspectRatio.x/(float)iUIAspectRatio.y;
}break;
}
return ar;
}
#if (__RENDERER__ >= 0xb000)
#define BRANCH [branch]
#else
#define BRANCH
#endif
float3 AspectRatioMultiGrid_PS(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
float3 backbuffer = tex2D(ReShade::BackBuffer, texcoord).rgb;
if (SCREENSHOT && HideOnScreenshot) {
return backbuffer;
}
float3 color = UseWhiteBackground ? float3(1,1,1) : backbuffer;
float3 realGridColor = gridInverseColor ? smoothstep(0, 1, 1 - color) : gridColor;
float ar = getAR();
float2 remappedTexcoord = texcoordRemapAR(ar, texcoord);
float lines = 0;
float line_sdf = 1000000;
BRANCH if(RuleofThirds) ruleOfThirds(line_sdf, remappedTexcoord);
BRANCH if(RuleofFifths) ruleOfFifths(line_sdf, remappedTexcoord);
BRANCH if(Triangles1) triangles(line_sdf, remappedTexcoord);
BRANCH if(Triangles2) triangles(line_sdf, float2(1-remappedTexcoord.x,remappedTexcoord.y));
BRANCH if(Diagonals) diagonals(line_sdf, remappedTexcoord);
BRANCH if(Diamond) diamond(line_sdf, remappedTexcoord);
BRANCH if(Special1) special(line_sdf, remappedTexcoord);
BRANCH if(Special2) special2(line_sdf, remappedTexcoord);
BRANCH if(Special3) special3(line_sdf, remappedTexcoord);
BRANCH if(FibonacciTopLeft) fibonacci(line_sdf, remappedTexcoord);
BRANCH if(FibonacciTopRight) fibonacci(line_sdf, float2(1-remappedTexcoord.x,remappedTexcoord.y));
BRANCH if(FibonacciBottomLeft) fibonacci(line_sdf, float2(remappedTexcoord.x,1-remappedTexcoord.y));
BRANCH if(FibonacciBottomRight) fibonacci(line_sdf, float2(1-remappedTexcoord.x,1-remappedTexcoord.y));
BRANCH if(CustomGrid) customGrid(line_sdf, remappedTexcoord);
BRANCH if(CustomGridImage) customGridImage(line_sdf, remappedTexcoord);
lines = shade_line(line_sdf, gridLinesWidth);
color = lerp(color, realGridColor, gridOpacity * saturate(lines));
if (ARMode != 0){
float aspectRatioBars = DrawAR(ar, texcoord);
color = lerp(color, ARColor, min(AROpacity, aspectRatioBars));
}
return color;
}
technique AspectRatioMultiGrid
{
pass
{
VertexShader = PostProcessVS;
PixelShader = AspectRatioMultiGrid_PS;
}
}
}