Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from fieldsJacksonG/main
Browse files Browse the repository at this point in the history
1.1.3 Update

Camera nullcheck without ARSession
Remove stale _DEBUG code in ASA module
Fix spatial mapping update logic to minimize mesh recreation and fix hitches caused by unnecessary physics baking.
  • Loading branch information
fieldsJacksonG authored Apr 26, 2021
2 parents fbf428b + dfe05fa commit 817e601
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.1.2",
"VersionName": "1.1.3",
"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": "Augmented Reality",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,18 +702,10 @@ EAzureSpatialAnchorsResult FAzureSpatialAnchorsForOpenXR::GetActiveWatchers(TArr

OutWatcherIDs.Reset(Watchers.Size());
int32_t Index = 0;
#if _DEBUG
// only lock and test the assert below in debug.
auto lock = std::unique_lock<std::mutex>(m_watcherMapMutex);
#endif

for (uint32_t i = 0; i < Watchers.Size(); ++i)
{
OutWatcherIDs.Add(Watchers.GetAt(i).Identifier());

// All watchers should be in the map.
#if _DEBUG
assert(m_watcherMap.find(ID) != m_watcherMap.end());
#endif
}

return EAzureSpatialAnchorsResult::Success;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ namespace MicrosoftOpenXR

UARTexture* FLocatableCamPlugin::OnGetARTexture(EARTextureType TextureType) const
{
if (TextureType == EARTextureType::CameraImage)
if (SharedTextureHolder && TextureType == EARTextureType::CameraImage)
{
return SharedTextureHolder->CameraImage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace MicrosoftOpenXR
if (FindMeshTransform(data.second->AnchorSpace, DisplayTime, TrackingSpace, MeshToCachedAnchorTransform, Transform, IsTracking))
{
// Add to the anchor localization data, so the next time this mesh is updated it will have a direct entry in the map.
AnchorLocalizationData.insert({ MeshID, data.second });
AnchorLocalizationData.insert_or_assign(MeshID, data.second);
return true;
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ namespace MicrosoftOpenXR
if (cachedAnchorDataForThisMesh == AnchorLocalizationData.end())
{
// Cache localization data to compare against future meshes.
AnchorLocalizationData.insert({ MeshID, MakeShared<WMRAnchorLocalizationData>(AnchorSpace, MeshCoordinateSystem) });
AnchorLocalizationData.insert_or_assign(MeshID, MakeShared<WMRAnchorLocalizationData>(AnchorSpace, MeshCoordinateSystem));
}
else
{
Expand Down Expand Up @@ -486,6 +486,17 @@ namespace MicrosoftOpenXR
FGuid MeshId = WMRUtility::GUIDToFGuid(Id);
ObservedSurfacesThisUpdate.push_back(MeshId);
SpatialSurfaceInfo SurfaceInfo = KVPair.Value();
winrt::Windows::Foundation::DateTime UpdateTime = SurfaceInfo.UpdateTime();

const auto& PrevMeshIterator = UniqueMeshes.find(MeshId);
bool bIsAdd = PrevMeshIterator == UniqueMeshes.end();
bool bIsUpdate = !bIsAdd && PrevMeshIterator->second.UpdateTime != UpdateTime;

// This is an old mesh with no new data.
if (!bIsAdd && !bIsUpdate)
{
continue;
}

winrt::Windows::Foundation::IAsyncOperation<SpatialSurfaceMesh> ComputeMeshAsyncOperation =
SurfaceInfo.TryComputeLatestMeshAsync(TriangleDensity, MeshOptions);
Expand All @@ -494,52 +505,50 @@ namespace MicrosoftOpenXR
continue;
}

ComputeMeshAsyncOperation.Completed([this, MeshId](winrt::Windows::Foundation::IAsyncOperation<SpatialSurfaceMesh> asyncOperation, winrt::Windows::Foundation::AsyncStatus status)
ComputeMeshAsyncOperation.Completed([this, MeshId, UpdateTime, bIsUpdate, PrevMeshIterator](winrt::Windows::Foundation::IAsyncOperation<SpatialSurfaceMesh> asyncOperation, winrt::Windows::Foundation::AsyncStatus status)
{
if (asyncOperation.Status() == winrt::Windows::Foundation::AsyncStatus::Completed)
{
auto SurfaceMesh = asyncOperation.GetResults();
if (SurfaceMesh != nullptr)
{
TrackedMeshHolder->StartMeshUpdates();


std::lock_guard<std::mutex> lock(MeshRefsLock);

FOpenXRMeshUpdate* MeshUpdate = TrackedMeshHolder->AllocateMeshUpdate(MeshId);
MeshUpdate->Type = EARObjectClassification::World;
CopyMeshData(MeshUpdate, SurfaceMesh);

const auto& it = UniqueMeshes.find(MeshId);
if (it != UniqueMeshes.end())
if (bIsUpdate)
{
// If a mesh entry already existed for this spatial mesh, use the last known transform for the first update to keep it close to where it was previously.
MeshUpdate->LocalToTrackingTransform = it->second.LastKnownTransform;
MeshUpdate->TrackingState = it->second.LastKnownTrackingState;
MeshUpdate->LocalToTrackingTransform = PrevMeshIterator->second.LastKnownTransform;
MeshUpdate->TrackingState = PrevMeshIterator->second.LastKnownTrackingState;

// Update the cached mesh data
std::lock_guard<std::mutex> lock(MeshRefsLock);
it->second.CoordinateSystem = SurfaceMesh.CoordinateSystem();
it->second.CollisionInfo.UpdateVertices(MeshUpdate->Vertices, MeshUpdate->Indices);
PrevMeshIterator->second.CoordinateSystem = SurfaceMesh.CoordinateSystem();
PrevMeshIterator->second.CollisionInfo.UpdateVertices(MeshUpdate->Vertices, MeshUpdate->Indices);
}
else
{
// This is the first time observing this mesh
std::lock_guard<std::mutex> lock(MeshRefsLock);
// Guarantee the mesh is not seen until a valid transform has been found.
// If this is a new mesh, guarantee the mesh is not seen until a valid transform has been found.
FTransform TempTransform = FTransform::Identity;
TempTransform.SetScale3D(FVector::ZeroVector);

// Don't set the tracking state until the LocalToTrackingTransform is identified in UpdateDeviceLocations.
MeshUpdate->TrackingState = EARTrackingState::NotTracking;

UniqueMeshes.insert({ MeshId, MeshLocalizationData {
TempTransform,
EARTrackingState::NotTracking,
SurfaceMesh.CoordinateSystem(),
TrackedGeometryCollision(MeshUpdate->Vertices, MeshUpdate->Indices)
} });

// Don't set the tracking state until the LocalToTrackingTransform is identified in UpdateDeviceLocations.
MeshUpdate->LocalToTrackingTransform = TempTransform;
MeshUpdate->TrackingState = EARTrackingState::NotTracking;
}

UniqueMeshes.insert_or_assign(MeshId, MeshLocalizationData{
UpdateTime,
MeshUpdate->LocalToTrackingTransform,
MeshUpdate->TrackingState,
SurfaceMesh.CoordinateSystem(),
TrackedGeometryCollision(MeshUpdate->Vertices, MeshUpdate->Indices)
});

TrackedMeshHolder->EndMeshUpdates();
}
}
Expand All @@ -554,7 +563,7 @@ namespace MicrosoftOpenXR
std::vector<FGuid> MeshesToRemove = std::vector<FGuid>();
for (const auto& Mesh : UniqueMeshes)
{
if (std::find(ObservedSurfacesThisUpdate.begin(),
if (std::find(ObservedSurfacesThisUpdate.begin(),
ObservedSurfacesThisUpdate.end(), Mesh.first) == ObservedSurfacesThisUpdate.end())
{
// Cached mesh was not found, it has been disposed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace MicrosoftOpenXR

struct MeshLocalizationData
{
winrt::Windows::Foundation::DateTime UpdateTime = winrt::Windows::Foundation::DateTime::min();
FTransform LastKnownTransform = FTransform::Identity;
EARTrackingState LastKnownTrackingState = EARTrackingState::NotTracking;
winrt::Windows::Perception::Spatial::SpatialCoordinateSystem CoordinateSystem;
Expand Down

0 comments on commit 817e601

Please sign in to comment.