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
|
/* Infinite grid
* Clément Foucault */
out vec4 FragColor;
uniform mat4 ProjectionMatrix;
uniform vec3 cameraPos;
uniform vec3 eye;
uniform vec4 gridSettings;
uniform vec2 viewportSize;
uniform vec4 screenvecs[3];
uniform float gridOneOverLogSubdiv;
#define gridDistance gridSettings.x
#define gridResolution gridSettings.y
#define gridScale gridSettings.z
#define gridSubdiv gridSettings.w
uniform int gridFlag;
#define AXIS_X (1 << 0)
#define AXIS_Y (1 << 1)
#define AXIS_Z (1 << 2)
#define GRID (1 << 3)
#define PLANE_XY (1 << 4)
#define PLANE_XZ (1 << 5)
#define PLANE_YZ (1 << 6)
#define GRID_LINE_SMOOTH 1.15
float grid(vec3 uv, vec3 fwidthCos, float grid_size)
{
float half_size = grid_size / 2.0;
/* triangular wave pattern, amplitude is [0, grid_size] */
vec3 grid_domain = abs(mod(uv + half_size, grid_size) - half_size);
/* modulate by the absolute rate of change of the uvs
* (make lines have the same width under perspective) */
grid_domain /= fwidthCos;
/* collapse waves and normalize */
grid_domain.x = min(grid_domain.x, min(grid_domain.y, grid_domain.z)) / grid_size;
return 1.0 - smoothstep(0.0, GRID_LINE_SMOOTH / grid_size, grid_domain.x);
}
float axis(float u, float fwidthU, float line_size)
{
float axis_domain = abs(u);
/* modulate by the absolute rate of change of the uvs
* (make line have the same width under perspective) */
axis_domain /= fwidthU;
return 1.0 - smoothstep(0.0, GRID_LINE_SMOOTH, axis_domain - line_size);
}
vec3 get_floor_pos(vec2 uv)
{
/* if perspective */
vec3 camera_vec, camera_pos;
vec3 pixel_pos = screenvecs[2].xyz + uv.x * screenvecs[0].xyz + uv.y * screenvecs[1].xyz;
if (ProjectionMatrix[3][3] == 0.0) {
camera_vec = normalize(pixel_pos - cameraPos);
camera_pos = cameraPos;
}
else {
camera_vec = normalize(eye);
camera_pos = pixel_pos;
}
vec3 plane_normal;
if ((gridFlag & PLANE_XZ) > 0) {
plane_normal = vec3(0.0, 1.0, 0.0);
}
else if ((gridFlag & PLANE_YZ) > 0) {
plane_normal = vec3(1.0, 0.0, 0.0);
}
else {
plane_normal = vec3(0.0, 0.0, 1.0);
}
float p = -dot(plane_normal, camera_pos) / dot(plane_normal, camera_vec);
vec3 plane = camera_pos + camera_vec * p;
/* fix residual imprecision */
vec3 mask = vec3(1.0) - plane_normal;
return plane * mask;
}
void main()
{
vec2 sPos = gl_FragCoord.xy / viewportSize; /* Screen [0,1] position */
vec3 wPos = get_floor_pos(sPos);
vec3 fwidthCos = fwidth(wPos);
float dist, fade;
/* if persp */
if (ProjectionMatrix[3][3] == 0.0) {
vec3 viewvec = cameraPos - wPos;
dist = length(viewvec);
viewvec /= dist;
float angle;
if ((gridFlag & PLANE_XZ) > 0)
angle = viewvec.y;
else if ((gridFlag & PLANE_YZ) > 0)
angle = viewvec.x;
else
angle = viewvec.z;
angle = 1.0 - abs(angle);
fade = 1.0 - angle * angle;
fade *= 1.0 - smoothstep(0.0, gridDistance, dist - gridDistance);
}
else {
dist = abs(gl_FragCoord.z * 2.0 - 1.0);
fade = 1.0 - smoothstep(0.0, 0.5, dist - 0.5);
dist = 1.0; /* avoid branch after */
if ((gridFlag & PLANE_XY) > 0) {
float angle = 1.0 - abs(eye.z);
fade *= 1.0 - angle * angle * angle;
dist = 1.0 + angle * 2.0;
}
}
if ((gridFlag & GRID) > 0) {
float grid_res = log(dist * gridResolution) * gridOneOverLogSubdiv;
float blend = fract(-max(grid_res, 0.0));
float lvl = floor(grid_res);
/* from smallest to biggest */
float scaleA = gridScale * pow(gridSubdiv, max(lvl - 1.0, 0.0));
float scaleB = gridScale * pow(gridSubdiv, max(lvl + 0.0, 0.0));
float scaleC = gridScale * pow(gridSubdiv, max(lvl + 1.0, 1.0));
float gridA = grid(wPos, fwidthCos, scaleA);
float gridB = grid(wPos, fwidthCos, scaleB);
float gridC = grid(wPos, fwidthCos, scaleC);
FragColor = vec4(colorGrid.rgb, gridA * blend);
FragColor = mix(FragColor, vec4(mix(colorGrid.rgb, colorGridEmphasise.rgb, blend), 1.0), gridB);
FragColor = mix(FragColor, vec4(colorGridEmphasise.rgb, 1.0), gridC);
}
else {
FragColor = vec4(colorGrid.rgb, 0.0);
}
if ((gridFlag & AXIS_X) > 0) {
float xAxis;
if ((gridFlag & PLANE_XZ) > 0) {
xAxis = axis(wPos.z, fwidthCos.z, 0.1);
}
else {
xAxis = axis(wPos.y, fwidthCos.y, 0.1);
}
FragColor = mix(FragColor, colorGridAxisX, xAxis);
}
if ((gridFlag & AXIS_Y) > 0) {
float yAxis;
if ((gridFlag & PLANE_YZ) > 0) {
yAxis = axis(wPos.z, fwidthCos.z, 0.1);
}
else {
yAxis = axis(wPos.x, fwidthCos.x, 0.1);
}
FragColor = mix(FragColor, colorGridAxisY, yAxis);
}
if ((gridFlag & AXIS_Z) > 0) {
float zAxis;
if ((gridFlag & PLANE_YZ) > 0) {
zAxis = axis(wPos.y, fwidthCos.y, 0.1);
}
else {
zAxis = axis(wPos.x, fwidthCos.x, 0.1);
}
FragColor = mix(FragColor, colorGridAxisZ, zAxis);
}
FragColor.a *= fade;
}
|