Three.js:将圆柱体旋转到 Vector3 方向

sch*_*ger 4 javascript three.js

我已经搜索过,但没有找到任何有帮助的内容:

我有一个向量和一个 CylinderGeometry Mesh。我想实现的是,圆柱体面向矢量显示的点。作为输入,我得到了位置 (c) 和方向 (m)(如直线方程:y = mx + c):

function draw (m,c, _color) {
  //... create the geometry and mesh
  // set the position
  line.position.x = c.x;
  line.position.y = c.y;
  line.position.z = c.z;

  // i've tried something like this:
  line.lookAt(c.add(m));

  //.. and add to scene
}
Run Code Online (Sandbox Code Playgroud)

但看起来方向与我想要实现的目标完全相反。

我也尝试过翻译之类的东西:

geometry.applyMatrix( new THREE.Matrix4().makeTranslation(0, length/2, 0));

并尝试像这样手动旋转line.rotation.x = direction.angleTo(vec3(1,0,0))* 180 / Math.PI;。但他们都没有像我需要的那样工作。

Lee*_*eft 6

这对我有用:

  // Make the geometry (of "distance" length)
  var geometry = new THREE.CylinderGeometry( 0.6, 0.6, distance, 8, 1, true );
  // shift it so one end rests on the origin
  geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, distance / 2, 0 ) );
  // rotate it the right way for lookAt to work
  geometry.applyMatrix( new THREE.Matrix4().makeRotationX( THREE.Math.degToRad( 90 ) ) );
  // Make a mesh with the geometry
  var mesh = new THREE.Mesh( geometry, material );
  // Position it where we want
  mesh.position.copy( from.sceneObject.position );
  // And make it point to where we want
  mesh.lookAt( to.sceneObject.position );
Run Code Online (Sandbox Code Playgroud)