1
+ //
2
+ // Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
3
+ //
4
+ // This code is released under an unmodified zlib license.
5
+ // For conditions of distribution and use, please see:
6
+ // https://opensource.org/licenses/Zlib
7
+ //
8
+
9
+ #version 460
10
+
11
+ layout (location= 0 ) in vec3 cellColour;
12
+ layout (location= 1 ) in flat float cellSpacing;
13
+ layout (location= 2 ) in flat float lineWidth;
14
+ layout (location= 3 ) in flat float cellMultiple;
15
+ layout (location= 4 ) in flat float fadeFactor;
16
+ layout (location= 5 ) in flat float scale;
17
+
18
+ // output variables
19
+ layout (location = 0 ) out vec4 outColour;
20
+
21
+ float getAxisGridColour(float uv, float width, float factor, float derivative)
22
+ {
23
+ /* *
24
+ * There are a few things happening here:
25
+ * 1. The fract function returns the fraction of the given value (the parts after the decimal place). This returns to us
26
+ * another normalised coordinate which we can then use to repeat patterns.
27
+ * 2. We get the fraction of the uv (remember the has been divided by the spacing we provided earlier), and offset it
28
+ * by half the screen.
29
+ * 3. We then get the absolute value of the new UV - half the screen (so that we don't get any negatives)
30
+ * 4. We divide this by the derivative to give us a smaller space (used for anti-aliasing)
31
+ * 5. We then use a step function to determine if the provided UV coordinate is within the cell borders
32
+ */
33
+ return max (step (abs (fract (uv - 0.5 ) - 0.5 ) / derivative, width), factor);
34
+ }
35
+
36
+ vec4 grid(float spacing, float width, vec2 uv, vec3 lColour)
37
+ {
38
+ float derivative = 1.0 / spacing;
39
+
40
+ vec4 colour = vec4 (getAxisGridColour(uv.y, width, getAxisGridColour(uv.x, width, 0.0 , derivative), derivative));
41
+
42
+ // Check if the uv sits within the range of the cell multiples
43
+ vec2 modCoords = vec2 (mod (uv.x, cellMultiple), mod (uv.y, cellMultiple));
44
+
45
+ // Fade the cell colours depending on whether they fall within the cell multiples range or not
46
+ vec4 mixColour = mix (vec4 (cellColour * fadeFactor, fadeFactor), vec4 (cellColour, 1.0 ), float (modCoords.x < width || modCoords.y < width));
47
+
48
+ // Change the alpha based on whether the fragment is a grid line or not
49
+ colour = mix (colour, vec4 (mixColour), colour.a);
50
+
51
+ return colour;
52
+ }
53
+
54
+ void main()
55
+ {
56
+ // Determine the spacing and width of our cells
57
+ const float spacing = cellSpacing * scale;
58
+ const float width = lineWidth / spacing;
59
+
60
+ // Determine the uv location of the fragment we're dealing with
61
+ // NOTE: in glsl, gl_FragCoord always provides the fragment location + 0.5. As such, fragment 0,0 is represented as
62
+ // (0.5,0.5)
63
+ vec2 uv = (gl_FragCoord .xy - 0.5 ) / spacing;
64
+
65
+ outColour = grid(spacing, width, uv, cellColour);
66
+ }
0 commit comments