Update line vertices on object move (mouseMove function)

Hello and good day, I love three.js I am using it to create a scene where I can move 3d object between the walls and I want to display the distance between object and wall or next object in realtime as user drags the item. For this, I added two lines to object’s 2d dimension object:

var lineGeotheo = new THREE.Geometry();
lineGeotheo.vertices.push( new THREE.Vector3(objDims.width, -50, -325) );
lineGeotheo.vertices.push( new THREE.Vector3(objDims.width+objDims.width, -50, -325) );
var lineinitials = new THREE.Line( lineGeotheo, new THREE.LineBasicMaterial( { color: 0x73ed11 } ) );
lineinitials.name = "lineInitials";
lineinitials.geometry.verticesNeedUpdate = true;
dimensions2D.add(lineinitials);


var lineGeotheon = new THREE.Geometry();
lineGeotheon.vertices.push( new THREE.Vector3(0-objDims.width, objDims.height + 500, -325) );
lineGeotheon.vertices.push( new THREE.Vector3(0, objDims.height + 500, -325) );
var linefinal = new THREE.Line( lineGeotheon, new THREE.LineBasicMaterial( { color: 0xed2b11 } ) );
linefinal.name = "lineFinal";
linefinal.geometry.verticesNeedUpdate = true;
dimensions2D.add(linefinal);

now I have a mousemove function in another file where I want to change these vertices in real-time as user drag this item between the walls within the drag function I tried this:

triban.geometry.vertices.push = [new THREE.Vector3(500, 500, -425), new THREE.Vector3( zuba, 500, -425)]
triban.geometry.verticesNeedUpdate = true;
triban.updateMatrixWorld();

but it is not working, in the console log, it shows vertices updated but it won’t reflect the change in the scene, I tried many suggested fixes but it isn’t working, will highly appreciate any help or advice on this.

If you want to change existing vertices in a geometry, do not push new instances of Vector3 to Geometry.vertices. Instead, modify the existing objects in the array.

BTW: When creating a new Geometry, setting verticesNeedUpdate to true is not required. This is only relevant when updating.

1 Like

Dear Mugen87, thanks a lot for your valuable suggestion! its fixed and vertices are updating really well now.