From 4478e67009dfdcfaa639f2feef01270a50c41fca Mon Sep 17 00:00:00 2001 From: Cedric Guillemet <1312968+CedricGuillemet@users.noreply.github.com> Date: Fri, 11 Jul 2025 08:10:59 +0200 Subject: [PATCH] Dispose ragdoll resources --- packages/dev/core/src/Physics/v2/ragdoll.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/dev/core/src/Physics/v2/ragdoll.ts b/packages/dev/core/src/Physics/v2/ragdoll.ts index 41337f0d421..34d43b70857 100644 --- a/packages/dev/core/src/Physics/v2/ragdoll.ts +++ b/packages/dev/core/src/Physics/v2/ragdoll.ts @@ -10,6 +10,7 @@ import type { Nullable } from "../../types"; import type { Bone } from "../../Bones/bone"; import { Logger } from "../../Misc/logger"; import { TransformNode } from "../../Meshes/transformNode"; +import type { Observer } from "core/Misc/observable"; /** * Ragdoll bone properties @@ -81,6 +82,7 @@ export class Ragdoll { private _rootBoneIndex: number = -1; private _mass: number = 10; private _restitution: number = 0; + private _beforeRenderObserver: Nullable> = null; /** * Pause synchronization between physics and bone position/orientation @@ -362,7 +364,7 @@ export class Ragdoll { } this._initJoints(); - this._scene.registerBeforeRender(() => { + this._beforeRenderObserver = this._scene.onBeforeRenderObservable.add(() => { this._syncBonesAndBoxes(); }); this._syncBonesToPhysics(); @@ -392,5 +394,19 @@ export class Ragdoll { for (const aggregate of this._aggregates) { aggregate.dispose(); } + this._aggregates.length = 0; + for (const transform of this._transforms) { + transform.dispose(); + } + this._transforms.length = 0; + for (const constraint of this._constraints) { + constraint.dispose(); + } + this._constraints.length = 0; + + if (this._beforeRenderObserver) { + this._scene.onBeforeRenderObservable.remove(this._beforeRenderObserver); + this._beforeRenderObserver = null; + } } }