简体   繁体   English

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

[英]Rotate each grid item around its own center in THREE.js

I want to create a grid of geometries (ROWS * COLUMNS) and wanna rotate each individual mesh around it's own center.我想创建一个几何图形网格 (ROWS * COLUMNS) 并想围绕它自己的中心旋转每个单独的网格。

I generate the grid through two for loops, translate the single elements to there correct position and push them into an array to use them again in the animation function. In the animation function I iterate again over all the elements and rotate every single one.我通过两个 for 循环生成网格,将单个元素转换为正确的 position 并将它们推入数组以在 animation function 中再次使用它们。在 animation function 中,我再次遍历所有元素并旋转每个元素。

the problem is that even though I address each element individually and I have translated each element to the right place, each element still revolves around the center of the page and not its own center (see the screenshot)问题是即使我单独处理每个元素并且我已经将每个元素翻译到正确的位置,每个元素仍然围绕页面的中心而不是它自己的中心旋转(见截图)

Here is my current attempt:这是我目前的尝试:

 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>

Can someone explain to me why the elements individually addressed in the animation function still do not revolve around themselves?有人可以向我解释为什么 animation function 中单独提到的元素仍然不围绕自己旋转吗?

Thanks for your help谢谢你的帮助

Because the code is moving the geometry itself so that it's center is no longer in the middle of the box.因为代码正在移动几何体本身,所以它的中心不再位于盒子的中间。

change改变

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

to

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

This will move the Object3D in the scenegraph instead of the vertices of the geometry data.这将移动场景图形中的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>

You might find this article useful您可能会发现这篇文章很有用

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

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