Skip to content
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

BatchedMesh: Add "toJSON" and "copy" support #27131

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions examples/jsm/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,17 +563,29 @@ class BatchedMesh extends Mesh {

}

copy() {
copy( source ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have copy return this to make it consistent with parent classes Object3D and Mesh?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// super.copy( source );
super.copy( source );

throw new Error( 'BatchedMesh: Copy function not implemented.' );
this.geometry = source.geometry.clone();

}
this._drawRanges = source._drawRanges.map( range => ( { ...range } ) );
this._reservedRanges = source._reservedRanges.map( range => ( { ...range } ) );

this._visible = source._visible.slice();
this._active = source._active.slice();

this._maxGeometryCount = source._maxGeometryCount;
this._maxVertexCount = source._maxVertexCount;
this._maxIndexCount = source._maxIndexCount;

toJSON() {
this._geometryInitialized = source._geometryInitialized;
this._geometryCount = source._geometryCount;
this._multiDrawCounts = source._multiDrawCounts.slice();
this._multiDrawStarts = source._multiDrawStarts.slice();

throw new Error( 'BatchedMesh: toJSON function not implemented.' );
this._matricesTexture = source._matricesTexture.clone();
this._matricesTexture.image.data = this._matricesTexture.image.slice();

}

Expand Down
20 changes: 20 additions & 0 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,26 @@ class Object3D extends EventDispatcher {

}

if ( this.isBatchedMesh ) {

object.type = 'BatchedMesh';
object._drawRanges = this._drawRanges;
object._reservedRanges = this._reservedRanges;

object._visible = this._visible;
object._active = this._active;

object._maxGeometryCount = this._maxGeometryCount;
object._maxVertexCount = this._maxVertexCount;
object._maxIndexCount = this._maxIndexCount;

object._geometryInitialized = this._geometryInitialized;
object._geometryCount = this._geometryCount;

object._matricesTexture = this._matricesTexture.toJSON();
Copy link
Collaborator

@Mugen87 Mugen87 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid serializing the internal texture? Ideally, it is just recreated in BatchedMesh if it is missing.

Besides, can we avoid the underscore syntax in the JSON object?

Also, at some point this logic should be moved to BatchedMesh.toJSON(). Object3D should not access private variables of subclasses.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid serializing the internal texture? Ideally, it is just recreated in BatchedMesh if it is missing.

This texture is how the matrices are canonically stored. I can generate a list of serialized matrices from the texture instead, though, if that's acceptable.

Besides, can we avoid the underscore syntax in the JSON object?

Yup! I'll fix it.

Also, at some point this logic should be moved to BatchedMesh.toJSON(). Object3D should not access private variables of subclasses.

I thought so, too, but this looked like how all the other classes like InstancedMesh were working. Though it's unclear to me why this is needed.

Copy link
Collaborator

@Mugen87 Mugen87 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This texture is how the matrices are canonically stored. I can generate a list of serialized matrices from the texture instead, though, if that's acceptable.

I forgot after #27130 there is only the texture that holds the matrices. In this case, I'm fine with serializing directly the texture.

I thought so, too, but this looked like how all the other classes like InstancedMesh were working. Though it's unclear to me why this is needed.

We can try to find a different solution in the future. Just wanted to note it in this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: ObjectLoader should also be able to handle BatchedMesh. Maybe we can add this to the todo list.


}

//

function serialize( library, element ) {
Expand Down