-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_web.tres
63 lines (52 loc) · 1.9 KB
/
grid_web.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
[gd_resource type="Shader" format=3 uid="uid://0krdi8i5i0v6"]
[resource]
code = "shader_type spatial;
render_mode blend_mix, unshaded, cull_disabled;
uniform vec4 gridColor: 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.025) *
(step(0., f.x) * vec3(0.,1.,1.) +
step(f.x, 0.) * vec3(1.,0.,0.)) +
step(abs(f.x), 0.025) *
(step(0., f.y) * vec3(1.,1.,0.) +
step(f.y, 0.) * vec3(0.,0.,1.));
return col - sub;
}
varying vec3 posWorld;
void vertex() {
posWorld = (vec4(VERTEX, 1.) * MODEL_MATRIX).rgb;
}
void fragment() {
// grid
float line = grid(posWorld.xz, unitSize, majorLineThickness);
line += grid(posWorld.xz, unitSize / float(subdivisions), minorLineThickness) * minorLineAlpha;
line = clamp(line, 0.0, 1.0);
// distance fade factor
float fadeFactor = 1.0 - clamp((distance(posWorld.xz, vec2(.0)) - fadeStart) / (fadeEnd - fadeStart), 0.0, 1.0);
fadeFactor = fadeFactor * fadeFactor * fadeFactor * fadeFactor * fadeFactor;
// final alpha
float alphaGrid = line * gridColor.a;
ALPHA = clamp(alphaGrid, 0.0, 1.0) * fadeFactor * COLOR.a;
// eliminate grid above the horizon
//ALPHA *= step(t, 0.0);
vec3 color = gridColor.rgb;
color = paint_axis(color, posWorld.xz);
// final color (premultiplied alpha blend)
ALBEDO = color;
}
"