Skip to content

Commit 05fb79f

Browse files
mgermeriegchoqueux
authored andcommitted
feature(webgl): support pick position from depth buffer with ortho
camera
1 parent de0dba6 commit 05fb79f

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

src/Core/View.js

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as THREE from 'three';
22
import Camera from 'Renderer/Camera';
33
import MainLoop, { MAIN_LOOP_EVENTS, RENDERING_PAUSED } from 'Core/MainLoop';
4+
import Capabilities from 'Core/System/Capabilities';
45
import { COLOR_LAYERS_ORDER_CHANGED } from 'Renderer/ColorLayersOrdering';
56
import c3DEngine from 'Renderer/c3DEngine';
67
import RenderMode from 'Renderer/RenderMode';
@@ -974,25 +975,32 @@ class View extends THREE.EventDispatcher {
974975
screen.x = (mouse.x / dim.x) * 2 - 1;
975976
screen.y = -(mouse.y / dim.y) * 2 + 1;
976977

977-
// Origin
978-
ray.origin.copy(camera.position);
979-
980-
// Direction
981-
ray.direction.set(screen.x, screen.y, 0.5);
982-
// Unproject
983-
matrix.multiplyMatrices(camera.matrixWorld, matrix.copy(camera.projectionMatrix).invert());
984-
ray.direction.applyMatrix4(matrix);
985-
ray.direction.sub(ray.origin);
986-
987-
direction.set(0, 0, 1.0);
988-
direction.applyMatrix4(matrix);
989-
direction.sub(ray.origin);
990-
991-
const angle = direction.angleTo(ray.direction);
992-
const orthoZ = g.depthBufferRGBAValueToOrthoZ(buffer, camera);
993-
const length = orthoZ / Math.cos(angle);
978+
if (Capabilities.isLogDepthBufferSupported() && camera.type == 'PerspectiveCamera') {
979+
// TODO: solve this part with gl_FragCoord_Z and unproject
980+
// Origin
981+
ray.origin.copy(camera.position);
982+
983+
// Direction
984+
ray.direction.set(screen.x, screen.y, 0.5);
985+
// Unproject
986+
matrix.multiplyMatrices(camera.matrixWorld, matrix.copy(camera.projectionMatrix).invert());
987+
ray.direction.applyMatrix4(matrix);
988+
ray.direction.sub(ray.origin);
989+
990+
direction.set(0, 0, 1.0);
991+
direction.applyMatrix4(matrix);
992+
direction.sub(ray.origin);
993+
994+
const angle = direction.angleTo(ray.direction);
995+
const orthoZ = g.depthBufferRGBAValueToOrthoZ(buffer, camera);
996+
const length = orthoZ / Math.cos(angle);
997+
target.addVectors(camera.position, ray.direction.setLength(length));
998+
} else {
999+
const gl_FragCoord_Z = g.depthBufferRGBAValueToOrthoZ(buffer, camera);
9941000

995-
target.addVectors(camera.position, ray.direction.setLength(length));
1001+
target.set(screen.x, screen.y, gl_FragCoord_Z);
1002+
target.unproject(camera);
1003+
}
9961004

9971005
camera.layers.mask = prev;
9981006

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT)
22
gl_FragColor = packDepthToRGBA(gl_FragDepthEXT);
33
#else
4-
gl_FragColor = packDepthToRGBA(gl_FragCoord.z);
5-
#endif
4+
float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
5+
gl_FragColor = packDepthToRGBA(fragCoordZ);
6+
#endif

src/Renderer/Shader/TileFS.glsl

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
uniform vec3 diffuse;
1414
uniform float opacity;
1515
varying vec3 vUv; // uv_0.x/uv_1.x, uv_0.y, uv_1.y
16+
varying vec2 vHighPrecisionZW;
1617

1718
void main() {
1819
#include <logdepthbuf_fragment>

src/Renderer/Shader/TileVS.glsl

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ attribute vec3 normal;
1212

1313
uniform mat4 modelMatrix;
1414
uniform bool lightingEnabled;
15+
varying vec2 vHighPrecisionZW;
1516

1617
#if MODE == MODE_FINAL
1718
#include <fog_pars_vertex>
@@ -25,6 +26,7 @@ void main() {
2526
#include <itowns/elevation_vertex>
2627
#include <project_vertex>
2728
#include <logdepthbuf_vertex>
29+
vHighPrecisionZW = gl_Position.zw;
2830
#if MODE == MODE_FINAL
2931
#include <fog_vertex>
3032
#if NUM_CRS > 1

src/Renderer/c3DEngine.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ class c3DEngine {
247247
depthBufferRGBAValueToOrthoZ(depthBufferRGBA, camera) {
248248
depthRGBA.fromArray(depthBufferRGBA).divideScalar(255.0);
249249

250-
if (Capabilities.isLogDepthBufferSupported()) {
250+
if (Capabilities.isLogDepthBufferSupported() && camera.type == 'PerspectiveCamera') {
251251
const gl_FragDepthEXT = unpack1K(depthRGBA);
252252
const logDepthBufFC = 2.0 / (Math.log(camera.far + 1.0) / Math.LN2);
253253
// invert function : gl_FragDepthEXT = log2(vFragDepth) * logDepthBufFC * 0.5;
254254
return 2 ** (2 * gl_FragDepthEXT / logDepthBufFC);
255255
} else {
256256
let gl_FragCoord_Z = unpack1K(depthRGBA);
257257
gl_FragCoord_Z = gl_FragCoord_Z * 2.0 - 1.0;
258-
return 2.0 * camera.near * camera.far / (camera.far + camera.near - gl_FragCoord_Z * (camera.far - camera.near));
258+
return gl_FragCoord_Z;
259259
}
260260
}
261261
}

0 commit comments

Comments
 (0)