Skip to content

Commit db23db4

Browse files
author
lawwong
committed
Fix VRModule update timing issue
Use onBeforeRender in 2017.1 or newer In onPreCull callback, skips not-in-game camera
1 parent 14c0449 commit db23db4

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

Assets/HTC.UnityPlugin/VRModule/VRModuleManager.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,22 @@ protected override void OnSingletonBehaviourInitialized()
8181
m_prevStates = new DeviceState[MAX_DEVICE_COUNT];
8282
for (var i = 0u; i < MAX_DEVICE_COUNT; ++i) { m_prevStates[i] = new DeviceState(i); }
8383

84+
#if UNITY_2017_1_OR_NEWER
85+
Application.onBeforeRender += DoUpdateFrame;
86+
#else
8487
Camera.onPreCull += OnCameraPreCull;
88+
#endif
8589
}
8690

8791
protected override void OnDestroy()
8892
{
8993
if (IsInstance)
9094
{
95+
#if UNITY_2017_1_OR_NEWER
96+
Application.onBeforeRender -= DoUpdateFrame;
97+
#else
9198
Camera.onPreCull -= OnCameraPreCull;
99+
#endif
92100

93101
m_isDestoryed = true;
94102

@@ -101,17 +109,25 @@ protected override void OnDestroy()
101109
base.OnDestroy();
102110
}
103111

112+
#if !UNITY_2017_1_OR_NEWER
104113
private void OnCameraPreCull(Camera cam)
105114
{
106-
#if UNITY_EDITOR
107-
// skip pre cull from scene camera (editor only?)
108-
// because at this point, the LastPoses seems not updated yet (the result is same as last frame)
109-
// shell wait till next game camera pre cull (top game camera)
110-
if (cam.depth == 0 && cam.name == "SceneCamera") { return; }
115+
var thisFrame = Time.frameCount;
116+
if (m_poseUpdatedFrame == thisFrame) { return; }
117+
118+
if (cam.cameraType != CameraType.Game) { return; }
119+
120+
#if VIU_STEAMVR && !UNITY_5_4_OR_NEWER
121+
if (activeModule == VRModuleActiveEnum.SteamVR && cam.GetComponent<SteamVR_Camera>() == null) { return; }
111122
#endif
112-
// update only once per frame
113-
if (!ChangeProp.Set(ref m_poseUpdatedFrame, Time.frameCount)) { return; }
114123

124+
m_poseUpdatedFrame = thisFrame;
125+
DoUpdateFrame();
126+
}
127+
#endif
128+
129+
private void DoUpdateFrame()
130+
{
115131
m_isUpdating = true;
116132

117133
UpdateActivatedModule();

0 commit comments

Comments
 (0)