Skip to content

Commit

Permalink
Merge pull request #25 from fieldsJacksonG/jacksonf/1.1.6
Browse files Browse the repository at this point in the history
Fix ASA anchor loading when using XR_MSFT_SPATIAL_ANCHOR_PERSISTENCE
  • Loading branch information
fieldsJacksonG committed Sep 1, 2021
2 parents 3d4aa7d + 033d243 commit e7a21e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.1.5",
"VersionName": "1.1.6",
"FriendlyName": "Microsoft OpenXR",
"Description": "The Microsoft OpenXR plugin is a game plugin which provides additional features available on Microsoft's Mixed Reality devices like the HoloLens 2 when using OpenXR.",
"Category": "Mixed Reality",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,9 @@ bool FAzureSpatialAnchorsForOpenXR::CreateARPinAroundAzureCloudSpatialAnchor(con
}

// First store the perception anchor in the anchor store.
// If a conflicting local anchor exists, remove it first.
UARBlueprintLibrary::RemoveARPinFromLocalStore(FName(InPinId.ToLower()));

if (!UMicrosoftOpenXRFunctionLibrary::StorePerceptionAnchor(InPinId.ToLower(), *(::IUnknown**)&localAnchor))
{
UE_LOG(LogHMD, Warning, TEXT("FAzureSpatialAnchorsForOpenXR::CreateARPinAroundAzureCloudSpatialAnchor failed to store anchor in local store."));
Expand All @@ -1241,12 +1244,20 @@ bool FAzureSpatialAnchorsForOpenXR::CreateARPinAroundAzureCloudSpatialAnchor(con
OutARPin = Pin.Value;

InAzureCloudSpatialAnchor->ARPin = OutARPin;
return OutARPin != nullptr;
break;
}
}

UE_LOG(LogHMD, Warning, TEXT("FAzureSpatialAnchorsForOpenXR::CreateARPinAroundAzureCloudSpatialAnchor failed to find anchor in local store."));
return false;
// Since this is a dummy ARPin, stop persisting it so future app launches don't load unnecessary pins.
UARBlueprintLibrary::RemoveARPinFromLocalStore(FName(InPinId.ToLower()));

if (OutARPin == nullptr)
{
UE_LOG(LogHMD, Warning, TEXT("FAzureSpatialAnchorsForOpenXR::CreateARPinAroundAzureCloudSpatialAnchor failed to find anchor in local store."));
return false;
}

return true;
}

IMPLEMENT_MODULE(FAzureSpatialAnchorsForOpenXR, AzureSpatialAnchorsForOpenXR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@ namespace MicrosoftOpenXR
SAnchorMSFT* AnchorMSFT = reinterpret_cast<SAnchorMSFT*>(nativeResource);

XrSpatialAnchorPersistenceInfoMSFT PersistenceInfo{ XR_TYPE_SPATIAL_ANCHOR_PERSISTENCE_INFO_MSFT };
strncpy_s(PersistenceInfo.spatialAnchorPersistenceName.name, TCHAR_TO_UTF8(*SaveId), SaveId.Len());

FTCHARToUTF8 UTF8ConvertedString(*SaveId);
memcpy(PersistenceInfo.spatialAnchorPersistenceName.name, UTF8ConvertedString.Get(), UTF8ConvertedString.Length());
PersistenceInfo.spatialAnchor = AnchorMSFT->Anchor;

XrResult result = xrPersistSpatialAnchorMSFT(SpatialAnchorStoreMSFT, &PersistenceInfo);
Expand Down Expand Up @@ -402,7 +404,9 @@ namespace MicrosoftOpenXR
{
const FString SaveId = InName.ToString().ToLower();
XrSpatialAnchorPersistenceNameMSFT SpatialAnchorPersistenceName;
strncpy_s(SpatialAnchorPersistenceName.name, TCHAR_TO_ANSI(*SaveId), SaveId.Len());

FTCHARToUTF8 UTF8ConvertedString(*SaveId);
memcpy(SpatialAnchorPersistenceName.name, UTF8ConvertedString.Get(), UTF8ConvertedString.Length());

xrUnpersistSpatialAnchorMSFT(SpatialAnchorStoreMSFT, &SpatialAnchorPersistenceName);
return;
Expand Down Expand Up @@ -464,6 +468,32 @@ namespace MicrosoftOpenXR
bool FSpatialAnchorPlugin::StorePerceptionAnchor(const FString& InPinId, ::IUnknown* InPerceptionAnchor)
{
#if WINRT_ANCHOR_STORE_AVAILABLE
if (!IsAnchorStoreReady())
{
return false;
}

if (bIsAnchorPersistenceExtensionSupported)
{
XrSpatialAnchorMSFT Anchor = {};
if (XR_FAILED(xrCreateSpatialAnchorFromPerceptionAnchorMSFT(Session, InPerceptionAnchor, &Anchor)))
{
return false;
}

SAnchorMSFT* AnchorMSFT = new SAnchorMSFT;
AnchorMSFT->Anchor = Anchor;

XrSpatialAnchorPersistenceInfoMSFT PersistenceInfo{ XR_TYPE_SPATIAL_ANCHOR_PERSISTENCE_INFO_MSFT };

FTCHARToUTF8 UTF8ConvertedString(*InPinId.ToLower());
memcpy(PersistenceInfo.spatialAnchorPersistenceName.name, UTF8ConvertedString.Get(), UTF8ConvertedString.Length());

PersistenceInfo.spatialAnchor = AnchorMSFT->Anchor;

return XR_SUCCEEDED(xrPersistSpatialAnchorMSFT(SpatialAnchorStoreMSFT, &PersistenceInfo));
}

std::lock_guard<std::mutex> lock(m_spatialAnchorStoreLock);
if (m_spatialAnchorStore == nullptr)
{
Expand Down

0 comments on commit e7a21e8

Please sign in to comment.