-
Notifications
You must be signed in to change notification settings - Fork 0
/
Physical.fx
399 lines (323 loc) · 11.9 KB
/
Physical.fx
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
#include "ReShade.fxh"
#include "ReShadeUI.fxh"
//////////////////////////////////////////////////
// Global Parameters
//////////////////////////////////////////////////
uniform float GlobalIntensity <
ui_type = "slider";
ui_label = "Global Effect Intensity";
ui_tooltip = "Master intensity control for all effects";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.01;
> = 1.0;
uniform float ChromaticIntensity <
ui_type = "slider";
ui_label = "Chromatic Aberration Intensity";
ui_tooltip = "Intensity control for chromatic aberration";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.01;
> = 0.5;
//////////////////////////////////////////////////
// Chroma Parameters
//////////////////////////////////////////////////
uniform float ChromaAmount <
ui_type = "slider";
ui_label = "Aberration Amount";
ui_category = "CA";
ui_tooltip = "Controls the strength of the chromatic aberration (in mm from optical axis)";
ui_min = 0.0;
ui_max = 10.0;
ui_step = 0.01;
> = 1.0;
uniform float FocalLength <
ui_type = "slider";
ui_label = "Focal Length";
ui_category = "CA";
ui_tooltip = "Lens focal length in mm";
ui_min = 10.0;
ui_max = 200.0;
ui_step = 1.0;
> = 50.0;
uniform bool UseRealGlass <
ui_label = "Use Real Glass Properties";
ui_category = "CA";
ui_tooltip = "Enable to use real glass dispersion properties (BK7/Flint combination)";
> = true;
//////////////////////////////////////////////////
// Blend Mode Parameters
//////////////////////////////////////////////////
uniform int BlendMode <
ui_type = "combo";
ui_label = "Blend Mode";
ui_tooltip = "How the effect blends with the original image";
ui_items = "Normal\0Multiply\0Screen\0Overlay\0Soft Light\0Color Dodge\0Linear Dodge (Add)\0Darken\0Lighten\0";
> = 0;
uniform float BlendStrength <
ui_type = "slider";
ui_label = "Blend Strength";
ui_tooltip = "Strength of the blend effect";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.01;
> = 1.0;
// Common functions
// These utilities provide physically-based calculations and transformations
// for implementing realistic lens effects in screen space.
//////////////////////////////////////////////////
// Coordinate Transformations
//////////////////////////////////////////////////
// Convert screen coordinates (0 to 1 range) to centered coordinates (-1 to 1 range)
// with aspect ratio correction for circular uniformity
float2 GetCenteredCoord(float2 texcoord)
{
float2 centered = (texcoord - 0.5) * 2.0;
// Correct for aspect ratio to maintain circular symmetry
centered.x *= BUFFER_WIDTH * (1.0 / BUFFER_HEIGHT);
return centered;
}
// Convert centered coordinates back to texture coordinates
// Inverse of GetCenteredCoord
float2 GetTexCoord(float2 centered)
{
// Reverse aspect ratio correction
centered.x *= BUFFER_HEIGHT * (1.0 / BUFFER_WIDTH);
return centered * 0.5 + 0.5;
}
// Get normalized radius from center (0 at center, 1 at screen corner)
float GetRadius(float2 centered)
{
return length(centered);
}
// Get angle from center in radians (-PI to PI)
float GetAngle(float2 centered)
{
return atan2(centered.y, centered.x);
}
// Convert polar coordinates back to Cartesian
float2 PolarToCartesian(float radius, float angle)
{
return float2(radius * cos(angle), radius * sin(angle));
}
//////////////////////////////////////////////////
// Color and Wavelength Handling
//////////////////////////////////////////////////
// More accurate wavelength to RGB conversion based on CIE curves
// Input wavelength should be in nanometers (380-750nm)
float3 WavelengthToRGB(float wavelength)
{
float3 rgb;
// Normalized wavelength between 0 and 1
float x = (wavelength - 380.0) / (750.0 - 380.0);
// More accurate conversion based on spectral distribution
// These curves approximate CIE color matching functions
rgb.r = smoothstep(0.0, 0.22, x) * smoothstep(1.0, 0.57, x);
rgb.g = smoothstep(0.2, 0.35, x) * smoothstep(0.75, 0.45, x);
rgb.b = smoothstep(0.0, 0.17, x) * smoothstep(0.4, 0.15, x);
// Normalize and adjust for perceived brightness
float luma = dot(rgb, float3(0.2126, 0.7152, 0.0722));
rgb = lerp(rgb, rgb/max(luma, 0.01), 0.8);
return rgb;
}
// Sellmeier dispersion equation for calculating wavelength-dependent IOR
// B and C are the Sellmeier coefficients specific to the glass type
// Common coefficients:
// BK7: B = (1.03961212, 0.231792344, 1.01046945)
// C = (0.00600069867, 0.0200179144, 103.560653)
float GetIOR(float wavelength, float3 B, float3 C)
{
float w2 = wavelength * wavelength * 1e-6; // Convert nm² to μm²
float n2 = 1.0 +
(B.x * w2) / (w2 - C.x) +
(B.y * w2) / (w2 - C.y) +
(B.z * w2) / (w2 - C.z);
return sqrt(n2);
}
//////////////////////////////////////////////////
// Advanced Sampling
//////////////////////////////////////////////////
// Cubic interpolation weight calculation
float4 GetCubicWeight(float x)
{
float x2 = x * x;
float x3 = x2 * x;
// Cubic coefficients for Catmull-Rom spline
float4 w;
w.x = -x + 2.0 * x2 - x3; // -1/6(x-1)(x-2)(x)
w.y = 2.0 - 5.0 * x2 + 3.0 * x3; // 1/2(x-2)(x+1)(x)
w.z = x + 4.0 * x2 - 3.0 * x3; // -1/2(x-1)(x+1)(x)
w.w = -x2 + x3; // 1/6(x)(x-1)(x-2)
return w / 6.0;
}
// High quality bicubic sampling
// This provides much better quality than bilinear for distortion effects
float4 SampleBicubic(sampler2D tex, float2 coord)
{
float2 texSize = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
float2 texelSize = 1.0 / texSize;
// Calculate sample positions
float2 pc = coord * texSize - 0.5;
float2 f = frac(pc);
float2 ic = floor(pc);
float4 xWeights = GetCubicWeight(f.x);
float4 yWeights = GetCubicWeight(f.y);
float4 color = 0;
// Sample 16 texels
for(int y = -1; y <= 2; y++)
{
for(int x = -1; x <= 2; x++)
{
float2 samplePos = (ic + float2(x, y)) * texelSize;
float weight = xWeights[x+1] * yWeights[y+1];
color += tex2D(tex, samplePos) * weight;
}
}
return color;
}
//////////////////////////////////////////////////
// Color Space Conversions
//////////////////////////////////////////////////
// sRGB to Linear conversion
float3 ToLinear(float3 srgb)
{
return pow(max(srgb, 0.0), 2.2);
}
// Linear to sRGB conversion
float3 ToSRGB(float3 color)
{
return pow(max(color, 0.0), 1.0/2.2);
}
//////////////////////////////////////////////////
// Constants and Definitions
//////////////////////////////////////////////////
#define PI 3.14159265359
#define WAVELENGTH_MIN 380.0 // nm
#define WAVELENGTH_MAX 750.0 // nm
// Common glass types Sellmeier coefficients
static const float3 BK7_B = float3(1.03961212, 0.231792344, 1.01046945);
static const float3 BK7_C = float3(0.00600069867, 0.0200179144, 103.560653);
// Crown glass (typical)
static const float3 CROWN_B = float3(1.12709, 0.124412, 0.827100);
static const float3 CROWN_C = float3(0.00720341, 0.0269835, 100.384);
// Flint glass (typical)
static const float3 FLINT_B = float3(1.34533359, 0.209073176, 0.937357162);
static const float3 FLINT_C = float3(0.00997743871, 0.0470450767, 111.886764);
//////////////////////////////////////////////////
// Shader Logic
//////////////////////////////////////////////////
// Blend mode functions
float3 Blend(float3 base, float3 blend, int mode, float opacity)
{
float3 result = base;
switch(mode)
{
case 0: // Normal
result = blend;
break;
case 1: // Multiply
result = base * blend;
break;
case 2: // Screen
result = 1.0 - (1.0 - base) * (1.0 - blend);
break;
case 3: // Overlay
result = lerp(
2.0 * base * blend,
1.0 - 2.0 * (1.0 - base) * (1.0 - blend),
step(0.5, base)
);
break;
case 4: // Soft Light
result = lerp(
2.0 * base * blend + base * base * (1.0 - 2.0 * blend),
sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend),
step(0.5, blend)
);
break;
case 5: // Color Dodge
result = base / (1.0 - clamp(blend, 0.0, 0.999));
break;
case 6: // Linear Dodge (Add)
result = base + blend;
break;
case 7: // Darken
result = min(base, blend);
break;
case 8: // Lighten
result = max(base, blend);
break;
}
return lerp(base, result, opacity);
}
// Calculate dispersion offset for a specific wavelength
float2 GetDispersionOffset(float wavelength, float2 centered_coord, float radius)
{
float ior;
if(UseRealGlass)
{
// Combine crown (BK7) and flint glass properties for achromatic-like behavior
float ior_crown = GetIOR(wavelength, BK7_B, BK7_C);
float ior_flint = GetIOR(wavelength, FLINT_B, FLINT_C);
// Simulate an achromatic doublet behavior
ior = (ior_crown - ior_flint) * 2.0;
}
else
{
// Simplified dispersion model
ior = 1.0 + (wavelength - WAVELENGTH_MIN) / (WAVELENGTH_MAX - WAVELENGTH_MIN) * 0.1;
}
// Calculate radial displacement based on IOR
// This simulates how different wavelengths bend differently through the lens
float displacement = (ior - 1.0) * radius * ChromaAmount * (FocalLength / 50.0);
return centered_coord * displacement;
}
float4 PhysicalChromaPS(float4 pos : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
{
float effectStrength = GlobalIntensity * ChromaticIntensity;
// Early exit if effect is disabled
if (effectStrength <= 0.0)
return tex2D(ReShade::BackBuffer, texcoord);
// Convert to centered coordinates with aspect ratio correction
float2 centered = GetCenteredCoord(texcoord);
float radius = GetRadius(centered);
float normalizedRadius = radius / sqrt(1.0 + pow(BUFFER_WIDTH / BUFFER_HEIGHT, 2));
// Get original color for mixing
float3 originalColor = tex2D(ReShade::BackBuffer, texcoord).rgb;
// Sample multiple wavelengths across the visible spectrum
const int WAVELENGTH_SAMPLES = 8;
float3 color = 0.0;
float3 wavelength_sum = 0.0001; // Avoid division by zero
[unroll]
for(int i = 0; i < WAVELENGTH_SAMPLES; i++)
{
float wavelength = lerp(WAVELENGTH_MIN, WAVELENGTH_MAX,
float(i) / (WAVELENGTH_SAMPLES - 1));
float2 offset = GetDispersionOffset(wavelength, centered, normalizedRadius);
float2 sample_coord = GetTexCoord(centered + offset);
float3 sample_color = SampleBicubic(ReShade::BackBuffer, sample_coord).rgb;
float3 wavelength_weight = WavelengthToRGB(wavelength);
// Weight the wavelength contribution by the original color
float3 channel_weight = wavelength_weight * originalColor;
color += sample_color * channel_weight;
wavelength_sum += channel_weight;
}
// Normalize while preserving color relationships
color = color / wavelength_sum;
// Blend based on both radius and color intensity
float colorIntensity = length(originalColor);
float blend = smoothstep(0.0, 1.0, normalizedRadius * normalizedRadius) * effectStrength;
// Adjust blend to preserve more of the original color
blend *= lerp(0.5, 1.0, colorIntensity);
// Final mix preserving original color characteristics
float3 finalColor = Blend(originalColor, color, BlendMode, blend * BlendStrength);
return float4(finalColor, 1.0);
}
technique PhysicalCA
{
pass ChromaticAberration
{
VertexShader = PostProcessVS;
PixelShader = PhysicalChromaPS;
}
}