DisposableMesh

Is your feature request related to a problem? Please describe.

When I remove a Mesh I want to clean up everything that was created with it. So that means all materials and geometry which are part of the mesh.

Describe the solution you’d like

This is the implementation I’ve been using. This could be a separate class as shown below or better yet part of the base Mesh class so all types of Meshes could benefit from this.

class DisposableMesh extends Mesh {
    public dispose(): void {
        this.geometry.dispose();
        const materials = Array.isArray(this.material) ? this.material : [this.material];
        materials.forEach((material) => {
            material.dispose();
        });
        this.removeFromParent();
    }
}

Describe alternatives you’ve considered

The class shown above can easily be implemented in userland and repeated in each project that it is used.

Additional context

Since garbage collection cannot be automatic for these things, having a single easy way to remove everything is very helpful when you have scenes that are constantly creating and destroying objects.

1 thought on “DisposableMesh

  1. While, like probly everyone at some point, I also fought angrily against having to write disposing mechanics per-project – imo it’s still best to keep it out of three.

    What would DisposableMesh do if other meshes reuse some / all its materials 🤔 ?
    What about texture disposal 🤔?
    What about material disposal, where material wasn’t reused but specific textures were 🤔?

    In apps / demoscene it doesn’t really happen – but in games disposing reusable materials will result in constant recompilation stutter, and, effectively, death of the entire gamedev genre within approximately 3-4 workdays 💀

Comments are closed.