Skip to content

BatchedMesh: Fix toJSON, ObjectLoader integration #30965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 20, 2025

Conversation

gkjohnson
Copy link
Collaborator

Fix #30960
Fix #30961

Description

It seems BatchedMesh's toJSON and ObjectLoader integration had become out of date with some of the past changes. I tested by adding the following lines to the webgl_mesh_batch example to ensure the BatchedMesh could be cloned and serialized without error and render:

// test clone
mesh = mesh.clone();

// test serialization
new THREE.ObjectLoader().parse( mesh.toJSON(), res => {

	mesh = res;
	scene.add( mesh );

} );

A couple questions (cc @Mugen87):

  • Have we considered adding toJSON and fromJSON functions to the Box3 or Sphere classes to make some of this kind of serialization more clean?
  • Would we be open to adding some tests that serialize and deserialize different three.js Object3D classes and performs a diff between the source and clone to ensure the end-to-end serialization is working correctly?
  • I'm sure I've asked it before but can your remind me why Object3D.toJSON handles serialization for all objects types rather than each child class implementing its own toJSON method?

@gkjohnson gkjohnson added this to the r176 milestone Apr 20, 2025
Copy link

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 336.33
78.33
336.33
78.33
+0 B
+0 B
WebGPU 545.96
151.35
545.96
151.35
+0 B
+0 B
WebGPU Nodes 545.31
151.19
545.31
151.19
+0 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 465.48
112.23
465.78
112.28
+294 B
+42 B
WebGPU 619.52
167.64
619.81
167.7
+294 B
+55 B
WebGPU Nodes 574.39
156.92
574.68
156.98
+294 B
+58 B

@Mugen87
Copy link
Collaborator

Mugen87 commented Apr 20, 2025

Have we considered adding toJSON and fromJSON functions to the Box3 or Sphere classes to make some of this kind of serialization more clean?

No that I am aware of but I that sounds like a good idea.

Normally, math objects are serialized/deserialized with toArray() and fromArray() but that is probably no good choice for more complex objects like bounding volumes.

Would we be open to adding some tests that serialize and deserialize different three.js Object3D classes and performs a diff between the source and clone to ensure the end-to-end serialization is working correctly?

The unit tests already do this partly. Example for Object3D:

QUnit.test( 'toJSON', ( assert ) => {

I'm sure I've asked it before but can your remind me why Object3D.toJSON handles serialization for all objects types rather than each child class implementing its own toJSON method?

This is done for historical reasons. The project started with this approach and it has been never refactored so far. Related #11266.

@Mugen87 Mugen87 merged commit 2b8b7aa into mrdoob:dev Apr 20, 2025
12 checks passed
RuthySheffi pushed a commit to RuthySheffi/three.js that referenced this pull request Jun 5, 2025
* BatchedMesh: fix copy function

* BatchedMesh: Update toJSON function

* BatchedMesh.toJSON: small cleanup

* ObjectLoader: Fix BatchedMesh support

* BatchedMesh.copy: Remove redundant field

* Object3D, ObjectLoader: adjust serialized definitions

* ObjectLoader: Copy over necessary info
RuthySheffi pushed a commit to RuthySheffi/three.js that referenced this pull request Jun 5, 2025
* BatchedMesh: fix copy function

* BatchedMesh: Update toJSON function

* BatchedMesh.toJSON: small cleanup

* ObjectLoader: Fix BatchedMesh support

* BatchedMesh.copy: Remove redundant field

* Object3D, ObjectLoader: adjust serialized definitions

* ObjectLoader: Copy over necessary info
@gkjohnson gkjohnson deleted the fix-batchedmesh-json branch June 19, 2025 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BatchedMesh.toJSON errors BatchedMesh.copy() fails with WebGL warning
2 participants