gkjohnson / three-bvh-csg

A flexible, memory compact, fast and dynamic CSG implementation on top of three-mesh-bvh
MIT License
623 stars 49 forks source link

Any hint to avoid hitting maxDepth? #196

Closed gpeu closed 10 months ago

gpeu commented 10 months ago

Objective: The goal is to merge the walls of a room and create openings for windows and doors.

Issue: These warnings from three-mesh-bvh sometimes start to appear during the process, but not always, even with approximately 60 walls and 50 holes, all using THREE.BoxGeometry.

buildTree.js:98 MeshBVH: The maximum depth of 40 is reached when generating the BVH. Consider increasing the maxDepth.

Unfortunately, it's not stable enough to obtain a clean, reproducible case so far. Sometimes I encounter it even with far fewer objects.

The constructor options for MeshBVH are not exposed by the Brush, and therefore, the maxDepth is not accessible. However, I assume that the default value of 40 exists for a good reason, and there may be other factors to consider.

I initially thought that my use-case, which mainly consists of almost straight boxes, would be simpler compared to examples with complex geometries. Still, I can also imagine that my straightforward boxes might lead to unexpected cases.

Question: This library is working very well, and I hope to soon gain a better understanding of how to use it more effectively. Are there any best practices to improve performance and avoid potential pitfalls?

What I've tried: I started with aggregating all walls, then removing each hole. It seems to work better though to first punch the holes in off their walls, and then aggregating the holy walls.

gkjohnson commented 10 months ago

Hello! Can you explain more about your usecase? Are you running these operations offline? The max depth of 40 is designed to prevent the BVH from taking extremely long times to build and stalling the app so I'd expect if you're reaching that the tree is taking a long time to build.

In terms of workarounds you can replace the "prepareGeometry" function with your own. Or if you want to propose an API that allows for setting custom options per brush we can find a solution and you can create a PR after.