Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions android/sharedCode/src/main/cpp/VROSceneRendererARCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,33 @@ void VROSceneRendererARCore::renderFrame() {

void VROSceneRendererARCore::updateARBackground(std::unique_ptr<VROARFrame> &frame,
bool forceReset) {
// Only update the rendered camera background if need be.
if (!forceReset && !((VROARFrameARCore *) frame.get())->hasDisplayGeometryChanged()) {
return;
// Check if occlusion mode has changed - if so, force update
VROOcclusionMode currentOcclusionMode = _session->getOcclusionMode();
bool occlusionModeChanged = (currentOcclusionMode != _lastOcclusionMode);
if (occlusionModeChanged) {
_lastOcclusionMode = currentOcclusionMode;
forceReset = true;
}

VROVector3f BL, BR, TL, TR;
((VROARFrameARCore *)frame.get())->getBackgroundTexcoords(&BL, &BR, &TL, &TR);
bool geometryChanged = ((VROARFrameARCore *) frame.get())->hasDisplayGeometryChanged();

// Only update texture coordinates if geometry changed or forced
if (forceReset || geometryChanged) {
VROVector3f BL, BR, TL, TR;
((VROARFrameARCore *)frame.get())->getBackgroundTexcoords(&BL, &BR, &TL, &TR);

_cameraBackground->setTextureCoordinates(BL, BR, TL, TR);
_cameraBackground->setTextureCoordinates(BL, BR, TL, TR);

// Wait until we have these proper texture coordinates before installing the background
if (!_sceneController->getScene()->getRootNode()->getBackground()) {
_sceneController->getScene()->getRootNode()->setBackground(_cameraBackground);
// Wait until we have these proper texture coordinates before installing the background
if (!_sceneController->getScene()->getRootNode()->getBackground()) {
_sceneController->getScene()->getRootNode()->setBackground(_cameraBackground);
}
}

// Update occlusion settings on the camera background
updateBackgroundOcclusion(frame);
// Always update occlusion settings if occlusion mode changed, even if geometry didn't change
if (occlusionModeChanged || forceReset || geometryChanged) {
updateBackgroundOcclusion(frame);
}
}

void VROSceneRendererARCore::updateBackgroundOcclusion(std::unique_ptr<VROARFrame> &frame) {
Expand Down
1 change: 1 addition & 0 deletions android/sharedCode/src/main/cpp/VROSceneRendererARCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class VROSceneRendererARCore : public VROSceneRenderer, public std::enable_share

std::shared_ptr<VROSurface> _cameraBackground;
bool _occlusionModifierAdded = false;
VROOcclusionMode _lastOcclusionMode = VROOcclusionMode::Disabled;
gvr::Sizei _surfaceSize;
bool _arcoreInstalled;
bool _destroyed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,18 +906,25 @@ VRO_METHOD(void, nativeResolveCloudAnchor)(VRO_ARGS
VRO_METHOD(void, nativeSetOcclusionMode)(VRO_ARGS
VRO_REF(VROARSceneController) sceneController_j,
VRO_INT mode) {
pinfo("[OCCLUSION JNI] nativeSetOcclusionMode called with mode: %d", mode);

std::weak_ptr<VROARScene> scene_w = std::dynamic_pointer_cast<VROARScene>(
VRO_REF_GET(VROARSceneController, sceneController_j)->getScene());

VROPlatformDispatchAsyncRenderer([scene_w, mode] {
std::shared_ptr<VROARScene> scene = scene_w.lock();
if (!scene) {
pinfo("[OCCLUSION JNI] Scene is null, cannot set occlusion mode");
return;
}
pinfo("[OCCLUSION JNI] Scene valid, getting AR session");
std::shared_ptr<VROARSession> session = scene->getARSession();
if (session) {
pinfo("[OCCLUSION JNI] Session valid, calling setOcclusionMode with mode %d", mode);
VROOcclusionMode occlusionMode = static_cast<VROOcclusionMode>(mode);
session->setOcclusionMode(occlusionMode);
} else {
pinfo("[OCCLUSION JNI] Session is null, cannot set occlusion mode");
}
});
}
Expand Down
15 changes: 11 additions & 4 deletions android/sharedCode/src/main/cpp/arcore/VROARSessionARCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ VROARSessionARCore::VROARSessionARCore(std::shared_ptr<VRODriverOpenGL> driver)
_updateMode(arcore::UpdateMode::Blocking),
_cloudAnchorMode(arcore::CloudAnchorMode::Enabled),
_focusMode(arcore::FocusMode::FIXED_FOCUS),
_depthMode(arcore::DepthMode::Automatic),
_depthMode(arcore::DepthMode::Disabled),
_semanticMode(arcore::SemanticMode::Disabled),
_geospatialMode(arcore::GeospatialMode::Disabled),
_cameraTextureId(0),
Expand Down Expand Up @@ -1255,9 +1255,16 @@ void VROARSessionARCore::setOcclusionMode(VROOcclusionMode mode) {

if (newDepthMode != _depthMode) {
_depthMode = newDepthMode;
updateARCoreConfig();
pinfo("VROARSessionARCore: Occlusion mode set to %d, depth mode set to %d",
(int)mode, (int)_depthMode);

// Only update config if session is ready
if (_session != nullptr) {
updateARCoreConfig();
pinfo("VROARSessionARCore: Occlusion mode set to %d, depth mode set to %d",
(int)mode, (int)_depthMode);
} else {
pinfo("VROARSessionARCore: Occlusion mode will be applied when session is ready (mode=%d, depth=%d)",
(int)mode, (int)_depthMode);
}
}
}

Expand Down
29 changes: 26 additions & 3 deletions ios/ViroKit/VROARSessioniOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ VROARSessioniOS::VROARSessioniOS(VROTrackingType trackingType,
_sessionPaused(true),
_monocularDepthEnabled(false),
_preferMonocularDepth(false),
_monocularDepthLoading(false),
_monocularDepthLoading(false),
_needsGeospatialModeApply(false),
_driver(driver) {

if (@available(iOS 11.0, *)) {
Expand Down Expand Up @@ -204,6 +205,13 @@ void VROARSessioniOS::run() {
_session.delegate = _delegateAR;

[_session runWithConfiguration:_sessionConfiguration];

// Apply pending geospatial mode if it was set before session started
if (_needsGeospatialModeApply && _cloudAnchorProviderARCore) {
pinfo("Applying pending geospatial mode after session start");
[_cloudAnchorProviderARCore setGeospatialModeEnabled:YES];
_needsGeospatialModeApply = false;
}
}

void VROARSessioniOS::pause() {
Expand Down Expand Up @@ -1306,10 +1314,25 @@ void VROARSessioniOS::setGeospatialAnchorProvider(VROGeospatialAnchorProvider pr
}
}

// Enable geospatial mode
// Defer geospatial mode activation if session is paused (not yet started)
// This prevents crashes from accessing ARKit session before it's ready
if (_sessionPaused || _session == nil) {
pinfo("ARSession not running yet, geospatial mode will be enabled when session starts");
_needsGeospatialModeApply = true;
} else {
// Session is running, enable geospatial mode immediately
if (_cloudAnchorProviderARCore) {
[_cloudAnchorProviderARCore setGeospatialModeEnabled:YES];
pinfo("ARCore Geospatial mode enabled");
}
}
} else {
// Disable geospatial mode
if (_cloudAnchorProviderARCore) {
[_cloudAnchorProviderARCore setGeospatialModeEnabled:YES];
[_cloudAnchorProviderARCore setGeospatialModeEnabled:NO];
pinfo("ARCore Geospatial mode disabled");
}
_needsGeospatialModeApply = false;
}
}

Expand Down
1 change: 1 addition & 0 deletions ios/ViroKit/VROARSessioniOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class API_AVAILABLE(ios(12.0)) VROARSessioniOS : public VROARSession, public std
The ARCore cloud anchor provider instance (for iOS using ARCore SDK).
*/
VROCloudAnchorProviderARCore *_cloudAnchorProviderARCore = nil;
bool _needsGeospatialModeApply = false;

/*
The last computed ARFrame.
Expand Down
Loading