简体   繁体   English

three.js object3d的操作顺序

[英]three.js object3d order of operations

I am trying to construct a merry-go-round using three.js. 我正在尝试使用three.js构建旋转木马。 I loaded my horse model and want to clone it 8 times and place it in equal intervals in a circle. 我加载了我的马模型,并希望将其克隆8次并将其以相等的间隔放置在一个圆圈中。 I know I can use math, and sin and cos functions to do this, but I wanted to try something different and I can't get it working. 我知道我可以使用数学以及sin和cos函数来执行此操作,但是我想尝试一些不同的方法,但我无法使其正常运行。

Basically what I want to do is place the horse in a single place, rotate the merry-go-round object, and place the next horse in the same single place. 基本上,我想做的是将马放在单个位置,旋转旋转木马对象,然后将下一个马放在同一位置。 I was thinking that if I do this, because the merry-go-round is rotating it will place each horse in a different place in the merry-go-round. 我当时想,如果这样做,因为旋转木马正在旋转,它将把每匹马放在旋转木马的不同位置。

What ends up happening, to the best of my understanding, is that first it adds all the horses (and have them overlap) and then it does all of the rotations. 据我所知,最终发生的事情是,首先添加所有马匹(并使它们重叠),然后进行所有旋转。 Thus I only see a single horse. 因此,我只看到一匹马。 Is this really whats happening? 这真的发生了吗? What can explain this? 有什么可以解释的? How can I solve this? 我该如何解决?

Here is my code: 这是我的代码:

for(var i = 0; i < NUM_HORSES; i++){
  merryGoRound.rotateY(-2*Math.PI/NUM_HORSES);
  var horseCopy = horse.clone();
  horseCopy.translateZ(MERRY_GO_ROUND_RADIUS);
  merryGoRound.add(horseCopy);
}
scene.add(merryGoRound);
render();

horse, and thus horseCopy is a THREE.Object3D. 马,因此horseCopy是THREE.Object3D。 I tried having merryGoRound be either a THREE.Object3D or THREE.Group but they both gave the same results. 我试图让merryGoRound成为THREE.Object3D或THREE.Group,但是它们都给出了相同的结果。

Any help would be greatly appreciated 任何帮助将不胜感激

If you add an object a to another object b (either THREE.Group or any other THREE.Object3D ), the position of a is always interpreted as relative to object b's origin and transformation. 如果将对象a添加到另一个对象b( THREE.Group或任何其他THREE.Object3D ),则a的位置始终被解释为相对于对象b的原点和变换。 This is the whole point of having this hierarchy (otherwise the objects couldn't rotate with the parent when it is rotated and your merry-go-round would be pretty boring). 这就是具有这种层次结构的全部要点(否则,对象在旋转时无法与父对象一起旋转,而旋转木马会很无聊)。

What you want is to specify coordinates in world-space ("this is the exact place I want that horse to be, no matter how the merry-go-round is rotated") and re-interpret this position as relative to the merry-go-round. 您想要的是在世界空间中指定坐标(“无论旋转木马如何旋转,这都是我想要的那匹马的确切位置”),然后将该位置重新解释为相对于旋转木马而言。去圆。 You can do that in three.js using the worldToLocal -function. 您可以使用worldToLocal在three.js中实现。

So in your example: 因此,在您的示例中:

var position = new THREE.Vector3(0,0,MERRY_GO_ROUND_RADIUS);
for(var i = 0; i < NUM_HORSES; i++){
  var horseCopy = horse.clone();

  merryGoRound.rotateY(-2 * Math.PI / NUM_HORSES);
  merryGoRound.updateMatrixWorld();

  // set the position in worldspace-coordinates
  horseCopy.position.copy(position);
  // convert from worldspace to objectspace
  merryGoRound.worldToLocal(horseCopy.position);
  merryGoRound.add(horseCopy);
}
scene.add(merryGoRound);

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

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