如何计算哪个顶点最接近3D点?

时间:2016-01-31 16:17:07

标签: javascript 3d three.js

enter image description here

目标:

我想知道哪个顶点/顶点最接近我点击的点。

设定:

  • 透视相机
  • 二十面体几何(basicmeshmaterial - >线框)
  • 旋转几何
  • Raycaster

我已经拥有的点击处理程序代码:

mouse = new THREE.Vector2();
mouse.x = (event.clientX / renderer.domElement.clientWidth) * 2 - 1;
mouse.y = - (event.clientY / renderer.domElement.clientHeight) * 2 + 1;

raycaster.setFromCamera(mouse, camera);

const intersects = raycaster.intersectObject(icosahedron);

// 3D point: intersects[0].point
// Object face: intersects[0].face

谢谢!

1 个答案:

答案 0 :(得分:5)

三个步骤:

  • 转换交叉面顶点的位置 当地的世界
  • 计算到3D点的距离
  • 按升序距离排序

-

vertices = [
  intersects[0].face.a,
  intersects[0].face.b,
  intersects[0].face.c
];

vertices.forEach( function(vId,i){
  vertices[i] = mesh.geometry.vertices[vId].clone();
  vertices[i].l2w = mesh.localToWorld(vertices[i].clone());
  vertices[i].id = vId;
  vertices[i].index = i;
  vertices[i].distance = vertices[i].l2w.distanceTo(intersects[0].point);
})

vertices.sort( function(a,b){
  return a.distance - b.distance;
})

https://jsfiddle.net/fwnjoc31/