-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.tres
86 lines (70 loc) · 2.77 KB
/
grid.tres
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
[gd_resource type="Shader" format=3 uid="uid://chg7w2lkioelq"]
[resource]
code = "shader_type spatial;
render_mode blend_mix,depth_draw_opaque, unshaded;
uniform vec4 gridColor: source_color;
uniform vec4 checkerColor: source_color;
uniform float fadeStart = 0.0;
uniform float fadeEnd = 10.0;
uniform float unitSize = 1.0;
uniform int subdivisions: hint_range(1, 10) = 5;
uniform float majorLineThickness = 2.0;
uniform float minorLineThickness = 1.0;
uniform float minorLineAlpha: hint_range(0.0, 1.0) = .3;
// calculate line mask, usning a bit of fwidth() magic to make line width not affected by perspective
float grid(vec2 pos, float unit, float thickness){
vec2 threshold = fwidth(pos) * thickness * .5 / unit;
vec2 posWrapped = pos / unit;
vec2 line = step(fract(-posWrapped), threshold) + step(fract(posWrapped), threshold);
return max(line.x, line.y);
}
vec3 paint_axis(vec3 col, vec2 f){
vec3 sub =
step(abs(f.y), 0.015) *
(step(0., f.x) * vec3(0.,1.,1.) +
step(f.x, 0.) * vec3(1.,0.,0.)) +
step(abs(f.x), 0.015) *
(step(0., f.y) * vec3(1.,1.,0.) +
step(f.y, 0.) * vec3(0.,0.,1.));
return col - sub;
}
// calculate checkerboard mask
float checker(vec2 pos, float unit){
float square1 = step(.5, fract(pos.x / unit *.5));
float square2 = step(.5, fract(pos.y / unit *.5));
return max(square1,square2) - square1 * square2;
}
void fragment() {
// ray from camera to fragemnt in wrold space
vec3 rayWorld = normalize(mat3(INV_VIEW_MATRIX) * VIEW);
// calculate fragment position in world space
vec3 posWorld;
float t = -CAMERA_POSITION_WORLD.y / rayWorld.y;
posWorld.y = 0.0;
posWorld.xz = CAMERA_POSITION_WORLD.xz + t * rayWorld.xz;
// calculate planar distance from camera to fragment (used for fading)
float distPlanar = distance(posWorld.xz, vec2(0.));
// grid
float line = grid(posWorld.xz, unitSize, majorLineThickness);
line += grid(posWorld.xz, unitSize / float(subdivisions), minorLineThickness) * minorLineAlpha;
line = clamp(line, 0.0, 1.0);
// checkerboard
float chec = checker(posWorld.xz, unitSize);
// distance fade factor
float fadeFactor = 1.0 - clamp((distPlanar - fadeStart) / (fadeEnd - fadeStart), 0.0, 1.0);
fadeFactor = fadeFactor * fadeFactor * fadeFactor * fadeFactor * fadeFactor;
// write ground plane depth into z buffer
vec4 pp = (PROJECTION_MATRIX * (VIEW_MATRIX * vec4(posWorld, 1.0)));
DEPTH = pp.z / pp.w;
// final alpha
float alphaGrid = line * gridColor.a;
float alphaChec = chec * checkerColor.a;
ALPHA = clamp(alphaGrid + alphaChec, 0.0, 1.0) * fadeFactor * COLOR.a;
// eliminate grid above the horizon
ALPHA *= step(t, 0.0);
vec3 color = (checkerColor.rgb * alphaChec) * (1.0 - alphaGrid) + (gridColor.rgb * alphaGrid);
color = paint_axis(color, posWorld.xz);
// final color (premultiplied alpha blend)
ALBEDO = color;
}
"