Initial Commit

This commit is contained in:
2025-07-04 20:33:06 +03:00
commit 8f09347ae0
9219 changed files with 2447903 additions and 0 deletions
@@ -0,0 +1,350 @@
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
using System;
using System.Collections;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.Serialization;
using System.Text;
/// <summary>
/// Specific functionality for spawned anchors
/// </summary>
[RequireComponent(typeof(OVRSpatialAnchor))]
public class Anchor : MonoBehaviour
{
public const string NumUuidsPlayerPref = "numUuids";
[SerializeField, FormerlySerializedAs("canvas_")]
private Canvas _canvas;
[SerializeField, FormerlySerializedAs("pivot_")]
private Transform _pivot;
[SerializeField, FormerlySerializedAs("anchorMenu_")]
private GameObject _anchorMenu;
private bool _isSelected;
private bool _isHovered;
[SerializeField, FormerlySerializedAs("anchorName_")]
private TextMeshProUGUI _anchorName;
[SerializeField, FormerlySerializedAs("saveIcon_")]
private GameObject _saveIcon;
[SerializeField, FormerlySerializedAs("labelImage_")]
private Image _labelImage;
[SerializeField, FormerlySerializedAs("labelBaseColor_")]
private Color _labelBaseColor;
[SerializeField, FormerlySerializedAs("labelHighlightColor_")]
private Color _labelHighlightColor;
[SerializeField, FormerlySerializedAs("labelSelectedColor_")]
private Color _labelSelectedColor;
[SerializeField, FormerlySerializedAs("uiManager_")]
private AnchorUIManager _uiManager;
[SerializeField, FormerlySerializedAs("renderers_")]
private MeshRenderer[] _renderers;
private int _menuIndex = 0;
[SerializeField, FormerlySerializedAs("buttonList_")]
private List<Button> _buttonList;
private Button _selectedButton;
private OVRSpatialAnchor _spatialAnchor;
private GameObject _icon;
#region Monobehaviour Methods
private void Awake()
{
_anchorMenu.SetActive(false);
_renderers = GetComponentsInChildren<MeshRenderer>();
_canvas.worldCamera = Camera.main;
_selectedButton = _buttonList[0];
_selectedButton.OnSelect(null);
_spatialAnchor = GetComponent<OVRSpatialAnchor>();
_icon = GetComponent<Transform>().FindChildRecursive("Sphere").gameObject;
}
static string ConvertUuidToString(System.Guid guid)
{
var value = guid.ToByteArray();
StringBuilder hex = new StringBuilder(value.Length * 2 + 4);
for (int ii = 0; ii < value.Length; ++ii)
{
if (3 < ii && ii < 11 && ii % 2 == 0)
{
hex.Append("-");
}
hex.AppendFormat("{0:x2}", value[ii]);
}
return hex.ToString();
}
private IEnumerator Start()
{
while (_spatialAnchor && !_spatialAnchor.Created)
{
yield return null;
}
if (_spatialAnchor)
{
_anchorName.text = ConvertUuidToString(_spatialAnchor.Uuid);
}
else
{
// Creation must have failed
Destroy(gameObject);
}
}
private void Update()
{
// Billboard the boundary
BillboardPanel(_canvas.transform);
// Billboard the menu
BillboardPanel(_pivot);
HandleMenuNavigation();
//Billboard the icon
BillboardPanel(_icon.transform);
}
#endregion // MonoBehaviour Methods
#region UI Event Listeners
/// <summary>
/// UI callback for the anchor menu's Save button
/// </summary>
public void OnSaveLocalButtonPressed()
{
if (!_spatialAnchor) return;
_spatialAnchor.Save((anchor, success) =>
{
if (!success) return;
// Enables save icon on the menu
ShowSaveIcon = true;
SaveUuidToPlayerPrefs(anchor.Uuid);
});
}
void SaveUuidToPlayerPrefs(Guid uuid)
{
// Write uuid of saved anchor to file
if (!PlayerPrefs.HasKey(NumUuidsPlayerPref))
{
PlayerPrefs.SetInt(NumUuidsPlayerPref, 0);
}
int playerNumUuids = PlayerPrefs.GetInt(NumUuidsPlayerPref);
PlayerPrefs.SetString("uuid" + playerNumUuids, uuid.ToString());
PlayerPrefs.SetInt(NumUuidsPlayerPref, ++playerNumUuids);
}
/// <summary>
/// UI callback for the anchor menu's Hide button
/// </summary>
public void OnHideButtonPressed()
{
Destroy(gameObject);
}
/// <summary>
/// UI callback for the anchor menu's Erase button
/// </summary>
public void OnEraseButtonPressed()
{
if (!_spatialAnchor) return;
_spatialAnchor.Erase((anchor, success) =>
{
if (success)
{
_saveIcon.SetActive(false);
}
});
}
#endregion // UI Event Listeners
#region Public Methods
public bool ShowSaveIcon
{
set => _saveIcon.SetActive(value);
}
/// <summary>
/// Handles interaction when anchor is hovered
/// </summary>
public void OnHoverStart()
{
if (_isHovered)
{
return;
}
_isHovered = true;
// Yellow highlight
foreach (MeshRenderer renderer in _renderers)
{
renderer.material.SetColor("_EmissionColor", Color.yellow);
}
_labelImage.color = _labelHighlightColor;
}
/// <summary>
/// Handles interaction when anchor is no longer hovered
/// </summary>
public void OnHoverEnd()
{
if (!_isHovered)
{
return;
}
_isHovered = false;
// Go back to normal
foreach (MeshRenderer renderer in _renderers)
{
renderer.material.SetColor("_EmissionColor", Color.clear);
}
if (_isSelected)
{
_labelImage.color = _labelSelectedColor;
}
else
{
_labelImage.color = _labelBaseColor;
}
}
/// <summary>
/// Handles interaction when anchor is selected
/// </summary>
public void OnSelect()
{
if (_isSelected)
{
// Hide Anchor menu on deselect
_anchorMenu.SetActive(false);
_isSelected = false;
_selectedButton = null;
if (_isHovered)
{
_labelImage.color = _labelHighlightColor;
}
else
{
_labelImage.color = _labelBaseColor;
}
}
else
{
// Show Anchor Menu on select
_anchorMenu.SetActive(true);
_isSelected = true;
_menuIndex = -1;
NavigateToIndexInMenu(true);
if (_isHovered)
{
_labelImage.color = _labelHighlightColor;
}
else
{
_labelImage.color = _labelSelectedColor;
}
}
}
#endregion // Public Methods
#region Private Methods
private void BillboardPanel(Transform panel)
{
// The z axis of the panel faces away from the side that is rendered, therefore this code is actually looking away from the camera
panel.LookAt(
new Vector3(panel.position.x * 2 - Camera.main.transform.position.x,
panel.position.y * 2 - Camera.main.transform.position.y,
panel.position.z * 2 - Camera.main.transform.position.z), Vector3.up);
}
private void HandleMenuNavigation()
{
if (!_isSelected)
{
return;
}
if (OVRInput.GetDown(OVRInput.RawButton.RThumbstickUp))
{
NavigateToIndexInMenu(false);
}
if (OVRInput.GetDown(OVRInput.RawButton.RThumbstickDown))
{
NavigateToIndexInMenu(true);
}
if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger))
{
_selectedButton.OnSubmit(null);
}
}
private void NavigateToIndexInMenu(bool moveNext)
{
if (moveNext)
{
_menuIndex++;
if (_menuIndex > _buttonList.Count - 1)
{
_menuIndex = 0;
}
}
else
{
_menuIndex--;
if (_menuIndex < 0)
{
_menuIndex = _buttonList.Count - 1;
}
}
if (_selectedButton)
{
_selectedButton.OnDeselect(null);
}
_selectedButton = _buttonList[_menuIndex];
_selectedButton.OnSelect(null);
}
#endregion // Private Methods
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 41d1cb331bd351f4d967860f51af129e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
namespace Oculus.Deprecated
{
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0add13ce028c7d94c9a6ef8369573e0b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
namespace Oculus.Deprecated
{
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 41eaaa4b02ccec843a6585b66e32ff1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
namespace Oculus.Deprecated
{
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: eb5a075e3decb0d49a881c4b110ec41b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,348 @@
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
/// <summary>
/// Manages UI of anchor sample.
/// </summary>
[RequireComponent(typeof(SpatialAnchorLoader))]
public class AnchorUIManager : MonoBehaviour
{
/// <summary>
/// Anchor UI manager singleton instance
/// </summary>
public static AnchorUIManager Instance;
/// <summary>
/// Anchor Mode switches between create and select
/// </summary>
public enum AnchorMode
{
Create,
Select
};
[SerializeField, FormerlySerializedAs("createModeButton_")]
private GameObject _createModeButton;
[SerializeField, FormerlySerializedAs("selectModeButton_")]
private GameObject _selectModeButton;
[SerializeField, FormerlySerializedAs("trackedDevice_")]
private Transform _trackedDevice;
private Transform _raycastOrigin;
private bool _drawRaycast = false;
[SerializeField, FormerlySerializedAs("lineRenderer_")]
private LineRenderer _lineRenderer;
private Anchor _hoveredAnchor;
private Anchor _selectedAnchor;
private AnchorMode _mode = AnchorMode.Select;
[SerializeField, FormerlySerializedAs("buttonList_")]
private List<Button> _buttonList;
private int _menuIndex = 0;
private Button _selectedButton;
[SerializeField]
private Anchor _anchorPrefab;
public Anchor AnchorPrefab => _anchorPrefab;
[SerializeField, FormerlySerializedAs("placementPreview_")]
private GameObject _placementPreview;
[SerializeField, FormerlySerializedAs("anchorPlacementTransform_")]
private Transform _anchorPlacementTransform;
private delegate void PrimaryPressDelegate();
private PrimaryPressDelegate _primaryPressDelegate;
private bool _isFocused = true;
#region Monobehaviour Methods
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this);
}
}
private void Start()
{
_raycastOrigin = _trackedDevice;
// Start in select mode
_mode = AnchorMode.Select;
StartSelectMode();
_menuIndex = 0;
_selectedButton = _buttonList[0];
_selectedButton.OnSelect(null);
_lineRenderer.startWidth = 0.005f;
_lineRenderer.endWidth = 0.005f;
}
private void Update()
{
if (_drawRaycast)
{
ControllerRaycast();
}
if (_selectedAnchor == null)
{
// Refocus menu
_selectedButton.OnSelect(null);
_isFocused = true;
}
HandleMenuNavigation();
if (OVRInput.GetDown(OVRInput.RawButton.A))
{
_primaryPressDelegate?.Invoke();
}
}
#endregion // Monobehaviour Methods
#region Menu UI Callbacks
/// <summary>
/// Create mode button pressed UI callback. Referenced by the Create button in the menu.
/// </summary>
public void OnCreateModeButtonPressed()
{
ToggleCreateMode();
_createModeButton.SetActive(!_createModeButton.activeSelf);
_selectModeButton.SetActive(!_selectModeButton.activeSelf);
}
/// <summary>
/// Load anchors button pressed UI callback. Referenced by the Load Anchors button in the menu.
/// </summary>
public void OnLoadAnchorsButtonPressed()
{
GetComponent<SpatialAnchorLoader>().LoadAnchorsByUuid();
}
#endregion // Menu UI Callbacks
#region Mode Handling
private void ToggleCreateMode()
{
if (_mode == AnchorMode.Select)
{
_mode = AnchorMode.Create;
EndSelectMode();
StartPlacementMode();
}
else
{
_mode = AnchorMode.Select;
EndPlacementMode();
StartSelectMode();
}
}
private void StartPlacementMode()
{
ShowAnchorPreview();
_primaryPressDelegate = PlaceAnchor;
}
private void EndPlacementMode()
{
HideAnchorPreview();
_primaryPressDelegate = null;
}
private void StartSelectMode()
{
ShowRaycastLine();
_primaryPressDelegate = SelectAnchor;
}
private void EndSelectMode()
{
HideRaycastLine();
_primaryPressDelegate = null;
}
#endregion // Mode Handling
#region Private Methods
private void HandleMenuNavigation()
{
if (!_isFocused)
{
return;
}
if (OVRInput.GetDown(OVRInput.RawButton.RThumbstickUp))
{
NavigateToIndexInMenu(false);
}
if (OVRInput.GetDown(OVRInput.RawButton.RThumbstickDown))
{
NavigateToIndexInMenu(true);
}
if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger))
{
_selectedButton.OnSubmit(null);
}
}
private void NavigateToIndexInMenu(bool moveNext)
{
if (moveNext)
{
_menuIndex++;
if (_menuIndex > _buttonList.Count - 1)
{
_menuIndex = 0;
}
}
else
{
_menuIndex--;
if (_menuIndex < 0)
{
_menuIndex = _buttonList.Count - 1;
}
}
_selectedButton.OnDeselect(null);
_selectedButton = _buttonList[_menuIndex];
_selectedButton.OnSelect(null);
}
private void ShowAnchorPreview()
{
_placementPreview.SetActive(true);
}
private void HideAnchorPreview()
{
_placementPreview.SetActive(false);
}
private void PlaceAnchor()
{
Instantiate(_anchorPrefab, _anchorPlacementTransform.position, _anchorPlacementTransform.rotation);
}
private void ShowRaycastLine()
{
_drawRaycast = true;
_lineRenderer.gameObject.SetActive(true);
}
private void HideRaycastLine()
{
_drawRaycast = false;
_lineRenderer.gameObject.SetActive(false);
}
private void ControllerRaycast()
{
Ray ray = new Ray(_raycastOrigin.position, _raycastOrigin.TransformDirection(Vector3.forward));
_lineRenderer.SetPosition(0, _raycastOrigin.position);
_lineRenderer.SetPosition(1,
_raycastOrigin.position + _raycastOrigin.TransformDirection(Vector3.forward) * 10f);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
Anchor anchorObject = hit.collider.GetComponent<Anchor>();
if (anchorObject != null)
{
_lineRenderer.SetPosition(1, hit.point);
HoverAnchor(anchorObject);
return;
}
}
UnhoverAnchor();
}
private void HoverAnchor(Anchor anchor)
{
_hoveredAnchor = anchor;
_hoveredAnchor.OnHoverStart();
}
private void UnhoverAnchor()
{
if (_hoveredAnchor == null)
{
return;
}
_hoveredAnchor.OnHoverEnd();
_hoveredAnchor = null;
}
private void SelectAnchor()
{
if (_hoveredAnchor != null)
{
if (_selectedAnchor != null)
{
// Deselect previous Anchor
_selectedAnchor.OnSelect();
_selectedAnchor = null;
}
// Select new Anchor
_selectedAnchor = _hoveredAnchor;
_selectedAnchor.OnSelect();
// Defocus menu
_selectedButton.OnDeselect(null);
_isFocused = false;
}
else
{
if (_selectedAnchor != null)
{
// Deselect previous Anchor
_selectedAnchor.OnSelect();
_selectedAnchor = null;
// Refocus menu
_selectedButton.OnSelect(null);
_isFocused = true;
}
}
}
#endregion // Private Methods
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9735d0f6df960334585926b5295e7d18
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,101 @@
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
using System;
using UnityEngine;
/// <summary>
/// Demonstrates loading existing spatial anchors from storage.
/// </summary>
/// <remarks>
/// Loading existing anchors involves two asynchronous methods:
/// 1. Call <see cref="OVRSpatialAnchor.LoadUnboundAnchors"/>
/// 2. For each unbound anchor you wish to localize, invoke <see cref="OVRSpatialAnchor.UnboundAnchor.Localize"/>.
/// 3. Once localized, your callback will receive an <see cref="OVRSpatialAnchor.UnboundAnchor"/>. Instantiate an
/// <see cref="OVRSpatialAnchor"/> component and bind it to the `UnboundAnchor` by calling
/// <see cref="OVRSpatialAnchor.UnboundAnchor.BindTo"/>.
/// </remarks>
public class SpatialAnchorLoader : MonoBehaviour
{
[SerializeField]
OVRSpatialAnchor _anchorPrefab;
Action<OVRSpatialAnchor.UnboundAnchor, bool> _onLoadAnchor;
public void LoadAnchorsByUuid()
{
// Get number of saved anchor uuids
if (!PlayerPrefs.HasKey(Anchor.NumUuidsPlayerPref))
{
PlayerPrefs.SetInt(Anchor.NumUuidsPlayerPref, 0);
}
var playerUuidCount = PlayerPrefs.GetInt(Anchor.NumUuidsPlayerPref);
Log($"Attempting to load {playerUuidCount} saved anchors.");
if (playerUuidCount == 0)
return;
var uuids = new Guid[playerUuidCount];
for (int i = 0; i < playerUuidCount; ++i)
{
var uuidKey = "uuid" + i;
var currentUuid = PlayerPrefs.GetString(uuidKey);
Log("QueryAnchorByUuid: " + currentUuid);
uuids[i] = new Guid(currentUuid);
}
Load(new OVRSpatialAnchor.LoadOptions
{
Timeout = 0,
StorageLocation = OVRSpace.StorageLocation.Local,
Uuids = uuids
});
}
private void Awake()
{
_onLoadAnchor = OnLocalized;
}
private void Load(OVRSpatialAnchor.LoadOptions options) => OVRSpatialAnchor.LoadUnboundAnchors(options, anchors =>
{
if (anchors == null)
{
Log("Query failed.");
return;
}
foreach (var anchor in anchors)
{
if (anchor.Localized)
{
_onLoadAnchor(anchor, true);
}
else if (!anchor.Localizing)
{
anchor.Localize(_onLoadAnchor);
}
}
});
private void OnLocalized(OVRSpatialAnchor.UnboundAnchor unboundAnchor, bool success)
{
if (!success)
{
Log($"{unboundAnchor} Localization failed!");
return;
}
var pose = unboundAnchor.Pose;
var spatialAnchor = Instantiate(_anchorPrefab, pose.position, pose.rotation);
unboundAnchor.BindTo(spatialAnchor);
if (spatialAnchor.TryGetComponent<Anchor>(out var anchor))
{
// We just loaded it, so we know it exists in persistent storage.
anchor.ShowSaveIcon = true;
}
}
private static void Log(string message) => Debug.Log($"[SpatialAnchorsUnity]: {message}");
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 432567d157dd9b1478a34a3d58ae077c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
namespace Oculus.Deprecated
{
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aed24f56a57860947a021f1c69dcbb19
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: