Skip to content

Commit

Permalink
Improve 3D surface-picking accuracy (1) #487
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Dec 9, 2020
1 parent f3062b7 commit f03ab39
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/viewer/scene/webgl/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,21 @@ const Renderer = function (scene, options) {

if (params.pickSurface) {

if (params.canvasPos) {

//---------------------------------------------------------
// TODO: Create pick projection matrix from camera
//---------------------------------------------------------

pickProjMatrix = scene.camera.projMatrix;

} else {

// Picking with arbitrary World-space ray

pickProjMatrix = createSurfacePickProjMatrix(origin, pickable.aabb, tempMat4a);
}

if (pickable.canPickTriangle && pickable.canPickTriangle()) {
pickTriangle(pickable, canvasX, canvasY, pickViewMatrix, pickProjMatrix, pickResult);
pickable.pickTriangleSurface(pickViewMatrix, pickProjMatrix, pickResult);
Expand All @@ -894,6 +909,41 @@ const Renderer = function (scene, options) {
};
})();

const createSurfacePickProjMatrix = (function () {

const tempVec3a = math.vec3();
const tempVec3b = math.vec3();
const tempOOBB3 = math.OBB3();

return function (origin, aabb, projMatrix) {

const obb = math.AABB3ToOBB3(aabb, tempOOBB3);
const p = tempVec3a;
const vec = tempVec3b;

let near = math.MAX_DOUBLE;
let far = math.MIN_DOUBLE;

for (let i = 0, len = obb.length; i < len; i += 4) {
p[0] = obb[i];
p[1] = obb[i + 1];
p[2] = obb[i + 2];
const dist = Math.abs(math.lenVec3(math.subVec3(origin, p, vec)));
if (dist < near) {
near = dist;
}
if (dist > far) {
far = dist;
}
}
console.log(near + ", " + far);
//math.frustumMat4(-1, 1, -1, 1, near, near+1000, projMatrix);
math.identityMat4(projMatrix);
math.frustumMat4(-1, 1, -1, 1, near, far, projMatrix);
return projMatrix;
};
})();

function pickPickable(canvasX, canvasY, pickViewMatrix, pickProjMatrix, params) {

frameCtx.reset();
Expand Down

0 comments on commit f03ab39

Please sign in to comment.