Moving point cloud to a position

I have a point cloud created as:

var pointCloud = new THREE.Points( geometry, shaderMaterialInstanced );

I want to move it to origin of the axis system.

pointCloud.position.set(0,0,0);

But I dont see any effect of this.

What is correct way of doing this?

Your point cloud’s position is already ( 0, 0, 0 ) by default.
I’m guessing you want the points to be centered at the origin, you can call geometry.center() ( see the docs ).

1 Like

So points in pointcloud have huge x,y cartesian values. So it gets rendered at faraway position from origin. I want it to render at cartesian system origin so that I can position my camera accordingly for better viewing.

You could try:

let bounds = new THREE.Box3();
bounds.setFromObject( pointCloud );
bounds.getCenter(pointCloud.position).multiplyScalar(-1);

to reposition the mesh.

or…
transform the geometry/vertices themselves:

let bounds = new THREE.Box3();
bounds.setFromObject( pointCloud );
let ncenter = bounds.getCenter(new THREE.Vector3()).multiplyScalar(-1);
pointCloud.geometry.translate(ncenter.x,ncenter.y,ncenter.z);

Thanks a lot. It worked like a charm.

What exactly it does?
So first we create a bound, and set it using pointcloud in second line of code, which I assume would compute a bounding box for pointcloud’s current position. So no shifting yet.
So I assume when we call multiplyScalar, it moves pointcloud to origin. Am I right?

1 Like

Yup!
I didn’t write it super clearly bc I like shrimpy code.
But it would probably break if .position starts out non-zero.
getCenter takes a vec3 to return the result in, so I’m just skipping a copy/saving an alloc.

A cleaner version:

let bounds = new THREE.Box3();
bounds.setFromObject( pointCloud );
let center = bounds.getCenter(new THREE.Vector3());
pointCloud.position.sub(center);
1 Like