Three.js line vector to cylinder?

后端 未结 9 1826
粉色の甜心
粉色の甜心 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!

    0 讨论(0)
  • 2020-12-15 00:21

    I found that only one point can be matched, so I edit it( change pointX to pointY) as below and it works

    edge.position = new
    THREE.Vector3().addVectors(pointY,direction.multiplyScalar(0.5));
    
    0 讨论(0)
  • 2020-12-15 00:24

    ArrowHelper since pull request #3307 are based on quaternions.

    This works in three.js r58:

        var cylinderMesh = function(point1, point2, material)
        {
            var direction = new THREE.Vector3().subVectors(point2, point1);
            var arrow = new THREE.ArrowHelper(direction.clone().normalize(), point1);
    
            var rotation = new THREE.Vector3().setEulerFromQuaternion(arrow.quaternion);
    
            var edgeGeometry = new THREE.CylinderGeometry( 2, 2, direction.length(), 10, 4 );
    
            var edge = new THREE.Mesh(edgeGeometry, material);
            edge.rotation = rotation.clone();
            edge.position = new THREE.Vector3().addVectors(point1, direction.multiplyScalar(0.5));
    
            return edge;
        }
    
    0 讨论(0)
提交回复
热议问题