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

Commit

Permalink
teardown csm
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed Oct 4, 2023
1 parent 5fa7674 commit 9cf6df9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/engine/src/assets/csm/CSM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,22 @@ export class CSM {
addOBCPlugin(material, material.userData.CSMPlugin)
}

teardownMaterial(mesh: Mesh): void {
const material = mesh.material as Material
if (!material.userData) material.userData = {}
if (material.userData.CSMPlugin) {
removeOBCPlugin(material, material.userData.CSMPlugin)
delete material.userData.CSMPlugin
}
if (material.defines) {
delete material.defines.USE_CSM
delete material.defines.CSM_CASCADES
delete material.defines.CSM_FADE
}
material.needsUpdate = true
this.shaders.delete(material)
}

updateUniforms(): void {
const far = Math.min(this.camera.far, this.maxFar)
this.shaders.forEach(function (shader: ShaderType, material: Material) {
Expand Down
9 changes: 9 additions & 0 deletions packages/engine/src/scene/systems/SceneObjectSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ function SceneObjectReactor(props: { entity: Entity; obj: Object3DWithEntity })
csm.setupMaterial(child)
}
})

return () => {
obj.traverse((child: Mesh<any, Material>) => {
if (!child.isMesh) return
if (csm) {
csm.teardownMaterial(child)
}
})
}
}, [shadowComponent?.cast, shadowComponent?.receive, csm])

return null
Expand Down

0 comments on commit 9cf6df9

Please sign in to comment.