Search code examples
javascriptthree.jslinevertices

create dynamic line from one object to another Three.js


I was like a line and other defined. My problem is I do not know how to dynamically so that a line is created from an object "a" to an object "b".


Solution

  • Adapted from the Line example at http://threejs.org/docs/#Reference/Objects/Line -- assuming your objects a and b are there too, and in world coordinates (not parented):

    var material = new THREE.LineBasicMaterial({
        color: 0x0000ff
    });
    
    var geometry = new THREE.Geometry();
    geometry.vertices.push(
        a.position,
        b.position
    );
    
    var line = new THREE.Line( geometry, material );
    scene.add( line );
    

    That creates a line from a to b.

    If your objects move, you can have just move those vertices in your update and say: geometry.verticesNeedUpdate = true.