Newer
Older
lacoche
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using GLTFast;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
public class SceneManagementGLTF : MonoBehaviour
{
/// <summary>
/// Types of World Analysis to Use
/// </summary>
public WorldAnalysisFactory.WorldAnalysisType _WorldAnalysisType;
/// <summary>
/// Path to GLTF File (relative to streaming assets in editor, else persistentdatapath)
/// </summary>
public string _PathToGLTF;
/// <summary>
/// List of trackables and anchors in the AR Scene
/// </summary>
private Dictionary<string, Transform> m_trackablesAndAnchorsInARScene;
/// <summary>
/// Current world analysis
/// </summary>
private WorldAnalysisInterface m_worldAnalysis;
/// <summary>
/// Unity Start Method
/// </summary>
protected async void Start()
{
m_trackablesAndAnchorsInARScene = new Dictionary<string, Transform>();
await LoadGltfBinaryFromMemory();
Transform loaded = this.transform.GetChild(0);
FindWorldStorageTransform(loaded);
m_worldAnalysis = WorldAnalysisFactory.CreateWorldAnalysis(_WorldAnalysisType, this.gameObject);
//Subscribe
foreach(KeyValuePair<string , Transform> toSubscribe in m_trackablesAndAnchorsInARScene)
{
int validity = 100000; //10s
string subscriptionUUID;
// TODO : if only one : subscribeToPose, if multiple subscribetoPoses
m_worldAnalysis.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.
}
}
/// <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;
if (m_trackablesAndAnchorsInARScene.ContainsKey(pose.Uuid.ToString()))
{
m_trackablesAndAnchorsInARScene[pose.Uuid.ToString()].transform.position = WorldAnalysisUnityHelper.ConvertETSIVector3ToUnity(value.Position);
m_trackablesAndAnchorsInARScene[pose.Uuid.ToString()].transform.rotation = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(value.Rotation);
}
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);
m_trackablesAndAnchorsInARScene.Add(id, trGLTF);
}
foreach(Transform child in trGLTF)
{
FindWorldStorageTransform(child);
}
}
}