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

fix(view/lights): for spotlights, restore parent on its target #1491

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
10 changes: 9 additions & 1 deletion packages/view/src/pools/SingleUserPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Property as PropertyDef, Mesh as MeshDef, Node as NodeDef, uuid } from '@gltf-transform/core';
import { Object3D } from 'three';
import{ DirectionalLight, type Object3D, SpotLight } from 'three';
import { LightLike } from '../constants.js';
import { Pool } from './Pool.js';

Expand Down Expand Up @@ -27,6 +27,14 @@ export class SingleUserPool<T extends Object3D> extends Pool<T, SingleUserParams
// any PrimitiveDef values (e.g. Mesh, Lines, Points) within it.
// Record the new outputs.
const dstObject = srcObject.clone();

// A clone of spot lights and directional lights clones its children and target separately, which results in duplicates.
if(dstObject instanceof SpotLight || dstObject instanceof DirectionalLight) {
dstObject.children[0]?.remove();
// Target is cloned but its parent needs to be restored to the cloned light.
dstObject.add(dstObject.target);
}

parallelTraverse(srcObject, dstObject, (base, variant) => {
if (base === srcObject) return; // Skip root; recorded elsewhere.
if ((srcObject as unknown as LightLike).isLight) return; // Skip light target.
Expand Down