I have a group, with several children:
var group = new THREE.Group();
for (i=0; i<objectsToAdd.length; i++) {
toolpath.add(objectsToAdd[i]); // some Line and Mesh objects
});
I can update the position of one of the children: for example below, and it renders fine.
group.children[4].position.x = 100;
If I export the group using .toJSON
group .toJSON();
then save the JSON out, later, import it again using
var newGroup = loader.parse(jsonstringfromfilereader);
It renders all the objects back to their original positions (ie as its in the Geometry, none of the .position attributes seems to get exported.
Any chance we can get .toJSON() extended to include position data as well? (Suspect I'll also find Scale, Rotation missing? Going to have to use those soon after this)
Otherwise, a way to update vertices after any position update
// move object with object.position = {x: 10, y:10, z:0 }
// do something to force (traversely) all vertices downward to +10 on X and Y
// reset object.position to 0,0 as the vertices carry the new offset?
Subnote: Did check Stackoverflow, https://stackoverflow.com/questions/37937713/three-js-object3d-tojson-submitted-parameters/37947511 was about the closest, and that says .toJSON doesnt export, therefore my request (:
Thanks again for all your awesome work, really couldnt do any of the cool things i can do with threejs, if it wasnt for you guys and the millions of man hours behind threejs
Temporary workaround: Before export I loop through all my objects and children and translate the geometry itself, by the position of the object and its parent. Then i zero the object and its parent:
function changePositionToGeoTranslate() {
for (i=0; i<objectsInScene.length; i++){
var object = objectsInScene[i]
for (j=0; j<object.children.length; j++) {
object.children[j].geometry.translate(object.children[j].position.x, object.children[j].position.y, 0)
object.children[j].geometry.translate(object.position.x, object.position.y, 0)
object.children[j].position.x = 0;
object.children[j].geometry.verticesNeedUpdate = true
object.children[j].position.y = 0;
}
object.position.x = 0;
object.position.y = 0;
}
}
Sorry for the late response but I don't think that's a bug.
Any chance we can get .toJSON() extended to include position data as well?
You need to call group.updateMatrixWorld() in your code since only Object3D.matrix is serialized. If it still does not work with the proposed fix, please reopen the issue with a live example that demonstrates the problem.
Just providing a very belated update (: so that it can help others, but YES the key is to run group.updateMatrixWorld() before converting to JSON! Does the trick perfectly (; now my calculations run in a webworker, run group.updateMatrixWorld() on it, and postMessage back to mainthread as JSON (:
Most helpful comment
Sorry for the late response but I don't think that's a bug.
You need to call
group.updateMatrixWorld()in your code since onlyObject3D.matrixis serialized. If it still does not work with the proposed fix, please reopen the issue with a live example that demonstrates the problem.