CesiumGS / cesium-unity

Bringing the 3D geospatial ecosystem to Unity
https://cesium.com/platform/cesium-for-unity/
Apache License 2.0
350 stars 83 forks source link

VR performance with Cesium for Unity #189

Open argallegos opened 1 year ago

argallegos commented 1 year ago

There seems to be a big performance hit when loading a Cesium for Unity scene in VR, at least if it's using the XR Interaction toolkit.

This can be observed in the VR01 sample scene - opening the scene immediately spikes CPU usage from a baseline Cesium for Unity scene (<1%) to over 90% on my machine. Deleting the Cesium georeference and 3D tilesets from the scene immediately reduces CPU usage.

Additional profiling is needed to help narrow down the cause of the issue.

argallegos commented 1 year ago

Ran the profiler to narrow down this issue. Summary: Profiler results in edit mode while the problem was occurring are detailed below. The trigger for the lag seemed to be the presence of the" XR Origin" gameobject's camera, which is discussed below the profiler results.

Profiler stats

Note that these stats were profiled in edit mode. @joseph-kaile, stats from play mode would also be quite useful, especially if you can run the profiler on a standalone build.

The vast majority of the time spent each frame came from Cesium. Inside of Application.TickSceneTracker, specifically UnityEditor.Coremodule.dll!UnityEditor::EditorApplication.Internal_CallUpdateFunctions(), CesiumRuntime.dll!::CallbackFunctionNativeFunction.UnityEditor_CallbackFunction_InvokeCallback() was creating a lot of overhead, at worst adding 95.37ms. I wasn't able to run a deeper profile on this, but from the listed method names, it seems that Reinterop::ReinteropInitializer is the culprit. image

The previous spike in that image occurred right after opening the scene, in Application.UpdateScene...CesiumRuntime.dll!CesiumForUnity::Cesium3DTileset.Update(). This latency reoccurred in several later spikes but wasn't as consistently high as the latency coming from Application.TickSceneTracker. image

XR Origin gameobject

Deleting the XR Origin/Camera Offset/Main Camera gameobject from the scene resolves the latency. This gameobject has a Camera script and a Tracked Pose Driver (Input System) script. It's actually the camera script itself that is causing this lag. Removing the Camera component fixes the issue, even if the other script is left on.

I wasn't able to recreate the issue by adding a new camera to the scene and dragging it to the same place in the hierarchy. Likewise, I wasn't able to recreate the issue by deleting the 3D Tilesets from the scene, then re-adding them.