Object3D.toJSON not saving updated position attributes

I’m creating a simple cuboid with an arbitrary amount of width segments and then updating the geometry.attributes.position.array to deform the geometry of the given cuboid, this works perfectly but i’m encountering a problem when trying to export the model as a json file with the method Object3D.toJSON… the deformation of the geometry doesn’t seem to be preserved when loading the file with the THREE.ObjectLoader() and is simply displaying the original cuboid at it’s set width height and depth… would anyone know why this may be happening and how i could preserve the updated geometry “position” attributes on converting the object to JSON?

any help with this would be greatly appreciated

Are you using a plain THREE.BufferGeometry, or a BoxGeometry? I suspect that BoxGeometry does not write the raw vertex data when exported to JSON.

thanks for the quick reply, yes i’m using BoxGeometry, as i understand from the documentation toJSON only performs a shallow clone of the object3D, i found this thread on the subject but it’s quite old…

yes the data on logging the json output displays the following…
which i assume is just a simple reference for the ObjectLoader() to recreate the geometry (?). do you have any ideas on a workaround to export the modified geometry as intended? would i need to create a new BufferGeometry() and assign the attributes from the modified geometry to this?

I think something like new BufferGeometry().copy( boxGeometry ) would do the trick.

oh perfect, i had no idea BufferGeometry() had a .copy method that’s amazing thank you!
i was going a bit rogue and copying attributes and values on a case by case basis…

newBufferGeometry = new BufferGeometry()
newBufferGeometry.attributes = newGeometry.attributes
newBufferGeometry.groups = newGeometry.groups
newBufferGeometry.index = newGeometry.index

as you can see just overkill if .copy can be used instead!
thanks for the answer!

@donmccurdy using the .copy method i’m getting the following error when using toJSON…
this is very odd becuase copying the attributes one for one as i had it ^^^ works fine and using .copy the result in the console looks exactly the same for each case :thinking: :grimacing: do you have any idea why this may be? it’s fine as i can copy the attributes one for one and it’ll work for me but maybe this could be an internal issue that’s faulty ( or likely i’m doing something odd…)

ok i found that i needed to delete the property “parameters” for the .copy method to work with toJSON in this case…

newBufferGeometry = new BufferGeometry().copy(newGeometry)
delete newBufferGeometry.parameters

this now works as intended when exporting toJSON

Hrm I wasn’t expecting that – feel free to report it as a bug, I think it’s worth a discussion about whether that can be fixed!

Hey, i just got round to it, you can find the report here
and a demonstration of the issue in this live example.

i hope this helps

1 Like

This issue was indeed a bug and will be fixed with r150. Related PR:

1 Like