Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
further object3d dipose cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed Sep 12, 2023
1 parent aaf00e3 commit 28901c8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ export const AvatarRigComponent = defineComponent({
setObjectLayers(helper, ObjectLayers.AvatarHelper)
Engine.instance.scene.add(helper)
rigComponent.helper.set(helper)
}

if ((!visible?.value || !debugEnabled.value || pending?.value) && rigComponent.helper.value) {
rigComponent.helper.value.removeFromParent()
rigComponent.helper.set(none)
return () => {
helper.dispose()
helper.removeFromParent()
rigComponent.helper.set(none)
}
}
}, [visible, debugEnabled, pending])

Expand Down
18 changes: 9 additions & 9 deletions packages/engine/src/scene/components/MountPointComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ export const MountPointComponent = defineComponent({
const mountPoint = useComponent(entity, MountPointComponent)

useEffect(() => {
if (debugEnabled.value && !mountPoint.helper.value) {
const helper = new ArrowHelper(new Vector3(0, 0, 1), new Vector3(0, 0, 0), 0.5, 0xffffff)
helper.name = `mount-point-helper-${entity}`
if (!debugEnabled.value || mountPoint.helper.value) return

setObjectLayers(helper, ObjectLayers.NodeHelper)
addObjectToGroup(entity, helper)
const helper = new ArrowHelper(new Vector3(0, 0, 1), new Vector3(0, 0, 0), 0.5, 0xffffff)
helper.name = `mount-point-helper-${entity}`

mountPoint.helper.set(helper)
}
setObjectLayers(helper, ObjectLayers.NodeHelper)
addObjectToGroup(entity, helper)

mountPoint.helper.set(helper)

if (!debugEnabled.value && mountPoint.helper.value) {
removeObjectFromGroup(entity, mountPoint.helper.value)
return () => {
removeObjectFromGroup(entity, mountPoint.helper.value!)
mountPoint.helper.set(none)
}
}, [debugEnabled])
Expand Down
45 changes: 28 additions & 17 deletions packages/engine/src/scene/systems/SceneObjectSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ export const disposeMaterial = (material: Material) => {
material.dispose()
}

export const disposeObject3D = (obj: Object3D) => {
const mesh = obj as Mesh<any, any>

if (mesh.material) {
if (Array.isArray(mesh.material)) {
mesh.material.forEach(disposeMaterial)
} else {
disposeMaterial(mesh.material)
}
}

if (mesh.geometry) {
mesh.geometry.dispose()
for (const key in mesh.geometry.attributes) {
mesh.geometry.deleteAttribute(key)
}
}

const skinnedMesh = obj as SkinnedMesh
if (skinnedMesh.isSkinnedMesh) {
skinnedMesh.skeleton?.dispose()
}

const light = obj as Light // anything with dispose function
if (typeof light.dispose === 'function') light.dispose()
}

export function setupObject(obj: Object3DWithEntity, forceBasicMaterials = false) {
const mesh = obj as any as Mesh<any, any>
/** @todo do we still need this? */
Expand Down Expand Up @@ -130,23 +157,7 @@ function SceneObjectReactor(props: { entity: Entity; obj: Object3DWithEntity })
if (layer.has(obj)) layer.delete(obj)
}

obj.traverse((object3D: Object3D) => {
const mesh = object3D as Mesh<any, any>
if (Array.isArray(mesh.material)) {
mesh.material.forEach(disposeMaterial)
} else if (mesh.material) {
disposeMaterial(mesh.material)
}
mesh.geometry?.dispose()

const skinnedMesh = object3D as SkinnedMesh
if (skinnedMesh.isSkinnedMesh) {
skinnedMesh.skeleton?.dispose()
}

const light = object3D as Light // anything with dispose function
if (typeof light.dispose === 'function') light.dispose()
})
obj.traverse(disposeObject3D)
}
}, [])

Expand Down

0 comments on commit 28901c8

Please sign in to comment.