@@ -432,32 +432,38 @@ export function drawStick(atoms, bondList, bondIndices, settings, radius = 0.1,
432432 const bondMesh = new THREE . InstancedMesh ( cylinderGeometry , material , bondIndices . length * 2 ) ;
433433 const bondCap = withCap ? new THREE . InstancedMesh ( sphereGeometry , material , bondIndices . length * 2 ) : null ;
434434
435+ // pre assign some vectors
436+ const position1 = new THREE . Vector3 ( ) ;
437+ const position2 = new THREE . Vector3 ( ) ;
438+ const midpoint1 = new THREE . Vector3 ( ) ;
439+ const midpoint2 = new THREE . Vector3 ( ) ;
440+ const instanceMatrix = new THREE . Matrix4 ( ) ;
435441 for ( let i = 0 ; i < bondIndices . length ; i ++ ) {
436442 const [ index1 , index2 , offset1 , offset2 ] = bondList [ bondIndices [ i ] ] ;
437- var position1 = atoms . positions [ index1 ] . map ( ( value , index ) => value + calculateCartesianCoordinates ( atoms . cell , offset1 ) [ index ] ) ;
438- position1 = new THREE . Vector3 ( ...position1 ) ;
443+ const p1 = atoms . positions [ index1 ] . map ( ( value , index ) => value + calculateCartesianCoordinates ( atoms . cell , offset1 ) [ index ] ) ;
444+ position1 . set ( ...p1 ) ;
439445
440- var position2 = atoms . positions [ index2 ] . map ( ( value , index ) => value + calculateCartesianCoordinates ( atoms . cell , offset2 ) [ index ] ) ;
441- position2 = new THREE . Vector3 ( ...position2 ) ;
446+ const p2 = atoms . positions [ index2 ] . map ( ( value , index ) => value + calculateCartesianCoordinates ( atoms . cell , offset2 ) [ index ] ) ;
447+ position2 . set ( ...p2 ) ;
442448
443449 const key = atoms . symbols [ index1 ] + "-" + atoms . symbols [ index2 ] ;
444450 const color1 = atomColors ? atomColors [ index1 ] : settings [ key ] . color1 ;
445451 const color2 = atomColors ? atomColors [ index2 ] : settings [ key ] . color2 ;
446452
447453 // Midpoints and quaternion for cylinder placement
448- const midpoint1 = new THREE . Vector3 ( ) . lerpVectors ( position1 , position2 , 0.25 ) ;
449- const midpoint2 = new THREE . Vector3 ( ) . lerpVectors ( position1 , position2 , 0.75 ) ;
454+ midpoint1 . lerpVectors ( position1 , position2 , 0.25 ) ;
455+ midpoint2 . lerpVectors ( position1 , position2 , 0.75 ) ;
450456 const quaternion = calculateQuaternion ( position1 , position2 ) ;
451457 const scale = calculateScale ( position1 , position2 , radius ) ;
452458
453459 // Set the first cylinder
454- const instanceMatrix1 = new THREE . Matrix4 ( ) . compose ( midpoint1 , quaternion , scale ) ;
455- bondMesh . setMatrixAt ( i * 2 , instanceMatrix1 ) ;
460+ instanceMatrix . compose ( midpoint1 , quaternion , scale )
461+ bondMesh . setMatrixAt ( i * 2 , instanceMatrix ) ;
456462 bondMesh . setColorAt ( i * 2 , color1 ) ;
457463
458464 // Set the second cylinder
459- const instanceMatrix2 = new THREE . Matrix4 ( ) . compose ( midpoint2 , quaternion , scale ) ;
460- bondMesh . setMatrixAt ( i * 2 + 1 , instanceMatrix2 ) ;
465+ instanceMatrix . compose ( midpoint2 , quaternion , scale )
466+ bondMesh . setMatrixAt ( i * 2 + 1 , instanceMatrix ) ;
461467 bondMesh . setColorAt ( i * 2 + 1 , color2 ) ;
462468
463469 // If withCap is true, add spheres at ends
0 commit comments