Skip to content

Commit

Permalink
feature(webgl): support pick position from depth buffer with ortho
Browse files Browse the repository at this point in the history
camera
  • Loading branch information
mgermerie authored and gchoqueux committed Jan 18, 2021
1 parent de0dba6 commit 05fb79f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
44 changes: 26 additions & 18 deletions src/Core/View.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as THREE from 'three';
import Camera from 'Renderer/Camera';
import MainLoop, { MAIN_LOOP_EVENTS, RENDERING_PAUSED } from 'Core/MainLoop';
import Capabilities from 'Core/System/Capabilities';
import { COLOR_LAYERS_ORDER_CHANGED } from 'Renderer/ColorLayersOrdering';
import c3DEngine from 'Renderer/c3DEngine';
import RenderMode from 'Renderer/RenderMode';
Expand Down Expand Up @@ -974,25 +975,32 @@ class View extends THREE.EventDispatcher {
screen.x = (mouse.x / dim.x) * 2 - 1;
screen.y = -(mouse.y / dim.y) * 2 + 1;

// Origin
ray.origin.copy(camera.position);

// Direction
ray.direction.set(screen.x, screen.y, 0.5);
// Unproject
matrix.multiplyMatrices(camera.matrixWorld, matrix.copy(camera.projectionMatrix).invert());
ray.direction.applyMatrix4(matrix);
ray.direction.sub(ray.origin);

direction.set(0, 0, 1.0);
direction.applyMatrix4(matrix);
direction.sub(ray.origin);

const angle = direction.angleTo(ray.direction);
const orthoZ = g.depthBufferRGBAValueToOrthoZ(buffer, camera);
const length = orthoZ / Math.cos(angle);
if (Capabilities.isLogDepthBufferSupported() && camera.type == 'PerspectiveCamera') {
// TODO: solve this part with gl_FragCoord_Z and unproject
// Origin
ray.origin.copy(camera.position);

// Direction
ray.direction.set(screen.x, screen.y, 0.5);
// Unproject
matrix.multiplyMatrices(camera.matrixWorld, matrix.copy(camera.projectionMatrix).invert());
ray.direction.applyMatrix4(matrix);
ray.direction.sub(ray.origin);

direction.set(0, 0, 1.0);
direction.applyMatrix4(matrix);
direction.sub(ray.origin);

const angle = direction.angleTo(ray.direction);
const orthoZ = g.depthBufferRGBAValueToOrthoZ(buffer, camera);
const length = orthoZ / Math.cos(angle);
target.addVectors(camera.position, ray.direction.setLength(length));
} else {
const gl_FragCoord_Z = g.depthBufferRGBAValueToOrthoZ(buffer, camera);

target.addVectors(camera.position, ray.direction.setLength(length));
target.set(screen.x, screen.y, gl_FragCoord_Z);
target.unproject(camera);
}

camera.layers.mask = prev;

Expand Down
5 changes: 3 additions & 2 deletions src/Renderer/Shader/Chunk/mode_depth_fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT)
gl_FragColor = packDepthToRGBA(gl_FragDepthEXT);
#else
gl_FragColor = packDepthToRGBA(gl_FragCoord.z);
#endif
float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
gl_FragColor = packDepthToRGBA(fragCoordZ);
#endif
1 change: 1 addition & 0 deletions src/Renderer/Shader/TileFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
uniform vec3 diffuse;
uniform float opacity;
varying vec3 vUv; // uv_0.x/uv_1.x, uv_0.y, uv_1.y
varying vec2 vHighPrecisionZW;

void main() {
#include <logdepthbuf_fragment>
Expand Down
2 changes: 2 additions & 0 deletions src/Renderer/Shader/TileVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ attribute vec3 normal;

uniform mat4 modelMatrix;
uniform bool lightingEnabled;
varying vec2 vHighPrecisionZW;

#if MODE == MODE_FINAL
#include <fog_pars_vertex>
Expand All @@ -25,6 +26,7 @@ void main() {
#include <itowns/elevation_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
vHighPrecisionZW = gl_Position.zw;
#if MODE == MODE_FINAL
#include <fog_vertex>
#if NUM_CRS > 1
Expand Down
4 changes: 2 additions & 2 deletions src/Renderer/c3DEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ class c3DEngine {
depthBufferRGBAValueToOrthoZ(depthBufferRGBA, camera) {
depthRGBA.fromArray(depthBufferRGBA).divideScalar(255.0);

if (Capabilities.isLogDepthBufferSupported()) {
if (Capabilities.isLogDepthBufferSupported() && camera.type == 'PerspectiveCamera') {
const gl_FragDepthEXT = unpack1K(depthRGBA);
const logDepthBufFC = 2.0 / (Math.log(camera.far + 1.0) / Math.LN2);
// invert function : gl_FragDepthEXT = log2(vFragDepth) * logDepthBufFC * 0.5;
return 2 ** (2 * gl_FragDepthEXT / logDepthBufFC);
} else {
let gl_FragCoord_Z = unpack1K(depthRGBA);
gl_FragCoord_Z = gl_FragCoord_Z * 2.0 - 1.0;
return 2.0 * camera.near * camera.far / (camera.far + camera.near - gl_FragCoord_Z * (camera.far - camera.near));
return gl_FragCoord_Z;
}
}
}
Expand Down

0 comments on commit 05fb79f

Please # to comment.