forked from immersive-web/webxr-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depth-api-gpu.frag
37 lines (28 loc) · 1.24 KB
/
depth-api-gpu.frag
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
precision mediump float;
uniform sampler2D uDepthTexture;
uniform mat4 uUvTransform;
varying vec2 vTexCoord;
float DepthGetMillimeters(in sampler2D depth_texture, in vec2 depth_uv) {
// Depth is packed into the luminance and alpha components of its texture.
// The texture is a normalized format, storing millimeters.
vec2 packedDepthAndVisibility = texture2D(depth_texture, depth_uv).ra;
return dot(packedDepthAndVisibility, vec2(255.0, 256.0 * 255.0));
}
const highp float kMaxDepth = 8000.0; // In millimeters.
const float kInvalidDepthThreshold = 0.01;
vec3 TurboColormap(in float x);
// Returns a color corresponding to the depth passed in. Colors range from red
// to green to blue, where red is closest and blue is farthest.
//
// Uses Turbo color mapping:
// https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html
vec3 DepthGetColorVisualization(in float x) {
return step(kInvalidDepthThreshold, x) * TurboColormap(x);
}
void main(void) {
vec2 texCoord = (uUvTransform * vec4(vTexCoord.xy, 0, 1)).xy;
highp float normalized_depth = clamp(
DepthGetMillimeters(uDepthTexture, texCoord) / kMaxDepth, 0.0, 1.0);
gl_FragColor = vec4(DepthGetColorVisualization(normalized_depth), 0.75);
}
// Insert turbo.glsl here.