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

WebGLRenderer: Use self instead of window. #23686

Merged

Conversation

begmec
Copy link
Contributor

@begmec begmec commented Mar 9, 2022

Summary:
Three.js throws an exception when WebGLRenderer is disposed in a web-worker.

Reason:
On WebGLRenderer.dispose, WebGLAnimation.stop() is called. The "context" which is usually window, is not accessible in a
web-worker context.

Fix:
If context is not availabe, call request/cancelAnimationFrame without context

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 9, 2022

Instead of modifying WebGLAnimation, I wonder if could change the following line:

if ( typeof window !== 'undefined' ) animation.setContext( window );

to:

if ( typeof self !== 'undefined' ) animation.setContext( self ); 

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 9, 2022

self should work in the main thread and workers, see https://developer.mozilla.org/en-US/docs/Web/API/Window/self.

- revert WebGLAnimation changes
@begmec
Copy link
Contributor Author

begmec commented Mar 9, 2022

self should work in browsers and workers, see https://developer.mozilla.org/en-US/docs/Web/API/Window/self.

Thanks for the input! I changed window to self. It works in web-workers and in the main thread.

@Mugen87 Mugen87 changed the title Fix WebGLAnimation crash in web-worker context WebGLRenderer: Use self instead of window. Mar 9, 2022
@mrdoob mrdoob added this to the r139 milestone Mar 9, 2022
@mrdoob mrdoob merged commit be137e6 into mrdoob:dev Mar 9, 2022
@mrdoob
Copy link
Owner

mrdoob commented Mar 9, 2022

Thanks!

abernier pushed a commit to abernier/three.js that referenced this pull request Sep 16, 2022
* WebGLAnimation: use global request/cancelAnimationFrame if window context is not available (OffscreenCanvas)

* Use self instead of window
- revert WebGLAnimation changes

Co-authored-by: Bernd Meyer <[email protected]>
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.

3 participants