Hubs-Foundation / hubs

Duck-themed multi-user virtual spaces in WebVR. Built with A-Frame.
https://hubsfoundation.org
Mozilla Public License 2.0
2.13k stars 1.42k forks source link

Improve raycasting performance #4497

Open takahirox opened 3 years ago

takahirox commented 3 years ago

Description

The FPS number significantly drops if you mouse over a (skinning) model. It seems to be caused by raycasting performance problem.

It may be a regression problem perhaps caused by the recent Three.js and/or three-mesh-bvh upgrades because we didn't recognize before.

To Reproduce

Steps to reproduce the behavior:

  1. Go to hubs.mozilla.com
  2. Place a skinning model
  3. Mouse over the model
  4. See the FPS number (enable FPS counter via preference beforehand)

Expected behavior

No or much less FPS number drop.

Hardware

Additional context

Alternative solutions to raycasting performance would be using the followings instead of three-mesh-bvh

or

┆Issue is synchronized with this Jira Task

gkjohnson commented 2 years ago

Hey! I was taking a look at the hubs repo to see if you guys were still using three-mesh-bvh after all these years and I came across this issue. Three-mesh-bvh has never been capable of providing raycast or any kind of query acceleration for SkinnedMeshes so I'd be surprised if the performance regression were related to a three-mesh-bvh upgrade. If you do see a correlation between the two I'd be curious to know more.

It would be possible to refit the BVH more quickly than building a new one for skinned meshes but it's still slower than you'd want for high framerate interaction. Using reasonably-fit capsules for each SkinnedMesh bones is probably what I'd look in to if that level of precision is good enough.

Also a small aside but I see that the three-mesh-bvh version was recently reverted from 0.5.0 back to 0.3.7 (c95c90a) and I'm curious what the motivation was -- if there's anything I can make smoother I'm happy to take feedback. If you guys do wind up sticking with the library I recommend upgrading because there have been a few improvements that might be relevant including InterleavedBufferAttribute support and support for generating the BVH asynchronously in a WebWorker to keep the main thread uninterupted.

takahirox commented 2 years ago

I'm not sure why but I couldn't reproduce the problem any more. I guess it has been somehow resolved by certain updates? Closing this issue for now. Let's reopen when we meet again the problem.

@gkjohnson I have no idea why we reverted it to 0.3.7. I guess misoperation? Maybe better to upgrade again. Anyways, thanks for the great lib!

takahirox commented 2 years ago

@netpro2k You seemed to upgrade three-mesh-bvh and then revert it. Did you encounter any trouble with the newer three-mesh-bvh in Hubs?

https://github.com/mozilla/hubs/commit/376c66e2f444c2e307372abd8671830b907cd826 https://github.com/mozilla/hubs/commit/c95c90a7736f418dd961e229771dfaf8fc748c97

takahirox commented 2 years ago

Reopening because I encountered likely the raycasting performance issue again.

What I did on Windows Firefox was,

  1. I placed the butterflies objects from Sketchfab https://sketchfab.com/3d-models/butterflies-764b7378c21b47039b40eca9309eb3ab
  2. Point to them
  3. The fps counter significantly drops from 60fps to 45fps

Probably worth to try to upgrade three-mesh-bvh first.

gkjohnson commented 2 years ago

It would be good to see if upgrading fixes your issues. v0.3.7 (which this project is on) included a "lazy generation" option that defaulted to true and meant that that the BVH hierarchy was generated as-needed once raycasting started. It meant a faster startup time but higher memory usage and slower initial performance until the BVH was more fully generated. It's possible this is related to your slow down. There have been a number of other fixes in the mean time, as well.

That aside - how is three-mesh-bvh being used with skinned geometry? If the model is animating or otherwise using bone configuration it will not just work out of the box. The upgrade should be pretty straight forward if you're just using it for raycasting. Feel free to ping me in a PR if something looks off.

netpro2k commented 2 years ago

@netpro2k You seemed to upgrade three-mesh-bvh and then revert it. Did you encounter any trouble with the newer three-mesh-bvh in Hubs?

I don't remember but I would be willing to bet it was actually unrelated to mesh-bvh and more the accidental reversion of buffered-interpolation in that commit that I was reverting. If I remember correctly I was updating mesh-bvh as part of some other work "just because' and when I ran into issues I just backed it out since it wasn't what I was focused on. I would agree we should get up on the latest version.

On raycasting in general I do think for many things we might consider moving our raycasting over to using our physics system. In many cases I bet it will be sufficient. Though our physics system runs off thread which means raycasts would need to be async which could add significant complexity.