实际上,没有其他答案对我有用,但为了表扬,我使用了调整边缘位置的代码并且工作正常。
使用这个答案的一部分并查看threejs的来源,我最终得到了以下结果
var cylinderMesh = function( pointX, pointY )
{
/* edge from X to Y */
var direction = new THREE.Vector3().subVectors( pointY, pointX );
var orientation = new THREE.Matrix4();
/* THREE.Object3D().up (=Y) default orientation for all objects */
orientation.lookAt(pointX, pointY, new THREE.Object3D().up);
/* rotation around axis X by -90 degrees
* matches the default orientation Y
* with the orientation of looking Z */
orientation.multiply(new THREE.Matrix4(1,0,0,0,
0,0,1,0,
0,-1,0,0,
0,0,0,1));
/* cylinder: radiusAtTop, radiusAtBottom,
height, radiusSegments, heightSegments */
var edgeGeometry = new THREE.CylinderGeometry( 2, 2, direction.length(), 8, 1);
var edge = new THREE.Mesh( edgeGeometry,
new THREE.MeshBasicMaterial( { color: 0x0000ff } ) );
edge.applyMatrix(orientation)
edge.position = new THREE.Vector3().addVectors( pointX, direction.multiplyScalar(0.5) );
return edge;
}
一个可能的改进是添加 edgeGeometry 和 material 作为参数,这样人们就可以重用相同的对象,而不是在每次调用中创建一个新对象