Three.js Object3d cylinder rotation to align to a vector

后端 未结 3 1433
梦毁少年i
梦毁少年i 2020-12-13 21:39

I have searched far and wide, but can\'t seem to figure this pretty basic thing out. I have seen other examples on stackoverflow and elsewhere from a year or two ago, but t

3条回答
  •  别那么骄傲
    2020-12-13 21:55

    i know this is an old question, but in case anyone is still wondering, what worked for me was adding the vector to the mesh position and use lookAt to align it to the vector:

    //Mesh to align
    var material = new THREE.MeshLambertMaterial({color: 0x0000ff});
    var cylinder = new THREE.Mesh(new THREE.CylinderGeometry(10, 10, 15), material);
    
    //vector to align to
    var vector = new THREE.Vector3(
        5,//x
        10,//y
        15 //z
    );
    
    //create a point to lookAt
    var focalPoint = new THREE.Vector3(
        cylinder.position.x + vector.x,
        cylinder.position.y + vector.y,
        cylinder.position.z + vector.z
    );
    
    //all that remains is setting the up vector (if needed) and use lookAt
    cylinder.up = new THREE.Vector3(0,0,1);//Z axis up
    cylinder.lookAt(focalPoint); 
    

提交回复
热议问题