3232package com .jme3 .bullet .debug ;
3333
3434import com .jme3 .app .Application ;
35+ import com .jme3 .app .VRAppState ;
3536import com .jme3 .app .state .AbstractAppState ;
3637import com .jme3 .app .state .AppStateManager ;
3738import com .jme3 .asset .AssetManager ;
@@ -67,6 +68,12 @@ public class BulletDebugAppState extends AbstractAppState {
6768 * message logger for this class
6869 */
6970 protected static final Logger logger = Logger .getLogger (BulletDebugAppState .class .getName ());
71+
72+ /**
73+ * caches the virtual reality state (null means not yet determined)
74+ */
75+ private Boolean isVr = null ;
76+
7077 /**
7178 * limit which objects are visualized, or null to visualize all objects
7279 */
@@ -165,9 +172,18 @@ public void initialize(AppStateManager stateManager, Application app) {
165172 this .assetManager = app .getAssetManager ();
166173 setupMaterials (app );
167174 physicsDebugRootNode .setCullHint (Spatial .CullHint .Never );
168- viewPort = rm .createMainView ("Physics Debug Overlay" , app .getCamera ());
169- viewPort .setClearFlags (false , true , false );
170- viewPort .attachScene (physicsDebugRootNode );
175+
176+ if (isVr ()) {
177+ /* This is a less good solution than the non-vr version (as the debug shapes can be obscured by the regular
178+ * geometry), however it is the best possible as VR does not currently support multiple viewports per eye */
179+ VRAppState vrAppState = stateManager .getState (VRAppState .ID , VRAppState .class );
180+ vrAppState .getLeftViewPort ().attachScene (physicsDebugRootNode );
181+ vrAppState .getRightViewPort ().attachScene (physicsDebugRootNode );
182+ } else {
183+ viewPort = rm .createMainView ("Physics Debug Overlay" , app .getCamera ());
184+ viewPort .setClearFlags (false , true , false );
185+ viewPort .attachScene (physicsDebugRootNode );
186+ }
171187 }
172188
173189 /**
@@ -178,7 +194,14 @@ public void initialize(AppStateManager stateManager, Application app) {
178194 */
179195 @ Override
180196 public void cleanup () {
181- rm .removeMainView (viewPort );
197+ if (isVr ()) {
198+ VRAppState vrAppState = app .getStateManager ().getState (VRAppState .ID , VRAppState .class );
199+ vrAppState .getLeftViewPort ().detachScene (physicsDebugRootNode );
200+ vrAppState .getRightViewPort ().detachScene (physicsDebugRootNode );
201+ } else {
202+ rm .removeMainView (viewPort );
203+ }
204+
182205 super .cleanup ();
183206 }
184207
@@ -413,4 +436,17 @@ public static interface DebugAppStateFilter {
413436 */
414437 public boolean displayObject (Object obj );
415438 }
416- }
439+
440+ private boolean isVr () {
441+ if (isVr == null ) {
442+ try {
443+ VRAppState vrAppState = app .getStateManager ().getState (VRAppState .ID , VRAppState .class );
444+ isVr = vrAppState != null && !vrAppState .DISABLE_VR ;
445+ } catch (NoClassDefFoundError e ) {
446+ //Vr isn't even on the classpath
447+ isVr = false ;
448+ }
449+ }
450+ return isVr ;
451+ }
452+ }
0 commit comments