MeTe-30
MeTe-30

Reputation: 2542

ThreeJS get element (Mesh) by id

I have a Mesh object in Three.JS.
How can i get that Mesh object with id, and change its position, like $(#"sample") in jQuery.
Or any better idea to change the position of an objects, in the middle of the code.

var voxel = new THREE.Mesh(new THREE.CubeGeometry(x, y, z), new THREE.MeshBasicMaterial(...))
voxel.id = "sample";
scene.add(voxel);

Upvotes: 2

Views: 6416

Answers (1)

WestLangley
WestLangley

Reputation: 104823

Just to be safe, don't overwrite the existing Object3D.id.

var objects = {};

mesh1.id2 = "mesh1";
objects[ mesh1.id2 ] = mesh1;

mesh2.id2 = "mesh2";
objects[ mesh2.id2 ] = mesh2;

objects[ "mesh2" ].position.set( 1, 1, 1 );

EDIT: Object3D has a property userData now. So you could use this pattern:

mesh1.userData.id = "mesh1";
objects[ mesh1.userData.id ] = mesh1;

three.js r.73

Upvotes: 4

Related Questions