• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#version 460 core
2#extension GL_ARB_separate_shader_objects : enable
3#extension GL_ARB_shading_language_420pack : enable
4
5// specialization
6
7// includes
8#include "render/shaders/common/render_compatibility_common.h"
9
10// sets
11
12// in / out
13
14layout(location = 0) in vec2 inUv;
15
16layout(location = 0) out vec4 outColor;
17
18struct PushConstantStruct {
19    vec4 texSizeInvTexSize;
20    vec4 rt;
21};
22
23layout(push_constant, std430) uniform uPushConstant
24{
25    PushConstantStruct uPc;
26};
27
28float RandomDither(vec2 st)
29{
30    return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453);
31}
32
33float Noise(float t)
34{
35    return RandomDither(t.xx);
36}
37
38float Noise(vec2 t)
39{
40    return RandomDither(t);
41}
42
43float RegularPolygon(vec2 p, int N)
44{ // Produces a regular polygon defined by N points on the unit circle.
45    float a = atan(p.x, p.y) + .2;
46    float b = 6.28319 / float(N);
47    return smoothstep(.5, .51, cos(floor(.5 + a / b) * b - a) * length(p.xy));
48}
49
50float RenderRing(float r, float offsetFromCenter, float fade)
51{
52    float radiusVal = 0.001 - pow(r - offsetFromCenter, 1.0 / fade) + sin(r * 30.0);
53    radiusVal = max(radiusVal, 0.0); // filter negative vals.
54    return radiusVal;
55}
56
57float RenderHexagon(vec2 uv, vec2 lightPos, vec2 offsetFromCenter, float hexagonIntervalDist, float size)
58{
59    const int hexagonPoints = 6;
60    vec2 hexagonCoord = uv * size + lightPos * hexagonIntervalDist * size + offsetFromCenter;
61    float hexValue = 0.01 - RegularPolygon(hexagonCoord, hexagonPoints);
62    hexValue = max(hexValue, 0.0); // filter negative vals.
63    return hexValue;
64}
65
66vec3 Lensflare(vec2 uv, vec2 pos, float time)
67{
68    vec2 main = (pos - uv);
69    vec2 uvd = uv * (length(uv));
70
71    float ang = atan(main.x, main.y);
72    float dist = length(main);
73    dist = pow(dist, .1);
74    float n = Noise(vec2(ang * 16.0, dist * 32.0));
75
76    float f0 = min(1.0 / (length(uv - pos) * 16.0 + 1.0), 0.3);
77
78    f0 = f0 + f0 * (sin(Noise(sin(ang * 2. + pos.x) * 4.0 - cos(ang * 3. + pos.y + time)) * 16.) * .1 + dist * .1 + .8);
79
80    float f1 = max(0.01 - pow(length(uv + 1.2 * pos), 1.9), .0) * 7.0;
81
82    float f2 = max(1.0 / (1.0 + 32.0 * pow(length(uvd + 0.8 * pos), 2.0)), .0) * 00.25;
83    float f22 = max(1.0 / (1.0 + 32.0 * pow(length(uvd + 0.85 * pos), 2.0)), .0) * 00.23;
84    float f23 = max(1.0 / (1.0 + 32.0 * pow(length(uvd + 0.9 * pos), 2.0)), .0) * 00.21;
85
86    vec2 uvx = mix(uv, uvd, -0.5);
87
88    float f4 = max(0.01 - pow(length(uvx + 0.4 * pos), 2.4), .0) * 6.0;
89    float f42 = max(0.01 - pow(length(uvx + 0.45 * pos), 2.4), .0) * 5.0;
90    float f43 = max(0.01 - pow(length(uvx + 0.5 * pos), 2.4), .0) * 3.0;
91
92    uvx = mix(uv, uvd, -.4);
93
94    float f5 = max(0.01 - pow(length(uvx + 0.2 * pos), 5.5), .0) * 2.0;
95    float f52 = max(0.01 - pow(length(uvx + 0.4 * pos), 5.5), .0) * 2.0;
96    float f53 = max(0.01 - pow(length(uvx + 0.6 * pos), 5.5), .0) * 2.0;
97
98    uvx = mix(uv, uvd, -0.5);
99
100    float f6 = max(0.01 - pow(length(uvx - 0.3 * pos), 1.6), .0) * 6.0;
101    float f62 = max(0.01 - pow(length(uvx - 0.325 * pos), 1.6), .0) * 3.0;
102    float f63 = max(0.01 - pow(length(uvx - 0.35 * pos), 1.6), .0) * 5.0;
103
104    vec3 c = vec3(.0);
105
106    c.r += f2 + f4 + f5 + f6;
107    c.g += f22 + f42 + f52 + f62;
108    c.b += f23 + f43 + f53 + f63;
109    c = c * 1.3 - vec3(length(uvd) * .05);
110    c += vec3(f0) * 0.15;
111
112    float sizes[] = { 7., 9., 12., 15., 8.5 };
113
114    for (int i = 0; i < 5; ++i) {
115        // Draws hexagons in order closest to light to farthest.
116        vec2 hexagon_offset = -pos * vec2(1.5) * float(i) + pos * 1.22;
117        float hexagon_size = 11.0 - 2.0 * float(i);
118        float hexagon_fade = 0.25 * pow(float(i), 2.5);
119        float hexagon = RenderHexagon(uv, -pos, -hexagon_offset, dist, sizes[i]) * hexagon_fade;
120        c += vec3(hexagon, hexagon, hexagon);
121    }
122
123    if (true) {
124        int i = 0;
125        float size = 0.4;
126        float ring_fade = 70.0 * float(2);
127        vec2 ring_offset =
128            // vec2(0.0);//<== rings around light source.
129            -pos * vec2(2.5) * float(i); //<== ring halos in light ray.
130        float ring_size = size * 5.0 * float(i);
131        float lt_coord = length(uv - pos - ring_offset);
132        float ring_radius = lt_coord + ring_size;
133        float ring = RenderRing(ring_radius, ring_size, ring_fade);
134        c += vec3(ring, ring, ring);
135    }
136
137    return c;
138}
139
140vec3 Lensflare2(vec2 uv, vec2 pos, float time)
141{
142    vec2 main = pos - uv;
143    vec2 direction = normalize(main);
144
145    float dist = length(main);
146
147    vec2 point = vec2(0.5, 0.5);
148    float l = length(uv - point);
149
150    // return vec3(1 - clamp( l / 0.3, 0, 1));
151
152    int i = 1;
153    vec2 hexagon_offset = point - uv;
154    float hexagon_size = 11.0 - 2.0 * float(i);
155    float hexagon_fade = 0.25 * pow(float(i), 2.5);
156    float hexagon = max(0.01 - RenderHexagon(uv, pos, vec2(0, 0), 0.0, 0.3), 0);
157
158    return vec3(1 - RegularPolygon((uv - (vec2(0.5, 0.5) + direction * -0.2)) * 9, 6)) * 0.1;
159}
160
161vec3 ColorModifier(vec3 color, float factor, float factor2) // color modifier
162{
163    float w = color.x + color.y + color.z;
164    return mix(color, vec3(w) * factor, w * factor2);
165}
166
167//   uniform int uGhosts; // number of ghost samples
168//   uniform float uGhostDispersal; // dispersion factor
169//   noperspective in vec2 vTexcoord;
170
171vec4 Plane(vec2 point, vec2 normal)
172{
173    vec4 p = vec4(point.xy, 0, 1);
174    vec4 n = vec4(normal.xy, 0, 1);
175    return vec4(n.x, n.y, n.z, -dot(p, n));
176}
177
178float Diag(vec2 p0, vec2 p1)
179{
180    vec2 d = abs(p1 - p0);
181    return max(d.x, d.y);
182}
183
184float Line(vec2 p, vec2 p0, vec2 p1)
185{
186    /*
187    float stp = diag(p,p0);
188    float n = diag(p0,p1);
189    float t = (n==0.0)? 0.0 : stp/n;
190
191    vec2 pt = mix(p0, p1, t);
192    vec2 d = abs(p - round(pt));
193    return (d.x < 0.5
194         && d.y < 0.5
195         && stp <= n)
196          ? 1.0 : 0.0 ;
197    */
198
199    vec2 dir = normalize(p0 - p1);
200
201    vec2 normal = -dir.xy;
202    vec4 plane2 = Plane(p0, normal);
203    float d = dot(vec4(p, 0, 1), plane2);
204    if (d < 0)
205        return 0;
206
207    vec2 normal1 = dir.yx * vec2(1, -1);
208    vec4 plane1 = Plane(p0, normal1);
209    float d1 = abs(dot(vec4(p, 0, 1), plane1)) / 1;
210    return 1 - clamp(d1, 0, 1);
211}
212
213vec4 Grid()
214{
215    if (mod(gl_FragCoord.x, 32) > 1.0 && mod(gl_FragCoord.y, 32) > 1.0) {
216        return vec4(0, 0, 0, 0);
217    } else {
218        return vec4(0.1, 0.1, 0.1, 0.1);
219    }
220}
221
222vec4 Guidlines()
223{
224    vec2 rt_min = floor(uPc.rt.xy * 0.5);
225    vec2 rt_max = floor(uPc.rt.xy * 0.5) + 1.f;
226
227    if (gl_FragCoord.x >= rt_min.x && gl_FragCoord.x <= rt_max.x) {
228        return vec4(1.0, 0.0, 0.0, 0.0);
229    } else if (gl_FragCoord.y >= rt_min.y && gl_FragCoord.y <= rt_max.y) {
230        return vec4(1.0, 0.0, 0.0, 0.0);
231    } else {
232        return vec4(0.0, 0.0, 0.0, 0.0);
233    }
234}
235
236float Point(vec2 p, vec2 p1)
237{
238    vec2 d = (p1 - p);
239    return 1 - clamp(length(d) / 10, 0, 1);
240}
241
242vec4 DrawFlareGuid(vec2 source, vec2 end)
243{
244    vec4 guides = vec4(0, 0, 0, 0);
245    guides += vec4(Line(gl_FragCoord.xy, source, end), 0, 0, 1);
246    guides += vec4(Point(gl_FragCoord.xy, source), 0, 0, 1);
247    guides += vec4(Point(gl_FragCoord.xy, end), 0, 0, 1);
248    return guides;
249}
250
251void main(void)
252{
253    vec2 aspectRatio = vec2(uPc.rt.x / uPc.rt.y, 1);
254    float iTime = uPc.texSizeInvTexSize.x;
255
256    // float x = 0.5 + sin(iTime)*.5;
257    // float y = 0.5 + cos(iTime*.913)*.5;
258
259    vec2 rtSize = uPc.rt.xy;
260    vec2 projectSunPos = uPc.texSizeInvTexSize.yz;
261    vec3 c = Lensflare((inUv.xy - 0.5) * aspectRatio, (projectSunPos - 0.5) * aspectRatio, iTime);
262    vec3 color = vec3(1.4, 1.2, 1.0) * c;
263    color -= Noise(inUv.xy) * .015;
264    color = ColorModifier(color, .5, .1);
265
266    vec2 center = uPc.rt.xy * 0.5;
267
268    // debugging
269    vec4 guides = vec4(0, 0, 0, 0);
270    // guides += grid();
271    // guides += guidlines();
272    // guides += drawFlareGuid(projectSunPos * rtSize, center);
273
274    // guides += drawFlareGuid(center - vec2(100,50), center);
275
276    const float intensity = uPc.rt.z;
277    outColor = guides + vec4(color, 0) * intensity;
278}
279