Newer
Older
lacoche
committed
using GLTFast;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
public class SceneManagementGLTF : MonoBehaviour
{
/// <summary>
/// Path to GLTF File (relative to streaming assets in editor, else persistentdatapath)
/// </summary>
public string _PathToGLTF;
/// <summary>
/// Validity in milliseconds for a subscription to the WA
/// </summary>
public int _ValiditySubscription = 100000;
lacoche
committed
/// <summary>
/// List of trackables and anchors in the AR Scene
/// </summary>
Sylvain Buche
committed
private Dictionary<Guid, Transform> m_trackablesAndAnchorsInARScene;
lacoche
committed
/// <summary>
/// Unity Start Method
/// </summary>
protected async void Start()
{
Sylvain Buche
committed
m_trackablesAndAnchorsInARScene = new Dictionary<Guid, Transform>();
lacoche
committed
await LoadGltfBinaryFromMemory();
Transform loaded = this.transform.GetChild(0);
FindWorldStorageTransform(loaded);
Sylvain Buche
committed
foreach(KeyValuePair<Guid , Transform> toSubscribe in m_trackablesAndAnchorsInARScene)
lacoche
committed
{
int validity = _ValiditySubscription;
Sylvain Buche
committed
Guid subscriptionUUID;
lacoche
committed
// TODO : if only one : subscribeToPose, if multiple subscribetoPoses
WorldAnalysisInterface.Instance.SubscribeToPose(null, toSubscribe.Key, ETSI.ARF.OpenAPI.WorldAnalysis.Mode_WorldAnalysis.DEVICE_TO_TRACKABLES, PoseCallback, ref validity, out subscriptionUUID); //TODO : find a value for the token parameter.
lacoche
committed
}
}
/// <summary>
/// The callback function used in the susbscription method. It instantiate a prefab or update its pose based on the current situation.
/// </summary>
/// <param name="result"></param>
/// <param name="pose"> The pose to update in the SM</param>
public void PoseCallback(WorldAnalysisInterface.PoseEstimationResult result, ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose)
{
if (pose.EstimationState != ETSI.ARF.OpenAPI.WorldAnalysis.PoseEstimationState.OK)
{
Debug.Log("State Not Ok for " + pose.Uuid + " "+ pose.EstimationState.ToString() + " " + pose.InstructionInfo);
return;
}
if (pose.Value.Type == ETSI.ARF.OpenAPI.WorldAnalysis.PoseValueType.VECTOR_QUATERNION)
{
ETSI.ARF.OpenAPI.WorldAnalysis.VectorQuaternionPoseValue value = (ETSI.ARF.OpenAPI.WorldAnalysis.VectorQuaternionPoseValue)pose.Value;
Sylvain Buche
committed
if (m_trackablesAndAnchorsInARScene.ContainsKey(pose.Uuid))
lacoche
committed
{
Sylvain Buche
committed
m_trackablesAndAnchorsInARScene[pose.Uuid].transform.position = WorldAnalysisUnityHelper.ConvertETSIVector3ToUnity(value.Position);
m_trackablesAndAnchorsInARScene[pose.Uuid].transform.rotation = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(value.Rotation);
lacoche
committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
}
else
{
Debug.LogWarning("Callback for unwanted uuid");
}
}
else
{
Debug.LogWarning("Pose value type not supported yet :" +pose.Value.Type); // Todo : manage other types
}
}
/// <summary>
/// Load a GLTF from local memory according to _PathToGLTF
/// </summary>
/// <returns>ascync method</returns>
protected async Task LoadGltfBinaryFromMemory()
{
#if UNITY_EDITOR
string prefix = Application.streamingAssetsPath;
#else
string prefix = Application.persistentDataPath;
#endif
var filePath = prefix + "/" + _PathToGLTF;
Debug.Log("PATH : " + filePath);
byte[] data = File.ReadAllBytes(filePath);
Debug.Log("File Size " + data.Length);
var gltf = new GltfImport();
bool success = await gltf.LoadGltfBinary(
data,
// The URI of the original data is important for resolving relative URIs within the glTF
new Uri(filePath)
);
GameObjectInstantiator instantiator = new GameObjectInstantiator(gltf, this.transform);
if (success)
{
success = await gltf.InstantiateMainSceneAsync(instantiator);
}
}
/// <summary>
/// In the loaded GLTF File : find all World Storage Trackables and Anchors
/// </summary>
/// <param name="transform">Loaded GLTF Transform</param>s
protected void FindWorldStorageTransform(Transform trGLTF)
{
if (trGLTF.name.StartsWith("ws:"))
{
string id = trGLTF.name.Substring(3);
Debug.Log("Add " + id + " " + trGLTF.name);
Sylvain Buche
committed
m_trackablesAndAnchorsInARScene.Add(new Guid(id), trGLTF);