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

Object3D.toJSON : Add demo implementation to retain typed buffers in result #21035

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

gkjohnson
Copy link
Collaborator

Related issue: #11746 (comment)

Description

As requested in #11746 (comment) here's a quick PR demonstrating what has to change in order for Object3D.toJSON to retain BufferGeometry typed arrays when serializing and for ObjectLoader to reuse the buffers when deserializing. A second argument has been added to Object3D.toJSON( meta, cloneTransferableTypes ) to give the option for transferable types to not be cloned and instead be returned by reference. The default setting defaults to true which means the behavior is unchanged from three.js master.

As mentioned in #11746 (comment) removing the conversion of arrays in toJSON and ObjectLoader greatly improves the performance in both cases and would make it viable for users to load and perform longer running processes on geometry in web workers because the serialization, deserialization, and transfer time to the main thread is much faster.

Here are some possible next steps for a full implementation which can be done in steps:

  • Update all three.js types (DataTextures, at least) that use typed arrays to respect the setting for returning typed array references
  • Update Texture and ObjectLoader to return a reference to and directly use ImageBitmap
  • Optionally return a list of transferrable types so they can be passed to the worker (granted these can be collected manually after serialization, as well)

@gkjohnson gkjohnson changed the title Object3D.toJSON : Add demo implementation to retain typed buffers in Object3D.toJSON : Add demo implementation to retain typed buffers in result Jan 10, 2021
@kaisalmen kaisalmen mentioned this pull request Jan 12, 2021
7 tasks
@gkjohnson
Copy link
Collaborator Author

Related to #19650 (comment) it occurs to me there are other use cases outside of web workers for retaining typed array buffers in toJSON (or some kind of serialize equivalent) beyond just web worker transfer time.

  • TypedArrays can be quickly and easily stored in browser cache objects like IndexedDB which can be used for things like long running undo stacks or caching / reloading assets more quickly on page reload compared to loading them from the server or reparsing them.
  • TypedArrays can be sent over WebSockets which is maybe useful for collaborative applications?

If returning the arrayBuffer used in the AttributeBuffer array is undesirable then cloning a typed array can still be more than 50x faster than converting it to an array:

const arr = new Float32Array( 100000 );

console.time( 'native slice' );
arr.slice();
console.timeEnd( 'native slice' );

console.time( 'array slice' );
Array.prototype.slice.apply( arr );
console.timeEnd( 'array slice' );

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.

1 participant