Object3D.toJSON not saving updated position attributes

Hello,
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…
image
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.

1 Like

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.computeBoundingBox()
newBufferGeometry.groups = newGeometry.groups
newBufferGeometry.index = newGeometry.index
newGeometry.dispose()

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…
image
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…)

EDIT:
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

1 Like

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:

2 Likes

hi - I’m a novice to ThreeJS but this thread just helped me solve an issue and I wondered if there was a similar challenge with ExtrudeGeometry. Apologies if the below is not an issue but is intended.

In my case -

  1. I was using ExtrudeGeometry.
  2. I then adjusted the position attribute (specifically I flipped the y and z positions so it’s in the xz plane instead of xy (I know I can rotate the mesh but this caused me headaches elsewhere in the code when applying color attributes based off y-height - feature request for the future perhaps)
  3. It all looked fine - I saved toJSON
  4. When using ObjectLoader the geometry was reset as if I’d never changed the position attribute.

My fix based on the above was to use

new BufferGeometry().copy( extrudeGeometry )

Which seems to force the positions to be saved. But as a novice - I was expecting everything to be saved automatically so this workaround took me a while to discover.

Other things like geometry.rotateZ() and geometry.center() don’t seem to get saved either. Perhaps this is just me not understanding the framework but I thought this would get saved in the same way a mesh rotation does.

1 Like