Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Three.js Can';t在任意轴上三次旋转一个物体_Three.js_Rotation - Fatal编程技术网

Three.js Can';t在任意轴上三次旋转一个物体

Three.js Can';t在任意轴上三次旋转一个物体,three.js,rotation,Three.js,Rotation,我有一个具有以下坐标的平面几何图形: var planVertices = new Float32Array(18); //Vertice 1 planVertices[0] = -47.63179171506315; planVertices[1] = 33.77709642112255; planVertices[2] = -39.992833485603335; //Vertice4 planVertices[3] = -47.63719374716282; planVertices[4]

我有一个具有以下坐标的平面几何图形:

var planVertices = new Float32Array(18);
//Vertice 1
planVertices[0] = -47.63179171506315;
planVertices[1] = 33.77709642112255;
planVertices[2] = -39.992833485603335;
//Vertice4
planVertices[3] = -47.63719374716282;
planVertices[4] =33.67262125968933;
planVertices[5] = -39.885335769653324;
//Vertice2
planVertices[6] = -46.49726260129362;
planVertices[7] = 33.71843400657177;
planVertices[8] = -39.992833485603335;
//Vertice4
planVertices[9] = -47.63719374716282;
planVertices[10] = 33.67262125968933;
planVertices[11] = -39.885335769653324;
//Vertice3
planVertices[12] = -46.50266463339329;
planVertices[13] = 33.61395884513855;
planVertices[14] = -39.885335769653324;
//Vertice2
planVertices[15] = -46.49726260129362;
planVertices[16] = 33.71843400657177;
planVertices[17] = -39.992833485603335;                        
var geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(planVertices,3));
var material = new THREE.MeshBasicMaterial({
   color: 0x000000,
   side: THREE.DoubleSide,
});
var segments = new THREE.Mesh(geometry, material);
scene.add(segments);
var p1 = new THREE.Vector3(planVertices[0],planVertices[1],planVertices[2]);
var p2 = new THREE.Vector3(planVertices[6],planVertices[7],planVertices[8]);
var axisLocal = new THREE.Vector3().subVectors(p2,p1).normalize;
segments.rotateOnAxis(axisLocal,0.707);
如何从axisLocal向量旋转它?这只是我的几何体的一部分,我有至少80个独立的平面,我想从它的轴旋转每个平面

[已解决] 我已将对象居中于原点,然后执行旋转并重新应用反向平移

//Center object to the origin
var matTrans = new THREE.Matrix4();
var matTransInv = new THREE.Matrix4();
segments.geometry.computeBoundingBox();
var center = new THREE.Vector3();
segments.geometry.boundingBox.getCenter(center);
center.negate();
matTrans.makeTranslation(center.x, center.y, center.z);
matTransInv.getInverse(matTrans);
segments.applyMatrix(matTrans);
//Rotate object at the origin from its axis
var matRot = new THREE.Matrix4();
matRot.makeRotationAxis(axis, -0.707);
segments.applyMatrix(matRot);
//Inverse translation
segments.applyMatrix(matTransInv);

网格从Object3D继承,Object3D有一个.rotateOnAxis(轴,角度)方法。

我使用该方法,但它不通过给定的轴旋转。我在这里找到一条线,它说如果顶点偏离原点,旋转就不是从对象中心开始的。现在我正在研究如何居中旋转然后平移对象。解决这个问题的一个简单方法是制作一个裸的THREE.Object3D(),并将网格添加到其中,而不是直接添加到场景中。。然后将网格移动到所需的轴心点,但操纵父对象以执行所需的旋转。理想情况下,您希望避免变换几何体本身,而是通过场景图/变换层次进行变换。变换几何体在GPU上进行,并且是永久性的,而仅仅操纵节点是动态的,并且可以在每帧的基础上进行修改。你的意思是,实例化一个空的Object3D,然后将我的对象添加到其中?如果是这样的话,我试过了,但我有同样的结果。看起来你找到了解决办法。很高兴你找到了工作。:)可能重复:谢谢,我使用了你的答案从这个线程的中心网格。