Updating an existing BufferGeometry's matrix

I have existing Mesh objects with CylinderGeometry, and as the animation proceeds I want to update the position and direction of the cylinders. I would like to reuse the old CylinderGeometry objects and just update them by calling geo.applyMatrix(…). But the CylinderGeometry object has no resetMatrix() method. It only has an applyMatrix(…) method. Nor is there a getMatrix() method. So I end up having to dispose the old geometry and create a new one. That is, I have to use:

function updateCylinderBetween(cyl,positionX, positionY ) {
        const direction = new THREE.Vector3().subVectors(positionX, positionY);
        const geo = new THREE.CylinderGeometry(cyl.geometry.radiusTop,cyl.geometry.radiusBottom, direction.length(), 6, 4, true);
        geo.height = direction.length();
        geo.applyMatrix4(new THREE.Matrix4().makeTranslation(0, direction.length() / 2, 0));
        geo.applyMatrix4(new THREE.Matrix4().makeRotationX(THREE.Math.degToRad(90)));
        cyl.geometry.dispose();
        cyl.geometry = geo;
        cyl.position.copy(positionX);
        cyl.lookAt(positionY);
}

I want to use:

// This version would be more efficient:
function updateCylinderBetweenBuggy(cyl,positionX, positionY ) {
        const direction = new THREE.Vector3().subVectors(positionX, positionY);
        const geo = cyl.geometry; // new THREE.CylinderGeometry(cyl.geometry.radiusTop,cyl.geometry.radiusBottom, direction.length(), 6, 4, true);
        geo.height = direction.length();
        //geo.resetMatrix(); ???  to identity matrix
        geo.applyMatrix4(new THREE.Matrix4().makeTranslation(0, direction.length() / 2, 0));
        geo.applyMatrix4(new THREE.Matrix4().makeRotationX(THREE.Math.degToRad(90)));
        cyl.position.copy(positionX);
        cyl.lookAt(positionY);
}

Am I missing something? Or does BufferGeometry need a new method clearMatrix()?

It is not clear from your description what your overall purpose is.

Normally, you don’t change the geometry during animation, but the position and orientation of the mesh. But you can create dynamic geometry.

Please describe your intention in more detail.

Examples for geometry/mesh with animation in the https://discourse.threejs.org/t/collection-of-examples-from-discourse-threejs-org/4315:

OrbitAroundObject
DynamicTubeGeometryCaps
RotatingPlaneAroundLine
RotatePartOfMerged
OnHover-MoveAndBack
MovementOnCurve
SpiralFromCylinder

and more …

I have a bunch of spheres in the scene. Some of them are connected by cylinders. When the spheres move, I want to move the cylinders to march the spheres. However, I [thought I] found a simpler way to do it, and I don’t need to reset the matrix:

function updateCylinderBetween(cyl,position1, position2 ) {
        const delX = position1.x - position2.x;
        const delY = position1.y - position2.y;
        const delZ = position1.z - position2.z;
        cyl.geometry.height = Math.sqrt(delX*delX+delY*delY+delZ*delZ);
        cyl.position.copy(position1);
        cyl.lookAt(position2);
}

[But as I say below, this method doesn’t work if I rotate the scene via OrbitControls.]

I’m wrong! My previous version doesn’t work if I rotate with OrbitControls. In order for the cylinders to appear correctly, I need to recreate the geometry via new THREE.CylinderGeometry as in my original Nov 2 post. So, I still think it would help if BufferGeometry had a resetMatrix() method.

The method certainly does not exist for a reason. You would have to save the starting positions separately.

Have you looked there? three.js/src/core/BufferGeometry.js at f021ec0c9051eb11d110b0c2b93305bffd0942e0 · mrdoob/three.js · GitHub

line 129 applyMatrix4( matrix ) { ...

You don’t have to recreate anything just to move something together in space.
Have you tried grouping meshes? see three.js docs

I want to move each sphere separately (in different directions), so moving them in a Group wouldn’t help. Maybe there’s some other way to do what I want to do without recreating the CylinderGeometry and without adding that resetMatrix method.