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

Recreate canvas and renderer when component remount #38

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

vincentfretin
Copy link

This PR is based on #37. Only the latest commit is new.

The changes are good if you want to use the AvatarEditorContainer component in a Dialog component that you can show and hide inside your react app.
The threejs renderer is created when the AvatarPreviewContainer mount and is disposed when the component unmount.

I created the functions window.gameState.initRenderer and window.gameState.disposeRenderer and refactored game.js to create only once the scene, and recreate the envmap and controls when the component is remounted.
Instead of setting material.envMap of each material, I just set it once with scene.environment.
I also added sky.geometry.dispose();sky.material.dispose(); to properly clean up sky geometry and material.
I'm using renderer.setAnimationLoop(tick) instead of handling the loop with window.requestAnimationFrame(tick);.
This way the loop is stopped automatically when disposing the renderer, so we don't need to handle a state ourself.

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