Three.js stretch of elements

Good day!

I need to arrange certain elements along the geometry, let’s say on a sphere.
if you take Tubegeometry
tubeGeometry = new THREE.TubeGeometry( path, 200, 0.3, 8, false );
then she has a path to do it

Modelmesh.position.copy(
tubeGeometry.parameters.path.getPointAt( 0.2+i*0.15 )
);
//also there is an option
but I have 3 elements and I need to stretch them evenly over this Torus.
Hiw i can do this?

const geometry = new THREE.SphereGeometry( 1, 32, 16 );
const material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
const sphere = new THREE.Mesh( geometry, material );
scene.add( sphere );

const baseS = new THREE.TorusGeometry( 10, 3, 16, 100 );

let pos = baseS.attributes.position;
console.log(pos.count);
for(let i = 0; i < 3; i++){
const mesh = new THREE.Mesh( geometry,new THREE.MeshBasicMaterial( { color: ‘red’ } ) );
mesh.position.fromBufferAttribute(pos, i);
scene.add(mesh);
mesh.scale.multiplyScalar(0.3)
}

Hello Lighty,
I have faced a similar task and solved it using THREE.Box3 class and the getSize method.
In your case:

const baseS = new THREE.TorusGeometry( 10, 3, 16, 100 );
let pos = baseS.attributes.position;

const baseS_mesh = new THREE.Mesh(baseS, new THREE.MeshBasicMaterial());
const bounding_box = new THREE.Box3().setFromObject(baseS_mesh);

for(let i = 0; i < 3; i++){
  let mesh = new THREE.Mesh( geometry,new THREE.MeshBasicMaterial( { color: ‘red’ } ) );
  mesh.position.fromBufferAttribute(pos, i);
  scene.add(mesh);
  let  b_box = new THREE.Box3().setFromObject(mesh);
  let ratio = bounding_box.getSize().x/b_box.getSize().x;
  mesh.scale.set(ratio, ratio, ratio)
}

hope this helps

can i have the full code snippet? because it throws an error three.module.js:573 Uncaught TypeError: Cannot read properties of undefined (reading ‘subVectors’)

1 Like

Happened in this way. Thank you!

const baseS = new THREE.TorusGeometry( 10, 3, 16, 100 );

let pos = baseS.attributes.position;

const baseS_mesh = new THREE.Mesh(baseS, new THREE.MeshBasicMaterial());
const bounding_box = new THREE.Box3().setFromObject(baseS_mesh);
console.log(bounding_box);

for(let i = 0; i < 3; i++){
     let mesh = new THREE.Mesh( new THREE.SphereGeometry( 1, 32, 16 ),new THREE.MeshBasicMaterial( { color: 'red' } ) );
     mesh.position.fromBufferAttribute(pos, i * 30);
     scene.add(mesh);
     let  b_box = new THREE.Box3().setFromObject(mesh);
}

https://threejs.org/examples/?q=scatter#webgl_instancing_scatter

there’s nothing to thank me for. I misunderstood your question - I thought the objects needed to be enlarged… box3 is not needed here, and if you do so (pos, i * 33,3), then the arrangement will be more evenly

Yes, I already did that :sunglasses:

100/3 or 100/n. n - number of objects - extreme accuracy