steveOw
steveOw

Reputation: 1021

THREE.js Migration r60 to r70: now cannot write: mesh.position = myVector3()

I am migrating a THREE.js app from r60 to r70. Amongst other changes I notice that r60 constructs of the following form no longer work in r70.

mesh.position.set(0,0,0);
myVector3 = new THREE.Vector3 (100,200,300);
mesh.position = myVector3;  

This applies to meshes, pointLights, presumably to all Object3D's but I havent tested further.

In the above example the mesh.position x,y,z values remain unchanged at (0,0,0). For illustration see this JSFiddle and compare lines 70 and 73.

    //...The next line DOES NOT update Sphere_mesh.position.x
    Sphere_mesh.position = NewPos_vector3;//...

    //...The next line DOES update Sphere_mesh.position.x
    Sphere_mesh.position.x = NewPos_vector3.x

In a debugger no console warning is given during execution that the assignment has not worked. In the very brief THREE.js migration notes for (r68 --> r69) I see something about an Object3D's position no longer being immutable but I don't know what that means.

Anyway, my question

Is there a standard THREE construct or function I can use to copy x,y,z values from a Vector3 object to the x,y,z values of a mesh.position rather than the effective, but verbose, form such as

mesh.position.x = myVector3.x;
mesh.position.y = myVector3.y;
mesh.position.z = myVector3.z; ?

e.g. something such as

mesh.position = F_Get_Object3DPosition_from_Vector3(myVector3);   ?

I know it would be easy to write my own function but a standard THREE function would be more likely to evolve smoothly with future versions of THREE.

Upvotes: 0

Views: 93

Answers (2)

vals
vals

Reputation: 64244

position beeing immutable means that the position property can not be changed.

so

mesh.position = anything;

won't work (but you already discovered this)

what you can do is not change the position, but you have to change position values.

in your case, the easiest way is

mesh.position.copy (myVector3);

Upvotes: 1

Arman
Arman

Reputation: 374

I think you meant myVector3 not myVector3() in the last line... Anyway, I though that would work too but the thing is, you are applying a Vector to something that supposed to be a point/Vertex. Even if that worked in my opinion it wasn't the right way to do it. How about using a simple array:

mesh.position.set(0,0,0);
new_position = [100,200,300]
mesh.position.fromArray(new_position,0)

in which 0 is the starting index. So you can have multiple position sets in one array

Upvotes: 1

Related Questions