text
stringlengths
0
1.11k
Control flow for rooms and child anchors
When you start without any prior knowledge for an environment, you will likely follow a specific flow to retrieve the contents of your Scene.
Find all anchors that have the component RoomLayout.
For each of these anchors, get the component AnchorContainer to access the room’s child anchors. Optionally, use the RoomLayout to access the ceiling, floor or walls directly.
Iterate over the child anchors, getting components whose data you are interested in retrieving.
If you want to localize the anchor, enable the locatable component. Then you can access its pose with respect to a camera, updating an object’s transform accordingly.
If you want to know the dimensions, query the 2D and/or 3D bounds and scale your Unity object accordingly.
In code, this looks as follows:
var anchors = new List<OVRAnchor>();
await OVRAnchor.FetchAnchorsAsync<OVRRoomLayout>(anchors);
// no rooms - call Space Setup or check Scene permission
if (anchors.Count == 0)
return;
// access anchor data by retrieving the components
var room = anchors.First();
// access the ceiling, floor and walls with the OVRRoomLayout component
var roomLayout = room.GetComponent<OVRRoomLayout>();
if (roomLayout.TryGetRoomLayout(out Guid ceiling, out Guid floor, out Guid[] walls))
{
// use these guids to fetch the OVRAnchor object directly
await OVRAnchor.FetchAnchorsAsync(walls, anchors);
}
// access the list of children with the OVRAnchorContainer component
if (!room.TryGetComponent(out OVRAnchorContainer container))
return;
// use the component helper function to access all child anchors
await container.FetchChildrenAsync(anchors);
Data components
As all data are stored in components, there is a 1 to 1 mapping between the components at the Scene data level and OVRAnchor. Not all anchors have all components, and so it is recommended to use OVRAnchor.TryGetComponent() to see if a certain anchor has the component in question.
OVRLocatable: controls the tracking of an anchor. Contains functions TryGetSceneAnchorPose() and TryGetSpatialAnchorPose() (see Spatial Anchors for more information).
OVRSemanticLabels: contains a list of all the semantic classification labels of an anchor.
OVRBounded2D: provides access to the bounding plane information of an anchor. Contains property BoundingBox and functions TryGetBoundaryPointsCount()/TryGetBoundaryPoints().
OVRBounded3D: provides access to the bounding volume information of an anchor. Contains property BoundingBox.
OVRTriangleMesh: provides access to triangle mesh information of an anchor. Contains functions TryGetCounts() and TryGetMesh().
OVRRoomLayout: provides access to the floor, ceiling and wall information of an anchor. An anchor only has this component when it is a room anchor. Contains functions FetchLayoutAnchorsAsync() and non-async TryGetRoomLayout().
OVRAnchorContainer: provides access to child anchors. This is most commonly used for room anchors, where the child anchors correspond to all the elements within a room. Contains function FetchChildrenAsync().
In the following code, you iterate over the room elements, finding the first floor anchor, match our transform to the anchors transform, and get the dimensions of the bounding plane.
foreach (var anchor in anchors)
{
// check that this anchor is the floor
if (!anchor.TryGetComponent(out OVRSemanticLabels labels) ||
!labels.Labels.Contains(OVRSceneManager.Classification.Floor)))
{
continue;
}
// enable locatable/tracking
if (!anchor.TryGetComponent(out OVRLocatable locatable))
continue;
await locatable.SetEnabledAsync(true);
// localize the anchor
locatable.TryGetSceneAnchorPose(out var pose);
this.transform.SetPositionAndRotation(
pose.ComputeWorldPosition(Camera.main),
pose.ComputeWorldRotation(Camera.main)
);
// get the floor dimensions
anchor.TryGetComponent(out OVRBounded2D bounded2D);
var size = bounded2D.BoundingBox.size;
// only interested in the first floor anchor
break;
}
In order to prevent pose transforms from affecting the raw component data, it is recommended to have a parent Unity game object that only applies the pose, and to have child Unity game objects for the plane, volume and mesh. This will allow you to use the component data on an object’s transform.
// we previously set this object's transform using the OVRLocatable pose
var parent = this.gameObject;
// create a child Unity game object
var plane = GameObject.CreatePrimitive(PrimitiveType.Cube);
plane.transform.SetParent(parent.transform, false);
// set the object transform to the bounds
anchor.TryGetComponent(out OVRBounded2D bounded2D);
plane.transform.localScale = new Vector3(
bounded2D.BoundingBox.size.x,
bounded2D.BoundingBox.size.y,
0.01f);
When to query the Scene data
The OVRSceneManager prefab will load data when its LoadSceneModel() function is called, which is commonly on app start. However, it is possible to query for Scene Model data at any point during the app’s lifecycle.
The Scene Model data changes when Space Setup/Scene Capture is invoked. An app may trigger this process, which will result in there being new Scene data to load. As the Space Setup process will pause an app, you can limit the query for new Scene data to when the OnAppPause method is called.
Next steps