1#version 130
2
3// license:BSD-3-Clause
4// copyright-holders:ImJezze
5//-----------------------------------------------------------------------------
6// Distortion Effect
7//-----------------------------------------------------------------------------
8
9#define saturate(c) clamp(c, 0.0, 1.0)
10#define mul(a,b) (b*a)
11const int ScreenCount = 1;
12
13#if defined(VERTEX)
14
15#if __VERSION__ >= 130
16#define COMPAT_VARYING out
17#define COMPAT_ATTRIBUTE in
18#define COMPAT_TEXTURE texture
19#else
20#define COMPAT_VARYING varying
21#define COMPAT_ATTRIBUTE attribute
22#define COMPAT_TEXTURE texture2D
23#endif
24
25#ifdef GL_ES
26#define COMPAT_PRECISION mediump
27#else
28#define COMPAT_PRECISION
29#endif
30
31COMPAT_ATTRIBUTE vec4 VertexCoord;
32COMPAT_ATTRIBUTE vec4 COLOR;
33COMPAT_ATTRIBUTE vec4 TexCoord;
34COMPAT_VARYING vec4 COL0;
35COMPAT_VARYING vec4 TEX0;
36
37vec4 _oPosition1;
38uniform mat4 MVPMatrix;
39uniform COMPAT_PRECISION int FrameDirection;
40uniform COMPAT_PRECISION int FrameCount;
41uniform COMPAT_PRECISION vec2 OutputSize;
42uniform COMPAT_PRECISION vec2 TextureSize;
43uniform COMPAT_PRECISION vec2 InputSize;
44
45// compatibility #defines
46#define vTexCoord TEX0.xy
47#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
48#define OutSize vec4(OutputSize, 1.0 / OutputSize)
49
50void main()
51{
52    gl_Position = MVPMatrix * VertexCoord;
53    TEX0.xy = TexCoord.xy;
54}
55
56#elif defined(FRAGMENT)
57
58#ifdef GL_ES
59#ifdef GL_FRAGMENT_PRECISION_HIGH
60precision highp float;
61#else
62precision mediump float;
63#endif
64#define COMPAT_PRECISION mediump
65#else
66#define COMPAT_PRECISION
67#endif
68
69#if __VERSION__ >= 130
70#define COMPAT_VARYING in
71#define COMPAT_TEXTURE texture
72out COMPAT_PRECISION vec4 FragColor;
73#else
74#define COMPAT_VARYING varying
75#define FragColor gl_FragColor
76#define COMPAT_TEXTURE texture2D
77#endif
78
79uniform COMPAT_PRECISION int FrameDirection;
80uniform COMPAT_PRECISION int FrameCount;
81uniform COMPAT_PRECISION vec2 OutputSize;
82uniform COMPAT_PRECISION vec2 TextureSize;
83uniform COMPAT_PRECISION vec2 InputSize;
84uniform sampler2D Texture;
85COMPAT_VARYING vec4 TEX0;
86
87// compatibility #defines
88#define Source Texture
89
90#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
91#define OutSize vec4(OutputSize, 1.0 / OutputSize)
92
93// effect toggles and multi
94uniform COMPAT_PRECISION float bloomtoggle, ntscsignal, scanlinetoggle, chromatoggle,
95   distortiontoggle, screenscale_x, screenscale_y, screenoffset_x, screenoffset_y, swapxy;
96// bloom params
97uniform COMPAT_PRECISION float bloomblendmode, bloomscale, bloomoverdrive_r, bloomoverdrive_g, bloomoverdrive_b,
98   level0weight, level1weight, level2weight, level3weight, level4weight, level5weight, level6weight, level7weight, level8weight;
99//	uniform COMPAT_PRECISION float vectorscreen; // unused
100// post params
101uniform COMPAT_PRECISION float mask_width, mask_height, mask_offset_x, mask_offset_y, preparebloom, shadowtilemode,
102   power_r, power_g, power_b, floor_r, floor_g, floor_b, chromamode, conversiongain_x, conversiongain_y, conversiongain_z,
103   humbaralpha, backcolor_r, backcolor_g, backcolor_b, shadowalpha, shadowcount_x, shadowcount_y, shadowuv_x, shadowuv_y;
104// ntsc params // doesn't work here, so commenting all of them.
105// uniform COMPAT_PRECISION float avalue, bvalue, ccvalue, ovalue, pvalue, scantime, notchhalfwidth, yfreqresponse, ifreqresponse, qfreqresponse, signaloffset;
106// color params
107uniform COMPAT_PRECISION float col_red, col_grn, col_blu, col_offset_x, col_offset_y, col_offset_z, col_scale_x,
108   col_scale_y, col_scale_z, col_saturation;
109// deconverge params
110uniform COMPAT_PRECISION float converge_x_r, converge_x_g, converge_x_b, converge_y_r, converge_y_g, converge_y_b,
111   radial_conv_x_r, radial_conv_x_g, radial_conv_x_b, radial_conv_y_r, radial_conv_y_g, radial_conv_y_b;
112// scanline params
113uniform COMPAT_PRECISION float scanlinealpha, scanlinescale, scanlineheight, scanlinevariation, scanlineoffset,
114   scanlinebrightscale, scanlinebrightoffset;
115// defocus params
116uniform COMPAT_PRECISION float defocus_x, defocus_y;
117// phosphor params
118uniform COMPAT_PRECISION float deltatime, phosphor_r, phosphor_g, phosphor_b, phosphortoggle;
119// chroma params
120uniform COMPAT_PRECISION float ygain_r, ygain_g, ygain_b, chromaa_x, chromaa_y, chromab_x, chromab_y, chromac_x, chromac_y;
121// distortion params
122uniform COMPAT_PRECISION float distortion_amount, cubic_distortion_amount, distort_corner_amount, round_corner_amount,
123   smooth_border_amount, vignette_amount, reflection_amount, reflection_col_r, reflection_col_g, reflection_col_b;
124// vector params //doesn't work here, so commenting all of them.
125// uniform COMPAT_PRECISION float timeratio, timescale, lengthratio, lengthscale, beamsmooth;
126// I'm not going to bother supporting implementations without runtime parameters.
127
128//-----------------------------------------------------------------------------
129// Constants
130//-----------------------------------------------------------------------------
131
132const float Epsilon = 1.0e-7;
133const float PI = 3.1415927;
134const float E = 2.7182817;
135const float Gelfond = 23.140692; // e^pi (Gelfond constant)
136const float GelfondSchneider = 2.6651442; // 2^sqrt(2) (Gelfond-Schneider constant)
137
138//-----------------------------------------------------------------------------
139// Functions
140//-----------------------------------------------------------------------------
141
142// www.stackoverflow.com/questions/5149544/can-i-generate-a-random-number-inside-a-pixel-shader/
143float random(vec2 seed)
144{
145	// irrationals for pseudo randomness
146	vec2 i = vec2(Gelfond, GelfondSchneider);
147
148	return fract(cos(dot(seed, i)) * 123456.0);
149}
150
151// www.dinodini.wordpress.com/2010/04/05/normalized-tunable-sigmoid-functions/
152float normalizedSigmoid(float n, float k)
153{
154	// valid for n and k in range of -1.0 and 1.0
155	return (n - n * k) / (k - abs(n) * 2.0 * k + 1);
156}
157
158// www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
159float roundBox(vec2 p, vec2 b, float r)
160{
161	return length(max(abs(p * InputSize / TextureSize) - b + r, 0.0)) - r;
162
163}
164
165#define DiffuseSampler Source
166
167float DistortionAmount = distortion_amount;      // k     - quartic distortion coefficient
168float CubicDistortionAmount = cubic_distortion_amount; // kcube - cubic distortion modifier
169float DistortCornerAmount = distort_corner_amount;
170float RoundCornerAmount = round_corner_amount;
171float SmoothBorderAmount = smooth_border_amount;
172float VignettingAmount = vignette_amount;
173float ReflectionAmount = reflection_amount;
174vec3 LightReflectionColor = vec3(reflection_col_r, reflection_col_g, reflection_col_b); // color temperature 5.000 Kelvin
175
176vec2 QuadDims = OutputSize.xy * InputSize / TextureSize;
177vec2 TargetDims = OutputSize.xy;
178float TargetScale = 1.0;
179
180vec2 ScreenScale = vec2(screenscale_x, screenscale_y);
181vec2 ScreenOffset = vec2(screenoffset_x, screenoffset_y);
182bool SwapXY = bool(swapxy);
183const float vectorscreen = 0.0;
184bool VectorScreen = bool(vectorscreen);
185
186bool Distortion = bool(distortiontoggle);
187
188float GetNoiseFactor(vec3 n, float random)
189{
190	// smaller n become more noisy
191	return 1.0 + random * max(0.0, 0.25 * pow(E, -8 * n.x));
192}
193
194float GetVignetteFactor(vec2 coord, float amount)
195{
196	vec2 VignetteCoord = coord;
197
198	float VignetteLength = length(VignetteCoord);
199	float VignetteBlur = (amount * 0.75) + 0.25;
200
201	// 0.5 full screen fitting circle
202	float VignetteRadius = 1.0 - (amount * 0.25);
203	float Vignette = smoothstep(VignetteRadius, VignetteRadius - VignetteBlur, VignetteLength);
204
205	return saturate(Vignette);
206}
207
208float GetSpotAddend(vec2 coord, float amount)
209{
210	vec2 SpotCoord = coord;
211
212	// upper right quadrant
213	vec2 spotOffset = vec2(-0.25, 0.25);
214
215	// normalized screen canvas ratio
216	vec2 CanvasRatio = SwapXY
217		? vec2(1.0, QuadDims.x / QuadDims.y)
218		: vec2(1.0, QuadDims.y / QuadDims.x);
219
220	SpotCoord += spotOffset;
221	SpotCoord *= CanvasRatio;
222
223	float SpotBlur = amount;
224
225	// 0.5 full screen fitting circle
226	float SpotRadius = amount * 0.75;
227	float Spot = smoothstep(SpotRadius, SpotRadius - SpotBlur, length(SpotCoord));
228
229	float SigmoidSpot = amount * normalizedSigmoid(Spot, 0.75);
230
231	// increase strength by 100%
232	SigmoidSpot = SigmoidSpot * 2.0;
233
234	return saturate(SigmoidSpot);
235}
236
237float GetBoundsFactor(vec2 coord, vec2 bounds, float radiusAmount, float smoothAmount)
238{
239coord = coord * TextureSize / InputSize;
240	// reduce smooth amount down to radius amount
241	smoothAmount = min(smoothAmount, radiusAmount);
242
243	float range = min(bounds.x, bounds.y);
244	float amountMinimum = 1.0 / range;
245	float radius = range * max(radiusAmount, amountMinimum);
246	float smooth_ = 1.0 / (range * max(smoothAmount, amountMinimum * 2.0));
247
248	// compute box
249	float box = roundBox(bounds * (coord * 2.0), bounds, radius);
250
251	// apply smooth
252	box *= smooth_;
253	box += 1.0 - pow(smooth_ * 0.5, 0.5);
254
255	float border = smoothstep(1.0, 0.0, box);
256
257	return saturate(border);
258}
259
260// www.francois-tarlier.com/blog/cubic-lens-distortion-shader/
261vec2 GetDistortedCoords(vec2 centerCoord, float amount, float amountCube)
262{
263centerCoord = centerCoord * TextureSize / InputSize;
264	// lens distortion coefficient
265	float k = amount;
266
267	// cubic distortion value
268	float kcube = amountCube;
269
270	// compute cubic distortion factor
271	float r2 = centerCoord.x * centerCoord.x + centerCoord.y * centerCoord.y;
272	float f = kcube == 0.0
273		? 1.0 + r2 * k
274		: 1.0 + r2 * (k + kcube * sqrt(r2));
275
276   	// fit screen bounds
277	f /= 1.0 + amount * 0.25 + amountCube * 0.125;
278
279	// apply cubic distortion factor
280   	centerCoord *= f;
281
282	return centerCoord * InputSize / TextureSize;
283}
284
285vec2 GetTextureCoords(vec2 coord, float distortionAmount, float cubicDistortionAmount)
286{
287coord = coord * TextureSize / InputSize;
288	// center coordinates
289	coord -= 0.5;
290
291	// distort coordinates
292	coord = GetDistortedCoords(coord, distortionAmount, cubicDistortionAmount);
293
294	// un-center coordinates
295	coord += 0.5;
296
297	return coord * InputSize / TextureSize;
298}
299
300vec2 GetQuadCoords(vec2 coord, vec2 scale, float distortionAmount, float cubicDistortionAmount)
301{
302coord = coord * TextureSize / InputSize;
303	// center coordinates
304	coord -= 0.5;
305
306	// apply scale
307	coord *= scale;
308
309	// distort coordinates
310	coord = GetDistortedCoords(coord, distortionAmount, cubicDistortionAmount);
311
312	return coord * InputSize / TextureSize;
313}
314
315
316void main()
317{
318vec2 vTexCoord = TEX0.xy;// * TextureSize / InputSize;
319   if(!Distortion)
320   {
321      FragColor = texture(Source, vTexCoord);
322      return;
323   }
324   else
325   {
326      // image distortion
327      float distortionAmount = DistortionAmount;
328      float cubicDistortionAmount = CubicDistortionAmount > 0.0
329         ? CubicDistortionAmount * 1.1  // cubic distortion need to be a little higher to compensate the quartic distortion
330         : CubicDistortionAmount * 1.2; // negativ values even more
331
332      // corner distortion at least by the amount of the image distorition
333      float distortCornerAmount = max(DistortCornerAmount, DistortionAmount + CubicDistortionAmount);
334
335      float roundCornerAmount = RoundCornerAmount * 0.5;
336      float smoothBorderAmount = SmoothBorderAmount * 0.5;
337
338      vec2 TexelDims = 1.0 / TargetDims;
339
340      // base-target dimensions (without oversampling)
341      vec2 BaseTargetDims = TargetDims / TargetScale;
342      BaseTargetDims = SwapXY
343         ? BaseTargetDims.yx
344         : BaseTargetDims.xy;
345
346      // base-target/quad difference scale
347      vec2 BaseTargetQuadScale = (ScreenCount == 1)
348         ? BaseTargetDims / QuadDims // keeps the coords inside of the quad bounds of a single screen
349         : vec2(1.0);
350
351      // Screen Texture Curvature
352      vec2 BaseCoord = GetTextureCoords(vTexCoord, distortionAmount, cubicDistortionAmount);
353
354      // Screen Quad Curvature
355      vec2 QuadCoord = GetQuadCoords(vTexCoord, BaseTargetQuadScale, distortCornerAmount, 0.0);
356   /*
357      // clip border
358      if (BaseCoord.x < 0.0 - TexelDims.x || BaseCoord.y < 0.0 - TexelDims.y ||
359         BaseCoord.x > 1.0 + TexelDims.x || BaseCoord.y > 1.0 + TexelDims.y)
360      {
361         // we don't use the clip function, because we don't clear the render target before
362         return vec4(0.0, 0.0, 0.0, 1.0);
363      }
364   */
365
366      // Color
367      vec4 BaseColor = COMPAT_TEXTURE(DiffuseSampler, BaseCoord);
368      BaseColor.a = 1.0;
369
370      // Vignetting Simulation
371      vec2 VignetteCoord = QuadCoord;
372
373      float VignetteFactor = GetVignetteFactor(VignetteCoord, VignettingAmount);
374      BaseColor.rgb *= VignetteFactor;
375
376      // Light Reflection Simulation
377      vec2 SpotCoord = QuadCoord;
378
379      vec3 SpotAddend = GetSpotAddend(SpotCoord, ReflectionAmount) * LightReflectionColor;
380      BaseColor.rgb += SpotAddend * GetNoiseFactor(SpotAddend, random(SpotCoord));
381
382      // Round Corners Simulation
383      vec2 RoundCornerCoord = QuadCoord;
384      vec2 RoundCornerBounds = (ScreenCount == 1)
385         ? QuadDims // align corners to quad bounds of a single screen
386         : BaseTargetDims; // align corners to target bounds of multiple screens
387      RoundCornerBounds = SwapXY
388         ? RoundCornerBounds.yx
389         : RoundCornerBounds.xy;
390
391      float roundCornerFactor = GetBoundsFactor(RoundCornerCoord, RoundCornerBounds, roundCornerAmount, smoothBorderAmount);
392      BaseColor.rgb *= roundCornerFactor;
393
394      FragColor = BaseColor;
395   }
396}
397#endif
398