简体   繁体   English

THREEJS如何围绕对象局部轴旋转

[英]THREEJS how do you rotate around an objects local axis

I am having a hard time understanding how the local axis rotation works in ThreeJS. 我很难理解在ThreeJS中局部轴旋转的工作方式。 I saw this Rotate around local axis answer and it is not working quite the way I expected. 我看到了“ 绕局部轴旋转”的答案,并且无法按我预期的方式工作。

If I create aa cylinder and rotate it 如果我创建一个圆柱体并将其旋转

const geometry = new THREE.CylinderBufferGeometry( 10, 10, 20 );
geometry.rotateX(Math.PI/2)

How do I then rotate the cylinder along its long axis? 然后如何沿着圆柱体的长轴旋转圆柱体?

I expect this to do the job: geometry.rotateY(Math.PI/5) but that appears to be rotating it around the world axis. 我希望这能完成工作: geometry.rotateY(Math.PI/5)但这似乎正在绕着世界轴旋转它。 I want to just rotate it around the local axis to make it look the same as if I had done 我想绕着局部轴旋转它,使其看起来像我做的一样

geometry.rotateY(Math.PI/5)
geometry.rotateX(Math.PI/2)

Can anyone help explain how to rotate around the objects internal axis? 任何人都可以帮助解释如何围绕对象的内轴旋转吗?

What actually happens: https://jsfiddle.net/h20hnn3n/46/ 实际发生的情况: https//jsfiddle.net/h20hnn3n/46/

What I want it to look like: https://jsfiddle.net/81j6g2ms/1/ 我希望它看起来像什么: https : //jsfiddle.net/81j6g2ms/1/

My use case requires me to rotate around the X and arbitrary amount first and then I need to rotate around the old Y axis, so it would be great if I could do it in this order 我的用例要求我先绕X和任意量旋转,然后再绕旧Y轴旋转,所以如果我能按此顺序执行,那将很棒

Edit: It appears that it matters whether you rotate the geometry vs a mesh. 编辑:看来旋转几何体还是旋转网格很重要。 Shouldn't it be equivalent to do it either way? 两种方法都等效吗?

I'm really confused why this doesn't act the same way: https://jsfiddle.net/10L8se71/2/ 我真的很困惑,为什么这样做的方式不一样: https : //jsfiddle.net/10L8se71/2/

In case anyone else runs into this I'll post a solution. 万一其他人遇到这个问题,我会发布解决方案。

Rotating a geometry works differently than rotating a mesh. 旋转几何体与旋转网格的方法不同。 So you need to add it to a mesh first. 因此,您需要先将其添加到网格中。 The following ends up with the right result: 以下是正确的结果:

const mesh = new THREE.Mesh( geometry, material ) ;
mesh.rotateX(Math.PI/2)
mesh.rotateY(Math.PI/5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM