Three.js Object3d 圆柱体旋转以与向量对齐
我进行了广泛的搜索,但似乎无法弄清楚这个非常基本的事情。我在一两年前在 stackoverflow 和其他地方看到过其他示例,但它们无法与最新版本的 Three.js 一起使用。
这是我正在研究的一个版本:http://medschoolgunners.com/sandbox/3d/。
我试图让灰色锥体与未标记的红色向量完全对齐。 IE。我希望圆锥体的尖端与矢量完全对齐,并从原点指向该方向。
这是我现在的代码:
//FUNCTION TO CREATE A CYLINDER
function create_cylinder(radiusTop,radiusBottom, height, segmentsRadius, segmentsHeight, openEnded, color)
{
var material = new THREE.MeshLambertMaterial({
color: color, //0x0000ff
opacity: 0.2
});
var cylinder = new THREE.Mesh(new THREE.CylinderGeometry(radiusTop,radiusBottom, height, segmentsRadius, segmentsHeight, openEnded), material);
cylinder.overdraw = true;
return cylinder;
}
//ALIGN THE CYLINDER TO A GIVEN VECTOR
var alignVector=new THREE.Vector3(-50,50,50); //the vector to be aligned with
var newcylinder = create_cylinder(0.1, 10, 40, 50, 50, false, "0x0ff0f0"); // the object to be aligned with the vector above
var cylinderQuaternion = new THREE.Quaternion();
cylinderQuaternion.setFromEuler(alignVector);
newcylinder.useQuaternion = true;
newcylinder.quaternion=cylinderQuaternion;
scatterPlot.add(newcylinder);
I have searched far and wide, but can't seem to figure this pretty basic thing out. I have seen other examples on stackoverflow and elsewhere from a year or two ago, but they fail to work with the latest version of Three.js.
Here is a version of what i'm working on: http://medschoolgunners.com/sandbox/3d/.
I'm trying to get the grey cone to exactly align with the unlabeled red vector. Ie. I want the tip of the cone to be exactly aligned with the vector and point out from the origin in that direction.
Here is the code I have right now:
//FUNCTION TO CREATE A CYLINDER
function create_cylinder(radiusTop,radiusBottom, height, segmentsRadius, segmentsHeight, openEnded, color)
{
var material = new THREE.MeshLambertMaterial({
color: color, //0x0000ff
opacity: 0.2
});
var cylinder = new THREE.Mesh(new THREE.CylinderGeometry(radiusTop,radiusBottom, height, segmentsRadius, segmentsHeight, openEnded), material);
cylinder.overdraw = true;
return cylinder;
}
//ALIGN THE CYLINDER TO A GIVEN VECTOR
var alignVector=new THREE.Vector3(-50,50,50); //the vector to be aligned with
var newcylinder = create_cylinder(0.1, 10, 40, 50, 50, false, "0x0ff0f0"); // the object to be aligned with the vector above
var cylinderQuaternion = new THREE.Quaternion();
cylinderQuaternion.setFromEuler(alignVector);
newcylinder.useQuaternion = true;
newcylinder.quaternion=cylinderQuaternion;
scatterPlot.add(newcylinder);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有任意向量:
您可以将一个对象(例如圆柱体)与该向量对齐,如下所示:
其中
axis
是圆柱体的原始方向(指向上方)。您还可以移动圆柱体以匹配矢量的位置,如下所示:
这会将圆柱体的一端置于
0, 0, 0
处,另一端置于100, 60, 20< /code>,并且可以工作,因为我将圆柱体长度设置为
vector.length()
。If you have an arbitrary vector:
You can align an object, such as a cylinder, to the vector like this:
Where
axis
is the original direction of the cylinder (pointing up).You can also move the cylinder to match the position of the vector like this:
This puts one end of the cylinder at the
0, 0, 0
and the other at100, 60, 20
, and works because I set the cylinder length tovector.length()
.我知道这是一个老问题,但如果有人仍然想知道,对我有用的是将向量添加到网格位置并使用 LookAt 将其与向量对齐:
i know this is an old question, but in case anyone is still wondering, what worked for me was adding the vector to the mesh position and use lookAt to align it to the vector:
不幸的是我没有使用过四元数,所以帮不上什么忙。在我看来,需要进行一些偏移,因为圆柱体的枢轴位于网格的中心,而不是一端。
如果稍微玩一下矩阵,我会得到不错的结果。
这是一种方法,使用 Mesh 的 viewAt() 方法:
这应该绘制一个从 p1 开始、到 p2 结束并朝向它的圆柱体。
偏移可能需要一些调整,但几何形状非常接近矢量方向。
还有更长的手动计算矩阵的版本,而不是依赖 LookAt() 功能:
从我在代码中看到的情况来看,这看起来比使用四元数需要更多的工作。如果 setFromEuler 发挥了定向作用,网格体的几何形状仍然可能需要移动(从一端而不是中心旋转)
HTH
Unfortunately I haven't worked with Quaternions, so can't help much. It seems to my that some offsetting is needed, since the cylinder's pivot is at the centre of the mesh, not at one end.
If played with matrices a bit, and I've got decent results.
Here's one way to this, using Mesh's lookAt() method:
This should draw a cylinder that starts at p1, ends at p2 and is oriented towards it.
Offsetting might need some tweaking, but the geometry follows the vector direction pretty close.
There's also the longer version of manually computing the matrices, as opposed to relying on the lookAt() functionality:
This looks like me more work than using quaternion, from what I see in you're code. If the setFromEuler does the magic for orientation, the mesh's geometry still might need to move (pivot from one end rather than centre)
HTH