Three.js line vector to cylinder?

后端 未结 9 1851
粉色の甜心
粉色の甜心 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:02

    Actually non of the other answers worked for me but to give credit I used the code that adjusts the position of edge and works fine.

    Using parts of this answer and looking at the source of threejs I ended up to the following

    var cylinderMesh = function( pointX, pointY )
    {
        /* edge from X to Y */
        var direction = new THREE.Vector3().subVectors( pointY, pointX );
        var orientation = new THREE.Matrix4();
        /* THREE.Object3D().up (=Y) default orientation for all objects */
        orientation.lookAt(pointX, pointY, new THREE.Object3D().up);
        /* rotation around axis X by -90 degrees 
         * matches the default orientation Y 
         * with the orientation of looking Z */
        orientation.multiply(new THREE.Matrix4(1,0,0,0,
                                                0,0,1,0, 
                                                0,-1,0,0,
                                                0,0,0,1));
    
        /* cylinder: radiusAtTop, radiusAtBottom, 
            height, radiusSegments, heightSegments */
        var edgeGeometry = new THREE.CylinderGeometry( 2, 2, direction.length(), 8, 1);
        var edge = new THREE.Mesh( edgeGeometry, 
                new THREE.MeshBasicMaterial( { color: 0x0000ff } ) );
    
        edge.applyMatrix(orientation)
        edge.position = new THREE.Vector3().addVectors( pointX, direction.multiplyScalar(0.5) );
        return edge;
    }
    

    A possible improvement is to add edgeGeometry and material as parameters so that someone can reuse the same objects and not create a new one in every call

提交回复
热议问题