I am trying to position and orient a set of cylinders. As input I have a list of xyz locations. Each cylinder is positioned and oriented from successive pairs of xyz values. One after another. Here is the code:
const geometries = []
let previousVertex = undefined
const vertices = EnsembleManager.getSingleCentroidVerticesWithTrace(trace)
for (let vertex of vertices) {
if (previousVertex) {
// create cylinder with length equal to the distance between successive vertices
const distance = previousVertex.distanceTo( vertex )
const cylinder = new THREE.CylinderGeometry(stickRadius, stickRadius, distance, stickTesselation.radial, stickTesselation.length)
// move cylinder to location central between vertices
const { x:cx, y:cy, z:cz } = previousVertex.lerp(vertex, 0.5)
cylinder.translate(cx, cy, cz)
// use quaterion to orient cylinder to align along the vector formed between
// the pair of vertices
const { x:px, y:py, z:pz } = previousVertex
const { x, y, z } = vertex
const alignmentVector = new THREE.Vector3(x-px, y-py, z-pz)
const cylinderDefaultAxis = new THREE.Vector3(0, 1, 0)
const quaternion = new THREE.Quaternion()
quaternion.setFromUnitVectors(cylinderDefaultAxis, alignmentVector.normalize())
cylinder.applyQuaternion(quaternion)
geometries.push(cylinder)
}
previousVertex = vertex
}
const material = sceneManager.stickMaterial.clone();
const mesh = new THREE.Mesh(mergeBufferGeometries( geometries ), material);
When I run this code none of the cylinders appear. If I remove the quaterion code, the cylinders appear in the correct location but lack - obviously - the required orientation.
What am I missing here?