update 3D scene when an <extra-model> is removed#5187
Merged
Merged
Conversation
Fixes #5186. ### Summary & Root Cause When an `<extra-model>` child element was removed from `<model-viewer>` (e.g. via `element.remove()`), the DOM child change triggered a `slotchange` event, which invoked `this[$updateSource]()`. However, because the primary model had already finished loading, `this.loaded` remained `true`. Inside `[$updateSource]()`, an early return check (`if (this.loaded || ...)`) caused the update to bail out before invoking `scene.setSource()`. As a result, the removed `<extra-model>`'s 3D mesh was orphaned and remained rendered inside the Three.js scene graph. ### Changes Made * **`model-viewer-base.ts`**: * Updated `[$onSlotChange]` to reset `this[$loaded] = false;` and `this[$loadedTime] = 0;` whenever the list of `<extra-model>` URLs or element count changes. This allows `[$updateSource]()` to proceed past the load guard and properly synchronize the Three.js scene graph with the DOM. * Added explicit handling for the edge case where all primary and extra models are removed (`this.src == null && extraModels.length === 0`), directly invoking `this[$scene].reset()` and clearing loaded state. * **`ModelScene.ts`**: * Updated `reset()` to ensure `this.extraUrls = [];` is properly cleared during scene resets. * **`extra-model-spec.ts`**:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5186.
Summary & Root Cause
When an
<extra-model>child element was removed from<model-viewer>(e.g. viaelement.remove()), the DOM child change triggered aslotchangeevent, which invokedthis[$updateSource](). However, because the primary model had already finished loading,this.loadedremainedtrue. Inside[$updateSource](), an early return check (if (this.loaded || ...)) caused the update to bail out before invokingscene.setSource(). As a result, the removed<extra-model>'s 3D mesh was orphaned and remained rendered inside the Three.js scene graph. ### Changes Mademodel-viewer-base.ts:[$onSlotChange]to resetthis[$loaded] = false;andthis[$loadedTime] = 0;whenever the list of<extra-model>URLs or element count changes. This allows[$updateSource]()to proceed past the load guard and properly synchronize the Three.js scene graph with the DOM.this.src == null && extraModels.length === 0), directly invokingthis[$scene].reset()and clearing loaded state.ModelScene.ts:reset()to ensurethis.extraUrls = [];is properly cleared during scene resets.extra-model-spec.ts:Reference Issue