SkinnedMeshRenderer.bounds freezes after PrefabResource.instantiate() — clone remap bypasses listener registration

Aperta
#2,978 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
typescript

Direzione di ricerca

Start in CloneManager.cloneProperty (around lines 109-115), where the _remap branch assigns _transformEntity before the @ignoreClone check, then read Renderer._setTransformEntity (around lines 470-477) and its use in SkinnedMeshRenderer._cloneTo and _applySkin. Done means the rootBone's _updateFlagManager has the _onTransformChanged listener after prefab.instantiate(), and bounds follow the instance when it moves. The payload names no test, so reproduce with the steps given.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Problem

After PrefabResource.instantiate(), SkinnedMeshRenderer.bounds freezes at the initial placement position and never updates when the entity moves. This causes frustum culling to permanently hide moved skinned characters.

Root Cause

Two layers:

Layer 1 (fundamental): CloneManager remap writes _transformEntity bypassing _setTransformEntity()

CloneManager.cloneProperty checks _remap (Entity references) before @ignoreClone:

// CloneManager.ts:109-111 — highest priority
if (sourceProperty instanceof Object && (<ICustomClone>sourceProperty)._remap) {
    target[k] = (<ICustomClone>sourceProperty)._remap(srcRoot, targetRoot);
    return;   // ← returns before cloneMode check at line 115
}

// line 115 — @ignoreClone never reached for Entity references
if (cloneMode === CloneMode.Ignore) return;

During clone, target._transformEntity is directly assigned the remapped rootBone entity without calling _setTransformEntity(), so no _onTransformChanged listener is registered on the new rootBone's _updateFlagManager.

Layer 2 (missing defense): _setTransformEntity equality guard

The subsequent _cloneTo → _applySkin → _setTransformEntity(rootBone) tries to fix this, but the equality guard skips it:

// Renderer.ts:470-477
protected _setTransformEntity(entity: Entity): void {
    const preEntity = this._transformEntity;
    if (entity !== preEntity) {   // ← false after remap set it, entire block skipped
        preEntity?._updateFlagManager.removeListener(this._onTransformChanged);
        entity?._updateFlagManager.addListener(this._onTransformChanged);
        this._transformEntity = entity;
    }
}

It assumes "same reference = listener already registered", which is not true after direct field writes from CloneManager.

Concrete example (character queue in a Cocos-to-Galacean migrated game)
Prefab entity tree:
  character_man
    ├── Body              ← SkinnedMeshRenderer is on this entity
    └── mixamorig:Hips    ← rootBone

Step 1 — SMR constructor:
  _setTransformEntity(Body') → listener on Body' ✓, _transformEntity = Body'

Step 2 — CloneManager property copy:
  _transformEntity is Entity → has _remap → direct assign: _transformEntity = Hips'
  ⚠️ No _setTransformEntity() call → Body' listener is orphaned, Hips' has 0 listeners

Step 3 — _cloneTo → _applySkin → _setTransformEntity(Hips'):
  Hips' === _transformEntity (set in step 2) → equality guard → SKIP

Result: Hips' (rootBone) has 0 listeners → bounds never update → frustum culling wrong

Evidence

Diagnostic data from 12 pooled skinned characters:

Field Value
_transformEntity === skin.rootBone true (remap worked correctly)
Listener count on rootBone _updateFlagManager 0
rootBone.worldMatrix Up-to-date (correct position)
bounds Frozen at initial spawn position
After forcing _dirtyUpdateFlag |= WorldVolume Bounds correct (proves _updateBounds logic is fine)

Suggested Fix

Two options (can be combined):

Option A: Fix _setTransformEntity (defensive, minimal change)

Remove the equality guard so it always ensures the listener is registered:

protected _setTransformEntity(entity: Entity): void {
    const preEntity = this._transformEntity;
    preEntity?._updateFlagManager.removeListener(this._onTransformChanged);
    if (entity) {
        entity._updateFlagManager.addListener(this._onTransformChanged);
    }
    this._transformEntity = entity;
}

Verified to fix the issue. Zero per-frame overhead — only called during clone/skin setup.

Option B: Fix CloneManager priority (fundamental)

Respect @ignoreClone before _remap so that entity references on ignored fields are not written at all:

// Move ignore check before remap check:
if (cloneMode === CloneMode.Ignore) return;

if (sourceProperty instanceof Object && (<ICustomClone>sourceProperty)._remap) {
    target[k] = (<ICustomClone>sourceProperty)._remap(srcRoot, targetRoot);
    return;
}

Combined with @ignoreClone on Renderer._transformEntity, this prevents the direct write entirely.

Note: Option B has broader impact — other components relying on the current remap-first behavior may need review.

Reproduction

  1. Create a prefab containing a SkinnedMeshRenderer with a rootBone (e.g. Mixamo character)
  2. const instance = prefab.instantiate()
  3. Add to scene, move the instance entity each frame
  4. Observe: instance.getComponentInChildren(SkinnedMeshRenderer).bounds stays at initial position
  5. With Camera.enableFrustumCulling = true, the character becomes invisible once moved away from spawn
Lingua principale
TypeScript
Stelle
5.9k
Fork
413
Merge medio
16h 17m
PR unite (30g)
41

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di galacean/engine

Tutte le issue di galacean/engine

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.