首页 > 解决方案 > 在场景轴上旋转,而不是对象

问题描述

我如何围绕世界的轴而不是对象本身旋转对象?我一直在寻找答案,但没有什么对我有用。

如果我想要旋转的漩涡,我创建了一个层次结构,使它看起来有点像波浪。不幸的是,即使我已经检查并测试了所有的角度,它仍会摇摆不定,而且我无法让它保持正常。

以下是我正在使用的功能:

function spinThePortal(portal, axis, speed, rotation) {
  if (axis === 0) {
    portal.rotation.y += speed;
  } else {
    portal.rotation.x += speed;
  }

  setTimeout(function () {spinThePortal(portal, axis, speed, rotation);}, 50);
}
function createPortals(firstDoubleCoor, secondDoubleCoor, number) {
  var direction = 1, notDirection = 0;
  if (firstDoubleCoor[0][0] === firstDoubleCoor[1][0]) {
    direction = 0; //vertical lines
    notDirection = 1;
  }
  var amount = difference(firstDoubleCoor[0][notDirection], firstDoubleCoor[1][notDirection]);
  var material = new THREE.MeshBasicMaterial({color: portalColorList[number]});
  var block = new THREE.CubeGeometry(baseSize / 20, baseSize / 20, baseSize / 20);
  function squiggle(coor) {
    var root = new THREE.Mesh(block, material);
    root.position.set(coor[0][0] * baseSize, coor[0][1] * baseSize, baseSize);
    var parent = root;
    for (var i = 0; i < amount * 43; i++) {
      object = new THREE.Mesh(block, material);
      object.position.x = baseSize / 30;
      parent.add(object);
      parent = object;
    }
    scene.add(root);
    root.traverse(function(object) {
      object.rotation.set(Math.PI / 10, Math.PI / 10, 0);
    });
    if (direction === 1) root.rotation.set(0, 0, -Math.PI / 4);
    else root.rotation.set(0, 0, Math.PI / 4);
    if (spinPortals) spinThePortal(root, direction, portalSpinSpeed, 0);
  }
  squiggle(firstDoubleCoor);
  squiggle(secondDoubleCoor);
}

我不知道问题出在哪里,但是如果您需要查看我的完整创作,请点击以下链接:单击

标签: javascriptthree.js

解决方案


推荐阅读