-
Notifications
You must be signed in to change notification settings - Fork 2
/
extraction.glsl
157 lines (120 loc) · 5.39 KB
/
extraction.glsl
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
#[compute]
#version 450
#define DEBUG
#include "includes/scene_data.glsl"
#include "includes/scene_data_helpers.glsl"
#include "includes/df_header.glsl"
layout(constant_id = 0) const int EXTRACTION_TYPE = 0;
layout(constant_id = 1) const int DEPTH_FADE_MODE = 0;
#ifdef DEBUG
layout(rgba16f, set = 1, binding = 1) uniform restrict writeonly image2D debug_image;
#endif // DEBUG
layout(rg16f, set = 0, binding = 1) uniform restrict readonly image2D color_image;
layout(rg16ui, set = 2, binding = 1) uniform restrict writeonly uimage2D out_image;
layout(r16f, set = 2, binding = 2) uniform restrict writeonly image2D depth_image;
const float X_KERNEL[9] = {
-1.0, 0.0, 1.0,
-2.0, 0.0, 2.0,
-1.0, 0.0, 1.0};
const float Y_KERNEL[9] = {
-1.0, -2.0, -1.0,
0.0, 0.0, 0.0,
1.0, 2.0, 1.0};
// Here, we are using coordinates in texture space,
// so the offset between adjacent coordinates is equal to the values in KERNEL_OFFSETS.
// We do not need to adjust for texel size.
vec4[9] get_image_colors(ivec2 p_image_coord) {
vec4 colors[9];
for (int i = 0; i < KERNEL_OFFSETS.length(); i++) {
ivec2 offset = KERNEL_OFFSETS[i];
ivec2 coord = p_image_coord + offset;
// If the neighbor coordinate is outside of our texture,
// we will sample the nearest edge texel's color instead.
// (Repeat modes aren't relevant for imageLoad or texel fetches.)
coord = ivec2(
clamp(coord.x, 0, int(scene.data.viewport_size.x) - 1),
clamp(coord.y, 0, int(scene.data.viewport_size.y) - 1));
colors[i] = imageLoad(color_image, coord);
}
return colors;
}
vec4[9] get_normal_roughness_colors(ivec2 p_image_coord) {
vec4 colors[9];
for (int i = 0; i < KERNEL_OFFSETS.length(); i++) {
ivec2 offset = KERNEL_OFFSETS[i];
ivec2 coord = p_image_coord + offset;
// If the neighbor coordinate is outside of our texture,
// we will sample the nearest edge texel's color instead.
// (Repeat modes aren't relevant for imageLoad or texel fetches.)
coord = ivec2(
clamp(coord.x, 0, int(scene.data.viewport_size.x) - 1),
clamp(coord.y, 0, int(scene.data.viewport_size.y) - 1));
vec4 normal_roughness_color = get_normal_roughness_color(coord);
vec3 screen_normal = normal_roughness_color.xyz * 2.0 - 1.0;
colors[i] = vec4(screen_normal, 0.0);
}
return colors;
}
vec4 get_axis_gradient(vec4[9] p_colors, float[9] p_kernel) {
vec4 gradient = vec4(0.0);
for (int i = 0; i < p_colors.length(); i++) {
gradient += p_colors[i] * p_kernel[i];
}
return gradient;
}
// Because the seed position may be on either side of the edge,
// we will get the minimum depth from neighboring texels.
highp float get_seed_depth(ivec2 p_seed_coord) {
highp float min_depth = get_linear_depth(p_seed_coord);
// Skipping corners or using only corners is not measurably faster,
// and results are visibly worse.
for (int i = 0; i < NEIGHBOR_OFFSETS.length(); i++) {
ivec2 neighbor_coord = p_seed_coord + NEIGHBOR_OFFSETS[i];
// Convert depth to linar for correct comparison.
highp float depth = get_linear_depth(neighbor_coord);
min_depth = depth < min_depth ? depth : min_depth;
}
// Convert to normalized depth value.
return get_depth_value(min_depth);
}
void main() {
ivec2 image_coord = ivec2(gl_GlobalInvocationID.xy);
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Here, we use Sobel convolutions to identify texels where the color changes quickly.
// We then use those coordinates as the seeds for our Jump Flooding Algorithm.
// You can replace this section with a custom algorithm to detect outlines.
// Depth and normal-roughness buffers are available to use.
// For a simple way to get the normal-roughness, use:
// vec4 normal_roughness_color = get_normal_roughness_color(image_coord);
// And, for the linear depth value, use:
// float depth = get_linear_depth(image_coord);
// Transform matrices are also available, along with other helpful
// variables like viewport_size, time, and camera_visible_layers.
// They come from Godot's built-in SceneData uniform buffer object (UBO).
// See scene_data.glsl for the full list.
// Access SceneData variables like this:
// highp mat4 inv_proj_matrix = scene.data.inv_projection_matrix;
// (To see how the buffers and SceneData were implemented, see scene_data_helpers.glsl
// and base_compositor_effect.gd.)
// ---------------------------------------------------------------------------
// BASIC TEST OF A NORMAL SOBEL
vec4 normal_roughness_colors[9] = get_normal_roughness_colors(image_coord);
vec4 gx = get_axis_gradient(normal_roughness_colors, X_KERNEL);
vec4 gy = get_axis_gradient(normal_roughness_colors, Y_KERNEL);
vec4 sobel_magnitude = sqrt(gx * gx + gy * gy);
float max_sobel = max(sobel_magnitude.r, max(sobel_magnitude.g, sobel_magnitude.b));
const float NORMAL_THRESHOLD = 0.5;
bool is_seed = max_sobel > NORMAL_THRESHOLD;
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
highp float seed_depth = 1.0;
if (DEPTH_FADE_MODE != DEPTH_FADE_MODE_NONE) {
seed_depth = get_seed_depth(image_coord);
imageStore(depth_image, image_coord, vec4(seed_depth));
}
imageStore(out_image, image_coord, ivec4(is_seed ? image_coord : INVALID_COORD, 0, 0));
#ifdef DEBUG
imageStore(debug_image, image_coord, get_normal_roughness_color(image_coord));
#endif
}