坐标轴改变旋转轴三个js
Axis change the rotation axis three js
你好我实际上使用 three.js 并且我想通过 x 轴旋转我的 3d 模型但是当我尝试代码时:object.rotation.x += 0.01;
它不像我想要的那样工作。下图说明了它现在在左图上的工作方式以及我在右图上想要的内容。
PS: 公爵般的造型是我的3d模型
这里发生的事情是你的网格原点不在它的中心,正如你的鸭子图片(做得好!)清楚地说明的那样。
一种解决方案是在 Y 方向平移网格的顶点,使原点位于中间(另见 ):
geometry.translate( distX, distY, distZ );
还有一种自动重置网格原点的方法,方法是使用边界框来定义网格的中心(也就是说,您不必计算应该沿 Y 轴移动多远的顶点) ):
// Create a bounding box:
var box = new THREE.Box3().setFromObject( mesh );
// Reset mesh position:
box.center(mesh.position);
mesh.position.multiplyScalar(-1);
然后将网格添加到枢轴对象:
var pivot = new THREE.Group();
scene.add(pivot);
pivot.add(mesh);
(另见 )。您现在应该可以根据需要围绕 x 轴旋转鸭子了。
我尝试了两种方法来解决这个问题,但都没有用,这是我的代码:
安迪是我的对象
// Create a bounding box:
var box = new THREE.Box3().setFromObject( andy );
// Reset mesh position:
box.getCenter(andy.position);
andy.position.multiplyScalar(-1);
var pivot = new THREE.Group();
scene.add(pivot);
pivot.add(andy);
//var xAxis = new THREE.Vector3(0,-1,0);
//rotateAroundObjectAxis(andy, xAxis, Math.PI / 180);
markerRoot.add(andy);
/********* génère la vitesse de rotation de l'objet *************/
onRenderFcts.push(function(){ //loop function
//andy.children[0].material.opacity -= 0.01;
//andy.position.x -= 0.01;
pivot.rotation.x -= 0.01;
//andy.rotation.x += 0.01;
//andy.rotation.y += 0.01;
//andy.rotation.z += 0.01;
})
编辑:我用代码解决了这个问题:
// Create a bounding box:
var box = new THREE.Box3().setFromObject( andy );
// Reset mesh position:
box.getCenter(andy.position);
andy.position.multiplyScalar(-1);
var pivot = new THREE.Group();
scene.add(pivot);
pivot.add(andy);
//var xAxis = new THREE.Vector3(0,-1,0);
//rotateAroundObjectAxis(andy, xAxis, Math.PI / 180);
andy.children[0].geometry.center();
markerRoot.add(andy);
/********* génère la vitesse de rotation de l'objet *************/
onRenderFcts.push(function(){
//andy.children[0].material.opacity -= 0.01;
//andy.position.x -= 0.01;
andy.rotation.x += 0.01;
//andy.rotation.y += 0.01;
//andy.rotation.z += 0.01;
})
Object.children[0].geometry.center() - 这是关键行。
你好我实际上使用 three.js 并且我想通过 x 轴旋转我的 3d 模型但是当我尝试代码时:object.rotation.x += 0.01;
它不像我想要的那样工作。下图说明了它现在在左图上的工作方式以及我在右图上想要的内容。
PS: 公爵般的造型是我的3d模型
这里发生的事情是你的网格原点不在它的中心,正如你的鸭子图片(做得好!)清楚地说明的那样。
一种解决方案是在 Y 方向平移网格的顶点,使原点位于中间(另见
geometry.translate( distX, distY, distZ );
还有一种自动重置网格原点的方法,方法是使用边界框来定义网格的中心(也就是说,您不必计算应该沿 Y 轴移动多远的顶点) ):
// Create a bounding box:
var box = new THREE.Box3().setFromObject( mesh );
// Reset mesh position:
box.center(mesh.position);
mesh.position.multiplyScalar(-1);
然后将网格添加到枢轴对象:
var pivot = new THREE.Group();
scene.add(pivot);
pivot.add(mesh);
(另见
我尝试了两种方法来解决这个问题,但都没有用,这是我的代码: 安迪是我的对象
// Create a bounding box:
var box = new THREE.Box3().setFromObject( andy );
// Reset mesh position:
box.getCenter(andy.position);
andy.position.multiplyScalar(-1);
var pivot = new THREE.Group();
scene.add(pivot);
pivot.add(andy);
//var xAxis = new THREE.Vector3(0,-1,0);
//rotateAroundObjectAxis(andy, xAxis, Math.PI / 180);
markerRoot.add(andy);
/********* génère la vitesse de rotation de l'objet *************/
onRenderFcts.push(function(){ //loop function
//andy.children[0].material.opacity -= 0.01;
//andy.position.x -= 0.01;
pivot.rotation.x -= 0.01;
//andy.rotation.x += 0.01;
//andy.rotation.y += 0.01;
//andy.rotation.z += 0.01;
})
编辑:我用代码解决了这个问题:
// Create a bounding box:
var box = new THREE.Box3().setFromObject( andy );
// Reset mesh position:
box.getCenter(andy.position);
andy.position.multiplyScalar(-1);
var pivot = new THREE.Group();
scene.add(pivot);
pivot.add(andy);
//var xAxis = new THREE.Vector3(0,-1,0);
//rotateAroundObjectAxis(andy, xAxis, Math.PI / 180);
andy.children[0].geometry.center();
markerRoot.add(andy);
/********* génère la vitesse de rotation de l'objet *************/
onRenderFcts.push(function(){
//andy.children[0].material.opacity -= 0.01;
//andy.position.x -= 0.01;
andy.rotation.x += 0.01;
//andy.rotation.y += 0.01;
//andy.rotation.z += 0.01;
})
Object.children[0].geometry.center() - 这是关键行。