Three.js line vector to cylinder?

后端 未结 9 1836
粉色の甜心
粉色の甜心 2020-12-14 23:53

I have this to create a line between 2 points:

var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0, 0, 0));
geometry.vertices.pus         


        
9条回答
  •  遥遥无期
    2020-12-15 00:21

    I figured I would post this since the answers didn't get me 100% there. I noticed the cylinders were correctly oriented, but they were positioned in reference to the origin. The below code (based on the other answers to this question) worked for me:

    function cylinderMesh(pointX, pointY, material) {
        var direction = new THREE.Vector3().subVectors(pointY, pointX);
        var orientation = new THREE.Matrix4();
        orientation.lookAt(pointX, pointY, new THREE.Object3D().up);
        orientation.multiply(new THREE.Matrix4(1, 0, 0, 0,
                                               0, 0, 1, 0,
                                               0, -1, 0, 0,
                                               0, 0, 0, 1));
        var edgeGeometry = new THREE.CylinderGeometry(2, 2, direction.length(), 8, 1);
        var edge = new THREE.Mesh(edgeGeometry, material);
        edge.applyMatrix(orientation);
        // position based on midpoints - there may be a better solution than this
        edge.position.x = (pointY.x + pointX.x) / 2;
        edge.position.y = (pointY.y + pointX.y) / 2;
        edge.position.z = (pointY.z + pointX.z) / 2;
        return edge;
    }
    

    Hope this helps someone!

提交回复
热议问题