在THREE.js中围绕自己的中心旋转每个网格项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在THREE.js中围绕自己的中心旋转每个网格项目相关的知识,希望对你有一定的参考价值。

我想创建一个几何网格(ROWS * COLUMNS),并希望围绕其自身的中心旋转每个单独的网格。

我通过两个for循环生成网格,将单个元素转换到正确的位置,并将其推入数组以在动画功能中再次使用它们。在动画功能中,我再次遍历所有元素并旋转每个元素。

问题是,即使我分别处理每个元素并且将每个元素翻译到正确的位置,每个元素仍然围绕页面中心而不是其自身中心旋转(请参见屏幕截图)

这是我目前的尝试:

const main = () => {

  const ROWS = 4;
  const COLUMNS = 4;
  const ITEMS = [];

  const canvas = document.querySelector('#canvas');
  const renderer = new THREE.WebGLRenderer({canvas, antialias: true});
  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(1, window.innerWidth/window.innerHeight, 0.1, 1000);
  
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor('#e5e5e5');

  camera.position.z = 500;

  // GEOMETRY
  for (let x = 0; x < COLUMNS; x++) {
    for (let y = 0; y < ROWS; y++) {
      const geometry = new THREE.BoxGeometry(1, 1, 0.5);
      const edges = new THREE.EdgesGeometry(geometry);
      const edgesMaterial = new THREE.LineBasicMaterial({ color: 0x1940EB });
      const edgesMesh = new THREE.LineSegments(edges, edgesMaterial);
      ITEMS.push(edgesMesh);
      edges.translate(x*1.5, y*1.5, 0);
      scene.add(edgesMesh);
    }
  }

  scene.position.set(-COLUMNS/2, -ROWS/2, 0);

  const animation = () => {
    requestAnimationFrame(animation);
    ITEMS.map(item => {
      item.rotation.x += 0.01;
      item.rotation.y += 0.01;
    })
    camera.updateMatrixWorld();
    renderer.render(scene, camera);
  }

  const onWindowResize = () => {
    const w = window.innerWidth;
    const h = window.innerHeight;
    camera.aspect = w / h;
    camera.updateProjectionMatrix();
    renderer.setSize(w, h);
  }


  animation();
  onWindowResize();

  window.addEventListener('resize', onWindowResize, false);
};

window.addEventListener('load', main, false);
body {
  padding: 0;
  margin: 0;
  height: 100vh;
}

canvas {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/102/three.js"></script>

<canvas id="canvas"></canvas>

有人可以向我解释为什么动画功能中单独处理的元素仍然不围绕自身旋转吗?

感谢您的帮助

答案

因为代码正在移动几何本身,所以它的中心不再位于方框的中间。

更改

      edges.translate(x*1.5, y*1.5, 0);

to

      edgesMesh.position.set(x*1.5, y*1.5, 0);

这将在场景图形中移动Object3D,而不是几何数据的顶点。

const main = () => {

  const ROWS = 4;
  const COLUMNS = 4;
  const ITEMS = [];

  const canvas = document.querySelector('#canvas');
  const renderer = new THREE.WebGLRenderer({canvas, antialias: true});
  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(1, window.innerWidth/window.innerHeight, 0.1, 1000);
  
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor('#e5e5e5');

  camera.position.z = 500;

  // GEOMETRY
  for (let x = 0; x < COLUMNS; x++) {
    for (let y = 0; y < ROWS; y++) {
      const geometry = new THREE.BoxGeometry(1, 1, 0.5);
      const edges = new THREE.EdgesGeometry(geometry);
      const edgesMaterial = new THREE.LineBasicMaterial({ color: 0x1940EB });
      const edgesMesh = new THREE.LineSegments(edges, edgesMaterial);
      ITEMS.push(edgesMesh);
      edgesMesh.position.set(x*1.5, y*1.5, 0);
      scene.add(edgesMesh);
    }
  }

  scene.position.set(-COLUMNS/2, -ROWS/2, 0);

  const animation = () => {
    requestAnimationFrame(animation);
    ITEMS.map(item => {
      item.rotation.x += 0.01;
      item.rotation.y += 0.01;
    })
    camera.updateMatrixWorld();
    renderer.render(scene, camera);
  }

  const onWindowResize = () => {
    const w = window.innerWidth;
    const h = window.innerHeight;
    camera.aspect = w / h;
    camera.updateProjectionMatrix();
    renderer.setSize(w, h);
  }


  animation();
  onWindowResize();

  window.addEventListener('resize', onWindowResize, false);
};

window.addEventListener('load', main, false);
body {
  padding: 0;
  margin: 0;
  height: 100vh;
}

canvas {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/102/three.js"></script>

<canvas id="canvas"></canvas>

以上是关于在THREE.js中围绕自己的中心旋转每个网格项目的主要内容,如果未能解决你的问题,请参考以下文章

如何在three.js中围绕对象的中心旋转?

如何围绕组内的一个点旋转 Three.js 组?

使用鼠标围绕中心旋转 Three.js 相机

three.js 围绕 Y 轴的相机旋转似乎关闭

围绕其中心点旋转自定义几何网格

three.js围绕边缘旋转三角形