97 lines
2.8 KiB
GLSL
97 lines
2.8 KiB
GLSL
|
|
uniform sampler2D map;
|
|
|
|
uniform float width;
|
|
uniform float height;
|
|
uniform float nearClipping, farClipping;
|
|
|
|
uniform float pointSize;
|
|
uniform float boxSize;
|
|
uniform float cameraIndex;
|
|
|
|
uniform float numCameras;
|
|
|
|
uniform mat4 c2wm;
|
|
uniform mat4 prjm;
|
|
|
|
varying vec2 vUv;
|
|
varying vec2 vUv1pxOffset;
|
|
varying float paintfordiscard;
|
|
|
|
mat4 unity_to_opengl(mat4 U) {
|
|
return mat4(
|
|
1., 0., 0., 0.,
|
|
0., 1., 0., 0.,
|
|
0., 0.,-1., 0.,
|
|
0., 0., 0., 1.
|
|
) * U;
|
|
}
|
|
|
|
void main() {
|
|
|
|
vUv = vec2( position.x / width, position.y / height );
|
|
vUv1pxOffset = vec2( 1.0 / width, 1.0 / height ) * 2.0;
|
|
|
|
int skipctr = 0;
|
|
int skipthreshold = 1;
|
|
|
|
for (float xoff = -1.0; xoff < 2.0; xoff++) {
|
|
for (float yoff = -1.0; yoff < 2.0; yoff++) {
|
|
vec2 thisUv = vUv + (vec2(xoff, yoff) * vUv1pxOffset);
|
|
vec4 color = texture2D(map, thisUv*vec2(1.0/numCameras,(1.0/3.0))+vec2(cameraIndex/numCameras,(1.0/3.0)));
|
|
float depth = ( color.r + color.g + color.b ) / 3.0;
|
|
if (depth < 0.01) {
|
|
skipctr++;
|
|
}
|
|
}
|
|
}
|
|
|
|
paintfordiscard = float(skipctr > skipthreshold);
|
|
vec4 color = texture2D( map, vUv*vec2(1.0/numCameras,(1.0/3.0))+vec2(cameraIndex/numCameras,(1.0/3.0)));
|
|
|
|
// float depth = ( color.r + color.g + color.b ) / 3.0;
|
|
int decodeDepth = 0;
|
|
int rEncode = int(color.r*255.);
|
|
int gEncode = int(color.g*255.);
|
|
int bEncode = int(color.b*255.);
|
|
|
|
// This loop decodes depth into a 24 bit value split across all three color channels.
|
|
// The least significant digit is the last blue value.
|
|
// The most significant digit is the first red value.
|
|
for (int i = 0; i < 8; i++){
|
|
decodeDepth <<= 1;
|
|
decodeDepth |= ((rEncode >> (7-i)) & 1);
|
|
|
|
decodeDepth <<= 1;
|
|
decodeDepth |= ((gEncode >> (7-i)) & 1);
|
|
|
|
decodeDepth <<= 1;
|
|
decodeDepth |= ((bEncode >> (7-i)) & 1);
|
|
}
|
|
|
|
// decodeDepth = int(10000024);
|
|
// decodeDepth = int(1<<23);
|
|
// decodeDepth = int(color.r * 255.*255.);
|
|
float depth = float(decodeDepth) / float((1<<24) - 1);
|
|
// float depth = .5;
|
|
// depth = color.r;
|
|
// if (decodeDepth > 6) depth = .5;
|
|
|
|
|
|
vec4 pos = vec4(
|
|
2.*(position.x / float(width)) - 1.,
|
|
2.*(position.y / float(height)) - 1.,
|
|
mix(1., -1., depth),
|
|
1.0
|
|
);
|
|
|
|
// First undo projection, then undo view, then below model is undone; flip the Y and Z vectors since WebGL is right handed
|
|
vec4 pos2 = unity_to_opengl(c2wm) * inverse(prjm) * pos; // order matters here! Parentheses do not
|
|
|
|
pos2.xyz *= boxSize;
|
|
gl_PointSize = pointSize;
|
|
gl_Position = projectionMatrix * modelViewMatrix * pos2;
|
|
if (depth <.01 || (paintfordiscard != 0.0)) { // move this point out of the view box if the depth is nearly zero
|
|
gl_Position = vec4(-1., -1., -1., -1.);
|
|
}
|
|
} |